001/* 002 * Copyright 2021 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.cms.workflow; 018 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.activity.Initializable; 025import org.apache.commons.collections.ListUtils; 026 027import org.ametys.cms.duplicate.contents.DuplicateContentsManager; 028import org.ametys.cms.repository.Content; 029import org.ametys.cms.repository.WorkflowAwareContent; 030import org.ametys.plugins.workflow.EnhancedFunction; 031import org.ametys.runtime.i18n.I18nizableText; 032import org.ametys.runtime.i18n.I18nizableTextParameter; 033 034import com.opensymphony.module.propertyset.PropertySet; 035import com.opensymphony.workflow.WorkflowException; 036 037/** 038 * OS workflow function to send mail after an action is triggered. 039 */ 040public class CheckDuplicateContentsFunction extends AbstractContentWorkflowComponent implements Initializable, EnhancedFunction 041{ 042 043 private DuplicateContentsManager _duplicateContentsManager; 044 045 @Override 046 public void initialize() throws Exception 047 { 048 _duplicateContentsManager = (DuplicateContentsManager) _manager.lookup(DuplicateContentsManager.ROLE); 049 } 050 051 @Override 052 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 053 { 054 WorkflowAwareContent content = getContent(transientVars); 055 Map<String, Object> duplicateContentsInfo = _duplicateContentsManager.searchDuplicates(content); 056 @SuppressWarnings("unchecked") 057 List<Content> duplicateContents = (List<Content>) duplicateContentsInfo.get(DuplicateContentsManager.DUPLICATE_CONTENTS_KEY); 058 @SuppressWarnings("unchecked") 059 List<Content> nearDuplicateContents = (List<Content>) duplicateContentsInfo.get(DuplicateContentsManager.NEAR_DUPLICATE_CONTENTS_KEY); 060 boolean isTooComplex = duplicateContentsInfo.get(DuplicateContentsManager.STATUS_KEY) == DuplicateContentsManager.Status.TOO_COMPLEX; 061 062 if (duplicateContents.isEmpty() && nearDuplicateContents.isEmpty() && !isTooComplex) 063 { 064 return; 065 } 066 067 Map<String, Object> result = getResultsMap(transientVars); 068 @SuppressWarnings("unchecked") 069 List<Object> messages = (List<Object>) result.getOrDefault("messages", new ArrayList<>()); 070 071 if (!duplicateContents.isEmpty() || !nearDuplicateContents.isEmpty()) 072 { 073 messages.add(_getDuplicateMessage(content, duplicateContents, nearDuplicateContents)); 074 } 075 076 if (isTooComplex) 077 { 078 messages.add(_getTooComplexMessage()); 079 } 080 081 result.put("messages", messages); 082 } 083 084 private Map<String, Object> _getTooComplexMessage() 085 { 086 Map<String, Object> message = new HashMap<>(); 087 message.put("type", "warning"); 088 List<Object> warningMessages = new ArrayList<>(); 089 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_TOO_COMPLEX")); 090 message.put("message", warningMessages); 091 return message; 092 } 093 094 private Map<String, Object> _getDuplicateMessage(WorkflowAwareContent content, List<Content> duplicateContents, List<Content> nearDuplicateContents) 095 { 096 Map<String, Object> message = new HashMap<>(); 097 098 message.put("type", "warning"); 099 100 List<Object> warningMessages = new ArrayList<>(); 101 102 message.put("message", warningMessages); 103 104 Map<String, I18nizableTextParameter> introI18nParameters = new HashMap<>(); 105 introI18nParameters.put("contentTitle", new I18nizableText(content.getTitle())); 106 107 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_INTRO", introI18nParameters)); 108 warningMessages.add("<br/><br/>"); 109 110 if (!duplicateContents.isEmpty()) 111 { 112 113 Map<String, I18nizableTextParameter> duplicatesI18nParameters = new HashMap<>(); 114 duplicatesI18nParameters.put("duplicatesNumber", new I18nizableText(String.valueOf(duplicateContents.size()))); 115 116 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_DUPLICATE_CONTENTS", duplicatesI18nParameters)); 117 _fillToolLink(warningMessages, duplicateContents); 118 } 119 120 if (!nearDuplicateContents.isEmpty()) 121 { 122 123 Map<String, I18nizableTextParameter> nearDuplicates18nParameters = new HashMap<>(); 124 nearDuplicates18nParameters.put("nearDuplicatesNumber", new I18nizableText(String.valueOf(nearDuplicateContents.size()))); 125 126 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_NEAR_DUPLICATE_CONTENTS", nearDuplicates18nParameters)); 127 _fillToolLink(warningMessages, nearDuplicateContents); 128 } 129 130 return message; 131 } 132 133 private void _fillToolLink(List<Object> warningMessages, List<Content> nearDuplicateContents) 134 { 135 warningMessages.add("<ul>"); 136 nearDuplicateContents.forEach(duplicate -> 137 { 138 warningMessages.add("<li>"); 139 warningMessages.add(_getTitleTag(duplicate)); 140 warningMessages.add("</li>"); 141 }); 142 warningMessages.add("</ul>"); 143 } 144 145 /** 146 * Get the HTML tag to display the content 147 * @param duplicate the duplicate content info 148 * @return the HTML tag to display the content 149 */ 150 protected String _getTitleTag(Content duplicate) 151 { 152 String title = duplicate.getTitle(); 153 if (_contentHelper.isSimple(duplicate)) 154 { 155 return title; 156 } 157 else 158 { 159 String onclick = "Ametys.tool.ToolsManager.openTool('uitool-content', {id: '" + duplicate.getId() + "'})"; 160 return "<a href=\"#\" onclick=\"" + onclick + "\">" + title + "</a>"; 161 } 162 } 163 164 @Override 165 public List<FunctionArgument> getArguments() 166 { 167 return ListUtils.EMPTY_LIST; 168 } 169 170 @Override 171 public I18nizableText getDescription(Map<String, String> argumentsValues) 172 { 173 return new I18nizableText("plugin.cms", "PLUGINS_CMS_CHECK_DUPLICATES_FUNCTION_DESCRIPTION"); 174 } 175 176}