001/*
002 * (C) Copyright 2006-2012 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
016 *
017 * $Id: PropertiesDescriptor.java 26053 2007-10-16 01:45:43Z atchertchian $
018 */
019
020package org.nuxeo.ecm.platform.actions;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeMap;
028import org.nuxeo.common.xmap.annotation.XObject;
029
030/**
031 * Action property descriptor
032 *
033 * @since 5.6
034 */
035@XObject("properties")
036public class ActionPropertiesDescriptor implements Serializable {
037
038    private static final long serialVersionUID = 1L;
039
040    @XNode("@append")
041    boolean append;
042
043    @XNodeMap(value = "property", key = "@name", type = HashMap.class, componentType = String.class)
044    Map<String, String> properties = new HashMap<String, String>();
045
046    @XNodeMap(value = "propertyList", key = "@name", type = HashMap.class, componentType = ActionPropertyListDescriptor.class)
047    Map<String, ActionPropertyListDescriptor> listProperties = new HashMap<String, ActionPropertyListDescriptor>();
048
049    @XNodeMap(value = "propertyMap", key = "@name", type = HashMap.class, componentType = ActionPropertiesDescriptor.class)
050    Map<String, ActionPropertiesDescriptor> mapProperties = new HashMap<String, ActionPropertiesDescriptor>();
051
052    public HashMap<String, Serializable> getAllProperties() {
053        HashMap<String, Serializable> map = new HashMap<String, Serializable>();
054        map.putAll(properties);
055        for (Map.Entry<String, ActionPropertyListDescriptor> prop : listProperties.entrySet()) {
056            map.put(prop.getKey(), prop.getValue().getValues());
057        }
058        for (Map.Entry<String, ActionPropertiesDescriptor> prop : mapProperties.entrySet()) {
059            map.put(prop.getKey(), prop.getValue().getAllProperties());
060        }
061        return map;
062    }
063
064    public boolean isAppend() {
065        return append;
066    }
067
068    public void setAppend(boolean append) {
069        this.append = append;
070    }
071
072    public Map<String, String> getProperties() {
073        return properties;
074    }
075
076    public Map<String, ActionPropertyListDescriptor> getListProperties() {
077        return listProperties;
078    }
079
080    public void setListProperties(Map<String, ActionPropertyListDescriptor> listProperties) {
081        this.listProperties = listProperties;
082    }
083
084    public Map<String, ActionPropertiesDescriptor> getMapProperties() {
085        return mapProperties;
086    }
087
088    public void setMapProperties(Map<String, ActionPropertiesDescriptor> mapProperties) {
089        this.mapProperties = mapProperties;
090    }
091
092    public void setProperties(Map<String, String> properties) {
093        this.properties = properties;
094    }
095
096    public void merge(ActionPropertiesDescriptor other) {
097        if (other != null) {
098            if (other.getProperties() != null) {
099                if (properties == null) {
100                    properties = other.getProperties();
101                } else {
102                    properties.putAll(other.getProperties());
103                }
104            }
105            if (other.getListProperties() != null) {
106                if (listProperties == null) {
107                    listProperties = other.getListProperties();
108                } else {
109                    listProperties.putAll(other.getListProperties());
110                }
111            }
112            if (other.getMapProperties() != null) {
113                if (mapProperties == null) {
114                    mapProperties = other.getMapProperties();
115                } else {
116                    mapProperties.putAll(other.getMapProperties());
117                }
118            }
119        }
120    }
121
122    public ActionPropertiesDescriptor clone() {
123        ActionPropertiesDescriptor clone = new ActionPropertiesDescriptor();
124        if (properties != null) {
125            clone.properties = new HashMap<String, String>(properties);
126        }
127        if (listProperties != null) {
128            clone.listProperties = new HashMap<String, ActionPropertyListDescriptor>();
129            for (Map.Entry<String, ActionPropertyListDescriptor> item : listProperties.entrySet()) {
130                clone.listProperties.put(item.getKey(), item.getValue().clone());
131            }
132        }
133        if (mapProperties != null) {
134            clone.mapProperties = new HashMap<String, ActionPropertiesDescriptor>();
135            for (Map.Entry<String, ActionPropertiesDescriptor> item : mapProperties.entrySet()) {
136                clone.mapProperties.put(item.getKey(), item.getValue().clone());
137            }
138        }
139        return clone;
140    }
141
142}