001/* 002 * Copyright 2014 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.plugins.explorer.threads.generators; 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.AttributesImpl; 027import org.apache.cocoon.xml.XMLUtils; 028import org.xml.sax.SAXException; 029 030import org.ametys.core.user.User; 031import org.ametys.core.user.UserIdentity; 032import org.ametys.core.user.UserManager; 033import org.ametys.plugins.explorer.threads.jcr.JCRPost; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.runtime.parameter.ParameterHelper; 036 037/** 038 * SAX a post 039 */ 040public class PostGenerator extends ServiceableGenerator 041{ 042 /** The resolver for ametys object */ 043 protected AmetysObjectResolver _resolver; 044 045 /** The user manager */ 046 protected UserManager _userManager; 047 048 @Override 049 public void service(ServiceManager sManager) throws ServiceException 050 { 051 super.service(sManager); 052 _resolver = (AmetysObjectResolver) sManager.lookup(AmetysObjectResolver.ROLE); 053 _userManager = (UserManager) sManager.lookup(UserManager.ROLE); 054 } 055 056 public void generate() throws IOException, SAXException, ProcessingException 057 { 058 Request request = ObjectModelHelper.getRequest(objectModel); 059 060 String postId = request.getParameter("id"); 061 JCRPost post = (JCRPost) _resolver.resolveById(postId); 062 063 contentHandler.startDocument(); 064 saxPost(post); 065 contentHandler.endDocument(); 066 } 067 068 /** 069 * SAX a post 070 * @param post the post 071 * @throws SAXException if an error occurred 072 */ 073 protected void saxPost (JCRPost post) throws SAXException 074 { 075 AttributesImpl atts = new AttributesImpl(); 076 atts.addCDATAAttribute("id", post.getId()); 077 078 XMLUtils.startElement(contentHandler, "post"); 079 080 XMLUtils.createElement(contentHandler, "id", post.getId()); 081 082 UserIdentity userIdentity = post.getAuthor(); 083 User user = _userManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin()); 084 String fullName = user == null ? userIdentity.getLogin() : user.getFullName(); 085 086 XMLUtils.createElement(contentHandler, "author", fullName); 087 XMLUtils.createElement(contentHandler, "authorLogin", userIdentity.getLogin()); 088 XMLUtils.createElement(contentHandler, "creationDate", ParameterHelper.valueToString(post.getCreationDate())); 089 090 XMLUtils.endElement(contentHandler, "post"); 091 } 092 093}