001/*
002 *  Copyright 2011 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.blog.posts;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.cocoon.xml.AttributesImpl;
021import org.apache.cocoon.xml.XMLUtils;
022import org.xml.sax.ContentHandler;
023import org.xml.sax.SAXException;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.core.user.User;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.core.user.UserManager;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.web.contenttype.WebContentType;
031
032/**
033 * Generates creator information in addition to the content data.
034 */
035public class PostContentType extends WebContentType
036{
037    
038    /** Post content type. */
039    public static final String CONTENT_TYPE = "org.ametys.plugins.blog.Content.post";
040    
041    /** The users manager. */
042    protected UserManager _userManager;
043    
044    @Override
045    public void service(ServiceManager serviceManager) throws ServiceException
046    {
047        super.service(serviceManager);
048        _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE);
049    }
050    
051    @Override
052    public void saxContentTypeAdditionalData(ContentHandler contentHandler, Content content) throws AmetysRepositoryException, SAXException
053    {
054        super.saxContentTypeAdditionalData(contentHandler, content);
055        
056        UserIdentity creatorIdentity = content.getCreator();
057        User creator = _userManager.getUser(creatorIdentity.getPopulationId(), creatorIdentity.getLogin());
058        
059        if (creator != null)
060        {
061            AttributesImpl attrs = new AttributesImpl();
062            attrs.addCDATAAttribute("login", creatorIdentity.getLogin());
063            attrs.addCDATAAttribute("population", creatorIdentity.getPopulationId());
064            attrs.addCDATAAttribute("fullName", creator.getFullName());
065            attrs.addCDATAAttribute("email", creator.getEmail());
066            
067            XMLUtils.createElement(contentHandler, "creator", attrs);
068        }
069    }
070    
071}