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