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 */ 016package org.ametys.cms.content.version; 017 018import java.util.Collections; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023 024import org.ametys.cms.repository.Content; 025import org.ametys.cms.rights.ContentRightAssignmentContext; 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 class CompareContentVersionClientSideElement 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 * @param version The current version 056 * @return The information 057 */ 058 @Callable(rights = Callable.READ_ACCESS, paramIndex = 0, rightContext = ContentRightAssignmentContext.ID) 059 public Map<String, Object> isComparable(String contentId, String version) 060 { 061 Content content = _resolver.resolveById(contentId); 062 if (!(content instanceof VersionableAmetysObject)) 063 { 064 // is not versionable 065 return Collections.EMPTY_MAP; 066 } 067 VersionableAmetysObject versionableContent = (VersionableAmetysObject) content; 068 069 if (version != null) 070 { 071 versionableContent.switchToRevision(version); 072 } 073 074 String currentVersion = getVersionToCompare(versionableContent); 075 String oldVersion = getBaseVersion(versionableContent); 076 if (oldVersion == null) 077 { 078 return Map.of("noBaseVersion", true); 079 } 080 081 if (oldVersion.equals(currentVersion)) 082 { 083 return Map.of("isSameVersion", true); 084 } 085 086 return Map.of( 087 "isComparable", true, 088 "version", currentVersion, 089 "baseVersion", oldVersion 090 ); 091 } 092 093 /** 094 * Gets the version to compare. By default, it is the current one. 095 * @param versionable The {@link VersionAwareAmetysObject} 096 * @return the source version 097 */ 098 protected String getVersionToCompare(VersionAwareAmetysObject versionable) 099 { 100 return _compareVersionHelper.getCurrentVersion(versionable); 101 } 102 103 /** 104 * Gets the base version of comparison. By default the previous one 105 * @param versionable The {@link VersionAwareAmetysObject} 106 * @return the base version 107 */ 108 protected String getBaseVersion(VersionAwareAmetysObject versionable) 109 { 110 return _compareVersionHelper.getPreviousVersion(versionable); 111 } 112}