001/* 002 * Copyright 2018 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.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.repository.ModifiableContent; 027import org.ametys.core.right.RightManager.RightResult; 028import org.ametys.core.ui.Callable; 029import org.ametys.core.ui.StaticClientSideElement; 030import org.ametys.odf.cdmfr.CDMFRHandler; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032 033/** 034 * Display the button only if the 'odf.publish.server' parameter is checked. 035 */ 036public class RepublishContentClientSideElement extends StaticClientSideElement 037{ 038 /** The CDM-fr handler */ 039 protected CDMFRHandler _cdmfrHandler; 040 041 /** The ametys object resolver */ 042 protected AmetysObjectResolver _resolver; 043 044 @Override 045 public void service(ServiceManager smanager) throws ServiceException 046 { 047 super.service(smanager); 048 _cdmfrHandler = (CDMFRHandler) smanager.lookup(CDMFRHandler.ROLE); 049 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 050 } 051 052 @Override 053 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 054 { 055 if (_cdmfrHandler.needToHandle()) 056 { 057 return super.getScripts(ignoreRights, contextParameters); 058 } 059 060 return new ArrayList<>(); 061 } 062 063 /** 064 * Publish CDM-fr for the given contents 065 * @param contentIds the content ids 066 * @return the result 067 */ 068 @Callable (rights = Callable.SKIP_BUILTIN_CHECK) 069 public Map<String, Object> publish(List<String> contentIds) 070 { 071 Map<String, Object> results = new HashMap<>(); 072 073 try 074 { 075 List<ModifiableContent> contents = contentIds.stream() 076 .map(this::_getContent) 077 .toList(); 078 079 _cdmfrHandler.handleCDMFR(contents); 080 } 081 catch (IllegalAccessError e) 082 { 083 results.put("error", "no-right"); 084 } 085 catch (Exception e) 086 { 087 results.put("error", "error"); 088 } 089 090 return results; 091 } 092 093 private ModifiableContent _getContent(String contentId) 094 { 095 ModifiableContent content = _resolver.resolveById(contentId); 096 if (_rightManager.currentUserHasRight("ODF_Rights_Program_Validate", content) != RightResult.RIGHT_ALLOW) 097 { 098 throw new IllegalAccessError("Cannot republish content with id '" + content.getId() + "' without convenient rights"); 099 } 100 101 return content; 102 } 103 104}