001/*
002 *  Copyright 2020 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 */
016
017package org.ametys.plugins.odfpilotage.clientsideelement;
018
019import java.util.Comparator;
020import java.util.LinkedHashMap;
021import java.util.Locale;
022import java.util.Map;
023import java.util.Optional;
024import java.util.stream.Collectors;
025
026import org.apache.avalon.framework.context.Context;
027import org.apache.avalon.framework.context.ContextException;
028import org.apache.avalon.framework.context.Contextualizable;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.cocoon.components.ContextHelper;
032import org.apache.cocoon.i18n.I18nUtils;
033
034import org.ametys.cms.data.ContentValue;
035import org.ametys.odf.enumeration.OdfReferenceTableEntry;
036import org.ametys.odf.enumeration.OdfReferenceTableHelper;
037import org.ametys.plugins.contentstree.ui.TreeToolClientSideElement;
038
039/**
040 * Override the TreeTooClientSideElement to retrieve informations for the CostModeling 
041 */
042public class CostModelingClientSideElement extends TreeToolClientSideElement implements Contextualizable
043{
044    /** The Avalon role name */
045    public static final String ROLE = CostModelingClientSideElement.class.getName();
046    /** The ODF enumeration helper */
047    protected OdfReferenceTableHelper _refTableHelper;
048    /** The context */
049    protected Context _context;
050    
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        super.service(smanager);
055        _refTableHelper = (OdfReferenceTableHelper) smanager.lookup(OdfReferenceTableHelper.ROLE); 
056    }
057    
058    public void contextualize(Context context) throws ContextException
059    {
060        _context = context;
061    }
062    
063    @Override
064    protected void _lazyConfigure()
065    {
066        super._lazyConfigure();
067        _script.getParameters().put("enseignement-natures", getEnseignementNatures());
068    }
069    
070    /**
071     * Retrieve and get all teaching nature in a JSON map 
072     * @return a JSON map containing the grid structure for all teaching nature
073     */
074    public Map<String, Map<String, Object>> getEnseignementNatures()
075    {
076        Map objectModel = ContextHelper.getObjectModel(_context);
077        Locale locale = I18nUtils.findLocale(objectModel, "locale", null, Locale.getDefault(), true);
078        String lang = locale.getLanguage();
079        
080        return _refTableHelper.getItems(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE)
081            .stream()
082            .sequential()
083            .sorted(Comparator
084                .comparingLong(this::_orderFromCategory)
085                .thenComparingLong(OdfReferenceTableEntry::getOrder)
086                .thenComparing(OdfReferenceTableEntry::getCode)
087            )
088            .collect(
089                Collectors.toMap(
090                    OdfReferenceTableEntry::getCode,
091                    entry -> Map.of(
092                        "id", entry.getId(),
093                        "label", entry.getLabel(lang),
094                        "archived", entry.isArchived()
095                    ),
096                    (a, b) -> a,
097                    LinkedHashMap::new
098                )
099            );
100    }
101    
102    private Long _orderFromCategory(OdfReferenceTableEntry referenceTableEntry)
103    {
104        return Optional.of(referenceTableEntry)
105                .map(OdfReferenceTableEntry::getContent)
106                .map(c -> c.<ContentValue>getValue("category"))
107                .flatMap(ContentValue::getContentIfExists)
108                .map(c -> c.getValue("order", false, Long.MAX_VALUE))
109                .orElse(Long.MAX_VALUE);
110    }
111}