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.cms.content.version;
017
018import java.util.Collections;
019import java.util.Map;
020import java.util.Optional;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.core.ui.Callable;
027import org.ametys.core.ui.ClientSideElement;
028import org.ametys.core.ui.StaticClientSideElement;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
031import org.ametys.plugins.repository.version.VersionableAmetysObject;
032
033/**
034 * {@link ClientSideElement} for the button for comparing a content between two version
035 */
036public abstract class AbstractCompareContentVersionClientSideElement extends StaticClientSideElement
037{
038    /** The Ametys object resolver */
039    protected AmetysObjectResolver _resolver;
040    /** The compare version helper */
041    protected CompareVersionHelper _compareVersionHelper;
042
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
048        _compareVersionHelper = (CompareVersionHelper) smanager.lookup(CompareVersionHelper.ROLE);
049    }
050    
051    /**
052     * Gets comparison-information about the given content
053     * <br>Information is if the content is comparable with its last validation version and the concerned versions.
054     * @param contentId The content id
055     * @return The information
056     */
057    @Callable
058    public Map<String, Object> isComparable(String contentId)
059    {
060        Content content = _resolver.resolveById(contentId);
061        if (!(content instanceof VersionableAmetysObject))
062        {
063            // is not versionable
064            return Collections.EMPTY_MAP;
065        }
066        VersionableAmetysObject versionableContent = (VersionableAmetysObject) content;
067        
068        String currentVersion = getVersionToCompare(versionableContent);
069        Optional<String> optionalLastValidationVersion = getBaseVersion(versionableContent);
070        if (optionalLastValidationVersion.isEmpty())
071        {
072            return Map.of("noBaseVersion", true);
073        }
074        
075        String lastValidationVersion = optionalLastValidationVersion.get();
076        if (lastValidationVersion.equals(currentVersion))
077        {
078            return Map.of("isSameVersion", true);
079        }
080        
081        return Map.of(
082                "isComparable", true,
083                "version", currentVersion,
084                "baseVersion", lastValidationVersion
085        );
086    }
087    
088    /**
089     * Gets the version to compare. By default, it is the current one.
090     * @param versionable The {@link VersionAwareAmetysObject}
091     * @return the source version
092     */
093    protected abstract String getVersionToCompare(VersionAwareAmetysObject versionable);
094    
095    /**
096     * Gets the base version of comparison.
097     * @param versionable The {@link VersionAwareAmetysObject}
098     * @return the base version
099     */
100    protected abstract Optional<String> getBaseVersion(VersionAwareAmetysObject versionable);
101    
102}