001/*
002 *  Copyright 2023 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.cms.workflow;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.Comparator;
022import java.util.List;
023import java.util.Map;
024
025import javax.jcr.RepositoryException;
026
027import org.apache.avalon.framework.parameters.Parameters;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.cocoon.acting.ServiceableAction;
031import org.apache.cocoon.environment.ObjectModelHelper;
032import org.apache.cocoon.environment.Redirector;
033import org.apache.cocoon.environment.Request;
034import org.apache.cocoon.environment.SourceResolver;
035
036import org.ametys.cms.repository.Content;
037import org.ametys.cms.workflow.history.VersionInformation;
038import org.ametys.core.cocoon.JSonReader;
039import org.ametys.core.util.DateUtils;
040import org.ametys.plugins.repository.AmetysObjectResolver;
041import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
042
043/**
044 * This action returns the jcr history of a content
045 */
046public class ContentVersionHistoryAction extends ServiceableAction
047{
048    /** The ametys object resolver */
049    protected AmetysObjectResolver _resolver;
050
051    @Override
052    public void service(ServiceManager serviceManager) throws ServiceException
053    {
054        super.service(serviceManager);
055        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
056    }
057
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        List<Object> result = new ArrayList<>();
061
062        Request request = ObjectModelHelper.getRequest(objectModel);
063
064        String id = request.getParameter("contentId");
065        Content content = _resolver.resolveById(id);
066
067        if (content instanceof VersionAwareAmetysObject versionContent)
068        {
069            for (VersionInformation versionInformation : _resolveVersionInformations(versionContent))
070            {
071                if (versionInformation.getLabels().contains("NotCompatible"))
072                {
073                    break;
074                }
075                
076                result.add(Map.of(
077                    "label", versionInformation.getVersionName(),
078                    "value", versionInformation.getVersionRawName(),
079                    "date", DateUtils.dateToString(versionInformation.getCreatedAt())
080                ));
081            }
082        }
083        
084        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
085        return EMPTY_MAP;
086    }
087    
088    /**
089     * Get the jcr info
090     * @param content The content
091     * @return The info
092     * @throws RepositoryException if an error occurred
093     */
094    protected List<VersionInformation> _resolveVersionInformations(VersionAwareAmetysObject content) throws RepositoryException
095    {
096        List<VersionInformation> versionsInformation = new ArrayList<>();
097
098        for (String revision : content.getAllRevisions())
099        {
100            VersionInformation versionInformation = new VersionInformation(revision, content.getRevisionTimestamp(revision));
101
102            for (String label : content.getLabels(revision))
103            {
104                versionInformation.addLabel(label);
105            }
106
107            versionsInformation.add(versionInformation);
108        }
109
110        // Sort by date descendant
111        Collections.sort(versionsInformation, new Comparator<VersionInformation>()
112        {
113            public int compare(VersionInformation o1, VersionInformation o2)
114            {
115                try
116                {
117                    return -o1.getCreatedAt().compareTo(o2.getCreatedAt());
118                }
119                catch (RepositoryException e)
120                {
121                    throw new RuntimeException("Unable to retrieve a creation date", e);
122                }
123            }
124        });
125
126        // Set the version name
127        int count = versionsInformation.size();
128        for (VersionInformation versionInformation : versionsInformation)
129        {
130            versionInformation.setVersionName(String.valueOf(count--));
131        }
132
133        return versionsInformation;
134    }
135}