001/*
002 * (C) Copyright 2006-2012 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: PropertiesDescriptor.java 26053 2007-10-16 01:45:43Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.actions;
023
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeMap;
030import org.nuxeo.common.xmap.annotation.XObject;
031
032/**
033 * Action property descriptor
034 *
035 * @since 5.6
036 */
037@XObject("properties")
038public class ActionPropertiesDescriptor implements Serializable {
039
040    private static final long serialVersionUID = 1L;
041
042    @XNode("@append")
043    boolean append;
044
045    @XNodeMap(value = "property", key = "@name", type = HashMap.class, componentType = String.class)
046    Map<String, String> properties = new HashMap<String, String>();
047
048    @XNodeMap(value = "propertyList", key = "@name", type = HashMap.class, componentType = ActionPropertyListDescriptor.class)
049    Map<String, ActionPropertyListDescriptor> listProperties = new HashMap<String, ActionPropertyListDescriptor>();
050
051    @XNodeMap(value = "propertyMap", key = "@name", type = HashMap.class, componentType = ActionPropertiesDescriptor.class)
052    Map<String, ActionPropertiesDescriptor> mapProperties = new HashMap<String, ActionPropertiesDescriptor>();
053
054    public HashMap<String, Serializable> getAllProperties() {
055        HashMap<String, Serializable> map = new HashMap<String, Serializable>();
056        map.putAll(properties);
057        for (Map.Entry<String, ActionPropertyListDescriptor> prop : listProperties.entrySet()) {
058            map.put(prop.getKey(), prop.getValue().getValues());
059        }
060        for (Map.Entry<String, ActionPropertiesDescriptor> prop : mapProperties.entrySet()) {
061            map.put(prop.getKey(), prop.getValue().getAllProperties());
062        }
063        return map;
064    }
065
066    public boolean isAppend() {
067        return append;
068    }
069
070    public void setAppend(boolean append) {
071        this.append = append;
072    }
073
074    public Map<String, String> getProperties() {
075        return properties;
076    }
077
078    public Map<String, ActionPropertyListDescriptor> getListProperties() {
079        return listProperties;
080    }
081
082    public void setListProperties(Map<String, ActionPropertyListDescriptor> listProperties) {
083        this.listProperties = listProperties;
084    }
085
086    public Map<String, ActionPropertiesDescriptor> getMapProperties() {
087        return mapProperties;
088    }
089
090    public void setMapProperties(Map<String, ActionPropertiesDescriptor> mapProperties) {
091        this.mapProperties = mapProperties;
092    }
093
094    public void setProperties(Map<String, String> properties) {
095        this.properties = properties;
096    }
097
098    public void merge(ActionPropertiesDescriptor other) {
099        if (other != null) {
100            if (other.getProperties() != null) {
101                if (properties == null) {
102                    properties = other.getProperties();
103                } else {
104                    properties.putAll(other.getProperties());
105                }
106            }
107            if (other.getListProperties() != null) {
108                if (listProperties == null) {
109                    listProperties = other.getListProperties();
110                } else {
111                    listProperties.putAll(other.getListProperties());
112                }
113            }
114            if (other.getMapProperties() != null) {
115                if (mapProperties == null) {
116                    mapProperties = other.getMapProperties();
117                } else {
118                    mapProperties.putAll(other.getMapProperties());
119                }
120            }
121        }
122    }
123
124    public ActionPropertiesDescriptor clone() {
125        ActionPropertiesDescriptor clone = new ActionPropertiesDescriptor();
126        if (properties != null) {
127            clone.properties = new HashMap<String, String>(properties);
128        }
129        if (listProperties != null) {
130            clone.listProperties = new HashMap<String, ActionPropertyListDescriptor>();
131            for (Map.Entry<String, ActionPropertyListDescriptor> item : listProperties.entrySet()) {
132                clone.listProperties.put(item.getKey(), item.getValue().clone());
133            }
134        }
135        if (mapProperties != null) {
136            clone.mapProperties = new HashMap<String, ActionPropertiesDescriptor>();
137            for (Map.Entry<String, ActionPropertiesDescriptor> item : mapProperties.entrySet()) {
138                clone.mapProperties.put(item.getKey(), item.getValue().clone());
139            }
140        }
141        return clone;
142    }
143
144}