/*
 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Thomas Roger
 */

package org.nuxeo.ecm.automation.core.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nuxeo.ecm.core.api.DataModel;
import org.nuxeo.ecm.core.api.model.PropertyException;

/**
 *
 * Initialize a {@code Properties} object from one or more {@link DataModel}s.
 * <p>
 * This object can then be passed to any operation accepting {@link Properties}.
 *
 * @since 5.7
 */
public class DataModelProperties extends Properties {

    protected boolean loaded = false;

    protected Map<String, Serializable> properties = new HashMap<>();

    public DataModelProperties() {
    }

    public DataModelProperties(DataModel dm) throws PropertyException {
        addDataModel(dm);
    }

    public DataModelProperties(List<DataModel> dms) throws PropertyException {
        for (DataModel dm : dms) {
            addDataModel(dm);
        }
    }

    public void addDataModel(DataModel dm) throws PropertyException {
        for (Map.Entry<String, Object> entry : dm.getMap().entrySet()) {
            String key = entry.getKey();
            if (!key.contains(":")) {
                key = dm.getSchema() + ":" + key;
            }
            properties.put(key, (Serializable) entry.getValue());
        }
    }

    public Map<String, Serializable> getMap() {
        return properties;
    }

}

@@ -1158,14 +1158,27 @@
 ean 
-loaded
+onlyDirtyProperties
  = f
@@ -1344,36 +1344,133 @@
 l dm
-) throws PropertyException %7B
+, boolean onlyDirtyProperties)%0A            throws PropertyException %7B%0A        this.onlyDirtyProperties = onlyDirtyProperties;
 %0A   
@@ -1488,24 +1488,25 @@
 aModel(dm);%0A
+%0A
     %7D%0A%0A    p
@@ -1554,111 +1554,424 @@
  dms
-) throws PropertyException %7B%0A        for (DataModel dm : dms) %7B%0A            addDataModel(dm);%0A        %7D
+, boolean onlyDirtyProperties)%0A            throws PropertyException %7B%0A        this.onlyDirtyProperties = onlyDirtyProperties;%0A        for (DataModel dm : dms) %7B%0A            addDataModel(dm);%0A        %7D%0A    %7D%0A%0A    public DataModelProperties(DataModel dm) throws PropertyException %7B%0A        this(dm, false);%0A    %7D%0A%0A    public DataModelProperties(List%3CDataModel%3E dms) throws PropertyException %7B%0A        this(dms, false);
 %0A   
@@ -2158,16 +2158,124 @@
 tKey();%0A
+            if ((onlyDirtyProperties && dm.isDirty(key))%0A                    %7C%7C !onlyDirtyProperties) %7B%0A    
         
@@ -2300,24 +2300,28 @@
 ins(%22:%22)) %7B%0A
+    
             
@@ -2366,26 +2366,34 @@
             
+  
-%7D%0A
+  %7D%0A    
             
@@ -2446,16 +2446,30 @@
 lue());%0A
+            %7D%0A
         

allow to build DataModelProperties object only with the dirty properties of the given DataModels