1 | package org.greenstone.gsdl3.gs3build.doctypes;
|
---|
2 |
|
---|
3 | import java.io.PrintWriter;
|
---|
4 |
|
---|
5 | import java.sql.*;
|
---|
6 |
|
---|
7 | import org.greenstone.gsdl3.gs3build.util.XMLTools;
|
---|
8 | import org.greenstone.gsdl3.gs3build.database.*;
|
---|
9 |
|
---|
10 | public 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 | }
|
---|