KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > administer > Upgrade101To11


1 /*
2  * Upgrade101To11.java
3  *
4  * Version: $Revision: 1.5 $
5  *
6  * Date: $Date: 2006/05/26 14:12:12 $
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.administer;
41
42 import org.dspace.content.DCDate;
43 import org.dspace.core.Context;
44 import org.dspace.storage.rdbms.DatabaseManager;
45 import org.dspace.storage.rdbms.TableRow;
46 import org.dspace.storage.rdbms.TableRowIterator;
47
48 /**
49  * A command-line tool for performing necessary tweaks in the database for the
50  * new last_modified column in the item table.
51  *
52  * @author Robert Tansley
53  * @version $Revision: 1.5 $
54  */

55 public class Upgrade101To11
56 {
57     /**
58      * For invoking via the command line
59      *
60      * @param argv
61      * command-line arguments
62      */

63     public static void main(String JavaDoc[] argv)
64     {
65         Context context = null;
66
67         try
68         {
69             context = new Context();
70
71             // Deal with withdrawn items first.
72
// last_modified takes the value of the deletion date
73
TableRowIterator tri = DatabaseManager.queryTable(context, "item",
74                     "SELECT * FROM item WHERE withdrawal_date IS NOT NULL");
75
76             while (tri.hasNext())
77             {
78                 TableRow row = tri.next();
79                 DCDate d = new DCDate(row.getStringColumn("withdrawal_date"));
80                 row.setColumn("last_modified", d.toDate());
81                 DatabaseManager.update(context, row);
82             }
83             tri.close();
84
85             // Next, update those items with a date.available
86
tri = DatabaseManager.query(context,
87                         "SELECT item.item_id, dcvalue.text_value FROM item, dctyperegistry, "+
88                         "dcvalue WHERE item.item_id=dcvalue.item_id AND dcvalue.dc_type_id="+
89                         "dctyperegistry.dc_type_id AND dctyperegistry.element LIKE 'date' "+
90                         "AND dctyperegistry.qualifier LIKE 'available'");
91
92             while (tri.hasNext())
93             {
94                 TableRow resultRow = tri.next();
95                 DCDate d = new DCDate(resultRow.getStringColumn("text_value"));
96
97                 // Can't update the row, have to do a separate query
98
TableRow itemRow = DatabaseManager.find(context, "item",
99                         resultRow.getIntColumn("item_id"));
100                 itemRow.setColumn("last_modified", d.toDate());
101                 DatabaseManager.update(context, itemRow);
102             }
103             tri.close();
104
105             // Finally, for all items that have no date.available or withdrawal
106
// date, set the update time to now!
107
DatabaseManager.updateQuery(context,
108                         "UPDATE item SET last_modified=now() WHERE last_modified IS NULL");
109
110             context.complete();
111
112             System.out.println("Last modified dates set");
113
114             System.exit(0);
115         }
116         catch (Exception JavaDoc e)
117         {
118             System.err.println("Exception occurred:" + e);
119             e.printStackTrace();
120
121             if (context != null)
122             {
123                 context.abort();
124             }
125
126             System.exit(1);
127         }
128     }
129 }
130
Popular Tags