001: /*
002: * DSpaceObject.java
003: *
004: * Version: $Revision: 2074 $
005: *
006: * Date: $Date: 2007-07-19 14:40:11 -0500 (Thu, 19 Jul 2007) $
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.content;
041:
042: import java.sql.SQLException;
043:
044: import org.dspace.core.Constants;
045: import org.dspace.core.Context;
046: import org.dspace.eperson.EPerson;
047: import org.dspace.eperson.Group;
048:
049: /**
050: * Abstract base class for DSpace objects
051: */
052: public abstract class DSpaceObject {
053: // accumulate information to add to "detail" element of content Event,
054: // e.g. to document metadata fields touched, etc.
055: private StringBuffer eventDetails = null;
056:
057: /**
058: * Reset the cache of event details.
059: */
060: protected void clearDetails() {
061: eventDetails = null;
062: }
063:
064: /**
065: * Add a string to the cache of event details. Automatically
066: * separates entries with a comma.
067: * Subclass can just start calling addDetails, since it creates
068: * the cache if it needs to.
069: * @param detail detail string to add.
070: */
071: protected void addDetails(String d) {
072: if (eventDetails == null)
073: eventDetails = new StringBuffer(d);
074: else
075: eventDetails.append(", ").append(d);
076: }
077:
078: /**
079: * @returns summary of event details, or null if there are none.
080: */
081: protected String getDetails() {
082: return (eventDetails == null ? null : eventDetails.toString());
083: }
084:
085: /**
086: * Get the type of this object, found in Constants
087: *
088: * @return type of the object
089: */
090: public abstract int getType();
091:
092: /**
093: * Get the internal ID (database primary key) of this object
094: *
095: * @return internal ID of object
096: */
097: public abstract int getID();
098:
099: /**
100: * Get the Handle of the object. This may return <code>null</code>
101: *
102: * @return Handle of the object, or <code>null</code> if it doesn't have
103: * one
104: */
105: public abstract String getHandle();
106:
107: /**
108: * Get a proper name for the object. This may return <code>null</code>.
109: * Name should be suitable for display in a user interface.
110: *
111: * @return Name for the object, or <code>null</code> if it doesn't have
112: * one
113: */
114: abstract public String getName();
115:
116: /**
117: * Generic find for when the precise type of a DSO is not known, just the
118: * a pair of type number and database ID.
119: *
120: * @param context - the context
121: * @param type - type number
122: * @param id - id within table of type'd objects
123: * @return the object found, or null if it does not exist.
124: * @throws SQLException only upon failure accessing the database.
125: */
126: public static DSpaceObject find(Context context, int type, int id)
127: throws SQLException {
128: switch (type) {
129: case Constants.BITSTREAM:
130: return Bitstream.find(context, id);
131: case Constants.BUNDLE:
132: return Bundle.find(context, id);
133: case Constants.ITEM:
134: return Item.find(context, id);
135: case Constants.COLLECTION:
136: return Collection.find(context, id);
137: case Constants.COMMUNITY:
138: return Community.find(context, id);
139: case Constants.GROUP:
140: return Group.find(context, id);
141: case Constants.EPERSON:
142: return EPerson.find(context, id);
143: case Constants.SITE:
144: return Site.find(context, id);
145: }
146: return null;
147: }
148: }
|