Project

General

Profile

1
package edu.ucsb.nceas.metacat;
2

    
3
import java.sql.Statement;
4

    
5
import org.apache.log4j.Logger;
6

    
7
import edu.ucsb.nceas.metacat.service.PropertyService;
8

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

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