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