Project

General

Profile

1 3814 tao
package edu.ucsb.nceas.metacat;
2
3 3816 tao
import java.sql.Statement;
4
5
import org.apache.log4j.Logger;
6
7 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
8
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
9 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
10 3816 tao
11 3814 tao
/**
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 3827 tao
public class EML201DocumentCorrector
25 3814 tao
{
26 3816 tao
	private Logger logMetacat = Logger.getLogger(EML201DocumentCorrector.class);
27
28 3814 tao
	/**
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 3816 tao
      *  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 3814 tao
      */
42 3858 tao
     public boolean run()
43 3814 tao
     {
44 3816 tao
    	 DBConnection dbconn = null;
45 3858 tao
    	 boolean success = false;
46 3816 tao
    	 int serialNumber = 0;
47
    	   try
48
    	      {
49
50
    	           //checkout the dbconnection
51 3864 tao
    	          dbconn = DBConnectionPool.getDBConnection("EML201DocumentCorrector.run");
52 3816 tao
    	          serialNumber = dbconn.getCheckOutSerialNumber();
53
    	          Statement deletingStatement = dbconn.createStatement();
54
55
    	          // delete the records in xml_index table
56
    	          String deletingIndex = generateXML_IndexDeletingSQL();
57
    	          deletingStatement.execute(deletingIndex);
58
59
    	          // delete the records in xml_nodes table
60
    	          String deletingNode = generateXML_NodeDeletingSQL();
61
    	          deletingStatement.execute(deletingNode);
62
63 3827 tao
    	          // delete the records in xml_nodes_revisions table
64
    	          String deletingNodeRevision = generateXML_Node_RevisionsDeletingSQL();
65
    	          deletingStatement.execute(deletingNodeRevision);
66
67 3816 tao
    	          //close statement and connection
68
    	          deletingStatement.close();
69 3864 tao
    	          //dbconn.close();
70 3858 tao
    	          success = true;
71 3816 tao
    	      }
72
    	        catch (Exception ee)
73
    	        {
74 3864 tao
    	          logMetacat.error("EML201DocumentCorrector.run: "
75 3816 tao
    	                                   + ee.getMessage());
76
    	          ee.printStackTrace();
77
    	        }
78
    	        finally
79
    	        {
80
    	          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
81
    	        } //finally
82 3858 tao
    	        return success;
83 3814 tao
     }
84
85
     /*
86 3816 tao
      * Generate the sql command to delete the records in xml_node table.
87
      * Since it is leaf node, so we can just delete it without any other side-effect.
88 3814 tao
      */
89
     private String generateXML_NodeDeletingSQL()
90
     {
91 3922 tao
    	 String sql ="delete from xml_nodes where nodetype='ATTRIBUTE' and nodename='system' "+
92 3814 tao
    	                     "and parentnodeid in (select nodeid from xml_nodes where  nodetype='ELEMENT' and nodename='references') and docid in "+
93 3816 tao
    	                     "(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
94 3814 tao
    	 return sql;
95
     }
96
97
     /*
98 3827 tao
      * Generate the sql command to delete the records in xml_node table.
99
      * Since it is leaf node, so we can just delete it without any other side-effect.
100
      */
101
     private String generateXML_Node_RevisionsDeletingSQL()
102
     {
103 3922 tao
    	 String sql ="delete from xml_nodes_revisions where nodetype='ATTRIBUTE' and nodename='system' "+
104 3917 tao
    	                     "and parentnodeid in (select nodeid from xml_nodes_revisions where  nodetype='ELEMENT' and nodename='references') and docid in "+
105
    	                     "(select docid from xml_revisions where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
106 3827 tao
    	 return sql;
107
     }
108
109
     /*
110 3814 tao
      * Generate the sql command to delete the records in xml_nidex table;
111
      */
112
     private String generateXML_IndexDeletingSQL()
113
     {
114 3818 tao
    	 String sql ="delete from xml_index where doctype ='eml://ecoinformatics.org/eml-2.0.1' AND nodeid in "+
115
    	 "(select nodeid from xml_index where path ='references/@system')";
116 3814 tao
    	 return sql;
117
     }
118 3816 tao
119
     /**
120
      *  Runs the job to correct eml201 documents - deleting extral nodes in
121
      * @param argus
122
      * @throws Exception
123
      */
124 5030 daigle
     public static void main(String[] args) throws Exception
125 3816 tao
     {
126
127
    	 //initialize options and connection pool
128 5030 daigle
    	 PropertyService.getInstance(args[0]);
129 3816 tao
    	 DBConnectionPool connPool = DBConnectionPool.getInstance();
130 4124 daigle
131 3816 tao
    	 // run the thread
132
    	 EML201DocumentCorrector correct = new EML201DocumentCorrector();
133 3827 tao
    	 //Thread thread = new Thread(correct);
134
    	 //thread.start();
135
    	 correct.run();
136 3816 tao
     }
137 3814 tao
}