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