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