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 */
016package org.ametys.odf.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021import java.util.Map;
022
023import org.ametys.cms.CmsConstants;
024import org.ametys.cms.clientsideelement.SmartContentClientSideElement;
025import org.ametys.cms.repository.Content;
026import org.ametys.core.ui.Callable;
027import org.ametys.plugins.repository.jcr.DefaultAmetysObject;
028import org.ametys.runtime.i18n.I18nizableText;
029
030/**
031 * Static client side element for exporting pedagogical booklet for live subprogram
032 */
033public class EducationalBookletClientSideElement extends SmartContentClientSideElement
034{
035    @Override
036    @Callable
037    public Map<String, Object> getStatus(List<String> contentsId)
038    {
039        Map<String, Object> results = super.getStatus(contentsId);
040        
041        results.put("no-live-version-contents", new ArrayList<Map<String, Object>>());
042        
043        List<Map<String, Object>> hasLiveVersionContent = new ArrayList<>();
044        
045        @SuppressWarnings("unchecked")
046        List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents");
047        for (Map<String, Object> allRightContent : allrightContents)
048        {
049            String contentId = (String) allRightContent.get("id");
050            Content content = _resolver.resolveById(contentId);
051            
052            String[] allLabels = ((DefaultAmetysObject) content).getAllLabels();
053            if (Arrays.asList(allLabels).contains(CmsConstants.LIVE_LABEL))
054            {
055                // Yes, the content has a live version
056                hasLiveVersionContent.add(allRightContent);
057            }
058            else
059            {
060                // No, content is still referenced
061                Map<String, Object> contentParams = getContentDefaultParameters (content);
062                contentParams.put("description", _getNoLiveVersionDescription(content));
063
064                @SuppressWarnings("unchecked")
065                List<Map<String, Object>> notInLiveContents = (List<Map<String, Object>>) results.get("no-live-version-contents");
066                notInLiveContents.add(contentParams);
067            }
068        }
069        
070        // All right contents are only contents which has a live version
071        results.put("allright-contents", hasLiveVersionContent);
072        
073        return results;
074    }
075    
076    /**
077     * Get i18n description when the content has no live version
078     * @param content The content
079     * @return The {@link I18nizableText} description
080     */
081    protected I18nizableText _getNoLiveVersionDescription (Content content)
082    {
083        List<String> workflowI18nParameters = new ArrayList<>();
084        workflowI18nParameters.add(_contentHelper.getTitle(content));
085        
086        I18nizableText ed = (I18nizableText) this._script.getParameters().get("no-live-version-content-description");
087        return new I18nizableText(ed.getCatalogue(), ed.getKey(), workflowI18nParameters);
088    }
089}