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.web.attachments; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.parameters.Parameters; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.cocoon.acting.ServiceableAction; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.environment.SourceResolver; 029 030import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032import org.ametys.plugins.repository.ModifiableAmetysObject; 033import org.ametys.plugins.repository.RepositoryConstants; 034import org.ametys.plugins.repository.TraversableAmetysObject; 035import org.ametys.web.repository.content.WebContent; 036import org.ametys.web.repository.page.jcr.DefaultPage; 037 038/** 039 * This action retrieves the id of the attachments root node of a {@link TraversableAmetysObject}.<br/>If the attachments node does not exist, it will be created. 040 */ 041public class AttachmentsRootNodeAction extends ServiceableAction 042{ 043 /** Constant for the attachment node name. */ 044 public static final String ATTACHMENTS_NODE_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":attachments"; 045 046 private AmetysObjectResolver _resolver; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 053 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 054 } 055 056 @Override 057 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 058 { 059 Map<String, String> result = new HashMap<>(); 060 061 Request request = ObjectModelHelper.getRequest(objectModel); 062 String id = request.getParameter("id"); 063 064 TraversableAmetysObject object = _resolver.resolveById(id); 065 066 TraversableAmetysObject attachments = null; 067 068 if (object instanceof DefaultPage) 069 { 070 attachments = ((DefaultPage) object).getRootAttachments(); 071 } 072 else if (object instanceof WebContent) 073 { 074 attachments = ((WebContent) object).getRootAttachments(); 075 } 076 077 if (attachments != null) 078 { 079 result.put("rootID", attachments.getId()); 080 if (attachments instanceof ModifiableAmetysObject) 081 { 082 result.put("isModifiable", "true"); 083 } 084 if (attachments instanceof ModifiableResourceCollection) 085 { 086 result.put("canCreateChild", "true"); 087 } 088 089 return result; 090 } 091 092 throw new IllegalArgumentException("Element with id '" + id + "' is of type '" + object.getClass().getName() + "' that does not support attachments."); 093 } 094}