001/*
002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016
017package org.nuxeo.ecm.localconf;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Collections;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026
027import javax.faces.model.SelectItem;
028
029import org.jboss.seam.annotations.In;
030import org.jboss.seam.annotations.Install;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.schema.SchemaManager;
035import org.nuxeo.ecm.platform.contentview.jsf.ContentViewHeader;
036import org.nuxeo.ecm.platform.contentview.jsf.ContentViewService;
037import org.nuxeo.ecm.platform.types.Type;
038import org.nuxeo.ecm.platform.types.TypeManager;
039import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
040import org.nuxeo.ecm.platform.ui.web.component.SelectItemComparator;
041import org.nuxeo.runtime.api.Framework;
042
043import static org.jboss.seam.ScopeType.CONVERSATION;
044import static org.nuxeo.ecm.core.schema.FacetNames.FOLDERISH;
045
046@Name("contentViewConfigurationActions")
047@Scope(CONVERSATION)
048@Install(precedence = Install.FRAMEWORK)
049public class ContentViewConfigurationActions implements Serializable {
050
051    private static final long serialVersionUID = 1L;
052
053    protected transient SchemaManager schemaManager;
054
055    @In(create = true)
056    protected transient TypeManager typeManager;
057
058    @In(create = true)
059    protected transient NavigationContext navigationContext;
060
061    @In(create = true)
062    protected ContentViewService contentViewService;
063
064    @In(create = true)
065    protected Map<String, String> messages;
066
067    protected SchemaManager getSchemaManager() {
068        if (schemaManager == null) {
069            schemaManager = Framework.getService(SchemaManager.class);
070        }
071        return schemaManager;
072    }
073
074    public List<SelectItem> getAvailableDocTypes() {
075        List<SelectItem> items = new ArrayList<SelectItem>();
076        Set<String> folderishDocTypeNames = getSchemaManager().getDocumentTypeNamesForFacet(FOLDERISH);
077        DocumentModel currentDocument = navigationContext.getCurrentDocument();
078        String currentDocTypeName = currentDocument.getType();
079        Collection<Type> allowedSubTypes = typeManager.findAllAllowedSubTypesFrom(currentDocTypeName);
080        Type currentDocType = typeManager.getType(currentDocTypeName);
081        if (!allowedSubTypes.contains(currentDocType)) {
082            allowedSubTypes.add(currentDocType);
083        }
084        for (Type type : allowedSubTypes) {
085            String typeName = type.getId();
086            if (!folderishDocTypeNames.contains(typeName)) {
087                continue;
088            }
089            SelectItem item;
090            if (messages.containsKey(typeName)) {
091                item = new SelectItem(typeName, messages.get(typeName));
092            } else {
093                item = new SelectItem(typeName);
094            }
095            items.add(item);
096        }
097        Collections.sort(items, new SelectItemComparator("label", true));
098        return items;
099    }
100
101    public List<SelectItem> getAvailableContentViews() {
102        List<SelectItem> items = new ArrayList<SelectItem>();
103        for (String cvName : contentViewService.getContentViewNames()) { // TODO : use flag ?
104            ContentViewHeader contentViewHeader = contentViewService.getContentViewHeader(cvName);
105            String title = contentViewHeader.getTitle();
106            SelectItem item;
107            if (title == null) {
108                item = new SelectItem(cvName);
109            } else {
110                if (contentViewHeader.isTranslateTitle()) {
111                    title = messages.get(title);
112                }
113                item = new SelectItem(cvName, title);
114            }
115            items.add(item);
116        }
117        Collections.sort(items, new SelectItemComparator("label", true));
118        return items;
119    }
120
121}