001/*
002 *  Copyright 2018 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.content.referencetable.search;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Locale;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang3.LocaleUtils;
026
027import org.ametys.cms.contenttype.ContentAttributeDefinition;
028import org.ametys.cms.contenttype.ContentType;
029import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
030import org.ametys.cms.repository.Content;
031import org.ametys.core.ui.Callable;
032import org.ametys.core.ui.ClientSideElement;
033import org.ametys.core.ui.StaticClientSideElement;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.runtime.model.ModelItem;
036
037/**
038 * {@link ClientSideElement} for search tool on contents with the same referencing values for a given attribute.
039 */
040public class SearchReferencingContentsWithSameValuesClientSideElement extends StaticClientSideElement
041{
042    private AmetysObjectResolver _resolver;
043    private ContentTypeExtensionPoint _cTypeEP;
044
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
050        _cTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
051    }
052    
053    /**
054     * Get the search context
055     * @param initialContentId The id of initial content (from which the search was launched)
056     * @param attributePath The path of the attribute
057     * @param refIds The current values for this content and attribute
058     * @return The search context
059     */
060    @Callable
061    public Map<String, Object> getSearchContext(String initialContentId, String attributePath, List<String> refIds)
062    {
063        Map<String, Object> searchCtx = new HashMap<>();
064        
065        Content initialContent = _resolver.resolveById(initialContentId);
066        
067        Map<String, Object> initialContentInfos = new HashMap<>();
068        initialContentInfos.put("contentId", initialContentId);
069        initialContentInfos.put("contentTitle", initialContent.getTitle());
070        searchCtx.put("initialContent", initialContentInfos);
071        
072        if (initialContent.hasDefinition(attributePath))
073        {
074            ModelItem modelItem = initialContent.getDefinition(attributePath);
075            Map<String, Object> attributeInfos = new HashMap<>();
076            
077            attributeInfos.put("name", modelItem.getName());
078            attributeInfos.put("path", modelItem.getPath());
079            attributeInfos.put("label", modelItem.getLabel());
080            
081            ContentType refContentType = (ContentType) modelItem.getModel();
082            attributeInfos.put("refContentType", refContentType.getLabel());
083            
084            searchCtx.put("attribute", attributeInfos);
085            
086            if (modelItem instanceof ContentAttributeDefinition)
087            {
088                String refTableId = ((ContentAttributeDefinition) modelItem).getContentTypeId();
089                if (refTableId != null)
090                {
091                    Map<String, Object> refTableInfos = new HashMap<>();
092                    
093                    ContentType refTable = _cTypeEP.getExtension(refTableId);
094                    refTableInfos.put("id", refTableId);
095                    refTableInfos.put("label", refTable.getLabel());
096                    
097                    searchCtx.put("referenceTable", refTableInfos);
098                }
099            }
100        }
101        
102        Locale defaultLocale = initialContent.getLanguage() != null ? LocaleUtils.toLocale(initialContent.getLanguage()) : null;
103
104        Map<String, String> refValues = new HashMap<>();
105        for (String refId : refIds)
106        {
107            Content refContent = _resolver.resolveById(refId);
108            refValues.put(refId, refContent.getTitle(defaultLocale));
109        }
110        
111        searchCtx.put("refValues", refValues);
112        
113        return searchCtx;
114    }
115}