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.services; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.environment.ObjectModelHelper; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.generation.ServiceableGenerator; 026import org.apache.cocoon.xml.XMLUtils; 027import org.apache.commons.lang.ArrayUtils; 028import org.apache.commons.lang3.StringUtils; 029import org.xml.sax.SAXException; 030 031import org.ametys.cms.contenttype.ContentTypesHelper; 032import org.ametys.cms.repository.DefaultContent; 033import org.ametys.odf.workflow.ValidateODFContentFunction; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.plugins.repository.AmetysRepositoryException; 036import org.ametys.runtime.model.ModelHelper; 037 038/** 039 * Class to parse an attribute from an ODF content in Live version. 040 */ 041public class ODFContentFieldGenerator extends ServiceableGenerator 042{ 043 /** The source resolver */ 044 protected AmetysObjectResolver _resolver; 045 046 /** Content type helper */ 047 protected ContentTypesHelper _contentTypesHelper; 048 049 @Override 050 public void service(ServiceManager serviceManager) throws ServiceException 051 { 052 super.service(serviceManager); 053 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 054 _contentTypesHelper = (ContentTypesHelper) serviceManager.lookup(ContentTypesHelper.ROLE); 055 } 056 057 @Override 058 public void generate() throws ProcessingException 059 { 060 Request request = ObjectModelHelper.getRequest(objectModel); 061 String contentId = request.getParameter("contentId"); 062 if (StringUtils.isEmpty(contentId)) 063 { 064 throw new ProcessingException("The content ID is empty."); 065 } 066 067 try 068 { 069 DefaultContent content = _resolver.resolveById(contentId); 070 071 // Is this an ODF content ? 072 if (!_contentTypesHelper.isInstanceOf(content, "org.ametys.plugins.odf.Content.odfContent")) 073 { 074 throw new ProcessingException(String.format("The content with the ID '%s' is not an ODF content.", contentId)); 075 } 076 077 // The content Have a Live version ? 078 String[] labels = content.getAllLabels(); 079 if (!ArrayUtils.contains(labels, ValidateODFContentFunction.VALID_LABEL)) 080 { 081 throw new ProcessingException(String.format("The content with the ID '%s' hasn't a Live version.", contentId)); 082 } 083 084 // Get the Live version to the content 085 content.switchToLabel(ValidateODFContentFunction.VALID_LABEL); 086 087 // Do we have an attribute name ? 088 String attributeName = request.getParameter("metadata"); 089 if (StringUtils.isEmpty(attributeName)) 090 { 091 throw new ProcessingException(String.format("No attribute has been transmitted for the content '%s'.", contentId)); 092 } 093 094 // The attribute exists ? 095 if (!ModelHelper.hasModelItem(attributeName, content.getModel())) 096 { 097 throw new ProcessingException(String.format("There is no attribute named '%s' for the content '%s'.", attributeName, contentId)); 098 } 099 100 101 // Generate SAX events for the attribute 102 contentHandler.startDocument(); 103 XMLUtils.startElement(contentHandler, "metadata"); 104 105 content.dataToSAX(contentHandler, attributeName); 106 107 XMLUtils.endElement(contentHandler, "metadata"); 108 contentHandler.endDocument(); 109 } 110 catch (AmetysRepositoryException e) 111 { 112 throw new ProcessingException(String.format("Unable to get the content with the ID '%s'.", contentId), e); 113 } 114 catch (SAXException e) 115 { 116 throw new ProcessingException("Error while saxing the document.", e); 117 } 118 catch (IOException e) 119 { 120 throw new ProcessingException(String.format("Error while saxing the content '%s' on the 'export' metadata set.", contentId), e); 121 } 122 } 123}