KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > MetadataValue


1 /*
2  * MetadataValue.java
3  *
4  * Version: $Revision: 1.2 $
5  *
6  * Date: $Date: 2006/05/26 14:17:01 $
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.content;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 import org.apache.log4j.Logger;
47 import org.dspace.authorize.AuthorizeException;
48 import org.dspace.core.Context;
49 import org.dspace.core.LogManager;
50 import org.dspace.storage.rdbms.DatabaseManager;
51 import org.dspace.storage.rdbms.TableRow;
52 import org.dspace.storage.rdbms.TableRowIterator;
53
54 /**
55  * Database access class representing a Dublin Core metadata value.
56  * It represents a value of a given <code>MetadataField</code> on an Item.
57  * (The Item can have many values of the same field.) It contains element, qualifier, value and language.
58  * the field (which names the schema, element, and qualifer), language,
59  * and a value.
60  *
61  * @author Martin Hald
62  * @see org.dspace.content.MetadataSchema, org.dspace.content.MetadataField
63  */

64 public class MetadataValue
65 {
66     /** The reference to the metadata field */
67     private int fieldId = 0;
68
69     /** The primary key for the metadata value */
70     private int valueId = 0;
71
72     /** The reference to the DSpace item */
73     private int itemId;
74
75     /** The value of the field */
76     public String JavaDoc value;
77
78     /** The language of the field, may be <code>null</code> */
79     public String JavaDoc language;
80
81     /** The position of the record. */
82     public int place = 1;
83
84     /** log4j logger */
85     private static Logger log = Logger.getLogger(MetadataValue.class);
86
87     /** The row in the table representing this type */
88     private TableRow row;
89
90     /**
91      * Construct the metadata object from the matching database row.
92      *
93      * @param row database row to use for contents
94      */

95     public MetadataValue(TableRow row)
96     {
97         if (row != null)
98         {
99             fieldId = row.getIntColumn("metadata_field_id");
100             valueId = row.getIntColumn("metadata_value_id");
101             itemId = row.getIntColumn("item_id");
102             value = row.getStringColumn("text_value");
103             language = row.getStringColumn("text_lang");
104             place = row.getIntColumn("place");
105             this.row = row;
106         }
107     }
108
109     /**
110      * Default constructor.
111      */

112     public MetadataValue()
113     {
114     }
115
116     /**
117      * Constructor to create a value for a given field.
118      *
119      * @param field inital value for field
120      */

121     public MetadataValue(MetadataField field)
122     {
123         this.fieldId = field.getFieldID();
124     }
125
126     /**
127      * Get the field ID the metadata value represents.
128      *
129      * @return metadata field ID
130      */

131     public int getFieldId()
132     {
133         return fieldId;
134     }
135
136     /**
137      * Set the field ID that the metadata value represents.
138      *
139      * @param fieldId new field ID
140      */

141     public void setFieldId(int fieldId)
142     {
143         this.fieldId = fieldId;
144     }
145
146     /**
147      * Get the item ID.
148      *
149      * @return item ID
150      */

151     public int getItemId()
152     {
153         return itemId;
154     }
155
156     /**
157      * Set the item ID.
158      *
159      * @param itemId new item ID
160      */

161     public void setItemId(int itemId)
162     {
163         this.itemId = itemId;
164     }
165
166     /**
167      * Get the language (e.g. "en").
168      *
169      * @return language
170      */

171     public String JavaDoc getLanguage()
172     {
173         return language;
174     }
175
176     /**
177      * Set the language (e.g. "en").
178      *
179      * @param language new language
180      */

181     public void setLanguage(String JavaDoc language)
182     {
183         this.language = language;
184     }
185
186     /**
187      * Get the place ordering.
188      *
189      * @return place ordering
190      */

191     public int getPlace()
192     {
193         return place;
194     }
195
196     /**
197      * Set the place ordering.
198      *
199      * @param place new place (relative order in series of values)
200      */

201     public void setPlace(int place)
202     {
203         this.place = place;
204     }
205
206     /**
207      * Get the value ID.
208      *
209      * @return value ID
210      */

211     public int getValueId()
212     {
213         return valueId;
214     }
215
216     /**
217      * Get the metadata value.
218      *
219      * @return metadata value
220      */

221     public String JavaDoc getValue()
222     {
223         return value;
224     }
225
226     /**
227      * Set the metadata value
228      *
229      * @param value new metadata value
230      */

231     public void setValue(String JavaDoc value)
232     {
233         this.value = value;
234     }
235
236     /**
237      * Creates a new metadata value.
238      *
239      * @param context
240      * DSpace context object
241      * @throws SQLException
242      * @throws AuthorizeException
243      */

244     public void create(Context context) throws SQLException JavaDoc, AuthorizeException
245     {
246         // Create a table row and update it with the values
247
row = DatabaseManager.row("MetadataValue");
248         row.setColumn("item_id", itemId);
249         row.setColumn("metadata_field_id", fieldId);
250         row.setColumn("text_value", value);
251         row.setColumn("text_lang", language);
252         row.setColumn("place", place);
253         DatabaseManager.insert(context, row);
254
255         // Remember the new row number
256
this.valueId = row.getIntColumn("metadata_value_id");
257
258 // log.info(LogManager.getHeader(context, "create_metadata_value",
259
// "metadata_value_id=" + valueId));
260
}
261
262     /**
263      * Retrieves the metadata value from the database.
264      *
265      * @param context dspace context
266      * @param valueId database key id of value
267      * @return recalled metadata value
268      * @throws IOException
269      * @throws SQLException
270      * @throws AuthorizeException
271      */

272     public static MetadataValue find(Context context, int valueId)
273             throws IOException JavaDoc, SQLException JavaDoc, AuthorizeException
274     {
275         // Grab rows from DB
276
TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataValue",
277                 "SELECT * FROM MetadataValue where metadata_value_id= ? ",
278                 valueId);
279
280         TableRow row = null;
281         if (tri.hasNext())
282         {
283             row = tri.next();
284         }
285
286         // close the TableRowIterator to free up resources
287
tri.close();
288
289         if (row == null)
290         {
291             return null;
292         }
293         else
294         {
295             return new MetadataValue(row);
296         }
297     }
298
299     /**
300      * Retrieves the metadata values for a given field from the database.
301      *
302      * @param context dspace context
303      * @param fieldId field whose values to look for
304      * @return a collection of metadata values
305      * @throws IOException
306      * @throws SQLException
307      * @throws AuthorizeException
308      */

309     public static java.util.Collection JavaDoc findByField(Context context, int fieldId)
310             throws IOException JavaDoc, SQLException JavaDoc, AuthorizeException
311     {
312         // Grab rows from DB
313
TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataValue",
314                 "SELECT * FROM MetadataValue WHERE metadata_field_id= ? ",
315                 fieldId);
316
317         TableRow row = null;
318         java.util.Collection JavaDoc ret = new ArrayList JavaDoc();
319         while (tri.hasNext())
320         {
321             row = tri.next();
322             ret.add(new MetadataValue(row));
323         }
324
325         // close the TableRowIterator to free up resources
326
tri.close();
327
328         return ret;
329     }
330
331     /**
332      * Update the metadata value in the database.
333      *
334      * @param context dspace context
335      * @throws SQLException
336      * @throws AuthorizeException
337      */

338     public void update(Context context) throws SQLException JavaDoc, AuthorizeException
339     {
340         row.setColumn("item_id", itemId);
341         row.setColumn("metadata_field_id", fieldId);
342         row.setColumn("text_value", value);
343         row.setColumn("text_lang", language);
344         row.setColumn("place", place);
345         DatabaseManager.update(context, row);
346
347         log.info(LogManager.getHeader(context, "update_metadatavalue",
348                 "metadata_value_id=" + getValueId()));
349     }
350
351     /**
352      * Delete the metadata field.
353      *
354      * @param context dspace context
355      * @throws SQLException
356      * @throws AuthorizeException
357      */

358     public void delete(Context context) throws SQLException JavaDoc, AuthorizeException
359     {
360         log.info(LogManager.getHeader(context, "delete_metadata_value",
361                 " metadata_value_id=" + getValueId()));
362         DatabaseManager.delete(context, row);
363     }
364 }
365
Popular Tags