KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > person > PersonDataBlockGroup


1 /*
2  * Created on 18.03.2004
3  */

4 package com.nightlabs.ipanema.person;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11
12 /**
13  * One instance of this class exists per personStructBlockID and groups all instances of
14  * PersonDataBlock with the same personStructBlockID.
15  *
16  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
17  * @author marco
18  */

19
20 public class PersonDataBlockGroup
21 implements
22     Serializable JavaDoc,
23     Comparable JavaDoc
24 {
25
26     protected PersonDataBlockGroup() { }
27     
28     public PersonDataBlockGroup(Person _person, String JavaDoc _personStructBlockOrganisationID, String JavaDoc _personStructBlockID) {
29         this.person = _person;
30         this.personStructBlockOrganisationID = _personStructBlockOrganisationID;
31         this.personStructBlockID = _personStructBlockID;
32         this.structBlockKey = PersonStructBlock.getPrimaryKey(_personStructBlockOrganisationID,_personStructBlockID);
33         this.organisationID = _person.getOrganisationID();
34         this.personID = _person.getPersonID();
35     }
36     
37     
38     void addPersonDataFieldToStructure(AbstractPersonDataField field) {
39         PersonDataBlock fieldBlock = null;
40         if (!personDataBlocks.containsKey(field.getPersonDataBlockID())) {
41             for (int i=0; i<=field.getPersonDataBlockID().intValue(); i++) {
42                 if (!personDataBlocks.containsKey(new Integer JavaDoc(i))) {
43                     Integer JavaDoc blockID = new Integer JavaDoc(i);
44                     PersonDataBlock block = new PersonDataBlock(this,new Integer JavaDoc(i));
45                     personDataBlocks.put(blockID, block);
46                     if (blockID.equals(field.getPersonDataBlockID())) {
47                         fieldBlock = block;
48                     }
49                 }
50             }
51         } else {
52             fieldBlock = (PersonDataBlock)personDataBlocks.get(field.getPersonDataBlockID());
53         }
54         fieldBlock.addPersonDataFieldToStructure(field);
55     }
56     
57     private String JavaDoc organisationID;
58     private long personID;
59     private String JavaDoc personStructBlockOrganisationID;
60     private String JavaDoc personStructBlockID;
61
62     public PersonStructBlock getPersonStructBlock(PersonStruct structure) {
63         try {
64             return structure.getPersonStructBlock(getPersonStructBlockOrganisationID(),getPersonStructBlockID());
65         } catch (PersonStructBlockNotFoundException e) {
66             IllegalStateException JavaDoc ill = new IllegalStateException JavaDoc("Caught Exception when accessing PersonStructFactory");
67             ill.initCause(e);
68             throw ill;
69         }
70     }
71
72     private String JavaDoc structBlockKey;
73     
74     public String JavaDoc getStructBlockKey() {
75         return structBlockKey;
76     }
77
78     private Person person;
79
80     public Person getPerson() { return person; }
81     
82     void setPerson(Person person) {
83         this.person = person;
84     }
85     
86     /**
87      * key: Integer personDataBlockID<br/>
88      * value: PersonDataBlock personDataBlock
89      */

90     protected Map JavaDoc personDataBlocks = new HashMap JavaDoc();
91     
92     public boolean isEmpty()
93     {
94         if (personDataBlocks == null) return true;
95         return personDataBlocks.isEmpty();
96     }
97
98     /**
99      * @param personDataBlockID The ID of the desired DataBlock.
100      * @return Returns the desired PersonDataBlock, if existent.
101      * @throws PersonDataBlockNotFoundException if the desired PersonDataBlock does not exist.
102      *
103      * @see getPersonDataBlock(int personDataBlockID, boolean throwExceptionIfNotFound)
104      */

105     public PersonDataBlock getPersonDataBlock(int personDataBlockID)
106     throws PersonDataBlockNotFoundException
107     {
108         return getPersonDataBlock(personDataBlockID, true);
109     }
110     
111     /**
112      * @param personDataBlockID The ID of the desired DataBlock.
113      * @param throwExceptionIfNotFound Controls whether or not to throw an exception, if the desired DataBlock does not exist.
114      * @return the desired PersonDataBlock, if existent, otherwise it may return null or throw an exception.
115      * @throws PersonDataBlockNotFoundException if the desired PersonDataBlock does not exist and throwExceptionIfNotFound is true.
116      *
117      * @see getPersonDataBlock(int personDataBlockID)
118      */

119     public PersonDataBlock getPersonDataBlock(int personDataBlockID, boolean throwExceptionIfNotFound)
120     throws PersonDataBlockNotFoundException
121     {
122         PersonDataBlock pdb = null;
123         if (personDataBlocks != null) {
124           pdb = (PersonDataBlock) personDataBlocks.get(new Integer JavaDoc(personDataBlockID));
125         }
126         
127         if (throwExceptionIfNotFound && pdb == null)
128             throw new PersonDataBlockNotFoundException("PersonDataBlock(organisationID=\""+organisationID+"\", personID=\""+personID+"\", personStructBlockID=\""+personStructBlockID+"\", personDataBlockID=\""+personDataBlockID+"\") not existent!");
129         return pdb;
130     }
131     
132     /**
133      * Adds a new DataBlock to this group.
134      * All fields within the Block are initialized as well.<br/>
135      * If the second Block for a unique PersonStructBlock
136      * should be created a DataBlockUniqueException is thrown.
137      * @return
138      */

139     public PersonDataBlock newPersonDataBlock(PersonStruct structure)
140     throws PersonDataBlockUniqueException
141     {
142         int personDataBlockID = 0;
143         Integer JavaDoc personDataBlockIDInt = new Integer JavaDoc(personDataBlockID);
144         while (personDataBlocks.containsKey(personDataBlockIDInt)) {
145           ++personDataBlockID;
146           personDataBlockIDInt = new Integer JavaDoc(personDataBlockID);
147         }
148         if (personDataBlockID > 0) {
149             // not the first DataBlock
150
if (getPersonStructBlock(structure).isUnique())
151                 throw new PersonDataBlockUniqueException("Attempt to add second DataBlock for StructBlock "+getPersonStructBlock(structure).getPrimaryKey()+" wich is unique!");
152         }
153         // this constructor initializes fields as well
154
PersonDataBlock pdb = new PersonDataBlock(this, personDataBlockIDInt);
155         
156         personDataBlocks.put(personDataBlockIDInt, pdb);
157         return pdb;
158     }
159     
160     public Collection JavaDoc getPersonDataBlocks()
161     {
162         return personDataBlocks.values();
163     }
164
165
166     /**
167      * @return Returns the organisationID.
168      */

169     public String JavaDoc getOrganisationID() {
170         return organisationID;
171     }
172
173     /**
174      * @return Returns the personID.
175      */

176     public long getPersonID() {
177         return personID;
178     }
179     
180     /**
181      * Package visible modifier for the personID
182      * @param _personID
183      */

184     void setPersonID(long _personID) {
185         this.personID = _personID;
186     }
187
188     /**
189      * @return Returns the personStructBlockID.
190      */

191     public String JavaDoc getPersonStructBlockID() {
192         return personStructBlockID;
193     }
194
195     /**
196      * @return Returns the personStructBlockID.
197      */

198     public String JavaDoc getPersonStructBlockOrganisationID() {
199         return personStructBlockOrganisationID;
200     }
201     
202     /**
203      * Scans all DataBlocks and its containing DataFields
204      * and removes all entries where isEmpty() returns true.
205      */

206     public void implodeBlockGroup() {
207         for (Iterator JavaDoc it = personDataBlocks.entrySet().iterator(); it.hasNext(); ) {
208             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
209             PersonDataBlock block = (PersonDataBlock)entry.getValue();
210             block.implodeBlock();
211             if (block.isEmpty())
212                 it.remove();
213         }
214     }
215     
216     /**
217      * Adds all not contained fields for all
218      * blocks in this group.
219      */

220     public void explodeBlockGroup(PersonStruct structure) {
221         for (Iterator JavaDoc it = personDataBlocks.values().iterator(); it.hasNext(); ) {
222             PersonDataBlock block = (PersonDataBlock)it.next();
223             block.explode(structure);
224         }
225     }
226     
227     private int priority = Integer.MAX_VALUE;
228     
229     public void setPriority(int priority) {
230         this.priority = priority;
231     }
232     
233     /**
234      * @see java.lang.Comparable#compareTo(java.lang.Object)
235      */

236     public int compareTo(Object JavaDoc o) {
237         if (!(o instanceof PersonDataBlockGroup)) {
238             if (this.equals(o)) {
239                 // to be consistent with equals
240
return 0;
241             }
242             else return 1;
243         }
244         PersonDataBlockGroup other = (PersonDataBlockGroup)o;
245         if (other.priority < this.priority)
246             return 1;
247         else if (other.priority > this.priority)
248             return -1;
249         else {
250             if (this.equals(o)) {
251                 // to be consistent with equals
252
return 0;
253             }
254             else return 1;
255         }
256     }
257     
258 }
259
Popular Tags