001/* 002 * Copyright 2013 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.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.repository.Content; 026import org.ametys.core.observation.ObservationManager; 027import org.ametys.core.ui.Callable; 028import org.ametys.runtime.i18n.I18nizableText; 029 030/** 031 * This element creates a ribbon button to delete a content 032 */ 033public class DeleteContentClientSideElement extends SmartContentClientSideElement 034{ 035 /** Observer manager. */ 036 protected ObservationManager _observationManager; 037 038 @Override 039 public void service(ServiceManager smanager) throws ServiceException 040 { 041 super.service(smanager); 042 _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE); 043 } 044 045 @Override 046 @Callable(rights = Callable.CHECKED_BY_IMPLEMENTATION) 047 public Map<String, Object> getStatus(List<String> contentsId) 048 { 049 // perform the right checks on every contents 050 // all contents in "allright-contents" are allowed 051 Map<String, Object> results = super.getStatus(contentsId); 052 053 results.put("referenced-contents", new ArrayList<>()); 054 055 List<Map<String, Object>> orphanContents = new ArrayList<>(); 056 057 @SuppressWarnings("unchecked") 058 List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents"); 059 for (Map<String, Object> allRightContent : allrightContents) 060 { 061 String contentId = (String) allRightContent.get("id"); 062 Content content = _resolver.resolveById(contentId); 063 064 // Is orphan content ? 065 if (!_isContentReferenced(content)) 066 { 067 // Yes, this is a 'all-right' content 068 orphanContents.add(allRightContent); 069 } 070 else 071 { 072 // No, content is still referenced 073 Map<String, Object> contentParams = getContentDefaultParameters (content); 074 contentParams.put("description", _getReferencedDescription(content)); 075 076 @SuppressWarnings("unchecked") 077 List<Map<String, Object>> referencedContents = (List<Map<String, Object>>) results.get("referenced-contents"); 078 referencedContents.add(contentParams); 079 } 080 } 081 082 // All right contents are only orphan contents 083 results.put("allright-contents", orphanContents); 084 085 return results; 086 } 087 088 /** 089 * Test if content is still referenced before removing it 090 * @param content The content to remove 091 * @return true if content is still referenced 092 */ 093 protected boolean _isContentReferenced (Content content) 094 { 095 return content.hasReferencingContents(); 096 } 097 098 /** 099 * Get i18n description when the content is orphan 100 * @param content The content 101 * @return The {@link I18nizableText} description 102 */ 103 protected I18nizableText _getReferencedDescription (Content content) 104 { 105 List<String> workflowI18nParameters = new ArrayList<>(); 106 workflowI18nParameters.add(_contentHelper.getTitle(content)); 107 108 I18nizableText ed = (I18nizableText) this._script.getParameters().get("referenced-content-description"); 109 return new I18nizableText(ed.getCatalogue(), ed.getKey(), workflowI18nParameters); 110 } 111}