001/* 002 * (C) Copyright 2006-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 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.client.model; 020 021import java.io.Serializable; 022import java.util.Collections; 023import java.util.Date; 024import java.util.LinkedHashMap; 025import java.util.Map; 026import java.util.Set; 027 028/** 029 * A flat representation of a document properties. Dates are in YYYY-MM-DDThh:mm:ssZ (UTC) format 030 * 031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 032 */ 033public class PropertyMap implements Serializable { 034 035 private static final long serialVersionUID = -3260084599278006841L; 036 037 protected final LinkedHashMap<String, Object> map; 038 039 public PropertyMap() { 040 map = new LinkedHashMap<String, Object>(); 041 } 042 043 public PropertyMap(PropertyMap props) { 044 map = new LinkedHashMap<String, Object>(props.map); 045 } 046 047 public PropertyMap(Map<String, Object> map) { 048 this.map = new LinkedHashMap<String, Object>(map); 049 } 050 051 public PropertyMap(int size) { 052 map = new LinkedHashMap<String, Object>(size); 053 } 054 055 public String getString(String key) { 056 return getString(key, null); 057 } 058 059 public Boolean getBoolean(String key) { 060 return getBoolean(key, null); 061 } 062 063 public Long getLong(String key) { 064 return getLong(key, null); 065 } 066 067 public Double getDouble(String key) { 068 return getDouble(key, null); 069 } 070 071 public Date getDate(String key) { 072 return getDate(key, null); 073 } 074 075 public PropertyList getList(String key) { 076 return getList(key, null); 077 } 078 079 public PropertyMap getMap(String key) { 080 return getMap(key, null); 081 } 082 083 public String getString(String key, String defValue) { 084 return PropertiesHelper.getString(map.get(key), defValue); 085 } 086 087 public Boolean getBoolean(String key, Boolean defValue) { 088 return PropertiesHelper.getBoolean(map.get(key), defValue); 089 } 090 091 public Long getLong(String key, Long defValue) { 092 return PropertiesHelper.getLong(map.get(key), defValue); 093 } 094 095 public Double getDouble(String key, Double defValue) { 096 return PropertiesHelper.getDouble(map.get(key), defValue); 097 } 098 099 public Date getDate(String key, Date defValue) { 100 return PropertiesHelper.getDate(map.get(key), defValue); 101 } 102 103 public PropertyList getList(String key, PropertyList defValue) { 104 return PropertiesHelper.getList(map.get(key), defValue); 105 } 106 107 public PropertyMap getMap(String key, PropertyMap defValue) { 108 return PropertiesHelper.getMap(map.get(key), defValue); 109 } 110 111 public Object get(String key) { 112 return map.get(key); 113 } 114 115 public Set<String> getKeys() { 116 return map.keySet(); 117 } 118 119 public int size() { 120 return map.size(); 121 } 122 123 public boolean isEmpty() { 124 return map.isEmpty(); 125 } 126 127 /** 128 * @deprecated since 5.7. Use {@link Document#set(String, String)} to inject data. 129 */ 130 @Deprecated 131 public void set(String key, String value) { 132 if (value == null) { 133 map.remove(key); 134 } 135 map.put(key, value); 136 } 137 138 /** 139 * @deprecated since 5.7. Use {@link Document#set(String, Boolean)} to inject data. 140 */ 141 @Deprecated 142 public void set(String key, Boolean value) { 143 if (value == null) { 144 map.remove(key); 145 } 146 map.put(key, value.toString()); 147 } 148 149 /** 150 * @deprecated since 5.7. Use {@link Document#set(String, Long)} to inject data. 151 */ 152 @Deprecated 153 public void set(String key, Long value) { 154 if (value == null) { 155 map.remove(key); 156 } 157 map.put(key, value.toString()); 158 } 159 160 /** 161 * @deprecated since 5.7. Use {@link Document#set(String, Double)} to inject data. 162 */ 163 @Deprecated 164 public void set(String key, Double value) { 165 if (value == null) { 166 map.remove(key); 167 } 168 map.put(key, value.toString()); 169 } 170 171 /** 172 * @deprecated since 5.7. Use {@link Document#set(String, java.util.Date)} to inject data. 173 */ 174 @Deprecated 175 public void set(String key, Date value) { 176 if (value == null) { 177 map.remove(key); 178 } 179 map.put(key, DateUtils.formatDate(value)); 180 } 181 182 /** 183 * @deprecated since 5.7. Use {@link Document#set(String, PropertyList)} to inject data. 184 */ 185 @Deprecated 186 public void set(String key, PropertyList value) { 187 if (value == null) { 188 map.remove(key); 189 } 190 map.put(key, value); 191 } 192 193 /** 194 * @deprecated since 5.7. Use {@link Document#set(String, PropertyMap)} to inject data. 195 */ 196 @Deprecated 197 public void set(String key, PropertyMap value) { 198 if (value == null) { 199 map.remove(key); 200 } 201 map.put(key, value); 202 } 203 204 public Map<String, Object> map() { 205 return Collections.unmodifiableMap(map); 206 } 207 208 @Override 209 public String toString() { 210 StringBuilder buf = new StringBuilder(); 211 for (Map.Entry<String, Object> entry : map.entrySet()) { 212 Object v = entry.getValue(); 213 if (v != null) { 214 if (v.getClass() == String.class) { 215 buf.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); // TODO escape \n 216 // in value 217 } else { 218 // TODO - use full xpath 219 // buf.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); 220 // //TODO escape \n in value 221 } 222 } else { 223 buf.append(entry.getKey()).append("=").append("\n"); 224 } 225 } 226 return buf.toString(); 227 } 228}