001/*
002 *  Copyright 2017 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.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.configuration.DefaultConfiguration;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028
029import org.ametys.cms.content.referencetable.HierarchicalReferenceTablesHelper;
030import org.ametys.cms.contenttype.ContentType;
031
032/**
033 * This element creates a menu with one gallery item per reference table content type classified by category. 
034 */
035public class ReferenceTablesGallery extends ContentTypesGallery
036{
037    /** The helper component for hierarchical simple contents */
038    protected HierarchicalReferenceTablesHelper _hierarchicalSimpleContentsHelper;
039
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _hierarchicalSimpleContentsHelper = (HierarchicalReferenceTablesHelper) smanager.lookup(HierarchicalReferenceTablesHelper.ROLE);
045    }
046    
047    @Override
048    protected boolean isValidContentType(ContentType contentType)
049    {
050        return contentType != null && contentType.isReferenceTable() && !contentType.isAbstract()  && !contentType.isPrivate()
051               && (!_hierarchicalSimpleContentsHelper.isHierarchical(contentType) || _hierarchicalSimpleContentsHelper.isLeaf(contentType));
052    }
053    
054    @Override
055    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
056    {
057        List<ContentType> cTypes = getReferenceTables();
058        if (cTypes.isEmpty())
059        {
060            // Hide menu if there is no simple contents
061            return new ArrayList<>();
062        }
063        
064        return super.getScripts(ignoreRights, contextParameters);
065    }
066    
067    /**
068     * Get the list of available simple content types
069     * @return The simple content types
070     */
071    protected List<ContentType> getReferenceTables()
072    {
073        List<ContentType> refTables = new ArrayList<>();
074        
075        Set<String> cTypeIds = _contentTypeExtensionPoint.getExtensionsIds();
076        for (String cTypeId : cTypeIds)
077        {
078            ContentType cType = _contentTypeExtensionPoint.getExtension(cTypeId);
079            if (isValidContentType(cType))
080            {
081                refTables.add(cType);
082            }
083        }
084        
085        return refTables;
086    }
087    
088    @SuppressWarnings("unchecked")
089    @Override
090    protected void _addContentTypeConfiguration (DefaultConfiguration rootConf, ContentType cType)
091    {
092        boolean hasParentAttribute = !cType.getParentAttributeDefinition().isEmpty();
093        
094        DefaultConfiguration classConf = new DefaultConfiguration("class");
095        if (hasParentAttribute)
096        {
097            classConf.setAttribute("name", "Ametys.plugins.cms.content.controller.OpenReferenceTableToolsButtonController");
098
099        }
100        else
101        {
102            classConf.setAttribute("name", "Ametys.ribbon.element.ui.button.OpenToolButtonController");
103        }
104        
105        // Label
106        DefaultConfiguration labelConf = _getI18nizableTextConfiguration("label", cType.getLabel());
107        classConf.addChild(labelConf);
108        
109        // Description
110        DefaultConfiguration descConf = _getI18nizableTextConfiguration("description", cType.getDescription());
111        classConf.addChild(descConf);
112        
113        // Icons or glyph
114        _addContentTypeIconsConfiguration(classConf, cType);
115        
116        // Open tool params
117        DefaultConfiguration toolParams = new DefaultConfiguration("opentool-params");
118        // Tool id
119        DefaultConfiguration idConf = new DefaultConfiguration("id");
120        idConf.setValue("reference-table-search-ui." + cType.getId());
121        toolParams.addChild(idConf);
122        // Content Type
123        DefaultConfiguration typeConf = new DefaultConfiguration("contentType");
124        typeConf.setValue(cType.getId());
125        toolParams.addChild(typeConf);
126        // Content default title
127        DefaultConfiguration defaultTitleConf = _getI18nizableTextConfiguration("contentDefaultTitle", cType.getDefaultTitle());
128        toolParams.addChild(defaultTitleConf);
129        // Content language
130        DefaultConfiguration langConf = null;
131        if (this._script.getParameters().containsKey("contentLanguage"))
132        {
133            langConf = new DefaultConfiguration("contentLanguage");
134            langConf.setValue((String) this._script.getParameters().get("contentLanguage"));
135            toolParams.addChild(langConf);
136        }
137        // Start search at opening
138        DefaultConfiguration startSearchConf = new DefaultConfiguration("startSearchAtOpening");
139        startSearchConf.setValue(true);
140        toolParams.addChild(startSearchConf);
141        
142        classConf.addChild(toolParams);
143        
144        // Common configuration
145        Map<String, Object> commonConfig = (Map<String, Object>) this._script.getParameters().get("items-config");
146        for (String tagName : commonConfig.keySet())
147        {
148            DefaultConfiguration c = new DefaultConfiguration(tagName);
149            c.setValue(String.valueOf(commonConfig.get(tagName)));
150            classConf.addChild(c);
151        }
152        
153        // Potential configuration if the content type defines a parent
154        if (hasParentAttribute)
155        {
156            // HierarchicalSimpleContentsTool id
157            DefaultConfiguration treeIdConf = new DefaultConfiguration("open-tree-tool-id");
158            treeIdConf.setValue("uitool-hierarchical-reference-tables");
159            classConf.addChild(treeIdConf);
160            
161            // HierarchicalSimpleContentsTool params
162            DefaultConfiguration treeToolParams = new DefaultConfiguration("open-tree-tool-params");
163            
164            DefaultConfiguration treeParamIdConf = new DefaultConfiguration("id");
165            treeParamIdConf.setValue(cType.getId());
166            treeToolParams.addChild(treeParamIdConf);
167            
168            DefaultConfiguration treeWorkflowEditActionIdConf = new DefaultConfiguration("workflowEditActionId");
169            treeWorkflowEditActionIdConf.setValue("2");
170            treeToolParams.addChild(treeWorkflowEditActionIdConf);
171            
172            treeToolParams.addChild(typeConf);
173            treeToolParams.addChild(defaultTitleConf);
174            if (langConf != null)
175            {
176                treeToolParams.addChild(langConf);
177            }
178            treeToolParams.addChild(startSearchConf);
179            
180            classConf.addChild(treeToolParams);
181            
182            // Action
183            DefaultConfiguration action = new DefaultConfiguration("action");
184            action.setValue("Ametys.plugins.cms.content.controller.OpenReferenceTableToolsButtonController._act");
185            classConf.addChild(action);
186        }
187        
188        rootConf.addChild(classConf);
189        
190        if (hasParentAttribute)
191        {
192            // When using the class Ametys.plugins.cms.content.controller.OpenReferenceTableToolsButtonController
193            // we need the associated files
194            
195            DefaultConfiguration scriptsConfiguration = new DefaultConfiguration("scripts");
196            
197            DefaultConfiguration script1Configuration = new DefaultConfiguration("file");
198            script1Configuration.setValue("js/Ametys/plugins/cms/content/controller/OpenReferenceTableToolsButtonController.js");
199            scriptsConfiguration.addChild(script1Configuration);
200            
201            DefaultConfiguration script2Configuration = new DefaultConfiguration("file");
202            script2Configuration.setValue("js/Ametys/plugins/cms/content/actions/ReferenceTableActions.js");
203            scriptsConfiguration.addChild(script2Configuration);
204    
205            rootConf.addChild(scriptsConfiguration);
206        }
207
208        _addRightsOnContentTypeConfiguration(rootConf);
209    }
210    
211    @Override
212    protected boolean hasRight(ContentType cType)
213    {
214        return true;
215    }
216    
217    @Override
218    protected Map<String, List<String>> _configureDependencies(Configuration configuration) throws ConfigurationException
219    {
220        Map<String, List<String>> dependencies = super._configureDependencies(configuration);
221        
222        boolean needHierarchicalTool = _hierarchicalSimpleContentsHelper.hasAtLeastOneHierarchy();
223        
224        if (needHierarchicalTool)
225        {
226            List<String> uitoolsDependencies = dependencies.get("org.ametys.core.ui.UIToolsFactoriesManager");
227            if (uitoolsDependencies == null)
228            {
229                uitoolsDependencies = new ArrayList<>();
230            }
231            uitoolsDependencies.add("uitool-hierarchical-reference-tables");
232        }
233        
234        return dependencies;
235    }
236}