Revision 3816
Added by Jing Tao over 16 years ago
src/edu/ucsb/nceas/metacat/EML201DocumentCorrector.java | ||
---|---|---|
1 | 1 |
package edu.ucsb.nceas.metacat; |
2 | 2 |
|
3 |
import java.io.File; |
|
4 |
import java.sql.Statement; |
|
5 |
|
|
6 |
import org.apache.log4j.Logger; |
|
7 |
|
|
8 |
import edu.ucsb.nceas.utilities.Options; |
|
9 |
|
|
3 | 10 |
/** |
4 | 11 |
* Before Metacat 1.8.1 release, Metacat uses the eml201 schema with the tag |
5 | 12 |
* RELEASE_EML_2_0_1_UPDATE_5. Unfortunately, this tag points at wrong version |
... | ... | |
15 | 22 |
*/ |
16 | 23 |
public class EML201DocumentCorrector implements Runnable |
17 | 24 |
{ |
25 |
private Logger logMetacat = Logger.getLogger(EML201DocumentCorrector.class); |
|
26 |
|
|
18 | 27 |
/** |
19 | 28 |
* Default constructor |
20 | 29 |
* |
... | ... | |
26 | 35 |
|
27 | 36 |
/** |
28 | 37 |
* It will remove the records - attribute system="document" of element "refrence" |
29 |
* in both xml_nodes and xml_index table |
|
38 |
* in both xml_nodes and xml_index table. Since xml_index has a foreign key (nodeid)which |
|
39 |
* references nodeid in xml_nodes table, we should delete records in xml_index table first. |
|
30 | 40 |
*/ |
31 | 41 |
public void run() |
32 | 42 |
{ |
33 |
|
|
43 |
DBConnection dbconn = null; |
|
44 |
int serialNumber = 0; |
|
45 |
try |
|
46 |
{ |
|
47 |
|
|
48 |
//checkout the dbconnection |
|
49 |
dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments"); |
|
50 |
serialNumber = dbconn.getCheckOutSerialNumber(); |
|
51 |
Statement deletingStatement = dbconn.createStatement(); |
|
52 |
|
|
53 |
// delete the records in xml_index table |
|
54 |
String deletingIndex = generateXML_IndexDeletingSQL(); |
|
55 |
deletingStatement.execute(deletingIndex); |
|
56 |
|
|
57 |
// delete the records in xml_nodes table |
|
58 |
String deletingNode = generateXML_NodeDeletingSQL(); |
|
59 |
deletingStatement.execute(deletingNode); |
|
60 |
|
|
61 |
//close statement and connection |
|
62 |
deletingStatement.close(); |
|
63 |
dbconn.close(); |
|
64 |
} |
|
65 |
catch (Exception ee) |
|
66 |
{ |
|
67 |
logMetacat.error("Exception in DBQuery.findDocuments: " |
|
68 |
+ ee.getMessage()); |
|
69 |
ee.printStackTrace(); |
|
70 |
} |
|
71 |
finally |
|
72 |
{ |
|
73 |
DBConnectionPool.returnDBConnection(dbconn, serialNumber); |
|
74 |
} //finally |
|
34 | 75 |
} |
35 | 76 |
|
36 | 77 |
/* |
37 |
* Generate the sql command to delete the records in xml_node table |
|
78 |
* Generate the sql command to delete the records in xml_node table. |
|
79 |
* Since it is leaf node, so we can just delete it without any other side-effect. |
|
38 | 80 |
*/ |
39 | 81 |
private String generateXML_NodeDeletingSQL() |
40 | 82 |
{ |
41 |
String sql ="delete from xml_nodes where nodetype='ATTRIBUTE' and nodename='system' and nodevalue='document' "+
|
|
83 |
String sql ="delete from xml_nodes where nodetype='ATTRIBUTE' and nodename='system' and nodedata='document' "+
|
|
42 | 84 |
"and parentnodeid in (select nodeid from xml_nodes where nodetype='ELEMENT' and nodename='references') and docid in "+ |
43 |
"(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1'"; |
|
85 |
"(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
|
|
44 | 86 |
return sql; |
45 | 87 |
} |
46 | 88 |
|
... | ... | |
50 | 92 |
private String generateXML_IndexDeletingSQL() |
51 | 93 |
{ |
52 | 94 |
String sql ="delete from xml_index where path ='references/@system' AND docid in "+ |
53 |
"(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1'"; |
|
95 |
"(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
|
|
54 | 96 |
return sql; |
55 | 97 |
} |
98 |
|
|
99 |
/** |
|
100 |
* Runs the job to correct eml201 documents - deleting extral nodes in |
|
101 |
* @param argus |
|
102 |
* @throws Exception |
|
103 |
*/ |
|
104 |
public static void main(String[] argus) throws Exception |
|
105 |
{ |
|
106 |
|
|
107 |
//initialize options and connection pool |
|
108 |
Options.initialize(new File("build/metacat.properties")); |
|
109 |
DBConnectionPool connPool = DBConnectionPool.getInstance(); |
|
110 |
|
|
111 |
|
|
112 |
// run the thread |
|
113 |
EML201DocumentCorrector correct = new EML201DocumentCorrector(); |
|
114 |
Thread thread = new Thread(correct); |
|
115 |
thread.start(); |
|
116 |
} |
|
56 | 117 |
} |
Also available in: Unified diff
Add code to execute sql command and finish the task.