001: /*
002: * ResourcePolicy.java
003: *
004: * Version: $Revision: 2714 $
005: *
006: * Date: $Date: 2008-02-27 08:23:45 -0600 (Wed, 27 Feb 2008) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.authorize;
041:
042: import java.sql.SQLException;
043: import java.util.Date;
044:
045: import org.dspace.content.DSpaceObject;
046: import org.dspace.core.Constants;
047: import org.dspace.core.Context;
048: import org.dspace.eperson.EPerson;
049: import org.dspace.eperson.Group;
050: import org.dspace.storage.rdbms.DatabaseManager;
051: import org.dspace.storage.rdbms.TableRow;
052:
053: /**
054: * Class representing a ResourcePolicy
055: *
056: * @author David Stuve
057: * @version $Revision: 2714 $
058: */
059: public class ResourcePolicy {
060: /** Our context */
061: private Context myContext;
062:
063: /** The row in the table representing this object */
064: private TableRow myRow;
065:
066: /**
067: * Construct an ResourcePolicy
068: *
069: * @param context
070: * the context this object exists in
071: * @param row
072: * the corresponding row in the table
073: */
074: ResourcePolicy(Context context, TableRow row) {
075: myContext = context;
076: myRow = row;
077: }
078:
079: /**
080: * Get an ResourcePolicy from the database.
081: *
082: * @param context
083: * DSpace context object
084: * @param id
085: * ID of the ResourcePolicy
086: *
087: * @return the ResourcePolicy format, or null if the ID is invalid.
088: */
089: public static ResourcePolicy find(Context context, int id)
090: throws SQLException {
091: TableRow row = DatabaseManager.find(context, "ResourcePolicy",
092: id);
093:
094: if (row == null) {
095: return null;
096: } else {
097: return new ResourcePolicy(context, row);
098: }
099: }
100:
101: /**
102: * Create a new ResourcePolicy
103: *
104: * @param context
105: * DSpace context object
106: */
107: public static ResourcePolicy create(Context context)
108: throws SQLException, AuthorizeException {
109: // FIXME: Check authorisation
110: // Create a table row
111: TableRow row = DatabaseManager
112: .create(context, "ResourcePolicy");
113:
114: return new ResourcePolicy(context, row);
115: }
116:
117: /**
118: * Delete an ResourcePolicy
119: *
120: */
121: public void delete() throws SQLException {
122: // FIXME: authorizations
123: // Remove ourself
124: DatabaseManager.delete(myContext, myRow);
125: }
126:
127: /**
128: * Get the e-person's internal identifier
129: *
130: * @return the internal identifier
131: */
132: public int getID() {
133: return myRow.getIntColumn("policy_id");
134: }
135:
136: /**
137: * Get the type of the objects referred to by policy
138: *
139: * @return type of object/resource
140: */
141: public int getResourceType() {
142: return myRow.getIntColumn("resource_type_id");
143: }
144:
145: /**
146: * set both type and id of resource referred to by policy
147: *
148: */
149: public void setResource(DSpaceObject o) {
150: setResourceType(o.getType());
151: setResourceID(o.getID());
152: }
153:
154: /**
155: * Set the type of the resource referred to by the policy
156: *
157: * @param mytype
158: * type of the resource
159: */
160: public void setResourceType(int mytype) {
161: myRow.setColumn("resource_type_id", mytype);
162: }
163:
164: /**
165: * Get the ID of a resource pointed to by the policy (is null if policy
166: * doesn't apply to a single resource.)
167: *
168: * @return resource_id
169: */
170: public int getResourceID() {
171: return myRow.getIntColumn("resource_id");
172: }
173:
174: /**
175: * If the policy refers to a single resource, this is the ID of that
176: * resource.
177: *
178: * @param myid id of resource (database primary key)
179: */
180: public void setResourceID(int myid) {
181: myRow.setColumn("resource_id", myid);
182: }
183:
184: /**
185: * @return get the action this policy authorizes
186: */
187: public int getAction() {
188: return myRow.getIntColumn("action_id");
189: }
190:
191: /**
192: * @return action text or 'null' if action row empty
193: */
194: public String getActionText() {
195: int myAction = myRow.getIntColumn("action_id");
196:
197: if (myAction == -1) {
198: return "...";
199: } else {
200: return Constants.actionText[myAction];
201: }
202: }
203:
204: /**
205: * set the action this policy authorizes
206: *
207: * @param myid action ID from <code>org.dspace.core.Constants</code>
208: */
209: public void setAction(int myid) {
210: myRow.setColumn("action_id", myid);
211: }
212:
213: /**
214: * @return eperson ID, or -1 if EPerson not set
215: */
216: public int getEPersonID() {
217: return myRow.getIntColumn("eperson_id");
218: }
219:
220: /**
221: * get EPerson this policy relates to
222: *
223: * @return EPerson, or null
224: */
225: public EPerson getEPerson() throws SQLException {
226: int eid = myRow.getIntColumn("eperson_id");
227:
228: if (eid == -1) {
229: return null;
230: }
231:
232: return EPerson.find(myContext, eid);
233: }
234:
235: /**
236: * assign an EPerson to this policy
237: *
238: * @param e EPerson
239: */
240: public void setEPerson(EPerson e) {
241: if (e != null) {
242: myRow.setColumn("eperson_id", e.getID());
243: } else {
244: myRow.setColumnNull("eperson_id");
245: }
246: }
247:
248: /**
249: * gets ID for Group referred to by this policy
250: *
251: * @return groupID, or -1 if no group set
252: */
253: public int getGroupID() {
254: return myRow.getIntColumn("epersongroup_id");
255: }
256:
257: /**
258: * gets Group for this policy
259: *
260: * @return Group, or -1 if no group set
261: */
262: public Group getGroup() throws SQLException {
263: int gid = myRow.getIntColumn("epersongroup_id");
264:
265: if (gid == -1) {
266: return null;
267: } else {
268: return Group.find(myContext, gid);
269: }
270: }
271:
272: /**
273: * set Group for this policy
274: *
275: * @param g group
276: */
277: public void setGroup(Group g) {
278: if (g != null) {
279: myRow.setColumn("epersongroup_id", g.getID());
280: } else {
281: myRow.setColumnNull("epersongroup_id");
282: }
283: }
284:
285: /**
286: * figures out if the date is valid for the policy
287: *
288: * @return true if policy has begun and hasn't expired yet (or no dates are
289: * set)
290: */
291: public boolean isDateValid() {
292: Date sd = getStartDate();
293: Date ed = getEndDate();
294:
295: // if no dates set, return true (most common case)
296: if ((sd == null) && (ed == null)) {
297: return true;
298: }
299:
300: // one is set, now need to do some date math
301: Date now = new Date();
302:
303: // check start date first
304: if (sd != null) {
305: // start date is set, return false if we're before it
306: if (now.before(sd)) {
307: return false;
308: }
309: }
310:
311: // now expiration date
312: if (ed != null) {
313: // end date is set, return false if we're after it
314: if (now.after(ed)) {
315: return false;
316: }
317: }
318:
319: // if we made it this far, start < now < end
320: return true; // date must be okay
321: }
322:
323: /**
324: * Get the start date of the policy
325: *
326: * @return start date, or null if there is no start date set (probably most
327: * common case)
328: */
329: public java.util.Date getStartDate() {
330: return myRow.getDateColumn("start_date");
331: }
332:
333: /**
334: * Set the start date for the policy
335: *
336: * @param d
337: * date, or null for no start date
338: */
339: public void setStartDate(java.util.Date d) {
340: myRow.setColumn("start_date", d);
341: }
342:
343: /**
344: * Get end date for the policy
345: *
346: * @return end date or null for no end date
347: */
348: public java.util.Date getEndDate() {
349: return myRow.getDateColumn("end_date");
350: }
351:
352: /**
353: * Set end date for the policy
354: *
355: * @param d
356: * end date, or null
357: */
358: public void setEndDate(java.util.Date d) {
359: myRow.setColumn("end_date", d);
360: }
361:
362: /**
363: * Update the ResourcePolicy
364: */
365: public void update() throws SQLException {
366: // FIXME: Check authorisation
367: DatabaseManager.update(myContext, myRow);
368: }
369: }
|