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.thesaurus.content;
017
018import java.util.HashMap;
019import java.util.Map;
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.contenttype.DefaultContentType;
029import org.ametys.cms.repository.Content;
030import org.ametys.plugins.repository.AmetysRepositoryException;
031import org.ametys.plugins.thesaurus.MicroThesaurus;
032import org.ametys.plugins.thesaurus.Thesaurus;
033import org.ametys.plugins.thesaurus.ThesaurusDAO;
034
035/**
036 * Specific content type for thesaurus items
037 *
038 */
039public class ThesaurusItemContentType extends DefaultContentType
040{
041    /** Content type id for term */
042    public static final String ITEM_CONTENT_TYPE_ID = "org.ametys.plugins.thesaurus.Content.item";
043    /** Content type id for term */
044    public static final String TERM_CONTENT_TYPE_ID = "org.ametys.plugins.thesaurus.Content.term";
045    /** Content type id for candidate */
046    public static final String CANDIDAT_CONTENT_TYPE_ID = "org.ametys.plugins.thesaurus.Content.candidate";
047    
048    private ThesaurusDAO _thesaurusDAO;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _thesaurusDAO = (ThesaurusDAO) smanager.lookup(ThesaurusDAO.ROLE);
055    }
056    
057    @Override
058    public void saxContentTypeAdditionalData(ContentHandler contentHandler, Content content) throws AmetysRepositoryException, SAXException
059    {
060        super.saxContentTypeAdditionalData(contentHandler, content);
061        
062        AttributesImpl attrs = new AttributesImpl();
063        MicroThesaurus microThesaurus = _thesaurusDAO.getParentMicrothesaurus(content);
064        attrs.addCDATAAttribute("id", microThesaurus.getId());
065        attrs.addCDATAAttribute("label", microThesaurus.getLabel());
066        
067        XMLUtils.createElement(contentHandler, "microthesaurus", attrs);
068        
069        attrs = new AttributesImpl();
070        Thesaurus thesaurus = _thesaurusDAO.getParentThesaurus(microThesaurus);
071        attrs.addCDATAAttribute("id", thesaurus.getId());
072        attrs.addCDATAAttribute("label", thesaurus.getLabel());
073        
074        XMLUtils.createElement(contentHandler, "thesaurus", attrs);
075        
076        attrs = new AttributesImpl();
077        Content genericTerm = _thesaurusDAO.getGenericTerm(content);
078        
079        if (genericTerm != null)
080        {
081            attrs.addCDATAAttribute("id", genericTerm.getId());
082            attrs.addCDATAAttribute("label", genericTerm.getTitle());
083            
084            XMLUtils.createElement(contentHandler, "genericTerm", attrs);
085        }
086        
087        int numberOfIndexedContents = _thesaurusDAO.getNumberOfIndexedContents(content.getId());
088        XMLUtils.createElement(contentHandler, "numberOfIndexedContents", String.valueOf(numberOfIndexedContents));
089    }
090    
091    @Override
092    public Map<String, Object> getAdditionalData(Content content)
093    {
094        Map<String, Object> additionalData = new HashMap<>();
095        
096        MicroThesaurus microthesaurus = _thesaurusDAO.getParentMicrothesaurus(content);
097        additionalData.put("microthesaurusId", microthesaurus.getId());
098        
099        Thesaurus thesaurus = _thesaurusDAO.getParentThesaurus(microthesaurus);
100        additionalData.put("thesaurusId", thesaurus.getId());
101        
102        if (TERM_CONTENT_TYPE_ID.equals(_id))
103        {
104            Content genericTerm = _thesaurusDAO.getGenericTerm(content);
105            if (genericTerm != null)
106            {
107                additionalData.put("parentId", genericTerm.getId());
108            }
109        }
110        else if (CANDIDAT_CONTENT_TYPE_ID.equals(_id))
111        {
112            additionalData.put("parentId", content.getParent().getId());
113            
114            String comment = content.getMetadataHolder().getString("comment", "");
115            additionalData.put("comment", comment);
116        }
117        
118        additionalData.put("path", _thesaurusDAO.getTermPath(content));
119        
120        return additionalData;
121    }
122}