001/*
002 * (C) Copyright 2010 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.Collections;
022import java.util.Comparator;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.annotations.Install;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Scope;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentRef;
034import org.nuxeo.ecm.core.api.security.SecurityConstants;
035import org.nuxeo.ecm.core.schema.DocumentType;
036import org.nuxeo.ecm.core.schema.SchemaManager;
037import org.nuxeo.ecm.platform.types.Type;
038import org.nuxeo.ecm.platform.types.TypeManager;
039import org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfiguration;
040import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
041import org.nuxeo.runtime.api.Framework;
042
043import static org.jboss.seam.ScopeType.CONVERSATION;
044import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_FACET;
045import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_DEFAULT_NEEDED_SCHEMA;
046
047/**
048 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
049 */
050@Name("typesConfigurationActions")
051@Scope(CONVERSATION)
052@Install(precedence = Install.FRAMEWORK)
053public class UITypesConfigurationActions implements Serializable {
054
055    private static final long serialVersionUID = 1L;
056
057    @In(create = true)
058    protected Map<String, String> messages;
059
060    /**
061     * @deprecated since 5.7.
062     */
063    @Deprecated
064    public static final Comparator<? super Type> TYPE_ALPHABETICAL_ORDER = new Comparator<Type>() {
065
066        @Override
067        public int compare(Type type1, Type type2) {
068            return type1.getId().compareTo(type2.getId());
069        }
070
071    };
072
073    /**
074     * @since 5.9.1
075     */
076    protected static class TypeLabelAlphabeticalOrder implements Comparator<Type> {
077
078        private final Map<String, String> messages;
079
080        public TypeLabelAlphabeticalOrder(Map<String, String> messages) {
081            super();
082            this.messages = messages;
083        }
084
085        @Override
086        public int compare(Type type1, Type type2) {
087            String label1 = messages.get(type1.getLabel());
088            String label2 = messages.get(type2.getLabel());
089            return label1.compareTo(label2);
090        }
091    }
092
093    @In(create = true)
094    protected transient TypeManager typeManager;
095
096    @In(create = true)
097    protected transient NavigationContext navigationContext;
098
099    @In(create = true, required = false)
100    protected transient CoreSession documentManager;
101
102    protected transient SchemaManager schemaManager;
103
104    public List<Type> getNotSelectedTypes() {
105        DocumentModel currentDoc = navigationContext.getCurrentDocument();
106        return getNotSelectedTypes(currentDoc);
107    }
108
109    /**
110     * Returns a List of type not selected for the domain given as parameter
111     *
112     * @param document the domain to configure
113     * @return a List of type of document, not currently selected for the domain
114     * @since 5.5
115     */
116    public List<Type> getNotSelectedTypes(DocumentModel document) {
117        if (!document.hasFacet(UI_TYPES_CONFIGURATION_FACET)) {
118            return Collections.emptyList();
119        }
120
121        List<String> allowedTypes = getAllowedTypes(document);
122
123        List<Type> notSelectedTypes = new ArrayList<Type>(typeManager.findAllAllowedSubTypesFrom(document.getType()));
124
125        for (Iterator<Type> it = notSelectedTypes.iterator(); it.hasNext();) {
126            Type type = it.next();
127            if (allowedTypes.contains(type.getId())) {
128                it.remove();
129            }
130        }
131
132        Collections.sort(notSelectedTypes, new TypeLabelAlphabeticalOrder(messages));
133
134        return notSelectedTypes;
135    }
136
137    protected List<String> getAllowedTypes(DocumentModel doc) {
138        UITypesConfiguration uiTypesConfiguration = doc.getAdapter(UITypesConfiguration.class);
139        if (uiTypesConfiguration == null) {
140            return Collections.emptyList();
141        }
142        List<String> allowedTypes = new ArrayList<String>(uiTypesConfiguration.getAllowedTypes());
143        if (allowedTypes.isEmpty()) {
144            allowedTypes = computeAllowedTypes(doc);
145        }
146        return allowedTypes;
147    }
148
149    public List<Type> getSelectedTypes() {
150        DocumentModel currentDoc = navigationContext.getCurrentDocument();
151        return getSelectedTypes(currentDoc);
152    }
153
154    /**
155     * Returns a List of type selected for the domain given as parameter
156     *
157     * @param document the domain to configure
158     * @return List of documen type selected for the domain
159     * @since 5.5
160     */
161    public List<Type> getSelectedTypes(DocumentModel document) {
162        if (!document.hasFacet(UI_TYPES_CONFIGURATION_FACET)) {
163            return Collections.emptyList();
164        }
165
166        List<String> allowedTypes = getAllowedTypes(document);
167
168        List<Type> selectedTypes = new ArrayList<Type>();
169        for (String type : allowedTypes) {
170            Type existingType = typeManager.getType(type);
171            if (existingType != null) {
172                selectedTypes.add(existingType);
173            }
174        }
175
176        Collections.sort(selectedTypes, new TypeLabelAlphabeticalOrder(messages));
177        return selectedTypes;
178    }
179
180    protected List<String> computeAllowedTypes(DocumentModel currentDoc) {
181        List<String> types = new ArrayList<String>();
182
183        DocumentModel parent = documentManager.getRootDocument();
184
185        DocumentRef parentRef = currentDoc.getParentRef();
186        if (parentRef != null && documentManager.hasPermission(parentRef, SecurityConstants.READ)) {
187            parent = documentManager.getDocument(parentRef);
188        }
189
190        for (Type type : typeManager.findAllAllowedSubTypesFrom(currentDoc.getType(), parent)) {
191            types.add(type.getId());
192        }
193
194        return types;
195    }
196
197    public List<Type> getTypesWithSchemaFile() {
198        DocumentModel document = navigationContext.getCurrentDocument();
199        return getTypesWithSchemaFile(document);
200    }
201
202    /**
203     * Returns a List of Document Types associated with Schema file for the domain given as parameter, if they're
204     * allowed for it.
205     *
206     * @param document the domain
207     * @return List of Document types which have assoctiated Schema files.
208     * @Since 5.5
209     */
210    public List<Type> getTypesWithSchemaFile(DocumentModel document) {
211        List<Type> types = new ArrayList<Type>();
212        for (String type : getAllowedTypes(document)) {
213            DocumentType documentType = getSchemaManager().getDocumentType(type);
214            if (documentType != null && documentType.hasSchema(UI_TYPES_DEFAULT_NEEDED_SCHEMA)) {
215                types.add(typeManager.getType(type));
216            }
217        }
218        Collections.sort(types, new TypeLabelAlphabeticalOrder(messages));
219        return Collections.unmodifiableList(types);
220    }
221
222    protected SchemaManager getSchemaManager() {
223        if (schemaManager == null) {
224            schemaManager = Framework.getService(SchemaManager.class);
225        }
226        return schemaManager;
227    }
228
229}