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 /** 040 * 041 */ 042 private static final long serialVersionUID = 1L; 043 044 protected boolean onlyDirtyProperties = false; 045 046 protected Map<String, Serializable> properties = new HashMap<>(); 047 048 public DataModelProperties() { 049 } 050 051 public DataModelProperties(DataModel dm, boolean onlyDirtyProperties) throws PropertyException { 052 this.onlyDirtyProperties = onlyDirtyProperties; 053 addDataModel(dm); 054 055 } 056 057 public DataModelProperties(List<DataModel> dms, boolean onlyDirtyProperties) throws PropertyException { 058 this.onlyDirtyProperties = onlyDirtyProperties; 059 for (DataModel dm : dms) { 060 addDataModel(dm); 061 } 062 } 063 064 public DataModelProperties(DataModel dm) throws PropertyException { 065 this(dm, false); 066 } 067 068 public DataModelProperties(List<DataModel> dms) throws PropertyException { 069 this(dms, false); 070 } 071 072 public void addDataModel(DataModel dm) throws PropertyException { 073 for (Map.Entry<String, Object> entry : dm.getMap().entrySet()) { 074 String key = entry.getKey(); 075 if ((onlyDirtyProperties && dm.isDirty(key)) || !onlyDirtyProperties) { 076 if (!key.contains(":")) { 077 key = dm.getSchema() + ":" + key; 078 } 079 properties.put(key, (Serializable) entry.getValue()); 080 } 081 } 082 } 083 084 public Map<String, Serializable> getMap() { 085 return properties; 086 } 087 088}