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.web.contenttype;
017
018import java.util.HashMap;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.cocoon.components.ContextHelper;
030import org.apache.cocoon.environment.Request;
031import org.apache.commons.lang.StringUtils;
032
033import org.ametys.cms.contenttype.ContentType;
034import org.ametys.cms.contenttype.ContentTypeEnumerator;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.web.repository.page.ContentTypesAssignmentHandler;
037import org.ametys.web.repository.site.Site;
038import org.ametys.web.repository.site.SiteManager;
039
040/**
041 * Enumerator for {@link ContentType}
042 *
043 */
044public class AvailableContentTypesEnumerator extends ContentTypeEnumerator implements Contextualizable
045{
046    /** The content types assignment handler */
047    protected ContentTypesAssignmentHandler _cTypeHandler;
048    
049    /** The site manager */
050    protected SiteManager _siteManager;
051    
052    /** The avalon context */
053    protected Context _context;
054    
055    /** True to include private content types */
056    protected boolean _includePrivate;
057    
058    @Override
059    public void service(ServiceManager smanager) throws ServiceException
060    {
061        super.service(smanager);
062        _cTypeHandler = (ContentTypesAssignmentHandler) smanager.lookup(ContentTypesAssignmentHandler.ROLE);
063        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
064    }
065    
066    @Override
067    public void configure(Configuration configuration) throws ConfigurationException
068    {
069        super.configure(configuration);
070        Configuration privateConf = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("include-private", false);
071        _includePrivate = privateConf != null ? privateConf.getValueAsBoolean(false) : false;
072    }
073    
074    @Override
075    public void contextualize(Context context) throws ContextException
076    {
077        _context = context;
078    }
079    
080    @Override
081    public Map<Object, I18nizableText> getEntries() throws Exception
082    {
083        Map<Object, I18nizableText> entries = new HashMap<>();
084        
085        Request request = ContextHelper.getRequest(_context);
086       
087        String siteName = (String) request.getAttribute("siteName");
088        if (siteName == null)
089        {
090            siteName = (String) request.getAttribute("site");
091        }
092        
093        if (siteName != null)
094        {
095            Site site = _siteManager.getSite(siteName);
096            Set<String> cTypeIds = _cTypeHandler.getAvailableContentTypes(site, _includePrivate);
097            
098            for (String cTypeId : cTypeIds)
099            {
100                ContentType cType = _cTypeExtPt.getExtension(cTypeId);
101                entries.put(cTypeId, cType.getLabel());
102            }
103        }
104        else
105        {
106            Set<String> cTypeIds = _cTypeExtPt.getExtensionsIds();
107            for (String cTypeId : cTypeIds)
108            {
109                ContentType cType = _cTypeExtPt.getExtension(cTypeId);
110                entries.put(cTypeId, cType.getLabel());
111            }
112        }
113        
114        // All contents
115        if (!_allOption.equals("disabled"))
116        {
117            String value = _allOption.equals("concat") ? StringUtils.join(entries.keySet(), ',') : "";
118            entries.put(value, new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS"));
119        }
120
121        return entries;
122    }
123}