001/* 002 * (C) Copyright 2010-2018 Nuxeo (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.platform.types.localconfiguration; 020 021import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_ALLOWED_TYPES_PROPERTY; 022import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DEFAULT_TYPE; 023import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DENIED_TYPES_PROPERTY; 024import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DENY_ALL_TYPES_PROPERTY; 025import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_DEFAULT_TYPE; 026 027import java.util.ArrayList; 028import java.util.Arrays; 029import java.util.Collections; 030import java.util.HashMap; 031import java.util.List; 032import java.util.Map; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.DocumentRef; 038import org.nuxeo.ecm.core.api.PropertyException; 039import org.nuxeo.ecm.core.api.localconfiguration.AbstractLocalConfiguration; 040import org.nuxeo.ecm.platform.types.SubType; 041 042/** 043 * Default implementation of {@code UITypesConfiguration}. 044 * 045 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 046 */ 047public class UITypesConfigurationAdapter extends AbstractLocalConfiguration<UITypesConfiguration> 048 implements UITypesConfiguration { 049 050 private static final Log log = LogFactory.getLog(UITypesConfigurationAdapter.class); 051 052 protected DocumentRef documentRef; 053 054 protected List<String> allowedTypes; 055 056 protected List<String> deniedTypes; 057 058 protected boolean denyAllTypes; 059 060 protected boolean canMerge = true; 061 062 protected String defaultType; 063 064 public UITypesConfigurationAdapter(DocumentModel doc) { 065 documentRef = doc.getRef(); 066 allowedTypes = getTypesList(doc, UI_TYPES_CONFIGURATION_ALLOWED_TYPES_PROPERTY); 067 deniedTypes = getTypesList(doc, UI_TYPES_CONFIGURATION_DENIED_TYPES_PROPERTY); 068 defaultType = getDefaultType(doc); 069 070 denyAllTypes = getDenyAllTypesProperty(doc); 071 if (denyAllTypes) { 072 canMerge = false; 073 } 074 } 075 076 protected List<String> getTypesList(DocumentModel doc, String property) { 077 String[] types; 078 try { 079 types = (String[]) doc.getPropertyValue(property); 080 } catch (PropertyException e) { 081 return Collections.emptyList(); 082 } 083 if (types == null) { 084 return Collections.emptyList(); 085 } 086 return Collections.unmodifiableList(Arrays.asList(types)); 087 } 088 089 protected boolean getDenyAllTypesProperty(DocumentModel doc) { 090 try { 091 Boolean value = (Boolean) doc.getPropertyValue(UI_TYPES_CONFIGURATION_DENY_ALL_TYPES_PROPERTY); 092 return Boolean.TRUE.equals(value); 093 } catch (PropertyException e) { 094 return false; 095 } 096 } 097 098 protected String getDefaultType(DocumentModel doc) { 099 String value = UI_TYPES_DEFAULT_TYPE; 100 try { 101 value = (String) doc.getPropertyValue(UI_TYPES_CONFIGURATION_DEFAULT_TYPE); 102 } catch (PropertyException e) { 103 log.debug("can't get default type for:" + doc.getPathAsString(), e); 104 } 105 return value; 106 } 107 108 @Override 109 public List<String> getAllowedTypes() { 110 return allowedTypes; 111 } 112 113 @Override 114 public List<String> getDeniedTypes() { 115 return deniedTypes; 116 } 117 118 @Override 119 public boolean denyAllTypes() { 120 return denyAllTypes; 121 } 122 123 @Override 124 public DocumentRef getDocumentRef() { 125 return documentRef; 126 } 127 128 @Override 129 public boolean canMerge() { 130 return canMerge; 131 } 132 133 @Override 134 public UITypesConfiguration merge(UITypesConfiguration other) { 135 if (other == null) { 136 return this; 137 } 138 139 // set the documentRef to the other UITypesConfiguration to continue 140 // merging, if needed 141 documentRef = other.getDocumentRef(); 142 143 if (allowedTypes.isEmpty()) { 144 allowedTypes = Collections.unmodifiableList(new ArrayList<>(other.getAllowedTypes())); 145 } 146 147 List<String> deniedTypes = new ArrayList<>(this.deniedTypes); 148 deniedTypes.addAll(other.getDeniedTypes()); 149 this.deniedTypes = Collections.unmodifiableList(deniedTypes); 150 151 denyAllTypes = other.denyAllTypes(); 152 if (denyAllTypes) { 153 canMerge = false; 154 } 155 156 return this; 157 } 158 159 @Override 160 public Map<String, SubType> filterSubTypes(Map<String, SubType> allowedSubTypes) { 161 if (denyAllTypes()) { 162 return Collections.emptyMap(); 163 } 164 165 List<String> allowedTypes = getAllowedTypes(); 166 List<String> deniedTypes = getDeniedTypes(); 167 if (allowedTypes.isEmpty() && deniedTypes.isEmpty()) { 168 return allowedSubTypes; 169 } 170 171 Map<String, SubType> filteredAllowedSubTypes = new HashMap<>(allowedSubTypes); 172 filteredAllowedSubTypes.keySet().removeIf(subTypeName -> deniedTypes.contains(subTypeName) 173 || !allowedTypes.isEmpty() && !allowedTypes.contains(subTypeName)); 174 return filteredAllowedSubTypes; 175 } 176 177 /* 178 * (non-Javadoc) 179 * @see org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfiguration# getDefaultType() 180 */ 181 @Override 182 public String getDefaultType() { 183 return defaultType; 184 } 185 186}