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