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