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