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