001/*
002 *  Copyright 2013 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.cms.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.cms.clientsideelement.content.SmartContentClientSideElementHelper;
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.WorkflowAwareContent;
030import org.ametys.core.ui.Callable;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032
033/**
034 * This element creates a menu with one gallery item per content type classified by category. 
035 * The user rights are checked. 
036 */
037public class SmartContentTypesGallery extends ContentTypesGallery
038{
039    /** Ametys object resolver */
040    protected AmetysObjectResolver _resolver;
041    /** The context */
042    protected Context _context;
043    /** Helper for smart content client elements */
044    protected SmartContentClientSideElementHelper _smartHelper;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
051        _smartHelper = (SmartContentClientSideElementHelper) manager.lookup(SmartContentClientSideElementHelper.ROLE);
052    }
053    
054    /**
055     * Get informations on contents' state
056     * @param contentsId the ids of contents
057     * @return informations on contents' state
058     */
059    @Callable
060    public Map<String, Object> getStatus(List<String> contentsId)
061    {
062        Map<String, Object> results = new HashMap<>();
063        
064        results.put("unmodifiable-contents", new ArrayList<Map<String, Object>>());
065        results.put("locked-contents", new ArrayList<Map<String, Object>>());
066        results.put("noright-contents", new ArrayList<Map<String, Object>>());
067        results.put("invalidworkflowaction-contents", new ArrayList<Map<String, Object>>());
068        results.put("invalidworkflowstep-contents", new ArrayList<Map<String, Object>>());
069        results.put("allright-contents", new ArrayList<Map<String, Object>>());
070        
071        for (String contentId : contentsId)
072        {
073            Content content = _resolver.resolveById(contentId);
074            
075            boolean error = false;
076            
077            if (content instanceof WorkflowAwareContent)
078            {
079                // Is modifiable
080                String enabledOnModifiableOnly = (String) this._script.getParameters().get("enabled-on-modifiable-only"); 
081                if ("true".equals(enabledOnModifiableOnly) && !_smartHelper.isModifiable(content))
082                {
083                    Map<String, Object> contentParams = getContentDefaultParameters (content);
084                    contentParams.put("description", _smartHelper.getNoModifiableDescription(this._script.getParameters(), content));
085                    
086                    @SuppressWarnings("unchecked")
087                    List<Map<String, Object>> unModifiableContents = (List<Map<String, Object>>) results.get("unmodifiable-contents");
088                    unModifiableContents.add(contentParams);
089
090                    error = true;
091                }
092                
093                // Is locked
094                String enabledOnUnlockOnly = (String) this._script.getParameters().get("enabled-on-unlock-only"); 
095                if ("true".equals(enabledOnUnlockOnly) && _smartHelper.isLocked(content))
096                {
097                    Map<String, Object> contentParams = getContentDefaultParameters (content);
098                    contentParams.put("description", _smartHelper.getLockedDescription(this._script.getParameters(), content));
099                    
100                    @SuppressWarnings("unchecked")
101                    List<Map<String, Object>> lockedContents = (List<Map<String, Object>>) results.get("locked-contents");
102                    lockedContents.add(contentParams);
103                    
104                    error = true;
105                }
106                
107                // Has right correct
108                String enabledOnRightOnly = (String) this._script.getParameters().get("enabled-on-right-only"); 
109                if ("true".equals(enabledOnRightOnly) && !_smartHelper.hasRight(_rights, content))
110                {
111                    Map<String, Object> contentParams = getContentDefaultParameters (content);
112                    contentParams.put("description", _smartHelper.getNoRightDescription (this._script.getParameters(), content));
113                    
114                    @SuppressWarnings("unchecked")
115                    List<Map<String, Object>> norightContents = (List<Map<String, Object>>) results.get("noright-contents");
116                    norightContents.add(contentParams);
117                    
118                    error = true;
119                }
120                
121                // Is workflow action correct
122                String enabledOnWorkflowActionOnly = (String) this._script.getParameters().get("enabled-on-workflow-action-only"); 
123                if (enabledOnWorkflowActionOnly != null)
124                {
125                    int actionId = _smartHelper.workflowAction(this._script.getParameters(), content);
126                    if (actionId == -1)
127                    {
128                        Map<String, Object> contentParams = getContentDefaultParameters (content);
129                        contentParams.put("description", _smartHelper.getWorkflowActionUnvailableDescription(this._script.getParameters(), content));
130                        
131                        @SuppressWarnings("unchecked")
132                        List<Map<String, Object>> invalidActionContents = (List<Map<String, Object>>) results.get("invalidworkflowaction-contents");
133                        invalidActionContents.add(contentParams);
134                        
135                        error = true;
136                    }
137                    else
138                    {
139                        results.put("workflowaction-content-actionId", actionId);
140                    }
141                }
142                
143                // Is workflow step correct
144                String enabledOnWorkflowStepOnly = (String) this._script.getParameters().get("enabled-on-workflow-step-only"); 
145                if (enabledOnWorkflowStepOnly != null && !_smartHelper.isWorkflowStepCorrect(this._script.getParameters(), content))
146                {
147                    Map<String, Object> contentParams = getContentDefaultParameters (content);
148                    contentParams.put("description", _smartHelper.getIncorrectWorkflowStepDescription(this._script.getParameters(), content));
149                    
150                    @SuppressWarnings("unchecked")
151                    List<Map<String, Object>> invalidStepContents = (List<Map<String, Object>>) results.get("invalidworkflowstep-contents");
152                    invalidStepContents.add(contentParams);
153                    
154                    error = true;
155                }
156                
157                if (_isAllRight (content, error, results))
158                {
159                    Map<String, Object> contentParams = getContentDefaultParameters (content);
160                    contentParams.put("description", _smartHelper.getAllRightDescription(this._script.getParameters(), content));
161                    
162                    @SuppressWarnings("unchecked")
163                    List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents");
164                    allrightContents.add(contentParams);
165                }
166            }
167        }
168        
169        return results;
170    }
171    
172    /**
173     * Determines if the user can finally do action on content
174     * @param content The content
175     * @param hasError true if a error has already occurs
176     * @param results The result parameters to be passed to client side
177     * @return true if the user can finally do action on content
178     */
179    protected boolean _isAllRight (Content content, boolean hasError, Map<String, Object> results)
180    {
181        return !hasError;
182    }
183    
184    
185    /**
186     * Get the default content's parameters
187     * @param content The content
188     * @return The default parameters
189     */
190    protected Map<String, Object> getContentDefaultParameters (Content content)
191    {
192        return _smartHelper.getContentDefaultParameters(content);
193    }
194}