1 4 package com.nightlabs.ipanema.person; 5 6 import java.io.Serializable ; 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import com.nightlabs.ipanema.base.DuplicateKeyException; 13 import com.nightlabs.ipanema.person.id.PersonStructFieldID; 14 15 19 20 public class PersonDataBlock implements Serializable { 22 23 protected PersonDataBlock() { } 24 25 32 public PersonDataBlock(PersonDataBlockGroup _personDataBlockGroup, Integer _personDataBlockID) 33 { 34 this.personDataBlockGroup = _personDataBlockGroup; 35 36 this.organisationID = _personDataBlockGroup.getOrganisationID(); 37 this.personID = _personDataBlockGroup.getPersonID(); 38 this.personStructBlockOrganisationID = _personDataBlockGroup.getPersonStructBlockOrganisationID(); 39 this.personStructBlockID = _personDataBlockGroup.getPersonStructBlockID(); 40 41 this.personDataBlockID = _personDataBlockID; 42 } 43 44 void addPersonDataFieldToStructure(AbstractPersonDataField field) { 45 boolean found = false; 46 Object key = null; 47 for (Iterator iter = personDataFields.entrySet().iterator(); iter.hasNext();) { 48 Map.Entry entry = (Map.Entry )iter.next(); 49 AbstractPersonDataField containedField = (AbstractPersonDataField) entry.getValue(); 50 if (containedField.equals(field)) { 51 key = entry.getKey(); 52 found = true; 53 break; 54 } 55 } 56 if (found) { 57 personDataFields.put(key,field); 59 } 60 else { 61 personDataFields.put( 63 AbstractPersonStructField.getStructFieldKey(field.getPersonStructFieldOrganisationID(),field.getPersonStructFieldID()), 64 field 65 ); 66 } 67 } 68 69 70 77 public boolean haveStructFieldRepresentation(AbstractPersonStructField structField) { 78 return personDataFields.containsKey(structField.getStructFieldKey()); 79 } 80 81 84 public void explode(PersonStruct structure) { 85 if (this.personDataBlockGroup == null) 86 return; 87 PersonStructBlock psb; 88 try { 89 psb = structure.getPersonStructBlock( 90 this.personStructBlockOrganisationID, this.personStructBlockID 91 ); 92 } catch (PersonStructBlockNotFoundException e) { 93 IllegalStateException ill = new IllegalStateException ("Could not find StructBlock for DataBlock!"); 94 ill.initCause(e); 95 throw ill; 96 } 97 for (Iterator it = psb.getPersonStructFields().iterator(); it.hasNext(); ) { 98 AbstractPersonStructField structField = (AbstractPersonStructField)it.next(); 99 if (!haveStructFieldRepresentation(structField)) 100 structField.addNewDataFieldInstance(this); 101 } 102 } 103 104 private PersonDataBlockGroup personDataBlockGroup; 105 106 private String organisationID; 107 108 private long personID; 109 110 private String personStructBlockOrganisationID; 111 112 private String personStructBlockID; 113 114 private Integer personDataBlockID; 115 116 117 120 public String getOrganisationID() { 121 return organisationID; 122 } 123 124 127 public PersonDataBlockGroup getPersonDataBlockGroup() { 128 return personDataBlockGroup; 129 } 130 131 134 public Integer getPersonDataBlockID() { 135 return personDataBlockID; 136 } 137 138 141 public long getPersonID() { 142 return personID; 143 } 144 145 150 void setPersonID(long _personID) { 151 this.personID = _personID; 152 } 153 154 157 public String getPersonStructBlockOrganisationID() { 158 return personStructBlockOrganisationID; 159 } 160 161 164 public String getPersonStructBlockID() { 165 return personStructBlockID; 166 } 167 168 169 170 175 protected Map personDataFields = new HashMap (); 176 177 178 public Collection getPersonDataFields() { 179 return personDataFields.values(); 180 } 181 182 192 public void addPersonDataField(AbstractPersonDataField dataField) 193 throws DuplicateKeyException 194 { 195 if (dataField == null) 196 throw new IllegalArgumentException ("Parameter dataField must not be null!"); 197 String structFieldKey = AbstractPersonStructField.getStructFieldKey(dataField.getPersonStructFieldOrganisationID(),dataField.getPersonStructFieldID()); 198 if (personDataFields.containsKey(structFieldKey)) 199 throw new DuplicateKeyException("DataBlock "+this+" already contains a field with structFiedKey = "+structFieldKey); 200 personDataFields.put(structFieldKey,dataField); 201 personDataBlockGroup.getPerson().putDataFieldToPersistentList(dataField); 203 } 204 205 public AbstractPersonDataField getPersonDataField(PersonStructFieldID personStructFieldID) 206 throws PersonDataFieldNotFoundException 207 { 208 return getPersonDataField(personStructFieldID.personStructFieldOrganisationID, personStructFieldID.personStructFieldID); 209 } 210 211 public AbstractPersonDataField getPersonDataField(String personStructFieldOrganisationID, String personStructFieldID) 212 throws PersonDataFieldNotFoundException 213 { 214 String structFieldKey = AbstractPersonStructField.getStructFieldKey(personStructFieldOrganisationID,personStructFieldID); 215 AbstractPersonDataField field = (AbstractPersonDataField)personDataFields.get(structFieldKey); 216 if (field == null) 217 throw new PersonDataFieldNotFoundException("No field "+structFieldKey+" found in this DataBlock for StructBlock "+PersonStructBlock.getPrimaryKey(personStructBlockOrganisationID,personStructBlockID)); 218 return field; 219 } 220 221 225 public void implodeBlock() { 226 for (Iterator it = personDataFields.entrySet().iterator(); it.hasNext(); ) { 227 Map.Entry entry = (Map.Entry )it.next(); 228 AbstractPersonDataField field = (AbstractPersonDataField)entry.getValue(); 229 if (field.isEmpty()) { 230 personDataBlockGroup.getPerson().removeDataFieldFromPersistentList(field); 231 it.remove(); 232 } 233 } 234 } 235 236 240 public boolean isEmpty() { 241 return personDataFields.isEmpty(); 242 } 243 244 245 248 public void jdoPostLoad() { 249 System.out.println(this.getClass().getName()+"|"+this+": jdoPostLoad()"); 250 } 252 253 256 public void jdoPreStore() { 257 } 258 259 262 public void jdoPreClear() { 263 } 264 265 268 public void jdoPreDelete() { 269 } 270 271 } 272 | Popular Tags |