001/*
002 * (C) Copyright 2006-2016 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<>();
041    }
042
043    public PropertyMap(PropertyMap props) {
044        map = new LinkedHashMap<>(props.map);
045    }
046
047    public PropertyMap(Map<String, Object> map) {
048        this.map = new LinkedHashMap<>(map);
049    }
050
051    public PropertyMap(int size) {
052        map = new LinkedHashMap<>(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    public Map<String, Object> map() {
128        return Collections.unmodifiableMap(map);
129    }
130
131    @Override
132    public String toString() {
133        StringBuilder buf = new StringBuilder();
134        for (Map.Entry<String, Object> entry : map.entrySet()) {
135            Object v = entry.getValue();
136            if (v != null) {
137                if (v.getClass() == String.class) {
138                    buf.append(entry.getKey()).append("=").append(entry.getValue()).append("\n"); // TODO escape \n
139                                                                                                  // in value
140                } else {
141                    // TODO - use full xpath
142                    // buf.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
143                    // //TODO escape \n in value
144                }
145            } else {
146                buf.append(entry.getKey()).append("=").append("\n");
147            }
148        }
149        return buf.toString();
150    }
151}