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