001/*
002 *  Copyright 2015 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.plugins.contentstree.ui;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.commons.lang3.StringUtils;
030
031import org.ametys.cms.contenttype.ContentAttributeDefinition;
032import org.ametys.cms.contenttype.ContentType;
033import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
034import org.ametys.cms.contenttype.ContentTypesHelper;
035import org.ametys.core.ui.StaticClientSideElement;
036import org.ametys.plugins.contentstree.AttributeTreeConfigurationElementsChild;
037import org.ametys.plugins.contentstree.ContentsTreeHelper;
038import org.ametys.plugins.contentstree.TreeConfiguration;
039import org.ametys.plugins.contentstree.TreeConfigurationContentType;
040import org.ametys.plugins.contentstree.TreeConfigurationElements;
041import org.ametys.plugins.contentstree.TreeConfigurationElementsChild;
042import org.ametys.plugins.contentstree.TreeExtensionPoint;
043import org.ametys.runtime.model.ModelItem;
044
045/**
046 * This client side element automatically add elements to the js configuration in relation with the <tree-config>
047 */
048public class TreeToolClientSideElement extends StaticClientSideElement
049{
050    /** The content types helper instance */
051    protected ContentsTreeHelper _treeHelper;
052    /** The tree configuration EP */
053    protected TreeExtensionPoint _treeExtensionPoint;
054    /** During startup will contains the configured tree config id. Null once initialized */
055    protected String _treeConfigId;
056    /** The content type helper */
057    protected ContentTypesHelper _contentTypesHelper;
058    /** The content type extension point */
059    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
060
061    @Override
062    public void service(ServiceManager smanager) throws ServiceException
063    {
064        super.service(smanager);
065        
066        _treeHelper = (ContentsTreeHelper) smanager.lookup(ContentsTreeHelper.ROLE);
067        _treeExtensionPoint = (TreeExtensionPoint) smanager.lookup(TreeExtensionPoint.ROLE);
068        _contentTypesHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
069        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
070    }
071    
072    @Override
073    public void configure(Configuration configuration) throws ConfigurationException
074    {
075        super.configure(configuration);
076        
077        _treeConfigId = configuration.getChild("tree-config").getValue();
078    }
079    
080    /**
081     * Lazy configuration of the component
082     */
083    protected void _lazyConfigure()
084    {
085        if (_treeConfigId != null)
086        {
087            TreeConfiguration treeConfiguration = _treeExtensionPoint.getExtension(_treeConfigId);
088
089            _script.getParameters().put("treeId", _treeConfigId);
090            
091            Set<String> busMessageTypesRegExp = new HashSet<>();
092            Set<String> contentTypeIdsRegExp = new HashSet<>();
093            Map<String, String> contenttypeAndMessagebustype = new HashMap<>();
094            Map<String, Map<String, List<String>>> contentTypeAndAttributePathsByContentType = new HashMap<>();
095            
096            for (TreeConfigurationElements element : treeConfiguration.getElements())
097            {
098                for (TreeConfigurationContentType contentTypeInfo : element.getContentTypesConfiguration())
099                {
100                    if (contentTypeInfo.canBeRoot())
101                    {
102                        busMessageTypesRegExp.add("^" + contentTypeInfo.getMessageBusType() + "$");
103                    }
104                    
105                    for (String id : contentTypeInfo.getContentTypesIds())
106                    {
107                        Map<String, List<String>> contentTypeAndAttributePaths = new HashMap<>();
108                        contentTypeAndAttributePathsByContentType.put(id, contentTypeAndAttributePaths);
109                        
110                        if (contentTypeInfo.canBeRoot())
111                        {
112                            contentTypeIdsRegExp.add("^" + id + "$");
113                        }
114                        
115                        contenttypeAndMessagebustype.put(id, contentTypeInfo.getMessageBusType());
116                        
117                        for (TreeConfigurationElementsChild treeConfigurationElementsChild : element.getChildren())
118                        {
119                            if (treeConfigurationElementsChild instanceof AttributeTreeConfigurationElementsChild)
120                            {
121                                AttributeTreeConfigurationElementsChild attributeTreeConfigurationElementsChild = (AttributeTreeConfigurationElementsChild) treeConfigurationElementsChild;
122                                String attributePath = attributeTreeConfigurationElementsChild.getPath();
123                                
124                                List<String> contentTypes = new ArrayList<>();
125                                contentTypeAndAttributePaths.put(attributePath, contentTypes);
126                                
127                                ContentType contentType = _contentTypeExtensionPoint.getExtension(id);
128                                ModelItem metadataDefinition = contentType.getModelItem(attributePath);
129                                if (metadataDefinition instanceof ContentAttributeDefinition)
130                                {
131                                    String targetContentTypeId = ((ContentAttributeDefinition) metadataDefinition).getContentTypeId();
132                                    contentTypes.add(targetContentTypeId);
133                                    contentTypes.addAll(_contentTypeExtensionPoint.getSubTypes(targetContentTypeId));
134                                }
135                            }
136                        }
137                    }
138                }
139            }
140            _script.getParameters().put("messagebustype-by-contenttype", contenttypeAndMessagebustype);
141            
142            _script.getParameters().put("metadatapaths-by-contenttype", contentTypeAndAttributePathsByContentType);
143            
144            _script.getParameters().put("selection-target-id", StringUtils.join(busMessageTypesRegExp, "|"));
145            
146            Map<String, Object> selectionTargetParameter = new HashMap<>();
147            selectionTargetParameter.put("name", "^types$");
148            selectionTargetParameter.put("value", StringUtils.join(contentTypeIdsRegExp, "|"));
149            
150            _script.getParameters().put("selection-target-parameter", selectionTargetParameter);
151        }
152    }
153    
154    @Override
155    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
156    {
157        _lazyConfigure();
158        
159        return super.getScripts(ignoreRights, contextParameters);
160    }
161}