Project

General

Profile

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  
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
    	          // delete the records in xml_nodes_revisions table
62
    	          String deletingNodeRevision = generateXML_Node_RevisionsDeletingSQL();
63
    	          deletingStatement.execute(deletingNodeRevision);
64
    	          
65
    	          //close statement and connection
66
    	          deletingStatement.close();
67
    	          dbconn.close();
68
    	      }
69
    	        catch (Exception ee)
70
    	        {
71
    	          logMetacat.error("Exception in DBQuery.findDocuments: "
72
    	                                   + ee.getMessage());
73
    	          ee.printStackTrace();
74
    	        }
75
    	        finally
76
    	        {
77
    	          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
78
    	        } //finally
79
     }
80
     
81
     /*
82
      * Generate the sql command to delete the records in xml_node table.
83
      * Since it is leaf node, so we can just delete it without any other side-effect.
84
      */
85
     private String generateXML_NodeDeletingSQL()
86
     {
87
    	 String sql ="delete from xml_nodes where nodetype='ATTRIBUTE' and nodename='system' and nodedata='document' "+
88
    	                     "and parentnodeid in (select nodeid from xml_nodes where  nodetype='ELEMENT' and nodename='references') and docid in "+
89
    	                     "(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
90
    	 return sql;
91
     }
92
     
93
     /*
94
      * Generate the sql command to delete the records in xml_node table.
95
      * Since it is leaf node, so we can just delete it without any other side-effect.
96
      */
97
     private String generateXML_Node_RevisionsDeletingSQL()
98
     {
99
    	 String sql ="delete from xml_nodes_revisions where nodetype='ATTRIBUTE' and nodename='system' and nodedata='document' "+
100
    	                     "and parentnodeid in (select nodeid from xml_nodes where  nodetype='ELEMENT' and nodename='references') and docid in "+
101
    	                     "(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
102
    	 return sql;
103
     }
104
     
105
     /*
106
      * Generate the sql command to delete the records in xml_nidex table;
107
      */
108
     private String generateXML_IndexDeletingSQL()
109
     {
110
    	 String sql ="delete from xml_index where doctype ='eml://ecoinformatics.org/eml-2.0.1' AND nodeid in "+ 
111
    	 "(select nodeid from xml_index where path ='references/@system')";
112
    	 return sql;
113
     }
114
     
115
     /**
116
      *  Runs the job to correct eml201 documents - deleting extral nodes in
117
      * @param argus
118
      * @throws Exception
119
      */
120
     public static void main(String[] argus) throws Exception
121
     {
122
    	 
123
    	 //initialize options and connection pool
124
    	 Options.initialize(new File("build/metacat.properties"));
125
    	 DBConnectionPool connPool = DBConnectionPool.getInstance();
126
    	 
127
    	 
128
    	 // run the thread
129
    	 EML201DocumentCorrector correct = new EML201DocumentCorrector();
130
    	 //Thread thread = new Thread(correct);
131
    	 //thread.start();
132
    	 correct.run();
133
     }
134
}
(32-32/66)