source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/doctypes/DocumentSQLWriter.java@ 9858

Last change on this file since 9858 was 9858, checked in by kjdon, 19 years ago

OK, changed my mind about making SQLConnection kill off the previous statement.
To make it more transparent what is happening, you now have to create a Statement (connection.createStatement()), then use the Statement to execute the query. This means that the thing doing the query owns the Statement, and can kill it off when finished with it, and nothing else can kill it off unexpectedly. The previous way this was all implemented meant that there was a large memory leak, and some functionality actually relied on this. A newer version of the mysql connector/J has fixed the bug where the statement wasn't closed on garbage collection, but it still seems better to close it explicitly.
Hopefully I have got it all back to working as well as it was bfore, and haven't introduced any bugs :-)

  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1package org.greenstone.gsdl3.gs3build.doctypes;
2
3import java.io.PrintWriter;
4
5import java.sql.*;
6
7import org.greenstone.gsdl3.gs3build.util.XMLTools;
8import org.greenstone.gsdl3.gs3build.database.*;
9
10public class DocumentSQLWriter
11{
12 public DocumentSQLWriter()
13 {
14 }
15
16 public static boolean touchDocument(DocumentID docID, GS3SQLConnection connection, long touchTime, long modTime)
17 { GS3SQLUpdate update = new GS3SQLUpdate("document");
18 update.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("DocID", "=", docID.toString())));
19 update.addDate("IndexedDate", new java.sql.Timestamp(touchTime));
20 update.addDate("ModifiedDate", new java.sql.Timestamp(modTime));
21 try {
22 Statement statement = connection.createStatement();
23 statement.execute(update.toString());
24 statement.close();
25 } catch (SQLException e) {
26 System.err.println("DocumentSQLWriter.touchDocument() Error: "+ e);
27 return false;
28 }
29 System.out.println(update.toString());
30 return true;
31 }
32
33 /**
34 * Just write the document to the database, assuming that it is not already there
35 */
36 public boolean writeDocument(DocumentInterface document, GS3SQLConnection connection)
37 {
38 // TODO: output document in METS format, with Greenstone-specific metadata
39 // placed in a wrapper inside the descriptive metadata block
40
41 // put the document into the database
42 try {
43 if (document.getID() != null)
44 { //tag = XMLTools.addAttribute(tag, "OBJID", document.getID().toString());
45 GS3SQLSelect select = new GS3SQLSelect("document");
46 select.addField("*");
47 select.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("DocID", "=", document.getID().toString())));
48
49 Statement statement = connection.createStatement();
50 ResultSet results = statement.executeQuery(select.toString());
51 if (!results.first()) { // empty result
52 GS3SQLInsert insert = new GS3SQLInsert("document");
53 insert.addValue("DocID", document.getID().toString());
54 insert.addValue("DocType", document.getDocumentType());
55
56 insert.addDate("AccessionDate", new java.sql.Timestamp(document.getAccessionDate()));
57 insert.addDate("IndexedDate", new java.sql.Timestamp(document.getLastIndexedDate()));
58 insert.addDate("ModifiedDate", new java.sql.Timestamp(document.getModifiedDatestamp()));
59
60 statement.execute(insert.toString());
61 }
62 else {
63 /* redundant code - not used... */
64 // GS3SQLUpdate update = new GS3SQLUpdate("document");
65 // update.setWhere(new GS3SQLWhere(new GS3SQLWhereItem("DocID", "=", document.getID().toString())));
66 // connection.execute(update.toString());
67 }
68 statement.close();
69 }
70 } catch (SQLException ex) {
71 System.out.println("DocumentSQLWriter.writeDocument() Error:"+ex);
72 }
73 // output.println(tag);
74
75 // Write the structural metadata:
76 // if in "Create METS" mode:
77 // take all the documents as being in the one group, of the "physical"
78 // document; particular document plugins may extend this behaviour,
79 // which would suggest placing it in the AbstractDocument or similar
80 // class?!
81 if (document.getDocumentMetadata() == null) {
82 System.out.println("Null metadata document");
83 }
84 document.getDocumentMetadata().writeSQL(document, connection);
85
86 document.getDocumentFiles().writeSQL(document, connection);
87
88 document.getDocumentStructure().writeSQL(document, connection);
89
90 // Write the body information
91 // TODO: only output the document if there is some
92 // content/formatting change between the original and
93 // what greenstone actually does; if the original is
94 // good, then place a marker saying "get the document
95 // "from the original file"
96 return true;
97 }
98}
Note: See TracBrowser for help on using the repository browser.