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.web.contenttype;
017
018import java.io.IOException;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang.StringUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.repository.page.ContentTypesAssignmentHandler;
036import org.ametys.web.repository.site.Site;
037import org.ametys.web.repository.site.SiteManager;
038
039/**
040 * SAX common content types available for the given site(s)
041 */
042public class CommonAvailablesContentTypesGenerator extends ServiceableGenerator
043{
044    private ContentTypesAssignmentHandler _cTypeHandler;
045    private SiteManager _siteManager;
046    private ContentTypeExtensionPoint _cTypeEP;
047    
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _cTypeHandler = (ContentTypesAssignmentHandler) smanager.lookup(ContentTypesAssignmentHandler.ROLE);
053        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
054        _cTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
055    }
056
057    @Override
058    public void generate() throws IOException, SAXException, ProcessingException
059    {
060        Request request = ObjectModelHelper.getRequest(objectModel);
061        String[] siteNames = request.getParameterValues("siteName");
062        boolean includePrivate = Boolean.valueOf(request.getParameter("includePrivate"));
063        String allOption = request.getParameter("allOption");
064        
065        Set<String> commonCTypes = new HashSet<>();
066        commonCTypes.addAll(_cTypeEP.getExtensionsIds());
067        
068        if (siteNames != null)
069        {
070            for (String siteName : siteNames)
071            {
072                Site site = _siteManager.getSite(siteName);
073                Set<String> availableContentTypes = _cTypeHandler.getAvailableContentTypes(site, includePrivate);
074                // join sets
075                commonCTypes.retainAll(availableContentTypes);
076            }
077        }
078        
079        contentHandler.startDocument();
080        XMLUtils.startElement(contentHandler, "content-types");
081        
082        for (String cTypeId : commonCTypes)
083        {
084            AttributesImpl attrs = new AttributesImpl();
085            attrs.addAttribute("", "id", "id", "CDATA", cTypeId);
086            
087            XMLUtils.startElement(contentHandler, "content-type", attrs);
088            _cTypeEP.getExtension(cTypeId).getLabel().toSAX(contentHandler, "label");
089            XMLUtils.endElement(contentHandler, "content-type");
090        }
091        
092        // '- All-' option
093        if (allOption != null && commonCTypes.size() != 0)
094        {
095            AttributesImpl attrs = new AttributesImpl();
096            attrs.addAttribute("", "id", "id", "CDATA", allOption.equals("concat") ? StringUtils.join(commonCTypes, ',') : "");
097            
098            XMLUtils.startElement(contentHandler, "content-type", attrs);
099            new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS").toSAX(contentHandler, "label");
100            XMLUtils.endElement(contentHandler, "content-type");
101        }
102        
103        XMLUtils.endElement(contentHandler, "content-types");
104        contentHandler.endDocument();
105    }
106
107}