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.cms.workflow;
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.xml.AttributesImpl;
024import org.apache.cocoon.xml.XMLUtils;
025import org.apache.commons.lang.StringUtils;
026import org.xml.sax.SAXException;
027
028import org.ametys.core.user.User;
029import org.ametys.core.user.UserManager;
030import org.ametys.core.util.cocoon.AbstractCurrentUserProviderServiceableGenerator;
031
032/**
033 * Workflow tasks generator.
034 */
035public class WorkflowTasksGenerator extends AbstractCurrentUserProviderServiceableGenerator
036{
037    /** The Users Manager. */
038    protected UserManager _userManager;
039    /** The task component */
040    protected WorkflowTasksComponent _tasksManager;
041    
042    @Override
043    public void service(ServiceManager serviceManager) throws ServiceException
044    {
045        super.service(serviceManager);
046        _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE);
047        _tasksManager = (WorkflowTasksComponent) serviceManager.lookup(WorkflowTasksComponent.ROLE);
048    }
049    
050    public void generate() throws IOException, SAXException, ProcessingException
051    {
052        String login = parameters.getParameter("login", "");
053        String populationId = parameters.getParameter("populationId", "");
054        if (StringUtils.isEmpty(login) || StringUtils.isEmpty(populationId))
055        {
056            login = _getCurrentUser().getLogin();
057            populationId = _getCurrentUser().getPopulationId();
058        }
059        
060        User user = _userManager.getUser(populationId, login);
061        
062        if (user == null)
063        {
064            getLogger().error("Unknown user : " + login);
065            throw new IllegalStateException("Unknown user : " + login);
066        }
067        
068        AttributesImpl attrs = new AttributesImpl();
069        attrs.addCDATAAttribute("user", user.getFullName());
070        attrs.addCDATAAttribute("username", login);
071        attrs.addCDATAAttribute("email", user.getEmail());
072        
073        contentHandler.startDocument();
074        XMLUtils.startElement(contentHandler, "todo-list", attrs);
075        
076        long begin = System.currentTimeMillis();
077        saxTasks (user);
078        
079        attrs.clear();
080        attrs.addCDATAAttribute("time", String.valueOf(System.currentTimeMillis() - begin));
081        
082        XMLUtils.createElement(contentHandler, "render", attrs);
083        
084        XMLUtils.endElement(contentHandler, "todo-list");
085        contentHandler.endDocument();
086    }
087    
088    /**
089     * SAX tasks
090     * @param user the user
091     * @throws SAXException If an error occurred
092     */
093    protected void saxTasks (User user) throws SAXException
094    {
095        String taskId = parameters.getParameter("taskId", "");
096        if (StringUtils.isEmpty(taskId))
097        {
098            _tasksManager.toSAX(contentHandler, user);
099        }
100        else
101        {
102            _tasksManager.toSAX(contentHandler, user, taskId);
103        }
104    }
105}