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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang.StringUtils;
025
026import org.ametys.cms.contenttype.AbstractMetadataSetElement;
027import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
028import org.ametys.cms.contenttype.ContentTypesHelper;
029import org.ametys.cms.contenttype.Fieldset;
030import org.ametys.cms.contenttype.MetadataSet;
031import org.ametys.cms.repository.Content;
032import org.ametys.core.ui.Callable;
033import org.ametys.core.ui.SimpleMenu;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035
036/**
037 * This element creates a menu for the tag policy gallery.
038 * This gallery must be disabled is the current edited content does not contain any tab.
039 */
040public class FormEditionModeMenu extends SimpleMenu
041{
042    /** The ametys resolver */
043    protected AmetysObjectResolver _resolver;
044    
045    /** Content type extension point. */
046    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
047    /** Helper for content types */
048    protected ContentTypesHelper _contentTypesHelper;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
055        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
056        _contentTypesHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
057    }
058    
059    /**
060     * Return the current status of this menu.
061     * It determines if the current content contain at least one tab in edition.
062     * @param contentId The id of the content
063     * @param metadataSetName The name of the metadata set in which the search must be done.
064     * @param fallbackMetadataSetName The name of the fallback metadata set if the initial was not found. Can be null.
065     * @return A Map with ("has-tab", true/false).
066     */
067    @Callable
068    public Map<String, Object> getStatus(String contentId, String metadataSetName, String fallbackMetadataSetName)
069    {
070        Map<String, Object> results = new HashMap<>();
071        
072        Content content = _resolver.resolveById(contentId);
073        MetadataSet metadataSetForEdition = _contentTypesHelper.getMetadataSetWithFallback(metadataSetName, fallbackMetadataSetName, content.getTypes(), content.getMixinTypes(), true);
074        
075        if (metadataSetForEdition == null)
076        {
077            String errorMsg = String.format("Unknown metadata set '%s' of type 'edition' for content type(s) '%s'", metadataSetName, StringUtils.join(content.getTypes(), ','));
078            getLogger().error(errorMsg);
079            throw new IllegalArgumentException(errorMsg);
080        }
081        
082        boolean found = _searchForTabFieldSet(metadataSetForEdition.getElements());
083        results.put("has-tab", found);
084        
085        return results;
086    }
087
088    /**
089     * Determines if at least one tab in present in this list of {@link AbstractMetadataSetElement}
090     * @param elements The list of {@link AbstractMetadataSetElement}
091     * @return true if one tab has been found.
092     */
093    private boolean _searchForTabFieldSet(List<AbstractMetadataSetElement> elements)
094    {
095        // First search in elements.
096        for (AbstractMetadataSetElement metadataSetElement : elements)
097        {
098            if (metadataSetElement instanceof Fieldset)
099            {
100                Fieldset fieldSet = (Fieldset) metadataSetElement;
101                
102                if ("tab".equals(fieldSet.getRole().toLowerCase()))
103                {
104                    return true;
105                }
106            }
107        }
108        
109        // Recursive search.
110        for (AbstractMetadataSetElement metadataSetElement : elements)
111        {
112            if (_searchForTabFieldSet(metadataSetElement.getElements()))
113            {
114                return true;
115            }
116        }
117        
118        return false;
119    }
120}