KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > organisation > RegistrationStatus


1 /*
2  * Created on Dec 22, 2004
3  */

4 package com.nightlabs.ipanema.organisation;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Date JavaDoc;
9
10 import javax.jdo.JDOHelper;
11 import javax.jdo.PersistenceManager;
12 import javax.jdo.Query;
13
14 import com.nightlabs.ipanema.security.User;
15
16 /**
17  * Before two organisations can cooperate (buy/sell stuff from each other),
18  * they need to be mutually registered. This registration has two steps:
19  * <ul>
20  * <li>
21  * User A commands his Organisation A to register Organisation B.
22  * By doing that, a user for Organisation B is created within Organisation A
23  * and both Organisations have an instance of RegistrationStatus created.
24  * </li>
25  * <li>
26  * User B tells his Organisation B that it should either reject or accept
27  * the registration request of Organisation A. If it is accepted, a user
28  * for the Organisation A will be generated and the password will be
29  * transferred to Organisation A.
30  * </li>
31  * </ul>
32  * Additionally, Organisation A is able to cancel it's request.
33  *
34  * @author Marco Schulze - marco at nightlabs dot de
35  *
36  * @jdo.persistence-capable
37  * identity-type = "application"
38  * objectid-class = "com.nightlabs.ipanema.organisation.id.RegistrationStatusID"
39  * detachable = "true"
40  *
41  * @jdo.inheritance strategy = "new-table"
42  */

43 public class RegistrationStatus
44         implements Serializable JavaDoc
45 {
46     public static String JavaDoc DIRECTION_WE_APPLY = "we";
47     public static String JavaDoc DIRECTION_THEY_APPLY = "they";
48
49     public static String JavaDoc STATUS_PENDING = "pending";
50     public static String JavaDoc STATUS_ACCEPTED = "accepted";
51     public static String JavaDoc STATUS_REJECTED = "rejected";
52     public static String JavaDoc STATUS_CANCELLED = "cancelled";
53
54     public static void ensureRegisterability(
55             PersistenceManager pm,
56             LocalOrganisation localOrganisation,
57             String JavaDoc otherOrganisationID)
58         throws OrganisationAlreadyRegisteredException
59     {
60         Query query = pm.newQuery(RegistrationStatus.class, "this.organisationID == applicantOrganisationID && this.status == statusAccepted");
61         query.declareImports("import "+String JavaDoc.class.getName());
62         query.declareParameters("String applicantOrganisationID, String statusAccepted");
63         Collection JavaDoc c = (Collection JavaDoc)query.execute(
64                 otherOrganisationID, RegistrationStatus.STATUS_ACCEPTED);
65         if (!c.isEmpty())
66             throw new OrganisationAlreadyRegisteredException("The organisation \""+otherOrganisationID+"\" is already registered (status accepted) at organisation \""+localOrganisation.getOrganisationID()+"\"!");
67
68         RegistrationStatus registrationStatus = localOrganisation.getPendingRegistration(otherOrganisationID);
69         if (registrationStatus != null) {
70             // If there is already a pending registration, cancel it.
71
// If it is not pending, it's at leas sure that it is not accepted
72
// (because of check above) and therefore, it doesn't matter if it
73
// gets overwritten by a new one - and of course,
74
// it doesn't need to be cancelled, either.
75
if (RegistrationStatus.STATUS_PENDING.equals(registrationStatus.getStatus()))
76                 registrationStatus.cancel(null);
77         }
78     }
79
80     /**
81      * @jdo.field primary-key="true"
82      * @jdo.column length="100"
83      */

84     private String JavaDoc registrationID;
85
86     /**
87      * @jdo.field persistence-modifier="persistent"
88      * @jdo.column length="100"
89      */

90     private String JavaDoc organisationID;
91
92     /**
93      * @jdo.field persistence-modifier="persistent"
94      * @jdo.column length="100"
95      */

96     private String JavaDoc direction;
97
98     /**
99      * @jdo.field
100      * persistence-modifier="persistent"
101      */

102     private Organisation organisation = null;
103
104     /**
105      * @jdo.field persistence-modifier="persistent"
106      */

107     private String JavaDoc status;
108
109     /**
110      * When has the request for registration been done, means the registration
111      * been opened.
112      *
113      * @jdo.field persistence-modifier="persistent"
114      */

115     private Date JavaDoc openDT;
116     /**
117      * By whom was the registration done. Note, that this remains <tt>null</tt> in the
118      * organisation which is the receiver of the request, because the
119      * request needs to be done anonymously (logically - there's no user yet).
120      *
121      * @jdo.field persistence-modifier="persistent"
122      */

123     private User openUser = null;
124
125     /**
126      * When was the pending state closed. This can either be done by the organisation
127      * that has been asked (accept / reject) or by the organisation that was asking
128      * (cancel).
129      *
130      * @jdo.field persistence-modifier="persistent"
131      */

132     private Date JavaDoc closeDT = null;
133     /**
134      * Who closed the pending registration state. This is <tt>null</tt> on the side,
135      * where the command is issued by the other organisation (and not a "real" user).
136      *
137      * @jdo.field persistence-modifier="persistent"
138      */

139     private User closeUser = null;
140
141     /**
142      * @jdo.field persistence-modifier="persistent"
143      */

144     private String JavaDoc initialContextFactory = null;
145
146     /**
147      * @jdo.field persistence-modifier="persistent"
148      */

149     private String JavaDoc initialContextURL = null;
150
151     /**
152      * @deprecated This constructor is only existing for JDO.
153      */

154     protected RegistrationStatus()
155     {
156     }
157
158     /**
159      * Constructor to use if we are the ones who apply. There is no
160      * Organisation object existing yet, but we now our user.
161      *
162      * @param organisationID
163      * @param user
164      */

165     public RegistrationStatus(
166             String JavaDoc registrationID, String JavaDoc organisationID, User user,
167             String JavaDoc initialContextFactory, String JavaDoc initialContextURL)
168     {
169         if (registrationID == null)
170             throw new NullPointerException JavaDoc("registrationID");
171
172         this.registrationID = registrationID;
173         this.organisationID = organisationID;
174         this.openDT = new Date JavaDoc();
175         this.openUser = user;
176         this.direction = DIRECTION_WE_APPLY;
177         this.status = STATUS_PENDING;
178         this.initialContextFactory = initialContextFactory;
179         this.initialContextURL = initialContextURL;
180     }
181     
182     /**
183      * Constructor to use if another organisation applies for registration
184      * here. In this case, we now the Organisation object, but no user.
185      *
186      * @param organisation
187      * @param direction
188      */

189     public RegistrationStatus(String JavaDoc registrationID, Organisation organisation)
190     {
191         if (registrationID == null)
192             throw new NullPointerException JavaDoc("registrationID");
193
194         if (organisation == null)
195             throw new NullPointerException JavaDoc("organisation");
196
197         this.registrationID = registrationID;
198         this.organisation = organisation;
199         this.organisationID = organisation.getOrganisationID();
200         this.direction = DIRECTION_THEY_APPLY;
201         this.openDT = new Date JavaDoc();
202         this.status = STATUS_PENDING;
203     }
204     /**
205      * @return Returns the registrationID.
206      */

207     public String JavaDoc getRegistrationID()
208     {
209         return registrationID;
210     }
211     /**
212      * @return Returns the organisation.
213      */

214     public Organisation getOrganisation()
215     {
216         return organisation;
217     }
218     /**
219      * @return Returns the organisationID.
220      */

221     public String JavaDoc getOrganisationID()
222     {
223         return organisationID;
224     }
225     /**
226      * @return Returns the type.
227      */

228     public String JavaDoc getDirection()
229     {
230         return direction;
231     }
232     /**
233      * @return Returns the status.
234      */

235     public String JavaDoc getStatus()
236     {
237         return status;
238     }
239     
240     /**
241      * Close this registration and set the status to STATUS_ACCEPTED.
242      * This is only possible, if the current status is STATUS_PENDING.
243      *
244      * @param user Must NOT be <tt>null</tt>.
245      */

246     public void accept(User user)
247     {
248         if (user == null)
249             throw new NullPointerException JavaDoc("user");
250
251         if (!STATUS_PENDING.equals(getStatus()))
252             throw new IllegalStateException JavaDoc("Cannot accept, because status is not STATUS_PENDING!");
253
254         this.closeDT = new Date JavaDoc();
255         this.closeUser = user;
256         this.status = STATUS_ACCEPTED;
257     }
258
259     /**
260      * Close this registration and set the status to STATUS_CANCELLED.
261      * This is only possible, if the current status is STATUS_PENDING.
262      * Note that <tt>cancel</tt> and <tt>reject</tt> will both delete the
263      * <tt>Organisation</tt> object out of the datastore (if it exists).
264      *
265      * @param user Can be <tt>null</tt>, because the user is not known in the remote
266      * "granting" organisation.
267      *
268      * @see #reject(User)
269      * @see #cancelOrReject(User, String)
270      */

271     public void cancel(User user)
272     {
273         cancelOrReject(user, STATUS_CANCELLED);
274     }
275
276     /**
277      * This method does nearly the same as cancel, except that it uses STATUS_REJECTED.
278      *
279      * @see #cancel(User)
280      * @see #cancelOrReject(User, String)
281      */

282     public void reject(User user)
283     {
284         cancelOrReject(user, STATUS_REJECTED);
285     }
286
287     /**
288      * This method is called by <tt>cancel</tt> and by <tt>reject</tt> and does the actual
289      * work.
290      *
291      * @param user The user who is responsible (can be <tt>null</tt> if not known).
292      * @param status One of <tt>STATUS_CANCELLED</tt> or <tt>STATUS_REJECTED</tt>.
293      *
294      * @see #cancel(User)
295      * @see #reject(User)
296      */

297     protected void cancelOrReject(User user, String JavaDoc status)
298     {
299         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
300         if (pm == null)
301             throw new IllegalStateException JavaDoc("This instance of RegistrationStatus is currently not persistent!");
302
303         if (!STATUS_PENDING.equals(getStatus()))
304             throw new IllegalStateException JavaDoc("Cannot change status, because current status is not STATUS_PENDING!");
305
306         this.closeDT = new Date JavaDoc();
307         this.closeUser = user;
308         this.status = status;
309
310         if (this.organisation != null) {
311             Organisation org = this.organisation;
312             nullifyOrganisation();
313             pm.deletePersistent(org);
314         }
315     }
316
317     private void nullifyOrganisation()
318     {
319         this.organisation = null;
320     }
321     
322     /**
323      * @return Returns the closeDT.
324      */

325     public Date JavaDoc getCloseDT()
326     {
327         return closeDT;
328     }
329     /**
330      * @return Returns the closeUser.
331      */

332     public User getCloseUser()
333     {
334         return closeUser;
335     }
336     /**
337      * @return Returns the openDT.
338      */

339     public Date JavaDoc getOpenDT()
340     {
341         return openDT;
342     }
343     /**
344      * @return Returns the openUser.
345      */

346     public User getOpenUser()
347     {
348         return openUser;
349     }
350     /**
351      * @return Returns the initialContextFactory.
352      */

353     public String JavaDoc getInitialContextFactory()
354     {
355         return initialContextFactory;
356     }
357     /**
358      * @return Returns the initialContextURL.
359      */

360     public String JavaDoc getInitialContextURL()
361     {
362         return initialContextURL;
363     }
364 }
Popular Tags