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