001/* 002 * Copyright 2016 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.plugins.contentio.synchronize.rights; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.cms.repository.Content; 028import org.ametys.core.right.AbstractStaticRightAssignmentContext; 029import org.ametys.core.right.RightAssignmentContext; 030import org.ametys.core.ui.ClientSideElementHelper; 031import org.ametys.core.util.I18nUtils; 032import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 033import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO; 034import org.ametys.plugins.repository.AmetysObject; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.runtime.i18n.I18nizableText; 037import org.ametys.runtime.i18n.I18nizableTextParameter; 038 039/** 040 * {@link RightAssignmentContext} for assign rights to a {@link Content} or a jcr node root holding the contents 041 */ 042public class SynchronizeContentRightAssignmentContext extends AbstractStaticRightAssignmentContext 043{ 044 /** The prefix for rights on the root of a collection */ 045 public static final String ROOT_CONTEXT_PREFIX = "/synchronized-contents/"; 046 047 /** The Ametys object resolver */ 048 protected AmetysObjectResolver _resolver; 049 /** The synchronize collection DAO */ 050 protected SynchronizableContentsCollectionDAO _collectionsDAO; 051 /** The i18n utils */ 052 protected I18nUtils _i18nUtils; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(smanager); 058 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 059 _collectionsDAO = (SynchronizableContentsCollectionDAO) smanager.lookup(SynchronizableContentsCollectionDAO.ROLE); 060 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 061 } 062 063 @Override 064 public Object convertJSContext(Object context) 065 { 066 if (context instanceof String) 067 { 068 String stringContext = (String) context; 069 if (stringContext.startsWith(ROOT_CONTEXT_PREFIX)) 070 { 071 return context; 072 } 073 else 074 { 075 return _resolver.resolveById((String) context); 076 } 077 } 078 079 return null; 080 } 081 082 @Override 083 public String getContextIdentifier(Object context) 084 { 085 if (context instanceof Content) 086 { 087 return ((AmetysObject) context).getId(); 088 } 089 else 090 { 091 return (String) context; 092 } 093 } 094 095 @Override 096 public Set<Object> getParentContexts(Object context) 097 { 098 if (context instanceof Content) 099 { 100 String[] collectionIds = ((Content) context).getInternalDataHolder().getValue(SynchronizableContentsCollection.COLLECTION_ID_DATA_NAME); 101 return Set.of(ROOT_CONTEXT_PREFIX + collectionIds[0]); 102 } 103 104 return null; 105 } 106 107 @Override 108 public List<Object> getRootContexts(Map<String, Object> contextParameters) 109 { 110 List<Object> rootContexts = new ArrayList<>(); 111 if (matchWorkspace(contextParameters)) 112 { 113 for (SynchronizableContentsCollection synchronizableContentsCollection : _collectionsDAO.getSynchronizableContentsCollections()) 114 { 115 if (synchronizableContentsCollection.handleRightAssignmentContext()) 116 { 117 rootContexts.add(ROOT_CONTEXT_PREFIX + synchronizableContentsCollection.getId()); 118 } 119 } 120 } 121 return rootContexts; 122 } 123 124 @Override 125 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 126 { 127 List<Script> scripts = new ArrayList<>(); 128 129 List<Script> superScripts = super.getScripts(ignoreRights, contextParameters); 130 if (superScripts != null && superScripts.size() == 1) 131 { 132 for (SynchronizableContentsCollection synchronizableContentsCollection : _collectionsDAO.getSynchronizableContentsCollections()) 133 { 134 if (synchronizableContentsCollection.handleRightAssignmentContext()) 135 { 136 // First duplicate the script 137 Script script = ClientSideElementHelper.cloneScript(superScripts.get(0)); 138 139 script.getParameters().put("root-context", ROOT_CONTEXT_PREFIX + synchronizableContentsCollection.getId()); 140 script.getParameters().put("scc", synchronizableContentsCollection.getId()); 141 142 _parametrizeValue(script, "label", synchronizableContentsCollection.getLabel()); 143 _parametrizeValue(script, "radio-option-all-label", synchronizableContentsCollection.getLabel()); 144 _parametrizeValue(script, "hint-all-contents", synchronizableContentsCollection.getLabel()); 145 _parametrizeValue(script, "result-grid-mask-message", synchronizableContentsCollection.getLabel()); 146 147 scripts.add(script); 148 } 149 } 150 } 151 152 return scripts; 153 } 154 155 private void _parametrizeValue(Script script, String key, I18nizableText i18nKey) 156 { 157 Object label = script.getParameters().get(key); 158 if (label == null) 159 { 160 script.getParameters().put(key, i18nKey); 161 } 162 else if (label instanceof String) 163 { 164 script.getParameters().put(key, label + " " + _i18nUtils.translate(i18nKey)); 165 } 166 else if (label instanceof I18nizableText) 167 { 168 I18nizableText i18nLabel = (I18nizableText) label; 169 if (i18nLabel.isI18n()) 170 { 171 if (i18nLabel.getParameters() != null) 172 { 173 List<String> parameters = new ArrayList<>(i18nLabel.getParameters()); 174 parameters.add(_i18nUtils.translate(i18nKey)); 175 script.getParameters().put(key, new I18nizableText(i18nLabel.getCatalogue(), i18nLabel.getKey(), parameters)); 176 } 177 else 178 { 179 Map<String, I18nizableTextParameter> parametersMap = i18nLabel.getParameterMap() != null ? new HashMap<>(i18nLabel.getParameterMap()) : new HashMap<>(); 180 parametersMap.put("collection", i18nKey); 181 script.getParameters().put(key, new I18nizableText(i18nLabel.getCatalogue(), i18nLabel.getKey(), parametersMap)); 182 } 183 } 184 else 185 { 186 script.getParameters().put(key, i18nLabel.getLabel() + " " + _i18nUtils.translate(i18nKey)); 187 } 188 } 189 190 } 191}