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.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.cms.duplicate.contents.DuplicateContentsManager; 028import org.ametys.cms.repository.Content; 029import org.ametys.cms.repository.WorkflowAwareContent; 030import org.ametys.core.util.StringUtils; 031import org.ametys.plugins.workflow.EnhancedFunction; 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.runtime.i18n.I18nizableTextParameter; 034 035import com.opensymphony.module.propertyset.PropertySet; 036import com.opensymphony.workflow.WorkflowException; 037 038/** 039 * OS workflow function to send mail after an action is triggered. 040 */ 041public class CheckDuplicateContentsFunction extends AbstractContentWorkflowComponent implements EnhancedFunction 042{ 043 044 private DuplicateContentsManager _duplicateContentsManager; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 super.service(manager); 050 _duplicateContentsManager = (DuplicateContentsManager) manager.lookup(DuplicateContentsManager.ROLE); 051 } 052 053 @Override 054 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 055 { 056 WorkflowAwareContent content = getContent(transientVars); 057 Map<String, Object> duplicateContentsInfo = _duplicateContentsManager.searchDuplicates(content); 058 @SuppressWarnings("unchecked") 059 List<Content> duplicateContents = (List<Content>) duplicateContentsInfo.get(DuplicateContentsManager.DUPLICATE_CONTENTS_KEY); 060 @SuppressWarnings("unchecked") 061 List<Content> nearDuplicateContents = (List<Content>) duplicateContentsInfo.get(DuplicateContentsManager.NEAR_DUPLICATE_CONTENTS_KEY); 062 boolean isTooComplex = duplicateContentsInfo.get(DuplicateContentsManager.STATUS_KEY) == DuplicateContentsManager.Status.TOO_COMPLEX; 063 064 if (duplicateContents.isEmpty() && nearDuplicateContents.isEmpty() && !isTooComplex) 065 { 066 return; 067 } 068 069 Map<String, Object> result = getResultsMap(transientVars); 070 @SuppressWarnings("unchecked") 071 List<Object> messages = (List<Object>) result.getOrDefault("messages", new ArrayList<>()); 072 073 if (!duplicateContents.isEmpty() || !nearDuplicateContents.isEmpty()) 074 { 075 messages.add(_getDuplicateMessage(content, duplicateContents, nearDuplicateContents)); 076 } 077 078 if (isTooComplex) 079 { 080 messages.add(_getTooComplexMessage()); 081 } 082 083 result.put("messages", messages); 084 } 085 086 private Map<String, Object> _getTooComplexMessage() 087 { 088 Map<String, Object> message = new HashMap<>(); 089 message.put("type", "warning"); 090 List<Object> warningMessages = new ArrayList<>(); 091 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_TOO_COMPLEX")); 092 message.put("message", warningMessages); 093 return message; 094 } 095 096 private Map<String, Object> _getDuplicateMessage(WorkflowAwareContent content, List<Content> duplicateContents, List<Content> nearDuplicateContents) 097 { 098 Map<String, Object> message = new HashMap<>(); 099 100 message.put("type", "warning"); 101 102 List<Object> warningMessages = new ArrayList<>(); 103 104 message.put("message", warningMessages); 105 106 Map<String, I18nizableTextParameter> introI18nParameters = new HashMap<>(); 107 introI18nParameters.put("contentTitle", new I18nizableText(StringUtils.escapeHTML(content.getTitle()))); 108 109 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_INTRO", introI18nParameters)); 110 warningMessages.add("<br/><br/>"); 111 112 if (!duplicateContents.isEmpty()) 113 { 114 115 Map<String, I18nizableTextParameter> duplicatesI18nParameters = new HashMap<>(); 116 duplicatesI18nParameters.put("duplicatesNumber", new I18nizableText(String.valueOf(duplicateContents.size()))); 117 118 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_DUPLICATE_CONTENTS", duplicatesI18nParameters)); 119 _fillToolLink(warningMessages, duplicateContents); 120 } 121 122 if (!nearDuplicateContents.isEmpty()) 123 { 124 125 Map<String, I18nizableTextParameter> nearDuplicates18nParameters = new HashMap<>(); 126 nearDuplicates18nParameters.put("nearDuplicatesNumber", new I18nizableText(String.valueOf(nearDuplicateContents.size()))); 127 128 warningMessages.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_DUPLICATE_CONTENTS_SAVE_REPORT_NEAR_DUPLICATE_CONTENTS", nearDuplicates18nParameters)); 129 _fillToolLink(warningMessages, nearDuplicateContents); 130 } 131 132 return message; 133 } 134 135 private void _fillToolLink(List<Object> warningMessages, List<Content> nearDuplicateContents) 136 { 137 warningMessages.add("<ul>"); 138 nearDuplicateContents.forEach(duplicate -> 139 { 140 warningMessages.add("<li>"); 141 warningMessages.add(_getTitleTag(duplicate)); 142 warningMessages.add("</li>"); 143 }); 144 warningMessages.add("</ul>"); 145 } 146 147 /** 148 * Get the HTML tag to display the content 149 * @param duplicate the duplicate content info 150 * @return the HTML tag to display the content 151 */ 152 protected String _getTitleTag(Content duplicate) 153 { 154 String title = StringUtils.escapeHTML(duplicate.getTitle()); 155 if (_contentHelper.isSimple(duplicate)) 156 { 157 return title; 158 } 159 else 160 { 161 String onclick = "Ametys.tool.ToolsManager.openTool('uitool-content', {id: '" + duplicate.getId() + "'})"; 162 return "<a href=\"#\" onclick=\"" + onclick + "\">" + title + "</a>"; 163 } 164 } 165 166 @Override 167 public I18nizableText getLabel() 168 { 169 return new I18nizableText("plugin.cms", "PLUGINS_CMS_CHECK_DUPLICATES_FUNCTION_LABEL"); 170 } 171 172}