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 {
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<>();
045
046    @XNodeMap(value = "propertyList", key = "@name", type = HashMap.class, componentType = ActionPropertyListDescriptor.class)
047    Map<String, ActionPropertyListDescriptor> listProperties = new HashMap<>();
048
049    @XNodeMap(value = "propertyMap", key = "@name", type = HashMap.class, componentType = ActionPropertiesDescriptor.class)
050    Map<String, ActionPropertiesDescriptor> mapProperties = new HashMap<>();
051
052    public HashMap<String, Serializable> getAllProperties() {
053        HashMap<String, Serializable> map = new HashMap<>();
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    @Override
123    public ActionPropertiesDescriptor clone() {
124        ActionPropertiesDescriptor clone = new ActionPropertiesDescriptor();
125        if (properties != null) {
126            clone.properties = new HashMap<>(properties);
127        }
128        if (listProperties != null) {
129            clone.listProperties = new HashMap<>();
130            for (Map.Entry<String, ActionPropertyListDescriptor> item : listProperties.entrySet()) {
131                clone.listProperties.put(item.getKey(), item.getValue().clone());
132            }
133        }
134        if (mapProperties != null) {
135            clone.mapProperties = new HashMap<>();
136            for (Map.Entry<String, ActionPropertiesDescriptor> item : mapProperties.entrySet()) {
137                clone.mapProperties.put(item.getKey(), item.getValue().clone());
138            }
139        }
140        return clone;
141    }
142
143}