001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
016 * Nuxeo - initial API and implementation
017 */
018
019package org.nuxeo.ecm.localconf;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import javax.faces.model.SelectItem;
030
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Install;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Scope;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.schema.SchemaManager;
037import org.nuxeo.ecm.platform.contentview.jsf.ContentViewHeader;
038import org.nuxeo.ecm.platform.contentview.jsf.ContentViewService;
039import org.nuxeo.ecm.platform.types.Type;
040import org.nuxeo.ecm.platform.types.TypeManager;
041import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
042import org.nuxeo.ecm.platform.ui.web.component.SelectItemComparator;
043import org.nuxeo.runtime.api.Framework;
044
045import static org.jboss.seam.ScopeType.CONVERSATION;
046import static org.nuxeo.ecm.core.schema.FacetNames.FOLDERISH;
047
048@Name("contentViewConfigurationActions")
049@Scope(CONVERSATION)
050@Install(precedence = Install.FRAMEWORK)
051public class ContentViewConfigurationActions implements Serializable {
052
053    private static final long serialVersionUID = 1L;
054
055    protected transient SchemaManager schemaManager;
056
057    @In(create = true)
058    protected transient TypeManager typeManager;
059
060    @In(create = true)
061    protected transient NavigationContext navigationContext;
062
063    @In(create = true)
064    protected ContentViewService contentViewService;
065
066    @In(create = true)
067    protected Map<String, String> messages;
068
069    protected SchemaManager getSchemaManager() {
070        if (schemaManager == null) {
071            schemaManager = Framework.getService(SchemaManager.class);
072        }
073        return schemaManager;
074    }
075
076    public List<SelectItem> getAvailableDocTypes() {
077        List<SelectItem> items = new ArrayList<SelectItem>();
078        Set<String> folderishDocTypeNames = getSchemaManager().getDocumentTypeNamesForFacet(FOLDERISH);
079        DocumentModel currentDocument = navigationContext.getCurrentDocument();
080        String currentDocTypeName = currentDocument.getType();
081        Collection<Type> allowedSubTypes = typeManager.findAllAllowedSubTypesFrom(currentDocTypeName);
082        Type currentDocType = typeManager.getType(currentDocTypeName);
083        if (!allowedSubTypes.contains(currentDocType)) {
084            allowedSubTypes.add(currentDocType);
085        }
086        for (Type type : allowedSubTypes) {
087            String typeName = type.getId();
088            if (!folderishDocTypeNames.contains(typeName)) {
089                continue;
090            }
091            SelectItem item;
092            if (messages.containsKey(typeName)) {
093                item = new SelectItem(typeName, messages.get(typeName));
094            } else {
095                item = new SelectItem(typeName);
096            }
097            items.add(item);
098        }
099        Collections.sort(items, new SelectItemComparator("label", true));
100        return items;
101    }
102
103    public List<SelectItem> getAvailableContentViews() {
104        List<SelectItem> items = new ArrayList<SelectItem>();
105        for (String cvName : contentViewService.getContentViewNames()) { // TODO : use flag ?
106            ContentViewHeader contentViewHeader = contentViewService.getContentViewHeader(cvName);
107            String title = contentViewHeader.getTitle();
108            SelectItem item;
109            if (title == null) {
110                item = new SelectItem(cvName);
111            } else {
112                if (contentViewHeader.isTranslateTitle()) {
113                    title = messages.get(title);
114                }
115                item = new SelectItem(cvName, title);
116            }
117            items.add(item);
118        }
119        Collections.sort(items, new SelectItemComparator("label", true));
120        return items;
121    }
122
123}