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                if (synchronizableContentsCollection.handleRightAssignmentContext())
140                {
141                    // First duplicate the script
142                    Script script = ClientSideElementHelper.cloneScript(superScripts.get(0));
143                    
144                    script.getParameters().put("root-context", ROOT_CONTEXT_PREFIX + synchronizableContentsCollection.getId());
145                    script.getParameters().put("scc", synchronizableContentsCollection.getId());
146                    
147                    _parametrizeValue(script, "label", synchronizableContentsCollection.getLabel());
148                    _parametrizeValue(script, "radio-option-all-label", synchronizableContentsCollection.getLabel());
149                    _parametrizeValue(script, "hint-all-contents", synchronizableContentsCollection.getLabel());
150                    _parametrizeValue(script, "result-grid-mask-message", synchronizableContentsCollection.getLabel());
151                    
152                    scripts.add(script);
153                }
154            }
155        }
156
157        return scripts;
158    }
159    
160    private void _parametrizeValue(Script script, String key, I18nizableText i18nKey)
161    {
162        Object label = script.getParameters().get(key);
163        if (label == null)
164        {
165            script.getParameters().put(key, i18nKey);
166        }
167        else if (label instanceof String)
168        {
169            script.getParameters().put(key, label + " " + _i18nUtils.translate(i18nKey));
170        }
171        else if (label instanceof I18nizableText)
172        {
173            I18nizableText i18nLabel = (I18nizableText) label;
174            if (i18nLabel.isI18n())
175            {
176                if (i18nLabel.getParameters() != null)
177                {
178                    List<String> parameters = new ArrayList<>(i18nLabel.getParameters());
179                    parameters.add(_i18nUtils.translate(i18nKey));
180                    script.getParameters().put(key, new I18nizableText(i18nLabel.getCatalogue(), i18nLabel.getKey(), parameters));
181                }
182                else
183                {
184                    Map<String, I18nizableText> parametersMap = i18nLabel.getParameterMap() != null ? new HashMap<>(i18nLabel.getParameterMap()) : new HashMap<>();
185                    parametersMap.put("collection", i18nKey);
186                    script.getParameters().put(key, new I18nizableText(i18nLabel.getCatalogue(), i18nLabel.getKey(), parametersMap));
187                }
188            }
189            else
190            {
191                script.getParameters().put(key, i18nLabel.getLabel() + " " + _i18nUtils.translate(i18nKey));
192            }
193        }
194        
195    }
196}