001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.automation.core.util; 019 020import java.io.Serializable; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.nuxeo.ecm.core.api.DataModel; 026import org.nuxeo.ecm.core.api.PropertyException; 027 028/** 029 * Initialize a {@code Properties} object from one or more {@link DataModel}s. 030 * <p> 031 * This object can then be passed to any operation accepting {@link Properties}. 032 * 033 * @since 5.7 034 */ 035public class DataModelProperties extends Properties { 036 037 /** 038 * 039 */ 040 private static final long serialVersionUID = 1L; 041 042 protected boolean onlyDirtyProperties = false; 043 044 protected Map<String, Serializable> properties = new HashMap<>(); 045 046 public DataModelProperties() { 047 } 048 049 public DataModelProperties(DataModel dm, boolean onlyDirtyProperties) throws PropertyException { 050 this.onlyDirtyProperties = onlyDirtyProperties; 051 addDataModel(dm); 052 053 } 054 055 public DataModelProperties(List<DataModel> dms, boolean onlyDirtyProperties) throws PropertyException { 056 this.onlyDirtyProperties = onlyDirtyProperties; 057 for (DataModel dm : dms) { 058 addDataModel(dm); 059 } 060 } 061 062 public DataModelProperties(DataModel dm) throws PropertyException { 063 this(dm, false); 064 } 065 066 public DataModelProperties(List<DataModel> dms) throws PropertyException { 067 this(dms, false); 068 } 069 070 public void addDataModel(DataModel dm) throws PropertyException { 071 for (Map.Entry<String, Object> entry : dm.getMap().entrySet()) { 072 String key = entry.getKey(); 073 if ((onlyDirtyProperties && dm.isDirty(key)) || !onlyDirtyProperties) { 074 if (!key.contains(":")) { 075 key = dm.getSchema() + ":" + key; 076 } 077 properties.put(key, (Serializable) entry.getValue()); 078 } 079 } 080 } 081 082 public Map<String, Serializable> getMap() { 083 return properties; 084 } 085 086}