Project

General

Profile

1
package edu.ucsb.nceas.metacat;
2

    
3
import java.sql.PreparedStatement;
4

    
5
import org.apache.log4j.Logger;
6

    
7
import edu.ucsb.nceas.metacat.database.DBConnection;
8
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
9
import edu.ucsb.nceas.metacat.properties.PropertyService;
10

    
11
/**
12
 * Before Metacat 1.8.1 release, Metacat uses the eml201 schema with the tag
13
 * RELEASE_EML_2_0_1_UPDATE_5. Unfortunately, this tag points at wrong version
14
 * of eml-resource.xsd. In this schema, the element "references" has an attribute named
15
 * "system" and the attribute has a default value "document". Metacat will add 
16
 * the attribute system="document" to "references" element even the orginal eml didn't have it
17
 * (this is another bug and see bug 1601), so this causes metacat generated some invalid eml201
18
 * documents. This class provides a path to fix the existed invalid eml201 documents. It will
19
 * remove the attribute system="document" of the element "references" in xml_nodes and xml_index
20
 * tables. 
21
 * @author tao
22
 *
23
 */
24
public class EML201DocumentCorrector  
25
{
26
	private Logger logMetacat = Logger.getLogger(EML201DocumentCorrector.class);
27
	
28
	/**
29
	 * Default constructor
30
	 *
31
	 */
32
     public EML201DocumentCorrector()
33
     {
34
    	 
35
     }
36
     
37
     /**
38
      *  It will remove the records - attribute system="document" of element "refrence"
39
      *  in both xml_nodes and xml_index table. Since xml_index has a foreign key (nodeid)which
40
      *  references nodeid in xml_nodes table, we should delete records in xml_index table first.
41
      */
42
     public boolean run()
43
     {
44
    	 DBConnection dbconn = null;
45
    	 boolean success = false;
46
    	 int serialNumber = 0;
47
    	   try
48
    	      {
49

    
50
    	           //checkout the dbconnection
51
    	          dbconn = DBConnectionPool.getDBConnection("EML201DocumentCorrector.run");
52
    	          serialNumber = dbconn.getCheckOutSerialNumber();
53
    	          PreparedStatement deletingStatement = null;
54
    	         
55
    	          // delete the records in xml_index table 
56
    	          String deletingIndex = generateXML_IndexDeletingSQL();
57
    	          logMetacat.debug("EML201DocumentCorrector.run - deleting the records in xml_index table with sql: " + deletingIndex);
58
    	          deletingStatement = dbconn.prepareStatement(deletingIndex);
59
    	          deletingStatement.execute();
60
    	          deletingStatement.close();
61
    	          
62
    	          // delete the records in xml_nodes table
63
    	          String deletingNode = generateXML_NodeDeletingSQL();
64
    	          logMetacat.debug("EML201DocumentCorrector.run - deleting the records in xml_nodes table with sql: " + deletingNode);
65
    	          deletingStatement = dbconn.prepareStatement(deletingNode);
66
    	          deletingStatement.execute();
67
    	          deletingStatement.close();
68
    	          
69
    	          // delete the records in xml_nodes_revisions table
70
    	          String deletingNodeRevision = generateXML_Node_RevisionsDeletingSQL();
71
    	          logMetacat.debug("EML201DocumentCorrector.run - deleting the records in xml_nodes_revisions table with sql: " + deletingNodeRevision);
72
    	          deletingStatement = dbconn.prepareStatement(deletingNodeRevision);
73
    	          deletingStatement.execute();
74
    	          deletingStatement.close();
75
    	          
76
    	          success = true;
77
    	      }
78
    	        catch (Exception ee)
79
    	        {
80
    	          logMetacat.error("EML201DocumentCorrector.run: "
81
    	                                   + ee.getMessage());
82
    	          ee.printStackTrace();
83
    	        }
84
    	        finally
85
    	        {
86
    	          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
87
    	        } //finally
88
    	        return success;
89
     }
90
     
91
     /*
92
      * Generate the sql command to delete the records in xml_node table.
93
      * Since it is leaf node, so we can just delete it without any other side-effect.
94
      */
95
     private String generateXML_NodeDeletingSQL()
96
     {
97
    	 String sql ="delete from xml_nodes where nodetype='ATTRIBUTE' and nodename='system' "+
98
    	                     "and parentnodeid in (select nodeid from xml_nodes where  nodetype='ELEMENT' and nodename='references') and docid in "+
99
    	                     "(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
100
    	 return sql;
101
     }
102
     
103
     /*
104
      * Generate the sql command to delete the records in xml_node table.
105
      * Since it is leaf node, so we can just delete it without any other side-effect.
106
      */
107
     private String generateXML_Node_RevisionsDeletingSQL()
108
     {
109
    	 String sql ="delete from xml_nodes_revisions where nodetype='ATTRIBUTE' and nodename='system' "+
110
    	                     "and parentnodeid in (select nodeid from xml_nodes_revisions where  nodetype='ELEMENT' and nodename='references') and docid in "+
111
    	                     "(select docid from xml_revisions where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
112
    	 return sql;
113
     }
114
     
115
     /*
116
      * Generate the sql command to delete the records in xml_nidex table;
117
      */
118
     private String generateXML_IndexDeletingSQL()
119
     {
120
    	 String sql ="delete from xml_index where doctype ='eml://ecoinformatics.org/eml-2.0.1' AND nodeid in "+ 
121
    	 "(select nodeid from xml_index where path ='references/@system')";
122
    	 return sql;
123
     }
124
     
125
     /**
126
      *  Runs the job to correct eml201 documents - deleting extral nodes in
127
      * @param argus
128
      * @throws Exception
129
      */
130
     public static void main(String[] args) throws Exception
131
     {
132
    	 
133
    	 //initialize options and connection pool
134
    	 PropertyService.getInstance(args[0]);
135
    	 DBConnectionPool connPool = DBConnectionPool.getInstance();
136
    	   	 
137
    	 // run the thread
138
    	 EML201DocumentCorrector correct = new EML201DocumentCorrector();
139
    	 //Thread thread = new Thread(correct);
140
    	 //thread.start();
141
    	 correct.run();
142
     }
143
}
(29-29/64)