KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > authorize > ResourcePolicy


1 /*
2  * ResourcePolicy.java
3  *
4  * Version: $Revision: 1.12 $
5  *
6  * Date: $Date: 2006/03/16 18:31:09 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.authorize;
41
42 import java.sql.SQLException JavaDoc;
43 import java.util.Date JavaDoc;
44
45 import org.dspace.content.DSpaceObject;
46 import org.dspace.core.Constants;
47 import org.dspace.core.Context;
48 import org.dspace.eperson.EPerson;
49 import org.dspace.eperson.Group;
50 import org.dspace.storage.rdbms.DatabaseManager;
51 import org.dspace.storage.rdbms.TableRow;
52
53 /**
54  * Class representing a ResourcePolicy
55  *
56  * @author David Stuve
57  * @version $Revision: 1.12 $
58  */

59 public class ResourcePolicy
60 {
61     /** Our context */
62     private Context myContext;
63
64     /** The row in the table representing this object */
65     private TableRow myRow;
66
67     /**
68      * Construct an ResourcePolicy
69      *
70      * @param context
71      * the context this object exists in
72      * @param row
73      * the corresponding row in the table
74      */

75     ResourcePolicy(Context context, TableRow row)
76     {
77         myContext = context;
78         myRow = row;
79     }
80
81     /**
82      * Get an ResourcePolicy from the database.
83      *
84      * @param context
85      * DSpace context object
86      * @param id
87      * ID of the ResourcePolicy
88      *
89      * @return the ResourcePolicy format, or null if the ID is invalid.
90      */

91     public static ResourcePolicy find(Context context, int id)
92             throws SQLException JavaDoc
93     {
94         TableRow row = DatabaseManager.find(context, "ResourcePolicy", id);
95
96         if (row == null)
97         {
98             return null;
99         }
100         else
101         {
102             return new ResourcePolicy(context, row);
103         }
104     }
105
106     /**
107      * Create a new ResourcePolicy
108      *
109      * @param context
110      * DSpace context object
111      */

112     public static ResourcePolicy create(Context context) throws SQLException JavaDoc,
113             AuthorizeException
114     {
115         // FIXME: Check authorisation
116
// Create a table row
117
TableRow row = DatabaseManager.create(context, "ResourcePolicy");
118
119         return new ResourcePolicy(context, row);
120     }
121
122     /**
123      * Delete an ResourcePolicy
124      *
125      */

126     public void delete() throws SQLException JavaDoc
127     {
128         // FIXME: authorizations
129
// Remove ourself
130
DatabaseManager.delete(myContext, myRow);
131     }
132
133     /**
134      * Get the e-person's internal identifier
135      *
136      * @return the internal identifier
137      */

138     public int getID()
139     {
140         return myRow.getIntColumn("policy_id");
141     }
142
143     /**
144      * Get the type of the objects referred to by policy
145      *
146      * @return type of object/resource
147      */

148     public int getResourceType()
149     {
150         return myRow.getIntColumn("resource_type_id");
151     }
152
153     /**
154      * set both type and id of resource referred to by policy
155      *
156      */

157     public void setResource(DSpaceObject o)
158     {
159         setResourceType(o.getType());
160         setResourceID(o.getID());
161     }
162
163     /**
164      * Set the type of the resource referred to by the policy
165      *
166      * @param mytype
167      * type of the resource
168      */

169     public void setResourceType(int mytype)
170     {
171         myRow.setColumn("resource_type_id", mytype);
172     }
173
174     /**
175      * Get the ID of a resource pointed to by the policy (is null if policy
176      * doesn't apply to a single resource.)
177      *
178      * @return resource_id
179      */

180     public int getResourceID()
181     {
182         return myRow.getIntColumn("resource_id");
183     }
184
185     /**
186      * If the policy refers to a single resource, this is the ID of that
187      * resource.
188      *
189      * @param myid id of resource (database primary key)
190      */

191     public void setResourceID(int myid)
192     {
193         myRow.setColumn("resource_id", myid);
194     }
195
196     /**
197      * @return get the action this policy authorizes
198      */

199     public int getAction()
200     {
201         return myRow.getIntColumn("action_id");
202     }
203
204     /**
205      * @return action text or 'null' if action row empty
206      */

207     public String JavaDoc getActionText()
208     {
209         int myAction = myRow.getIntColumn("action_id");
210
211         if (myAction == -1)
212         {
213             return "...";
214         }
215         else
216         {
217             return Constants.actionText[myAction];
218         }
219     }
220
221     /**
222      * set the action this policy authorizes
223      *
224      * @param myid action ID from <code>org.dspace.core.Constants</code>
225      */

226     public void setAction(int myid)
227     {
228         myRow.setColumn("action_id", myid);
229     }
230
231     /**
232      * @return eperson ID, or -1 if EPerson not set
233      */

234     public int getEPersonID()
235     {
236         return myRow.getIntColumn("eperson_id");
237     }
238
239     /**
240      * get EPerson this policy relates to
241      *
242      * @return EPerson, or null
243      */

244     public EPerson getEPerson() throws SQLException JavaDoc
245     {
246         int eid = myRow.getIntColumn("eperson_id");
247
248         if (eid == -1)
249         {
250             return null;
251         }
252
253         return EPerson.find(myContext, eid);
254     }
255
256     /**
257      * assign an EPerson to this policy
258      *
259      * @param e EPerson
260      */

261     public void setEPerson(EPerson e)
262     {
263         if (e != null)
264         {
265             myRow.setColumn("eperson_id", e.getID());
266         }
267         else
268         {
269             myRow.setColumnNull("eperson_id");
270         }
271     }
272
273     /**
274      * gets ID for Group referred to by this policy
275      *
276      * @return groupID, or -1 if no group set
277      */

278     public int getGroupID()
279     {
280         return myRow.getIntColumn("epersongroup_id");
281     }
282
283     /**
284      * gets Group for this policy
285      *
286      * @return Group, or -1 if no group set
287      */

288     public Group getGroup() throws SQLException JavaDoc
289     {
290         int gid = myRow.getIntColumn("epersongroup_id");
291
292         if (gid == -1)
293         {
294             return null;
295         }
296         else
297         {
298             return Group.find(myContext, gid);
299         }
300     }
301
302     /**
303      * set Group for this policy
304      *
305      * @param g group
306      */

307     public void setGroup(Group g)
308     {
309         if (g != null)
310         {
311             myRow.setColumn("epersongroup_id", g.getID());
312         }
313         else
314         {
315             myRow.setColumnNull("epersongroup_id");
316         }
317     }
318
319     /**
320      * figures out if the date is valid for the policy
321      *
322      * @return true if policy has begun and hasn't expired yet (or no dates are
323      * set)
324      */

325     public boolean isDateValid()
326     {
327         Date JavaDoc sd = getStartDate();
328         Date JavaDoc ed = getEndDate();
329
330         // if no dates set, return true (most common case)
331
if ((sd == null) && (ed == null))
332         {
333             return true;
334         }
335
336         // one is set, now need to do some date math
337
Date JavaDoc now = new Date JavaDoc();
338
339         // check start date first
340
if (sd != null)
341         {
342             // start date is set, return false if we're before it
343
if (now.before(sd))
344             {
345                 return false;
346             }
347         }
348
349         // now expiration date
350
if (ed != null)
351         {
352             // end date is set, return false if we're after it
353
if (now.after(sd))
354             {
355                 return false;
356             }
357         }
358
359         // if we made it this far, start < now < end
360
return true; // date must be okay
361
}
362
363     /**
364      * Get the start date of the policy
365      *
366      * @return start date, or null if there is no start date set (probably most
367      * common case)
368      */

369     public java.util.Date JavaDoc getStartDate()
370     {
371         return myRow.getDateColumn("start_date");
372     }
373
374     /**
375      * Set the start date for the policy
376      *
377      * @param d
378      * date, or null for no start date
379      */

380     public void setStartDate(java.util.Date JavaDoc d)
381     {
382         myRow.setColumn("start_date", d);
383     }
384
385     /**
386      * Get end date for the policy
387      *
388      * @return end date or null for no end date
389      */

390     public java.util.Date JavaDoc getEndDate()
391     {
392         return myRow.getDateColumn("end_date");
393     }
394
395     /**
396      * Set end date for the policy
397      *
398      * @param d
399      * end date, or null
400      */

401     public void setEndDate(java.util.Date JavaDoc d)
402     {
403         myRow.setColumn("end_date", d);
404     }
405
406     /**
407      * Update the ResourcePolicy
408      */

409     public void update() throws SQLException JavaDoc
410     {
411         // FIXME: Check authorisation
412
DatabaseManager.update(myContext, myRow);
413     }
414 }
415
Popular Tags