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.web.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
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.clientsideelement.AbstractContentClientSideElement;
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.TaggableAmetysObject;
030import org.ametys.core.right.RightManager.RightResult;
031import org.ametys.core.ui.Callable;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.web.repository.page.Page;
035
036/**
037 * Tag page and/or content item
038 */
039public class TagClientSideElement extends AbstractContentClientSideElement
040{
041    private AmetysObjectResolver _resolver;
042
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    /**
051     * Get the status of given pages and/or contents
052     * @param pageIds The ids of pages
053     * @param contentIds The ids of contents
054     * @return the result
055     */
056    @Callable
057    public Map<String, Object> getStatus (List<String> pageIds, List<String> contentIds)
058    {
059        Map<String, Object> results = new HashMap<>();
060        
061        results.put("nomodifiable-pages", new ArrayList<Map<String, Object>>());
062        results.put("noright-pages", new ArrayList<Map<String, Object>>());
063        results.put("allright-pages", new ArrayList<Map<String, Object>>());
064        
065        if (pageIds != null)
066        {
067            for (String pageId : pageIds)
068            {
069                Page page = _resolver.resolveById(pageId);
070                
071                boolean error = false;
072                
073                String enabledOnModifiableOnly = (String) this._script.getParameters().get("enabled-on-modifiable-only"); 
074                if ("true".equals(enabledOnModifiableOnly) && !(page instanceof TaggableAmetysObject))
075                {
076                    Map<String, Object> pageParams = getPageDefaultParameters(page);
077                    pageParams.put("description", getNoModifiablePageDescription(page));
078
079                    @SuppressWarnings("unchecked")
080                    List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("nomodifiable-pages");
081                    norightPages.add(pageParams);
082                    
083                    error = true;
084                }
085                
086                String enabledOnRightOnly = (String) this._script.getParameters().get("enabled-on-right-only"); 
087                if ("true".equals(enabledOnRightOnly) && !hasRight(page))
088                {
089                    Map<String, Object> pageParams = getPageDefaultParameters(page);
090                    pageParams.put("description", getNoRightPageDescription(page));
091
092                    @SuppressWarnings("unchecked")
093                    List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("noright-pages");
094                    norightPages.add(pageParams);
095                    
096                    error = true;
097                }
098                
099                if (!error)
100                {
101                    Map<String, Object> pageParams = getPageDefaultParameters(page);
102                    pageParams.put("description", getAllRightPageDescription(page));
103
104                    @SuppressWarnings("unchecked")
105                    List<Map<String, Object>> allrightPages = (List<Map<String, Object>>) results.get("allright-pages");
106                    allrightPages.add(pageParams);
107                }
108            }
109        }
110        
111        results.put("nomodifiable-contents", new ArrayList<Map<String, Object>>());
112        results.put("locked-contents", new ArrayList<Map<String, Object>>());
113        results.put("noright-contents", new ArrayList<Map<String, Object>>());
114        results.put("allright-contents", new ArrayList<Map<String, Object>>());
115        
116        if (contentIds != null)
117        {
118            for (String contentId : contentIds)
119            {
120                Content content = _resolver.resolveById(contentId);
121                
122                boolean error = false;
123                
124                // Is modifiable
125                error = _hasModifiableError(results, content, error);
126                
127                // Is locked
128                error = _hasLockError(results, content, error);
129                
130                // Has right correct
131                error = _hasRightError(results, content, error);
132                
133                if (!error)
134                {
135                    Map<String, Object> contentParams = getContentDefaultParameters (content);
136                    contentParams.put("description", getAllRightContentDescription(content));
137                    
138                    @SuppressWarnings("unchecked")
139                    List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents");
140                    allrightContents.add(contentParams);
141                }
142            }
143        }
144        
145        return results;
146    }
147
148    private boolean _hasModifiableError(Map<String, Object> results, Content content, boolean error)
149    {
150        String enabledOnModifiableOnly = (String) this._script.getParameters().get("enabled-on-modifiable-only"); 
151        if ("true".equals(enabledOnModifiableOnly) && !(content instanceof TaggableAmetysObject))
152        {
153            Map<String, Object> contentParams = getContentDefaultParameters (content);
154            contentParams.put("description", getNoModifiableContentDescription(content));
155            
156            @SuppressWarnings("unchecked")
157            List<Map<String, Object>> unModifiableContents = (List<Map<String, Object>>) results.get("nomodifiable-contents");
158            unModifiableContents.add(contentParams);
159
160            return true;
161        }
162        return error;
163    }
164
165    private boolean _hasRightError(Map<String, Object> results, Content content, boolean error)
166    {
167        String enabledOnRightOnly = (String) this._script.getParameters().get("enabled-on-right-only"); 
168        if ("true".equals(enabledOnRightOnly) && !hasRight(content))
169        {
170            Map<String, Object> contentParams = getContentDefaultParameters (content);
171            contentParams.put("description", getNoRightContentDescription (content));
172            
173            @SuppressWarnings("unchecked")
174            List<Map<String, Object>> norightContents = (List<Map<String, Object>>) results.get("noright-contents");
175            norightContents.add(contentParams);
176            
177            return true;
178        }
179        return error;
180    }
181
182    private boolean _hasLockError(Map<String, Object> results, Content content, boolean error)
183    {
184        String enabledOnUnlockOnly = (String) this._script.getParameters().get("enabled-on-unlock-only"); 
185        if ("true".equals(enabledOnUnlockOnly) && isLocked(content))
186        {
187            Map<String, Object> contentParams = getContentDefaultParameters (content);
188            contentParams.put("description", getLockedContentDescription(content));
189            
190            @SuppressWarnings("unchecked")
191            List<Map<String, Object>> lockedContents = (List<Map<String, Object>>) results.get("locked-contents");
192            lockedContents.add(contentParams);
193            
194            return true;
195        }
196        return error;
197    }
198    
199    /**
200     * Get the default page's parameters
201     * @param page The page
202     * @return The default parameters
203     */
204    protected Map<String, Object> getPageDefaultParameters (Page page)
205    {
206        Map<String, Object> pageParams = new HashMap<>();
207        pageParams.put("id", page.getId());
208        pageParams.put("title", page.getTitle());
209        
210        return pageParams;
211    }
212    
213    /**
214     * Determines if user has convenient right on page
215     * @param page The page
216     * @return true if the user has convenient right
217     */
218    protected boolean hasRight (Page page)
219    {
220        for (String rightToCheck : _rights.keySet())
221        {
222            if (StringUtils.isNotEmpty(rightToCheck)
223                && _rightManager.hasRight(_currentUserProvider.getUser(), rightToCheck, page) == RightResult.RIGHT_ALLOW)
224            {
225                return true;
226            }
227        }
228                
229        return false;
230    }
231    
232    /**
233     * Get the description when user has no right on page
234     * @param page the page
235     * @return the description
236     */
237    protected I18nizableText getNoRightPageDescription (Page page)
238    {
239        List<String> i18nParameters = new ArrayList<>();
240        i18nParameters.add(page.getTitle());
241        
242        I18nizableText ed = (I18nizableText) this._script.getParameters().get("noright-page-description");
243        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
244    }
245    
246    /**
247     * Get the description when page is not modifiable
248     * @param page the page
249     * @return the description
250     */
251    protected I18nizableText getNoModifiablePageDescription (Page page)
252    {
253        List<String> i18nParameters = new ArrayList<>();
254        i18nParameters.add(page.getTitle());
255        
256        I18nizableText ed = (I18nizableText) this._script.getParameters().get("nomodifiable-page-description");
257        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
258    }
259    
260    /**
261     * Get i18n description for all right page
262     * @param page The page
263     * @return The {@link I18nizableText} description
264     */
265    protected I18nizableText getAllRightPageDescription (Page page)
266    {
267        List<String> i18nParameters = new ArrayList<>();
268        i18nParameters.add(page.getTitle());
269        
270        I18nizableText ed = (I18nizableText) this._script.getParameters().get("allright-page-description");
271        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
272    }
273    
274}