001/* 002 * Copyright 2017 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.plugins.contentio.synchronize.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024 025import org.ametys.cms.clientsideelement.SmartContentClientSideElement; 026import org.ametys.cms.repository.ModifiableDefaultContent; 027import org.ametys.core.ui.Callable; 028import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 029import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper; 030import org.ametys.plugins.core.ui.log.LogManager; 031import org.ametys.runtime.authentication.AccessDeniedException; 032 033/** 034 * Smart content client side element for SCC, the SCC model ID configured in plugin.xml is used. 035 */ 036public class SCCSmartContentClientSideElement extends SmartContentClientSideElement 037{ 038 /** The log manager */ 039 protected LogManager _logManager; 040 /** SCC helper */ 041 protected SynchronizableContentsCollectionHelper _sccHelper; 042 043 @Override 044 public void service(ServiceManager smanager) throws ServiceException 045 { 046 super.service(smanager); 047 _logManager = (LogManager) smanager.lookup(LogManager.ROLE); 048 _sccHelper = (SynchronizableContentsCollectionHelper) smanager.lookup(SynchronizableContentsCollectionHelper.ROLE); 049 } 050 051 @Override 052 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 053 { 054 List<Script> clonedScripts = new ArrayList<>(); 055 056 List<Script> scripts = super.getScripts(ignoreRights, contextParameters); 057 058 for (Script script : scripts) 059 { 060 if (script.getParameters().containsKey("sccModelId")) 061 { 062 String sccModelId = (String) script.getParameters().get("sccModelId"); 063 SynchronizableContentsCollection collection = _sccHelper.getSCCFromModelId(sccModelId); 064 if (collection != null) 065 { 066 clonedScripts.add(_getScriptFromCollection(script, collection)); 067 } 068 } 069 } 070 071 return clonedScripts; 072 } 073 074 /** 075 * Get a cloned script by updating configuration with collection values. 076 * @param script The script to clone 077 * @param collection The collection 078 * @return A cloned script 079 */ 080 protected Script _getScriptFromCollection(Script script, SynchronizableContentsCollection collection) 081 { 082 Script clonedScript = new Script(script); 083 clonedScript.getParameters().put("collectionId", collection.getId()); 084 return clonedScript; 085 } 086 087 /** 088 * Synchronize the content on the given collection with the given synchronization code. 089 * @param collectionId Collection ID 090 * @param contentId Content ID 091 * @param syncCode Synchronization code 092 * @return true if an error occurred 093 */ 094 @Callable(rights = Callable.CHECKED_BY_IMPLEMENTATION) 095 public boolean synchronizeContent(String collectionId, String contentId, String syncCode) 096 { 097 ModifiableDefaultContent content = _resolver.resolveById(contentId); 098 if (!_hasRight(content)) 099 { 100 throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' tried to synchronize content '" + contentId + "' without sufficient right"); 101 } 102 103 return _sccHelper.synchronizeContent(collectionId, contentId, syncCode); 104 } 105 106 /** 107 * Get the value of the synchronization field. 108 * @param collectionId Collection ID 109 * @param contentId Content ID 110 * @return The value of the synchronization field 111 */ 112 @Callable(rights = Callable.CHECKED_BY_IMPLEMENTATION) 113 public String getSyncCode(String contentId, String collectionId) 114 { 115 ModifiableDefaultContent content = _resolver.resolveById(contentId); 116 if (!_hasRight(content)) 117 { 118 throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' tried to get synchronization code of content '" + contentId + "' without sufficient right"); 119 } 120 121 return _sccHelper.getSyncCode(contentId, collectionId); 122 } 123 124 /** 125 * Get event logs 126 * @param timestamp Events after this timestamp will be retrieved 127 * @param categories Events will be filtered by these categories. If empty, all categories configured by the client side are accepted. 128 * @return the target types 129 */ 130 @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION) 131 public List<Map<String, Object>> getEvents (Long timestamp, List<String> categories) 132 { 133 if (!hasRight(getRights(Map.of()))) 134 { 135 throw new AccessDeniedException("The user '" + _currentUserProvider.getUser() + "' try to get logs without sufficient rights"); 136 } 137 138 return _logManager.getEvents(timestamp, List.of("org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper")); 139 } 140}