001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.tide.data; 023 024import java.io.Externalizable; 025import java.io.IOException; 026import java.io.ObjectInput; 027import java.io.ObjectOutput; 028import java.io.Serializable; 029import java.util.HashMap; 030import java.util.Map; 031import java.util.Map.Entry; 032 033 034/** 035 * @author William DRAI 036 */ 037public class Change implements Externalizable { 038 039 private static final long serialVersionUID = 1L; 040 041 private String className; 042 private String uid; 043 private Serializable id; 044 private Number version; 045 private Map<String, Object> changes; 046 047 048 public Change() { 049 } 050 051 public Change(String className, Serializable id, Number version, String uid) { 052 this.className = className; 053 this.id = id; 054 this.version = version; 055 this.uid = uid; 056 this.changes = new HashMap<String, Object>(); 057 } 058 059 public String getClassName() { 060 return className; 061 } 062 063 public String getUid() { 064 return uid; 065 } 066 067 public Serializable getId() { 068 return id; 069 } 070 071 public Number getVersion() { 072 return version; 073 } 074 075 public Map<String, Object> getChanges() { 076 return changes; 077 } 078 079 public void addCollectionChanges(String propertyName, CollectionChange[] collChanges) { 080 CollectionChanges existingChanges = (CollectionChanges)changes.get(propertyName); 081 if (existingChanges == null) 082 changes.put(propertyName, new CollectionChanges(collChanges)); 083 else { 084 CollectionChange[] newChanges = new CollectionChange[existingChanges.getChanges().length+collChanges.length]; 085 System.arraycopy(existingChanges.getChanges(), 0, newChanges, 0, existingChanges.getChanges().length); 086 System.arraycopy(collChanges, 0, newChanges, existingChanges.getChanges().length, collChanges.length); 087 existingChanges.setChanges(newChanges); 088 } 089 } 090 091 @Override 092 public String toString() { 093 StringBuilder sb = new StringBuilder(); 094 sb.append(getClassName()).append(':').append(getUid()).append(":").append(getId()).append(':').append(getVersion()).append("={"); 095 for (Entry<String, Object> change : getChanges().entrySet()) { 096 sb.append(change.getKey()).append(": ").append(change.getValue() != null ? change.getValue().toString() : "null"); 097 } 098 sb.append("}"); 099 return sb.toString(); 100 } 101 102 public void writeExternal(ObjectOutput out) throws IOException { 103 out.writeObject(className); 104 out.writeObject(uid); 105 out.writeObject(id); 106 out.writeObject(version); 107 out.writeObject(changes); 108 } 109 110 @SuppressWarnings("unchecked") 111 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 112 className = (String)in.readObject(); 113 uid = (String)in.readObject(); 114 id = (Serializable)in.readObject(); 115 version = (Number)in.readObject(); 116 changes = (Map<String, Object>)in.readObject(); 117 } 118}