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