001/* 002 * (C) Copyright 2006-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 * Vladimir Pasquier <[email protected]> 018 * Stéphane Lacoin <[email protected]> 019 */ 020package org.nuxeo.ecm.automation.client.model; 021 022import java.util.Date; 023import java.util.HashMap; 024import java.util.HashSet; 025import java.util.Map; 026import java.util.Set; 027 028/** 029 * @since 5.7 Delegate containing data injection for PropertyMap object. Keeping dirty properties in memory. 030 */ 031public class PropertyMapSetter { 032 033 protected final Map<String, Object> map; 034 035 protected final Set<String> dirties = new HashSet<String>(); 036 037 public PropertyMapSetter(PropertyMap propertyMap) { 038 map = propertyMap.map; 039 } 040 041 /** 042 * @since 5.7 This method fetch all dirty properties that has been defined. Warning: Dirty properties are not 043 * flushed when getting it. 044 * @return PropertyMap 045 */ 046 public PropertyMap getDirties() { 047 Map<String, Object> resultMap = new HashMap<String, Object>(); 048 for (String key : dirties) { 049 Object value = map.get(key); 050 resultMap.put(key, value); 051 } 052 return new PropertyMap(resultMap); 053 } 054 055 public void set(String key, String value) { 056 if (value == null) { 057 map.remove(key); 058 } 059 map.put(key, value); 060 dirties.add(key); 061 } 062 063 public void set(String key, Boolean value) { 064 if (value == null) { 065 map.remove(key); 066 } else { 067 map.put(key, value.toString()); 068 } 069 dirties.add(key); 070 } 071 072 public void set(String key, Long value) { 073 if (value == null) { 074 map.remove(key); 075 } else { 076 map.put(key, value.toString()); 077 } 078 dirties.add(key); 079 } 080 081 public void set(String key, Double value) { 082 if (value == null) { 083 map.remove(key); 084 } else { 085 map.put(key, value.toString()); 086 } 087 dirties.add(key); 088 } 089 090 public void set(String key, Date value) { 091 if (value == null) { 092 map.remove(key); 093 } 094 map.put(key, DateUtils.formatDate(value)); 095 dirties.add(key); 096 } 097 098 public void set(String key, PropertyList value) { 099 if (value == null) { 100 map.remove(key); 101 } 102 map.put(key, value); 103 dirties.add(key); 104 } 105 106 public void set(String key, PropertyMap value) { 107 if (value == null) { 108 map.remove(key); 109 } 110 map.put(key, value); 111 dirties.add(key); 112 } 113 114}