001/*
002 * (C) Copyright 2011 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.ContentViewConfigurationConstants.CONTENT_VIEW_CONFIGURATION_CONTENT_VIEW;
020import static org.nuxeo.ecm.platform.types.localconfiguration.ContentViewConfigurationConstants.CONTENT_VIEW_CONFIGURATION_DOC_TYPE;
021import static org.nuxeo.ecm.platform.types.localconfiguration.ContentViewConfigurationConstants.CONTENT_VIEW_CONFIGURATION_NAMES_BY_TYPE;
022
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentRef;
032import org.nuxeo.ecm.core.api.PropertyException;
033import org.nuxeo.ecm.core.api.localconfiguration.AbstractLocalConfiguration;
034
035/**
036 * Default implementation of {@code ContentViewConfiguration}.
037 *
038 * @author <a href="mailto:qlamerand@nuxeo.com">Quentin Lamerand</a>
039 */
040public class ContentViewConfigurationAdapter extends AbstractLocalConfiguration<ContentViewConfiguration> implements
041        ContentViewConfiguration {
042
043    private static final Log log = LogFactory.getLog(ContentViewConfigurationAdapter.class);
044
045    protected DocumentRef documentRef;
046
047    protected Map<String, List<String>> typeToContentViewNames;
048
049    protected boolean canMerge = true;
050
051    @SuppressWarnings("unchecked")
052    public ContentViewConfigurationAdapter(DocumentModel doc) {
053        documentRef = doc.getRef();
054        typeToContentViewNames = new HashMap<String, List<String>>();
055        try {
056            List<Map<String, String>> cvNamesByType = (List<Map<String, String>>) doc.getPropertyValue(CONTENT_VIEW_CONFIGURATION_NAMES_BY_TYPE);
057            for (Map<String, String> typeToCv : cvNamesByType) {
058                String type = typeToCv.get(CONTENT_VIEW_CONFIGURATION_DOC_TYPE);
059                String cvName = typeToCv.get(CONTENT_VIEW_CONFIGURATION_CONTENT_VIEW);
060                if (typeToContentViewNames.containsKey(type)) {
061                    typeToContentViewNames.get(type).add(cvName);
062                } else {
063                    List<String> cvNames = new ArrayList<String>();
064                    cvNames.add(cvName);
065                    typeToContentViewNames.put(type, cvNames);
066                }
067            }
068        } catch (PropertyException e) {
069            log.error("Failed to get ContentViewConfiguration", e);
070        }
071    }
072
073    @Override
074    public List<String> getContentViewsForType(String docType) {
075        return typeToContentViewNames.get(docType);
076    }
077
078    @Override
079    public boolean canMerge() {
080        return canMerge;
081    }
082
083    @Override
084    public DocumentRef getDocumentRef() {
085        return documentRef;
086    }
087
088    @Override
089    public ContentViewConfiguration merge(ContentViewConfiguration other) {
090        if (other == null) {
091            return this;
092        }
093
094        // set the documentRef to the other UITypesConfiguration to continue
095        // merging, if needed
096        documentRef = other.getDocumentRef();
097
098        Map<String, List<String>> t2cv = other.getTypeToContentViewNames();
099        for (String type : t2cv.keySet()) {
100            if (!typeToContentViewNames.containsKey(type)) {
101                typeToContentViewNames.put(type, t2cv.get(type));
102            }
103        }
104
105        return this;
106    }
107
108    @Override
109    public Map<String, List<String>> getTypeToContentViewNames() {
110        return typeToContentViewNames;
111    }
112
113}