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 047 public Map<String, Object> getStatus(List<String> contentsId) 048 { 049 Map<String, Object> results = super.getStatus(contentsId); 050 051 results.put("referenced-contents", new ArrayList<>()); 052 053 List<Map<String, Object>> orphanContents = new ArrayList<>(); 054 055 @SuppressWarnings("unchecked") 056 List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents"); 057 for (Map<String, Object> allRightContent : allrightContents) 058 { 059 String contentId = (String) allRightContent.get("id"); 060 Content content = _resolver.resolveById(contentId); 061 062 // Is orphan content ? 063 if (!_isContentReferenced(content)) 064 { 065 // Yes, this is a 'all-right' content 066 orphanContents.add(allRightContent); 067 } 068 else 069 { 070 // No, content is still referenced 071 Map<String, Object> contentParams = getContentDefaultParameters (content); 072 contentParams.put("description", _getReferencedDescription(content)); 073 074 @SuppressWarnings("unchecked") 075 List<Map<String, Object>> referencedContents = (List<Map<String, Object>>) results.get("referenced-contents"); 076 referencedContents.add(contentParams); 077 } 078 } 079 080 // All right contents are only orphan contents 081 results.put("allright-contents", orphanContents); 082 083 return results; 084 } 085 086 /** 087 * Test if content is still referenced before removing it 088 * @param content The content to remove 089 * @return true if content is still referenced 090 */ 091 protected boolean _isContentReferenced (Content content) 092 { 093 return content.hasReferencingContents(); 094 } 095 096 /** 097 * Get i18n description when the content is orphan 098 * @param content The content 099 * @return The {@link I18nizableText} description 100 */ 101 protected I18nizableText _getReferencedDescription (Content content) 102 { 103 List<String> workflowI18nParameters = new ArrayList<>(); 104 workflowI18nParameters.add(_contentHelper.getTitle(content)); 105 106 I18nizableText ed = (I18nizableText) this._script.getParameters().get("referenced-content-description"); 107 return new I18nizableText(ed.getCatalogue(), ed.getKey(), workflowI18nParameters); 108 } 109}