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 * 016 * Contributors: 017 * Thomas Roger <troger@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.localconf; 021 022import java.io.Serializable; 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.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.DocumentRef; 033import org.nuxeo.ecm.core.api.PropertyException; 034import org.nuxeo.ecm.core.api.localconfiguration.AbstractLocalConfiguration; 035 036/** 037 * Default implementation of {@code SimpleConfiguration}. 038 * 039 * @see SimpleConfiguration 040 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 041 * @since 5.5 042 */ 043public class SimpleConfigurationAdapter extends AbstractLocalConfiguration<SimpleConfiguration> implements 044 SimpleConfiguration { 045 046 private static final Log log = LogFactory.getLog(SimpleConfigurationAdapter.class); 047 048 protected DocumentModel detachedDocument; 049 050 protected Map<String, String> parameters; 051 052 public SimpleConfigurationAdapter(DocumentModel doc) { 053 loadFromDocument(doc); 054 } 055 056 protected void loadFromDocument(DocumentModel doc) { 057 detachedDocument = doc; 058 parameters = computeParametersFromDocument(doc); 059 } 060 061 @SuppressWarnings("unchecked") 062 protected Map<String, String> computeParametersFromDocument(DocumentModel doc) { 063 Map<String, String> parameters = new HashMap<String, String>(); 064 try { 065 List<Map<String, String>> parametersFromDocument = (List<Map<String, String>>) doc.getPropertyValue(SIMPLE_CONFIGURATION_PARAMETERS_PROPERTY); 066 if (parametersFromDocument != null) { 067 for (Map<String, String> parameter : parametersFromDocument) { 068 parameters.put(parameter.get(SIMPLE_CONFIGURATION_PARAMETER_KEY), 069 parameter.get(SIMPLE_CONFIGURATION_PARAMETER_VALUE)); 070 } 071 } 072 } catch (PropertyException e) { 073 log.warn("Unable to retrieve SimpleConfiguration parameters: " + e); 074 log.debug(e, e); 075 } 076 return parameters; 077 } 078 079 @Override 080 public String get(String key) { 081 return get(key, null); 082 } 083 084 @Override 085 public String get(String key, String defaultValue) { 086 String value = parameters.get(key); 087 return value != null ? value : defaultValue; 088 } 089 090 @Override 091 public String put(String key, String value) { 092 return parameters.put(key, value); 093 } 094 095 @Override 096 public void putAll(Map<String, String> parameters) { 097 this.parameters.putAll(parameters); 098 } 099 100 @Override 101 public DocumentRef getDocumentRef() { 102 return detachedDocument.getRef(); 103 } 104 105 @Override 106 public boolean canMerge() { 107 return true; 108 } 109 110 @Override 111 public SimpleConfiguration merge(SimpleConfiguration other) { 112 if (other == null) { 113 return this; 114 } 115 116 SimpleConfigurationAdapter adapter = (SimpleConfigurationAdapter) other; 117 // set the document to the other SimpleConfiguration document to 118 // continue merging, if needed 119 detachedDocument = adapter.detachedDocument; 120 121 for (Map.Entry<String, String> otherParameter : adapter.parameters.entrySet()) { 122 // add only non-existing parameter 123 if (!parameters.containsKey(otherParameter.getKey())) { 124 parameters.put(otherParameter.getKey(), otherParameter.getValue()); 125 } 126 } 127 128 return this; 129 } 130 131 @Override 132 public void save(CoreSession session) { 133 List<Map<String, String>> parametersForDocument = computeParametersForDocument(parameters); 134 detachedDocument.setPropertyValue(SIMPLE_CONFIGURATION_PARAMETERS_PROPERTY, 135 (Serializable) parametersForDocument); 136 DocumentModel doc = session.saveDocument(detachedDocument); 137 session.save(); 138 loadFromDocument(doc); 139 } 140 141 protected List<Map<String, String>> computeParametersForDocument(Map<String, String> parameters) { 142 List<Map<String, String>> parametersForDocument = new ArrayList<Map<String, String>>(); 143 144 for (Map.Entry<String, String> entry : parameters.entrySet()) { 145 Map<String, String> parameter = new HashMap<String, String>(); 146 parameter.put(SIMPLE_CONFIGURATION_PARAMETER_KEY, entry.getKey()); 147 parameter.put(SIMPLE_CONFIGURATION_PARAMETER_VALUE, entry.getValue()); 148 parametersForDocument.add(parameter); 149 } 150 151 return parametersForDocument; 152 } 153 154}