001/*
002 * (C) Copyright 2006-2013 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 *     Stéphane Lacoin <slacoin@nuxeo.com>
019 */
020package org.nuxeo.ecm.automation.client.model;
021
022import java.util.Date;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.Map;
026import java.util.Set;
027
028/**
029 * @since 5.7 Delegate containing data injection for PropertyMap object. Keeping dirty properties in memory.
030 */
031public class PropertyMapSetter {
032
033    protected final Map<String, Object> map;
034
035    protected final Set<String> dirties = new HashSet<String>();
036
037    public PropertyMapSetter(PropertyMap propertyMap) {
038        map = propertyMap.map;
039    }
040
041    /**
042     * @since 5.7 This method fetch all dirty properties that has been defined. Warning: Dirty properties are not
043     *        flushed when getting it.
044     * @return PropertyMap
045     */
046    public PropertyMap getDirties() {
047        Map<String, Object> resultMap = new HashMap<String, Object>();
048        for (String key : dirties) {
049            Object value = map.get(key);
050            resultMap.put(key, value);
051        }
052        return new PropertyMap(resultMap);
053    }
054
055    public void set(String key, String value) {
056        if (value == null) {
057            map.remove(key);
058        }
059        map.put(key, value);
060        dirties.add(key);
061    }
062
063    public void set(String key, Boolean value) {
064        if (value == null) {
065            map.remove(key);
066        }
067        map.put(key, value.toString());
068        dirties.add(key);
069    }
070
071    public void set(String key, Long value) {
072        if (value == null) {
073            map.remove(key);
074        }
075        map.put(key, value.toString());
076        dirties.add(key);
077    }
078
079    public void set(String key, Double value) {
080        if (value == null) {
081            map.remove(key);
082        }
083        map.put(key, value.toString());
084        dirties.add(key);
085    }
086
087    public void set(String key, Date value) {
088        if (value == null) {
089            map.remove(key);
090        }
091        map.put(key, DateUtils.formatDate(value));
092        dirties.add(key);
093    }
094
095    public void set(String key, PropertyList value) {
096        if (value == null) {
097            map.remove(key);
098        }
099        map.put(key, value);
100        dirties.add(key);
101    }
102
103    public void set(String key, PropertyMap value) {
104        if (value == null) {
105            map.remove(key);
106        }
107        map.put(key, value);
108        dirties.add(key);
109    }
110
111}