001/*
002 * (C) Copyright 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.automation.core.util;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.DataModel;
028import org.nuxeo.ecm.core.api.PropertyException;
029
030/**
031 * Initialize a {@code Properties} object from one or more {@link DataModel}s.
032 * <p>
033 * This object can then be passed to any operation accepting {@link Properties}.
034 *
035 * @since 5.7
036 */
037public class DataModelProperties extends Properties {
038
039    private static final long serialVersionUID = 1L;
040
041    protected boolean onlyDirtyProperties = false;
042
043    protected Map<String, Serializable> properties = new HashMap<>();
044
045    public DataModelProperties() {
046    }
047
048    public DataModelProperties(DataModel dm, boolean onlyDirtyProperties) throws PropertyException {
049        this.onlyDirtyProperties = onlyDirtyProperties;
050        addDataModel(dm);
051
052    }
053
054    public DataModelProperties(List<DataModel> dms, boolean onlyDirtyProperties) throws PropertyException {
055        this.onlyDirtyProperties = onlyDirtyProperties;
056        for (DataModel dm : dms) {
057            addDataModel(dm);
058        }
059    }
060
061    public DataModelProperties(DataModel dm) throws PropertyException {
062        this(dm, false);
063    }
064
065    public DataModelProperties(List<DataModel> dms) throws PropertyException {
066        this(dms, false);
067    }
068
069    public void addDataModel(DataModel dm) throws PropertyException {
070        for (Map.Entry<String, Object> entry : dm.getMap().entrySet()) {
071            String key = entry.getKey();
072            if ((onlyDirtyProperties && dm.isDirty(key)) || !onlyDirtyProperties) {
073                if (!key.contains(":")) {
074                    key = dm.getSchema() + ":" + key;
075                }
076                properties.put(key, (Serializable) entry.getValue());
077            }
078        }
079    }
080
081    public Map<String, Serializable> getMap() {
082        return properties;
083    }
084
085    @Override
086    public String toString() {
087        Map<String,Serializable> merged = new HashMap<String, Serializable>();
088        merged.putAll(properties);
089        merged.putAll(this);
090        return merged.toString();
091    }
092
093    @Override
094    public int hashCode() {
095        final int prime = 31;
096        int result = super.hashCode();
097        result = prime * result + properties.hashCode();
098        return result;
099    }
100
101    @Override
102    public boolean equals(Object obj) {
103        if (this == obj) {
104            return true;
105        }
106        if (!super.equals(obj)) {
107            return false;
108        }
109        if (!(obj instanceof DataModelProperties)) {
110            return false;
111        }
112        DataModelProperties other = (DataModelProperties) obj;
113        if (!properties.equals(other.properties)) {
114            return false;
115        }
116        return true;
117    }
118
119
120}