001/* 002 * Copyright 2010 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.Content; 027import org.ametys.core.ui.Callable; 028import org.ametys.odf.helper.DeleteODFContentHelper; 029import org.ametys.odf.orgunit.OrgUnit; 030import org.ametys.odf.orgunit.RootOrgUnitProvider; 031import org.ametys.plugins.repository.UnknownAmetysObjectException; 032 033/** 034 * This element creates an action button to delete a ODF content 035 */ 036public class DeleteContentClientSideElement extends org.ametys.cms.clientsideelement.DeleteContentClientSideElement 037{ 038 /** The root orgunit provider */ 039 protected RootOrgUnitProvider _rootOrgUnitProvider; 040 041 /** The delete ODF content helper */ 042 protected DeleteODFContentHelper _deleteODFContentHelper; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 super.service(manager); 048 _rootOrgUnitProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE); 049 _deleteODFContentHelper = (DeleteODFContentHelper) manager.lookup(DeleteODFContentHelper.ROLE); 050 } 051 052 @Override 053 protected boolean _hasRight(Content content) 054 { 055 return _deleteODFContentHelper.hasRight(content); 056 } 057 058 @Override 059 protected boolean _isModifiable(Content content) 060 { 061 if (content instanceof OrgUnit && _rootOrgUnitProvider.isRoot(content.getId())) 062 { 063 // The root orgunit can not be deleted 064 return false; 065 } 066 067 return super._isModifiable(content); 068 } 069 070 @Override 071 protected boolean _isContentReferenced(Content content) 072 { 073 return _deleteODFContentHelper.isContentReferenced(content); 074 } 075 076 /** 077 * Delete ODF contents 078 * @param contentsId The ids of contents to delete 079 * @param parameters the additional parameters 080 * @return the deleted and undeleted contents 081 */ 082 @SuppressWarnings("unchecked") 083 @Callable 084 public Map<String, Object> deleteContents(List<String> contentsId, Map<String, Object> parameters) 085 { 086 Map<String, Object> results = _deleteODFContentHelper.deleteContents(contentsId, (String) parameters.get("mode")); 087 088 Map<String, Object> updatedResults = new HashMap<>(); 089 090 for (String deletedContentId : results.keySet()) 091 { 092 Map<String, Object> updatedContentResults = new HashMap<>(); 093 094 // Update the description for locked contents 095 List<Map<String, Object>> lockedContents = (List<Map<String, Object>>) ((Map<String, Object>) results.get(deletedContentId)).get("locked-contents"); 096 List<Map<String, Object>> updatedLockedContents = new ArrayList<>(); 097 for (Map<String, Object> lockedContent : lockedContents) 098 { 099 String contentId = (String) lockedContent.get("id"); 100 try 101 { 102 Content content = _resolver.resolveById(contentId); 103 lockedContent.put("description", _getLockedDescription(content)); 104 } 105 catch (UnknownAmetysObjectException e) 106 { 107 getLogger().warn("The content {} may be deleted during the operation. It's impossible to retrieve it to get the locked description.", contentId); 108 } 109 updatedLockedContents.add(lockedContent); 110 } 111 updatedContentResults.put("locked-contents", updatedLockedContents); 112 113 // Update the description for unauthorized contents 114 List<Map<String, Object>> unauthorizedContents = (List<Map<String, Object>>) ((Map<String, Object>) results.get(deletedContentId)).get("unauthorized-contents"); 115 List<Map<String, Object>> updatedUnauthorizedContents = new ArrayList<>(); 116 for (Map<String, Object> unauthorizedContent : unauthorizedContents) 117 { 118 String contentId = (String) unauthorizedContent.get("id"); 119 try 120 { 121 Content content = _resolver.resolveById(contentId); 122 unauthorizedContent.put("description", _getNoRightDescription(content)); 123 } 124 catch (UnknownAmetysObjectException e) 125 { 126 getLogger().warn("The content {} may be deleted during the operation. It's impossible to retrieve it to get the unauthorized description.", contentId); 127 } 128 updatedUnauthorizedContents.add(unauthorizedContent); 129 } 130 updatedContentResults.put("unauthorized-contents", updatedUnauthorizedContents); 131 132 updatedResults.put(deletedContentId, updatedContentResults); 133 } 134 135 return results; 136 } 137}