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