1
|
package edu.ucsb.nceas.metacat;
|
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
|
|
10
|
/**
|
11
|
* Before Metacat 1.8.1 release, Metacat uses the eml201 schema with the tag
|
12
|
* RELEASE_EML_2_0_1_UPDATE_5. Unfortunately, this tag points at wrong version
|
13
|
* of eml-resource.xsd. In this schema, the element "references" has an attribute named
|
14
|
* "system" and the attribute has a default value "document". Metacat will add
|
15
|
* the attribute system="document" to "references" element even the orginal eml didn't have it
|
16
|
* (this is another bug and see bug 1601), so this causes metacat generated some invalid eml201
|
17
|
* documents. This class provides a path to fix the existed invalid eml201 documents. It will
|
18
|
* remove the attribute system="document" of the element "references" in xml_nodes and xml_index
|
19
|
* tables.
|
20
|
* @author tao
|
21
|
*
|
22
|
*/
|
23
|
public class EML201DocumentCorrector implements Runnable
|
24
|
{
|
25
|
private Logger logMetacat = Logger.getLogger(EML201DocumentCorrector.class);
|
26
|
|
27
|
/**
|
28
|
* Default constructor
|
29
|
*
|
30
|
*/
|
31
|
public EML201DocumentCorrector()
|
32
|
{
|
33
|
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* It will remove the records - attribute system="document" of element "refrence"
|
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.
|
40
|
*/
|
41
|
public void run()
|
42
|
{
|
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
|
75
|
}
|
76
|
|
77
|
/*
|
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.
|
80
|
*/
|
81
|
private String generateXML_NodeDeletingSQL()
|
82
|
{
|
83
|
String sql ="delete from xml_nodes where nodetype='ATTRIBUTE' and nodename='system' and nodedata='document' "+
|
84
|
"and parentnodeid in (select nodeid from xml_nodes where nodetype='ELEMENT' and nodename='references') and docid in "+
|
85
|
"(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
|
86
|
return sql;
|
87
|
}
|
88
|
|
89
|
/*
|
90
|
* Generate the sql command to delete the records in xml_nidex table;
|
91
|
*/
|
92
|
private String generateXML_IndexDeletingSQL()
|
93
|
{
|
94
|
String sql ="delete from xml_index where path ='references/@system' AND docid in "+
|
95
|
"(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
|
96
|
return sql;
|
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
|
}
|
117
|
}
|