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 4124 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
8 3816 tao
9 3814 tao
/**
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 3827 tao
public class EML201DocumentCorrector
23 3814 tao
{
24 3816 tao
	private Logger logMetacat = Logger.getLogger(EML201DocumentCorrector.class);
25
26 3814 tao
	/**
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 3816 tao
      *  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 3814 tao
      */
40 3858 tao
     public boolean run()
41 3814 tao
     {
42 3816 tao
    	 DBConnection dbconn = null;
43 3858 tao
    	 boolean success = false;
44 3816 tao
    	 int serialNumber = 0;
45
    	   try
46
    	      {
47
48
    	           //checkout the dbconnection
49 3864 tao
    	          dbconn = DBConnectionPool.getDBConnection("EML201DocumentCorrector.run");
50 3816 tao
    	          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 3827 tao
    	          // delete the records in xml_nodes_revisions table
62
    	          String deletingNodeRevision = generateXML_Node_RevisionsDeletingSQL();
63
    	          deletingStatement.execute(deletingNodeRevision);
64
65 3816 tao
    	          //close statement and connection
66
    	          deletingStatement.close();
67 3864 tao
    	          //dbconn.close();
68 3858 tao
    	          success = true;
69 3816 tao
    	      }
70
    	        catch (Exception ee)
71
    	        {
72 3864 tao
    	          logMetacat.error("EML201DocumentCorrector.run: "
73 3816 tao
    	                                   + ee.getMessage());
74
    	          ee.printStackTrace();
75
    	        }
76
    	        finally
77
    	        {
78
    	          DBConnectionPool.returnDBConnection(dbconn, serialNumber);
79
    	        } //finally
80 3858 tao
    	        return success;
81 3814 tao
     }
82
83
     /*
84 3816 tao
      * 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 3814 tao
      */
87
     private String generateXML_NodeDeletingSQL()
88
     {
89 3922 tao
    	 String sql ="delete from xml_nodes where nodetype='ATTRIBUTE' and nodename='system' "+
90 3814 tao
    	                     "and parentnodeid in (select nodeid from xml_nodes where  nodetype='ELEMENT' and nodename='references') and docid in "+
91 3816 tao
    	                     "(select docid from xml_documents where doctype ='eml://ecoinformatics.org/eml-2.0.1')";
92 3814 tao
    	 return sql;
93
     }
94
95
     /*
96 3827 tao
      * 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 3922 tao
    	 String sql ="delete from xml_nodes_revisions where nodetype='ATTRIBUTE' and nodename='system' "+
102 3917 tao
    	                     "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 3827 tao
    	 return sql;
105
     }
106
107
     /*
108 3814 tao
      * Generate the sql command to delete the records in xml_nidex table;
109
      */
110
     private String generateXML_IndexDeletingSQL()
111
     {
112 3818 tao
    	 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 3814 tao
    	 return sql;
115
     }
116 3816 tao
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 4124 daigle
    	 PropertyService.getInstance("build/metacat.properties");
127 3816 tao
    	 DBConnectionPool connPool = DBConnectionPool.getInstance();
128 4124 daigle
129 3816 tao
    	 // run the thread
130
    	 EML201DocumentCorrector correct = new EML201DocumentCorrector();
131 3827 tao
    	 //Thread thread = new Thread(correct);
132
    	 //thread.start();
133
    	 correct.run();
134 3816 tao
     }
135 3814 tao
}