001/* 002 * Copyright 2010 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 */ 016package org.ametys.cms.workflow; 017 018import java.util.HashSet; 019import java.util.List; 020import java.util.Map; 021import java.util.Set; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.commons.lang.StringUtils; 026 027import org.ametys.cms.contenttype.ContentType; 028import org.ametys.cms.repository.Content; 029import org.ametys.cms.workflow.copy.CreateContentByCopyFunction; 030import org.ametys.core.right.Right; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032import org.ametys.plugins.repository.UnknownAmetysObjectException; 033import org.ametys.runtime.i18n.I18nizableText; 034 035import com.opensymphony.workflow.WorkflowException; 036 037/** 038 * Condition for checking if an user has the right to create a content of the requested type. 039 */ 040public class CreateContentByCopyRightCondition extends CreateContentRightCondition 041{ 042 private AmetysObjectResolver _resolver; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 super.service(manager); 048 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 049 } 050 051 @Override 052 protected Set<String> getRights(Map transientVars) throws WorkflowException 053 { 054 Set<String> rights = new HashSet<>(); 055 056 String[] contentTypeIds; 057 058 if (transientVars.get(CreateContentFunction.CONTENT_TYPES_KEY) != null) 059 { 060 contentTypeIds = (String[]) transientVars.get(CreateContentFunction.CONTENT_TYPES_KEY); 061 } 062 else 063 { 064 Content baseContent = (Content) transientVars.get(CreateContentByCopyFunction.BASE_CONTENT_KEY); 065 if (baseContent == null) 066 { 067 String baseContentId = (String) transientVars.get(CreateContentByCopyFunction.BASE_CONTENT_ID); 068 069 try 070 { 071 baseContent = _resolver.resolveById(baseContentId); 072 } 073 catch (UnknownAmetysObjectException e) 074 { 075 throw new WorkflowException("Unable to retrieve the base content for the duplication."); 076 } 077 } 078 079 contentTypeIds = baseContent.getTypes(); 080 } 081 082 for (String contentTypeId : contentTypeIds) 083 { 084 ContentType contentType = _contentTypeExtensionPoint.getExtension(contentTypeId); 085 086 if (contentType == null) 087 { 088 throw new WorkflowException("Unknown content type: " + contentType); 089 } 090 091 String right = contentType.getRight(); 092 if (StringUtils.isNotEmpty(right)) 093 { 094 rights.add(right); 095 } 096 } 097 098 099 return rights; 100 } 101 102 @Override 103 public I18nizableText getDescription(Map<String, String> argumentsValues) 104 { 105 String rightNeeded = (String) argumentsValues.get(__RIGHT_KEY); 106 String translatedRight = ""; 107 if (StringUtils.isNotBlank(rightNeeded)) 108 { 109 if (rightNeeded.contains("|")) 110 { 111 String[] rightsOr = StringUtils.split(rightNeeded, '|'); 112 translatedRight = "<strong>" + _i18nUtils.translate(_rightsExtensionPoint.getExtension(rightsOr[0]).getLabel()) + "</strong>"; 113 for (String rightId: rightsOr) 114 { 115 Right right = _rightsExtensionPoint.getExtension(rightId); 116 translatedRight += _i18nUtils.translate(new I18nizableText("plugin.workflow", "PLUGINS_WORKFLOW_EDITOR_CONDITION_OR")) + "<strong>" + _i18nUtils.translate(right.getDescription()) + "</strong>"; 117 } 118 return new I18nizableText("plugin.cms", "PLUGINS_CMS_CREATE_CONTENT_BY_COPY_RIGHT_MULTIPLES_CONDITION_DESCRIPTION", List.of(translatedRight)); 119 } 120 else if (rightNeeded.contains("&")) 121 { 122 String[] rightsAnd = StringUtils.split(rightNeeded, '&'); 123 translatedRight = "<strong>" + _i18nUtils.translate(_rightsExtensionPoint.getExtension(rightsAnd[0]).getLabel()) + "</strong>"; 124 for (String rightId: rightsAnd) 125 { 126 Right right = _rightsExtensionPoint.getExtension(rightId); 127 translatedRight += _i18nUtils.translate(new I18nizableText("plugin.workflow", "PLUGINS_WORKFLOW_EDITOR_CONDITION_AND")) + "<strong>" + _i18nUtils.translate(right.getDescription()) + "</strong>"; 128 } 129 return new I18nizableText("plugin.cms", "PLUGINS_CMS_CREATE_CONTENT_BY_COPY_RIGHT_MULTIPLES_CONDITION_DESCRIPTION", List.of(translatedRight)); 130 } 131 Right right = _rightsExtensionPoint.getExtension(rightNeeded); 132 translatedRight = "<strong>" + _i18nUtils.translate(right.getLabel()) + "</strong>"; 133 return new I18nizableText("plugin.cms", "PLUGINS_CMS_CREATE_CONTENT_BY_COPY_RIGHT_CONDITION_DESCRIPTION_PARAM", List.of(translatedRight)); 134 } 135 return new I18nizableText("plugin.cms", "PLUGINS_CMS_CREATE_CONTENT_BY_COPY_RIGHT_CONDITION_DESCRIPTION"); 136 } 137}