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.odf.workflow.task;
017
018import java.util.List;
019import java.util.Optional;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.xml.AttributesImpl;
024import org.apache.cocoon.xml.XMLUtils;
025import org.xml.sax.ContentHandler;
026import org.xml.sax.SAXException;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.workflow.WorkflowTasksComponent;
030import org.ametys.core.user.User;
031import org.ametys.odf.ProgramItem;
032import org.ametys.odf.catalog.Catalog;
033import org.ametys.odf.catalog.CatalogsManager;
034import org.ametys.plugins.repository.query.expression.Expression;
035
036/**
037 * Override WorkflowTasksComponent to target ODF contents
038 */
039public abstract class AbstractOdfWorkflowTasksComponent extends WorkflowTasksComponent
040{
041    /** Catalogs manager */
042    protected CatalogsManager _catalogsManager;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _catalogsManager = (CatalogsManager) manager.lookup(CatalogsManager.ROLE);
049    }
050    
051    @Override
052    protected List<Expression> _getContentsAndExpressions(TaskStep step, User user)
053    {
054        List<Expression> exprs = super._getContentsAndExpressions(step, user);
055        
056        exprs.add(getContentTypeExpression());
057        
058        return exprs;
059    }
060
061    @Override
062    protected void _saxAdditionalData(ContentHandler ch, Content content, Task task) throws SAXException
063    {
064        super._saxAdditionalData(ch, content, task);
065        
066        if (content instanceof ProgramItem)
067        {
068            ProgramItem programItem = (ProgramItem) content;
069            
070            // Code
071            XMLUtils.createElement(ch, "code", programItem.getCode());
072            
073            // Catalog
074            Catalog catalog = Optional.of(programItem)
075                .map(ProgramItem::getCatalog)
076                .map(_catalogsManager::getCatalog)
077                .orElse(null);
078            if (catalog != null)
079            {
080                AttributesImpl attrs = new AttributesImpl();
081                attrs.addCDATAAttribute("id", catalog.getId());
082                XMLUtils.createElement(ch, "catalog", attrs, catalog.getTitle());
083            }
084        }
085    }
086    
087    /**
088     * Get the content type expression concerned by this component
089     * @return the content type expression
090     */
091    protected abstract Expression getContentTypeExpression ();
092}