Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents an XML document
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones
7
 *    Release: @release@
8
 *
9
 *   '$Author: jones $'
10
 *     '$Date: 2004-09-16 16:40:02 -0700 (Thu, 16 Sep 2004) $'
11
 * '$Revision: 2297 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.io.BufferedInputStream;
31
import java.io.BufferedOutputStream;
32
import java.io.File;
33
import java.io.FileOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.PrintWriter;
37
import java.io.Reader;
38
import java.io.StringWriter;
39
import java.io.Writer;
40
import java.net.URL;
41
import java.sql.PreparedStatement;
42
import java.sql.ResultSet;
43
import java.sql.SQLException;
44
import java.sql.Statement;
45
import java.util.Enumeration;
46
import java.util.Hashtable;
47
import java.util.HashMap;
48
import java.util.Iterator;
49
import java.util.Stack;
50
import java.util.TreeSet;
51
import java.util.Vector;
52

    
53
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
54

    
55
import org.xml.sax.ContentHandler;
56
import org.xml.sax.DTDHandler;
57
import org.xml.sax.EntityResolver;
58
import org.xml.sax.ErrorHandler;
59
import org.xml.sax.InputSource;
60
import org.xml.sax.SAXException;
61
import org.xml.sax.XMLReader;
62
import org.xml.sax.helpers.XMLReaderFactory;
63

    
64
/**
65
 * A class that represents an XML document. It can be created with a simple
66
 * document identifier from a database connection. It also will write an XML
67
 * text document to a database connection using SAX.
68
 */
69
public class DocumentImpl
70
{
71
    /* Constants */
72
    public static final String SCHEMA = "Schema";
73
    public static final String DTD = "DTD";
74
    public static final String EML200 = "eml200";
75
    public static final String EML210 = "eml210";
76
    public static final String EXTERNALSCHEMALOCATIONPROPERTY = "http://apache.org/xml/properties/schema/external-schemaLocation";
77

    
78
    /*
79
     * public static final String EXTERNALSCHEMALOCATION =
80
     * "eml://ecoinformatics.org/eml-2.0.0
81
     * http://dev.nceas.ucsb.edu/tao/schema/eml.xsd"+ "
82
     * http://www.xml-cml.org/schema/stmml
83
     * http://dev.nceas.ucsb.edu/tao/schema/stmml.xsd";
84
     */
85
    public static final String DECLARATIONHANDLERPROPERTY = "http://xml.org/sax/properties/declaration-handler";
86
    public static final String LEXICALPROPERTY = "http://xml.org/sax/properties/lexical-handler";
87
    public static final String VALIDATIONFEATURE = "http://xml.org/sax/features/validation";
88
    public static final String SCHEMAVALIDATIONFEATURE = "http://apache.org/xml/features/validation/schema";
89
    public static final String NAMESPACEFEATURE = "http://xml.org/sax/features/namespaces";
90
    public static final String NAMESPACEPREFIXESFEATURE = "http://xml.org/sax/features/namespace-prefixes";
91
    public static final String EML2_1_0NAMESPACE = MetaCatUtil
92
            .getOption("eml2_1_0namespace");
93
    // "eml://ecoinformatics.org/eml-2.1.0";
94
    public static final String EML2_0_1NAMESPACE = MetaCatUtil
95
            .getOption("eml2_0_1namespace");
96
    // "eml://ecoinformatics.org/eml-2.0.1";
97
    public static final String EML2_0_0NAMESPACE = MetaCatUtil
98
            .getOption("eml2_0_0namespace");
99
    // "eml://ecoinformatics.org/eml-2.0.0";
100
    public static final String DOCNAME = "docname";
101
    public static final String PUBLICID = "publicid";
102
    public static final String SYSTEMID = "systemid";
103
    static final int ALL = 1;
104
    static final int WRITE = 2;
105
    static final int READ = 4;
106
    private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
107
    private DBConnection connection = null;
108
    private String docid = null;
109
    private String updatedVersion = null;
110
    private String docname = null;
111
    private String doctype = null;
112
    private String validateType = null; //base on dtd or schema
113
    private String createdate = null;
114
    private String updatedate = null;
115
    private String system_id = null;
116
    private String userowner = null;
117
    private String userupdated = null;
118
    private int rev;
119
    private int serverlocation;
120
    private String docHomeServer;
121
    private String publicaccess;
122
    private long rootnodeid;
123
    private ElementNode rootNode = null;
124
    private TreeSet nodeRecordList = null;
125

    
126
    /**
127
     * Constructor used to create a document and read the document information
128
     * from the database. If readNodes is false, then the node data is not read
129
     * at this time, but is deferred until it is needed (such as when a call to
130
     * toXml() is made).
131
     *
132
     * @param conn
133
     *            the database connection from which to read the document
134
     * @param docid
135
     *            the identifier of the document to be created
136
     * @param readNodes
137
     *            flag indicating whether the xmlnodes should be read
138
     */
139
    public DocumentImpl(String docid, boolean readNodes) throws McdbException
140
    {
141
        try {
142
            //this.conn = conn;
143
            this.docid = docid;
144

    
145
            // Look up the document information
146
            getDocumentInfo(docid);
147

    
148
            if (readNodes) {
149
                // Download all of the document nodes using a single SQL query
150
                // The sort order of the records is determined by the
151
                // NodeComparator
152
                // class, and needs to represent a depth-first traversal for the
153
                // toXml() method to work properly
154
                nodeRecordList = getNodeRecordList(rootnodeid);
155
            }
156

    
157
        } catch (McdbException ex) {
158
            throw ex;
159
        } catch (Throwable t) {
160
            throw new McdbException("Error reading document: " + docid);
161
        }
162
    }
163

    
164
    /**
165
     * Constructor, creates document from database connection, used for reading
166
     * the document
167
     *
168
     * @param conn
169
     *            the database connection from which to read the document
170
     * @param docid
171
     *            the identifier of the document to be created
172
     */
173
    public DocumentImpl(String docid) throws McdbException
174
    {
175
        this(docid, true);
176
    }
177

    
178
    /**
179
     * Construct a new document instance, writing the contents to the database.
180
     * This method is called from DBSAXHandler because we need to know the root
181
     * element name for documents without a DOCTYPE before creating it.
182
     *
183
     * In this constructor, the docid is without rev. There is a string rev to
184
     * specify the revision user want to upadate. The revion is only need to be
185
     * greater than current one. It is not need to be sequent number just after
186
     * current one. So it is only used in update action
187
     *
188
     * @param conn
189
     *            the JDBC Connection to which all information is written
190
     * @param rootnodeid -
191
     *            sequence id of the root node in the document
192
     * @param docname -
193
     *            the name of DTD, i.e. the name immediately following the
194
     *            DOCTYPE keyword ( should be the root element name ) or the
195
     *            root element name if no DOCTYPE declaration provided (Oracle's
196
     *            and IBM parsers are not aware if it is not the root element
197
     *            name)
198
     * @param doctype -
199
     *            Public ID of the DTD, i.e. the name immediately following the
200
     *            PUBLIC keyword in DOCTYPE declaration or the docname if no
201
     *            Public ID provided or null if no DOCTYPE declaration provided
202
     * @param docid
203
     *            the docid to use for the UPDATE, no version number
204
     * @param version,
205
     *            need to be update
206
     * @param action
207
     *            the action to be performed (INSERT OR UPDATE)
208
     * @param user
209
     *            the user that owns the document
210
     * @param pub
211
     *            flag for public "read" access on document
212
     * @param serverCode
213
     *            the serverid from xml_replication on which this document
214
     *            resides.
215
     *
216
     */
217
    public DocumentImpl(DBConnection conn, long rootNodeId, String docName,
218
            String docType, String docId, String newRevision, String action,
219
            String user, String pub, String catalogId, int serverCode)
220
            throws SQLException, Exception
221
    {
222
        this.connection = conn;
223
        this.rootnodeid = rootNodeId;
224
        this.docname = docName;
225
        this.doctype = docType;
226
        this.docid = docId;
227
        this.updatedVersion = newRevision;
228
        writeDocumentToDB(action, user, pub, catalogId, serverCode);
229
    }
230

    
231
    /**
232
     * This method will be call in handleUploadRequest in MetacatServlet class
233
     */
234
    public static void registerDocument(String docname, String doctype,
235
            String accnum, String user, String[] groupnames) throws SQLException,
236
            AccessionNumberException, Exception
237
    {
238
        try {
239
            // get server location for this doc
240
            int serverLocation = getServerLocationNumber(accnum);
241
            registerDocument(docname, doctype, accnum, user, groupnames,
242
                             serverLocation);
243
        } catch (Exception e) {
244
            throw e;
245
        }
246
    }
247

    
248
    /**
249
     * Register a document that resides on the filesystem with the database.
250
     * (ie, just an entry in xml_documents, nothing in xml_nodes). Creates a
251
     * reference to a filesystem document (used for non-xml data files). This
252
     * class only be called in MetaCatServerlet.
253
     *
254
     * @param conn
255
     *            the JDBC Connection to which all information is written
256
     * @param docname -
257
     *            the name of DTD, i.e. the name immediately following the
258
     *            DOCTYPE keyword ( should be the root element name ) or the
259
     *            root element name if no DOCTYPE declaration provided (Oracle's
260
     *            and IBM parsers are not aware if it is not the root element
261
     *            name)
262
     * @param doctype -
263
     *            Public ID of the DTD, i.e. the name immediately following the
264
     *            PUBLIC keyword in DOCTYPE declaration or the docname if no
265
     *            Public ID provided or null if no DOCTYPE declaration provided
266
     * @param accnum
267
     *            the accession number to use for the INSERT OR UPDATE, which
268
     *            includes a revision number for this revision of the document
269
     *            (e.g., knb.1.1)
270
     * @param user
271
     *            the user that owns the document
272
     * @param groupnames
273
     *            the groups that owns the document
274
     * @param serverCode
275
     *            the serverid from xml_replication on which this document
276
     *            resides.
277
     */
278
    public static void registerDocument(String docname, String doctype,
279
            String accnum, String user, String[] groups, int serverCode)
280
            throws SQLException, AccessionNumberException, Exception
281
    {
282

    
283
        DBConnection dbconn = null;
284
        int serialNumber = -1;
285
        PreparedStatement pstmt = null;
286
        //MetaCatUtil util = new MetaCatUtil();
287
        AccessionNumber ac;
288
        String action = null;
289
        try {
290
            //dbconn = util.openDBConnection();
291
            //check out DBConnection
292
            dbconn = DBConnectionPool
293
                    .getDBConnection("DocumentImpl.registerDocument");
294
            serialNumber = dbconn.getCheckOutSerialNumber();
295
            String docIdWithoutRev = MetaCatUtil.getDocIdFromString(accnum);
296
            int userSpecifyRev = MetaCatUtil.getVersionFromString(accnum);
297
            int revInDataBase = getLatestRevisionNumber(docIdWithoutRev);
298
            //revIndataBase=-1, there is no record in xml_documents table
299
            //the data file is a new one, inert it into table
300
            //user specified rev should be great than 0
301
            if (revInDataBase == -1 && userSpecifyRev > 0) {
302
                ac = new AccessionNumber(accnum, "insert");
303
                action = "insert";
304
            }
305
            //rev is greater the last revsion number and revInDataBase isn't -1
306
            // it is a updated data file
307
            else if (userSpecifyRev > revInDataBase && revInDataBase > 0) {
308

    
309
                if (!hasWritePermission(user, groups, accnum)) { throw new Exception(
310
                   "User " + user
311
                   + " does not have permission to update the document"
312
                   + accnum); }
313

    
314
                //archive the old entry
315
                archiveDocRevision(docIdWithoutRev, user);
316
                //delete the old entry in xml_documents
317
                //deleteXMLDocuments(docIdWithoutRev);
318
                ac = new AccessionNumber(accnum, "update");
319
                action = "update";
320
            }
321
            //other situation
322
            else {
323

    
324
                throw new Exception("Revision number couldn't be "
325
                        + userSpecifyRev);
326
            }
327
            String docid = ac.getDocid();
328
            String rev = ac.getRev();
329
            /*
330
             * SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy
331
             * HH:mm:ss"); Date localtime = new Date(); String dateString =
332
             * formatter.format(localtime); String
333
             * sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY
334
             * HH24:MI:SS");
335
             */
336
            String sqlDateString = dbAdapter.getDateTimeFunction();
337

    
338
            StringBuffer sql = new StringBuffer();
339
            if (action != null && action.equals("insert")) {
340
                sql.append("insert into xml_documents (docid, docname, " +
341
                           "doctype, ");
342
                sql.append("user_owner, user_updated, server_location, " +
343
                           "rev,date_created");
344
                sql.append(", date_updated, public_access) values ('");
345
                sql.append(docid).append("','");
346
                sql.append(docname).append("','");
347
                sql.append(doctype).append("','");
348
                sql.append(user).append("','");
349
                sql.append(user).append("','");
350
                sql.append(serverCode).append("','");
351
                sql.append(rev).append("',");
352
                sql.append(sqlDateString).append(",");
353
                sql.append(sqlDateString).append(",");
354
                sql.append("'0')");
355
            } else if (action != null && action.equals("update")) {
356
                sql.append("update xml_documents set docname ='");
357
                sql.append(docname).append("', ");
358
                sql.append("user_updated='");
359
                sql.append(user).append("', ");
360
                sql.append("server_location='");
361
                sql.append(serverCode).append("',");
362
                sql.append("rev='");
363
                sql.append(rev).append("',");
364
                sql.append("date_updated=");
365
                sql.append(sqlDateString);
366
                sql.append(" where docid='");
367
                sql.append(docid).append("'");
368
            }
369
            pstmt = dbconn.prepareStatement(sql.toString());
370
            pstmt.execute();
371
            pstmt.close();
372
            //dbconn.close();
373
        } finally {
374
            try {
375
                if(pstmt != null){
376
                    pstmt.close();
377
                }
378
            } finally {
379
                DBConnectionPool.returnDBConnection(dbconn, serialNumber);
380
            }
381
        }
382
    }
383

    
384
    /**
385
     * Register a document that resides on the filesystem with the database.
386
     * (ie, just an entry in xml_documents, nothing in xml_nodes). Creates a
387
     * reference to a filesystem document (used for non-xml data files) This
388
     * method will be called for register data file in xml_documents in
389
     * Replication. This method is revised from registerDocument.
390
     *
391
     * @param conn
392
     *            the JDBC Connection to which all information is written
393
     * @param docname -
394
     *            the name of DTD, i.e. the name immediately following the
395
     *            DOCTYPE keyword ( should be the root element name ) or the
396
     *            root element name if no DOCTYPE declaration provided (Oracle's
397
     *            and IBM parsers are not aware if it is not the root element
398
     *            name)
399
     * @param doctype -
400
     *            Public ID of the DTD, i.e. the name immediately following the
401
     *            PUBLIC keyword in DOCTYPE declaration or the docname if no
402
     *            Public ID provided or null if no DOCTYPE declaration provided
403
     * @param accnum
404
     *            the accession number to use for the INSERT OR UPDATE, which
405
     *            includes a revision number for this revision of the document
406
     *            (e.g., knb.1.1)
407
     * @param user
408
     *            the user that owns the document
409
     * @param serverCode
410
     *            the serverid from xml_replication on which this document
411
     *            resides.
412
     */
413
    public static void registerDocumentInReplication(String docname,
414
            String doctype, String accnum, String user, int serverCode)
415
            throws SQLException, AccessionNumberException, Exception
416
    {
417
        DBConnection dbconn = null;
418
        int serialNumber = -1;
419
        //MetaCatUtil util = new MetaCatUtil();
420
        AccessionNumber ac;
421
        PreparedStatement pstmt = null;
422
        String action = null;
423
        try {
424
            //dbconn = util.openDBConnection();
425
            dbconn = DBConnectionPool.getDBConnection(
426
                    "DocumentImpl.registerDocumentInReplication");
427
            serialNumber = dbconn.getCheckOutSerialNumber();
428
            String docIdWithoutRev = MetaCatUtil
429
                    .getDocIdFromAccessionNumber(accnum);
430
            int userSpecifyRev = MetaCatUtil
431
                    .getRevisionFromAccessionNumber(accnum);
432
            int revInDataBase = getLatestRevisionNumber(docIdWithoutRev);
433
            //revIndataBase=-1, there is no record in xml_documents table
434
            //the data file is a new one, inert it into table
435
            //user specified rev should be great than 0
436
            if (revInDataBase == -1 && userSpecifyRev >= 0) {
437
                ac = new AccessionNumber(accnum, "insert");
438
                action = "insert";
439
            }
440
            //rev is greater the last revsion number and revInDataBase isn't -1
441
            // it is a updated data file
442
            else if (userSpecifyRev > revInDataBase && revInDataBase >= 0) {
443

    
444
                //archive the old entry
445
                archiveDocRevision(docIdWithoutRev, user);
446
                //delete the old entry in xml_documents
447
                //deleteXMLDocuments(docIdWithoutRev);
448
                ac = new AccessionNumber(accnum, "update");
449
                action = "update";
450
            }
451
            // local server has newer version, then notify the remote server
452
            else if (userSpecifyRev < revInDataBase && revInDataBase > 0) {
453
                throw new Exception("Local server: "
454
                        + MetaCatUtil.getOption("server")
455
                        + " has newer revision of doc: " + docIdWithoutRev
456
                        + "." + revInDataBase + ". Please notify it.");
457
            }
458
            //other situation
459
            else {
460

    
461
                throw new Exception("Revision number couldn't be "
462
                        + userSpecifyRev);
463
            }
464
            String docid = ac.getDocid();
465
            String rev = ac.getRev();
466
            /*
467
             * SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy
468
             * HH:mm:ss"); Date localtime = new Date(); String dateString =
469
             * formatter.format(localtime); String
470
             * sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY
471
             * HH24:MI:SS");
472
             */
473
            String sqlDateString = dbAdapter.getDateTimeFunction();
474

    
475
            StringBuffer sql = new StringBuffer();
476
            if (action != null && action.equals("insert")) {
477
                sql.append("insert into xml_documents (docid, docname, " +
478
                           "doctype, ");
479
                sql.append("user_owner, user_updated, server_location, " +
480
                           "rev,date_created");
481
                sql.append(", date_updated, public_access) values ('");
482
                sql.append(docid).append("','");
483
                sql.append(docname).append("','");
484
                sql.append(doctype).append("','");
485
                sql.append(user).append("','");
486
                sql.append(user).append("','");
487
                sql.append(serverCode).append("','");
488
                sql.append(rev).append("',");
489
                sql.append(sqlDateString).append(",");
490
                sql.append(sqlDateString).append(",");
491
                sql.append("'0')");
492
            } else if (action != null && action.equals("update")) {
493
                sql.append("update xml_documents set docname ='");
494
                sql.append(docname).append("', ");
495
                sql.append("user_updated='");
496
                sql.append(user).append("', ");
497
                sql.append("server_location='");
498
                sql.append(serverCode).append("',");
499
                sql.append("rev='");
500
                sql.append(rev).append("',");
501
                sql.append("date_updated=");
502
                sql.append(sqlDateString);
503
                sql.append(" where docid='");
504
                sql.append(docid).append("'");
505
            }
506
            // Set auto commit fasle
507
            dbconn.setAutoCommit(false);
508
            pstmt = dbconn.prepareStatement(sql.toString());
509

    
510
            pstmt.execute();
511
            // Commit the insert
512
            dbconn.commit();
513
            pstmt.close();
514
            //dbconn.close();
515
        } finally {
516
            // Set DBConnection auto commit true
517
            dbconn.setAutoCommit(true);
518
            pstmt.close();
519
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
520
        }
521
    }
522

    
523
    /**
524
     * This method will register a data file entry in xml_documents and save a
525
     * data file input Stream into file system.. It is only used in replication
526
     *
527
     * @param input,
528
     *            the input stream which contain the file content.
529
     * @param ,
530
     *            the input stream which contain the file content
531
     * @param docname -
532
     *            the name of DTD, for data file, it is a docid number.
533
     * @param doctype -
534
     *            "BIN" for data file
535
     * @param accnum
536
     *            the accession number to use for the INSERT OR UPDATE, which
537
     *            includes a revision number for this revision of the document
538
     *            (e.g., knb.1.1)
539
     * @param user
540
     *            the user that owns the document
541
     * @param docHomeServer,
542
     *            the home server of the docid
543
     * @param notificationServer,
544
     *            the server to notify force replication info to local metacat
545
     */
546
    public static void writeDataFileInReplication(InputStream input,
547
            String filePath, String docname, String doctype, String accnum,
548
            String user, String docHomeServer, String notificationServer)
549
            throws SQLException, AccessionNumberException, Exception
550
    {
551
        int serverCode = -2;
552

    
553
        if (filePath == null || filePath.equals("")) { throw new Exception(
554
                "Please specify the directory where file will be store"); }
555
        if (accnum == null || accnum.equals("")) { throw new Exception(
556
                "Please specify the stored file name"); }
557

    
558
        // If server is not int the xml replication talbe, insert it into
559
        // xml_replication table
560
        //serverList.addToServerListIfItIsNot(docHomeServer);
561
        insertServerIntoReplicationTable(docHomeServer);
562

    
563
        // Get server code again
564
        serverCode = getServerCode(docHomeServer);
565

    
566
        //register data file into xml_documents table
567
        registerDocumentInReplication(docname, doctype, accnum, user,
568
                serverCode);
569
        //write inputstream into file system.
570
        File dataDirectory = new File(filePath);
571
        File newFile = new File(dataDirectory, accnum);
572

    
573
        // create a buffered byte output stream
574
        // that uses a default-sized output buffer
575
        FileOutputStream fos = new FileOutputStream(newFile);
576
        BufferedOutputStream outPut = new BufferedOutputStream(fos);
577

    
578
        BufferedInputStream bis = null;
579
        bis = new BufferedInputStream(input);
580
        byte[] buf = new byte[4 * 1024]; // 4K buffer
581
        int b = bis.read(buf);
582

    
583
        while (b != -1) {
584
            outPut.write(buf, 0, b);
585
            b = bis.read(buf);
586
        }
587
        bis.close();
588
        outPut.close();
589
        fos.close();
590

    
591
        // Force replicate data file
592
        ForceReplicationHandler forceReplication = new ForceReplicationHandler(
593
                accnum, false, notificationServer);
594
    }
595

    
596
    /**
597
     * Get a lock for a given document.
598
     */
599
    public static boolean getDataFileLockGrant(String accnum) throws Exception
600
    {
601
        try {
602
            int serverLocation = getServerLocationNumber(accnum);
603
            return getDataFileLockGrant(accnum, serverLocation);
604
        } catch (Exception e) {
605
            throw e;
606
        }
607
    }
608

    
609
    /**
610
     * The method will check if metacat can get data file lock grant If server
611
     * code is 1, it get. If server code is not 1 but call replication getlock
612
     * successfully, it get else, it didn't get
613
     *
614
     * @param accnum,
615
     *            the ID of the document
616
     * @param action,
617
     *            the action to the document
618
     * @param serverCode,
619
     *            the server location code
620
     */
621
    public static boolean getDataFileLockGrant(String accnum, int serverCode)
622
            throws Exception
623
    {
624
        boolean flag = true;
625
        //MetaCatUtil util = new MetaCatUtil();
626
        String docid = MetaCatUtil.getDocIdFromString(accnum);
627
        int rev = MetaCatUtil.getVersionFromString(accnum);
628

    
629
        if (serverCode == 1) {
630
            flag = true;
631
            return flag;
632
        }
633

    
634
        //if((serverCode != 1 && action.equals("UPDATE")) )
635
        if (serverCode != 1) { //if this document being written is not a
636
                               // resident of this server then
637
            //we need to try to get a lock from it's resident server. If the
638
            //resident server will not give a lock then we send the user a
639
            // message
640
            //saying that he/she needs to download a new copy of the file and
641
            //merge the differences manually.
642

    
643
            String server = MetacatReplication
644
                    .getServerNameForServerCode(serverCode);
645
            MetacatReplication.replLog("attempting to lock " + accnum);
646
            URL u = new URL("https://" + server + "?server="
647
                    + MetaCatUtil.getLocalReplicationServerName()
648
                    + "&action=getlock&updaterev=" + rev + "&docid=" + docid);
649
            //System.out.println("sending message: " + u.toString());
650
            String serverResStr = MetacatReplication.getURLContent(u);
651
            String openingtag = serverResStr.substring(0, serverResStr
652
                    .indexOf(">") + 1);
653
            if (openingtag.equals("<lockgranted>")) {
654
                //the lock was granted go ahead with the insert
655
                //System.out.println("In lockgranted");
656
                MetacatReplication.replLog("lock granted for " + accnum
657
                        + " from " + server);
658
                flag = true;
659
                return flag;
660
            }//if
661

    
662
            else if (openingtag.equals("<filelocked>")) {//the file is
663
                                                         // currently locked by
664
                                                         // another user
665
                //notify our user to wait a few minutes, check out a new copy
666
                // and try
667
                //again.
668
                //System.out.println("file locked");
669
                MetacatReplication.replLog("lock denied for " + accnum + " on "
670
                        + server + " reason: file already locked");
671
                throw new Exception(
672
                        "The file specified is already locked by another "
673
                                + "user.  Please wait 30 seconds, checkout the "
674
                                + "newer document, merge your changes and try "
675
                                + "again.");
676
            } else if (openingtag.equals("<outdatedfile>")) {//our file is
677
                                                             // outdated. notify
678
                                                             // our user to
679
                                                             // check out a new
680
                                                             // copy of the
681
                //file and merge his version with the new version.
682
                //System.out.println("outdated file");
683
                MetacatReplication.replLog("lock denied for " + accnum + " on "
684
                        + server + " reason: local file outdated");
685
                throw new Exception(
686
                        "The file you are trying to update is an outdated"
687
                         + " version.  Please checkout the newest document, "
688
                         + "merge your changes and try again.");
689
            }
690
        }
691
        return flag;
692
    }
693

    
694
    /**
695
     * get the document name
696
     */
697
    public String getDocname()
698
    {
699
        return docname;
700
    }
701

    
702
    /**
703
     * get the document type (which is the PublicID)
704
     */
705
    public String getDoctype()
706
    {
707
        return doctype;
708
    }
709

    
710
    /**
711
     * get the system identifier
712
     */
713
    public String getSystemID()
714
    {
715
        return system_id;
716
    }
717

    
718
    /**
719
     * get the root node identifier
720
     */
721
    public long getRootNodeID()
722
    {
723
        return rootnodeid;
724
    }
725

    
726
    /**
727
     * get the creation date
728
     */
729
    public String getCreateDate()
730
    {
731
        return createdate;
732
    }
733

    
734
    /**
735
     * get the update date
736
     */
737
    public String getUpdateDate()
738
    {
739
        return updatedate;
740
    }
741

    
742
    /**
743
     * Get the document identifier (docid)
744
     */
745
    public String getDocID()
746
    {
747
        return docid;
748
    }
749

    
750
    public String getUserowner()
751
    {
752
        return userowner;
753
    }
754

    
755
    public String getUserupdated()
756
    {
757
        return userupdated;
758
    }
759

    
760
    public int getServerlocation()
761
    {
762
        return serverlocation;
763
    }
764

    
765
    public String getDocHomeServer()
766
    {
767
        return docHomeServer;
768
    }
769

    
770
    public String getPublicaccess()
771
    {
772
        return publicaccess;
773
    }
774

    
775
    public int getRev()
776
    {
777
        return rev;
778
    }
779

    
780
    public String getValidateType()
781
    {
782
        return validateType;
783
    }
784

    
785
    /**
786
     * Print a string representation of the XML document
787
     */
788
    public String toString(String user, String[] groups, boolean withInlinedata)
789
    {
790
        StringWriter docwriter = new StringWriter();
791
        try {
792
            this.toXml(docwriter, user, groups, withInlinedata);
793
        } catch (McdbException mcdbe) {
794
            return null;
795
        }
796
        String document = docwriter.toString();
797
        return document;
798
    }
799

    
800
    /**
801
     * Print a string representation of the XML document
802
     */
803
    public String toString()
804
    {
805
        StringWriter docwriter = new StringWriter();
806
        String userName = null;
807
        String[] groupNames = null;
808
        boolean withInlineData = true;
809
        try {
810
            this.toXml(docwriter, userName, groupNames, withInlineData);
811
        } catch (McdbException mcdbe) {
812
            return null;
813
        }
814
        String document = docwriter.toString();
815
        return document;
816
    }
817

    
818
    /**
819
     * Get a text representation of the XML document as a string This older
820
     * algorithm uses a recursive tree of Objects to represent the nodes of the
821
     * tree. Each object is passed the data for the document and searches all of
822
     * the document data to find its children nodes and recursively build. Thus,
823
     * because each node reads the whole document, this algorithm is extremely
824
     * slow for larger documents, and the time to completion is O(N^N) wrt the
825
     * number of nodes. See toXml() for a better algorithm.
826
     */
827
    public String readUsingSlowAlgorithm() throws McdbException
828
    {
829
        StringBuffer doc = new StringBuffer();
830

    
831
        // First, check that we have the needed node data, and get it if not
832
        if (nodeRecordList == null) {
833
            nodeRecordList = getNodeRecordList(rootnodeid);
834
        }
835

    
836
        // Create the elements from the downloaded data in the TreeSet
837
        rootNode = new ElementNode(nodeRecordList, rootnodeid);
838

    
839
        // Append the resulting document to the StringBuffer and return it
840
        doc.append("<?xml version=\"1.0\"?>\n");
841

    
842
        if (docname != null) {
843
            if ((doctype != null) && (system_id != null)) {
844
                doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype
845
                        + "\" \"" + system_id + "\">\n");
846
            } else {
847
                doc.append("<!DOCTYPE " + docname + ">\n");
848
            }
849
        }
850
        doc.append(rootNode.toString());
851

    
852
        return (doc.toString());
853
    }
854

    
855
    /**
856
     * Print a text representation of the XML document to a Writer
857
     *
858
     * @param pw
859
     *            the Writer to which we print the document Now we decide no
860
     *            matter withinInlineData's value, the document will
861
     *
862
     */
863
    public void toXml(Writer pw, String user, String[] groups,
864
            boolean withInLineData) throws McdbException
865
    {
866
        // flag for process eml2
867
        boolean proccessEml2 = false;
868
        boolean storedDTD = false;//flag to inidate publicid or system
869
        // id stored in db or not
870
        boolean firstElement = true;
871
        String dbDocName = null;
872
        String dbPublicID = null;
873
        String dbSystemID = null;
874

    
875
        if (doctype != null
876
                && (doctype.equals(EML2_0_0NAMESPACE)
877
                        || doctype.equals(EML2_0_1NAMESPACE) || doctype
878
                        .equals(EML2_1_0NAMESPACE))) {
879
            proccessEml2 = true;
880
        }
881
        // flag for process inline data
882
        boolean prcocessInlineData = false;
883

    
884
        TreeSet nodeRecordLists = null;
885
        PrintWriter out = null;
886
        if (pw instanceof PrintWriter) {
887
            out = (PrintWriter) pw;
888
        } else {
889
            out = new PrintWriter(pw);
890
        }
891

    
892
        MetaCatUtil util = new MetaCatUtil();
893

    
894
        // Here add code to handle subtree access control
895
        /*
896
         * PermissionController control = new PermissionController(docid);
897
         * Hashtable unaccessableSubTree =control.hasUnaccessableSubTree(user,
898
         * groups, AccessControlInterface.READSTRING);
899
         *
900
         * if (!unaccessableSubTree.isEmpty()) {
901
         *
902
         * nodeRecordLists = getPartNodeRecordList(rootnodeid,
903
         * unaccessableSubTree);
904
         *  } else { nodeRecordLists = getNodeRecordList(rootnodeid); }
905
         */
906
        nodeRecordLists = getNodeRecordList(rootnodeid);
907
        Stack openElements = new Stack();
908
        boolean atRootElement = true;
909
        boolean previousNodeWasElement = false;
910

    
911
        // Step through all of the node records we were given
912

    
913
        Iterator it = nodeRecordLists.iterator();
914

    
915
        while (it.hasNext()) {
916

    
917
            NodeRecord currentNode = (NodeRecord) it.next();
918
            util.debugMessage("[Got Node ID: " + currentNode.nodeid + " ("
919
                    + currentNode.parentnodeid + ", " + currentNode.nodeindex
920
                    + ", " + currentNode.nodetype + ", " + currentNode.nodename
921
                    + ", " + currentNode.nodedata + ")]", 50);
922
            // Print the end tag for the previous node if needed
923
            //
924
            // This is determined by inspecting the parent nodeid for the
925
            // currentNode. If it is the same as the nodeid of the last element
926
            // that was pushed onto the stack, then we are still in that
927
            // previous
928
            // parent element, and we do nothing. However, if it differs, then
929
            // we
930
            // have returned to a level above the previous parent, so we go into
931
            // a loop and pop off nodes and print out their end tags until we
932
            // get
933
            // the node on the stack to match the currentNode parentnodeid
934
            //
935
            // So, this of course means that we rely on the list of elements
936
            // having been sorted in a depth first traversal of the nodes, which
937
            // is handled by the NodeComparator class used by the TreeSet
938
            if (!atRootElement) {
939
                NodeRecord currentElement = (NodeRecord) openElements.peek();
940
                if (currentNode.parentnodeid != currentElement.nodeid) {
941
                    while (currentNode.parentnodeid != currentElement.nodeid) {
942
                        currentElement = (NodeRecord) openElements.pop();
943
                        util.debugMessage("\n POPPED: "
944
                                + currentElement.nodename, 50);
945
                        if (previousNodeWasElement) {
946
                            out.print(">");
947
                            previousNodeWasElement = false;
948
                        }
949
                        if (currentElement.nodeprefix != null) {
950
                            out.print("</" + currentElement.nodeprefix + ":"
951
                                    + currentElement.nodename + ">");
952
                        } else {
953
                            out.print("</" + currentElement.nodename + ">");
954
                        }
955
                        currentElement = (NodeRecord) openElements.peek();
956
                    }
957
                }
958
            }
959

    
960
            // Handle the DOCUMENT node
961
            if (currentNode.nodetype.equals("DOCUMENT")) {
962
                out.print("<?xml version=\"1.0\"?>");
963

    
964
                // Handle the ELEMENT nodes
965
            } else if (currentNode.nodetype.equals("ELEMENT")) {
966
                if (atRootElement) {
967
                    atRootElement = false;
968
                } else {
969
                    if (previousNodeWasElement) {
970
                        out.print(">");
971
                    }
972
                }
973

    
974
                // if publicid or system is not stored into db send it out by
975
                // default
976
                if (!storedDTD & firstElement) {
977
                    if (docname != null && validateType != null
978
                            && validateType.equals(DTD)) {
979
                        if ((doctype != null) && (system_id != null)) {
980

    
981
                            out.print("<!DOCTYPE " + docname + " PUBLIC \""
982
                                    + doctype + "\" \"" + system_id + "\">");
983
                        } else {
984

    
985
                            out.print("<!DOCTYPE " + docname + ">");
986
                        }
987
                    }
988
                }
989
                firstElement = false;
990
                openElements.push(currentNode);
991
                util.debugMessage("\n PUSHED: " + currentNode.nodename, 50);
992
                previousNodeWasElement = true;
993
                if (currentNode.nodeprefix != null) {
994
                    out.print("<" + currentNode.nodeprefix + ":"
995
                            + currentNode.nodename);
996
                } else {
997
                    out.print("<" + currentNode.nodename);
998
                }
999

    
1000
                // if currentNode is inline and handle eml2, set flag proccess
1001
                // in
1002
                if (currentNode.nodename != null
1003
                        && currentNode.nodename.equals(Eml200SAXHandler.INLINE)
1004
                        && proccessEml2) {
1005
                    prcocessInlineData = true;
1006
                }
1007

    
1008
                // Handle the ATTRIBUTE nodes
1009
            } else if (currentNode.nodetype.equals("ATTRIBUTE")) {
1010
                if (currentNode.nodeprefix != null) {
1011
                    out.print(" " + currentNode.nodeprefix + ":"
1012
                            + currentNode.nodename + "=\""
1013
                            + currentNode.nodedata + "\"");
1014
                } else {
1015
                    out.print(" " + currentNode.nodename + "=\""
1016
                            + currentNode.nodedata + "\"");
1017
                }
1018

    
1019
                // Handle the NAMESPACE nodes
1020
            } else if (currentNode.nodetype.equals("NAMESPACE")) {
1021
                out.print(" xmlns:" + currentNode.nodename + "=\""
1022
                        + currentNode.nodedata + "\"");
1023

    
1024
                // Handle the TEXT nodes
1025
            } else if (currentNode.nodetype.equals("TEXT")) {
1026
                if (previousNodeWasElement) {
1027
                    out.print(">");
1028
                }
1029
                if (!prcocessInlineData) {
1030
                    // if it is not inline data just out put data
1031
                    out.print(currentNode.nodedata);
1032
                } else {
1033
                    // if it is inline data first to get the inline data
1034
                    // internal id
1035
                    String fileName = currentNode.nodedata;
1036
                    String accessfileName = MetaCatUtil
1037
                            .getDocIdWithoutRevFromInlineDataID(fileName);
1038
                    // check if user has read permision for this inline data
1039
                    boolean readInlinedata = false;
1040
                    try {
1041
                        Hashtable unReadableInlineDataList =
1042
                            PermissionController
1043
                                .getUnReadableInlineDataIdList(accessfileName,
1044
                                        user, groups, true);
1045
                        if (!unReadableInlineDataList.containsValue(fileName)) {
1046
                            readInlinedata = true;
1047
                        }
1048
                    } catch (Exception e) {
1049
                        throw new McdbException(e.getMessage());
1050
                    }
1051

    
1052
                    if (readInlinedata) {
1053
                        //user want to see it, pull out from
1054
                        // file system and output it
1055
                        // for inline data, the data base only store the file
1056
                        // name, so we
1057
                        // can combine the file name and inline data file path,
1058
                        // to get it
1059

    
1060
                        Reader reader = Eml200SAXHandler
1061
                                .readInlineDataFromFileSystem(fileName);
1062
                        char[] characterArray = new char[4 * 1024];
1063
                        try {
1064
                            int length = reader.read(characterArray);
1065
                            while (length != -1) {
1066
                                out
1067
                                        .print(new String(characterArray, 0,
1068
                                                length));
1069
                                out.flush();
1070
                                length = reader.read(characterArray);
1071
                            }
1072
                            reader.close();
1073
                        } catch (IOException e) {
1074
                            throw new McdbException(e.getMessage());
1075
                        }
1076
                    }//if can read inline data
1077
                    else {
1078
                        // if user can't read it, we only send it back a empty
1079
                        // string
1080
                        // in inline element.
1081
                        out.print("");
1082
                    }// else can't read inlinedata
1083
                    // reset proccess inline data false
1084
                    prcocessInlineData = false;
1085
                }// in inlinedata part
1086
                previousNodeWasElement = false;
1087
                // Handle the COMMENT nodes
1088
            } else if (currentNode.nodetype.equals("COMMENT")) {
1089
                if (previousNodeWasElement) {
1090
                    out.print(">");
1091
                }
1092
                out.print("<!--" + currentNode.nodedata + "-->");
1093
                previousNodeWasElement = false;
1094

    
1095
                // Handle the PI nodes
1096
            } else if (currentNode.nodetype.equals("PI")) {
1097
                if (previousNodeWasElement) {
1098
                    out.print(">");
1099
                }
1100
                out.print("<?" + currentNode.nodename + " "
1101
                        + currentNode.nodedata + "?>");
1102
                previousNodeWasElement = false;
1103
                // Handle the DTD nodes (docname, publicid, systemid)
1104
            } else if (currentNode.nodetype.equals(DTD)) {
1105
                storedDTD = true;
1106
                if (currentNode.getNodeName().equals(DOCNAME)) {
1107
                    dbDocName = currentNode.getNodeData();
1108
                }
1109
                if (currentNode.getNodeName().equals(PUBLICID)) {
1110
                    dbPublicID = currentNode.getNodeData();
1111
                }
1112
                if (currentNode.getNodeName().equals(SYSTEMID)) {
1113
                    dbSystemID = currentNode.getNodeData();
1114
                    // send out <!doctype .../>
1115
                    if (dbDocName != null) {
1116
                        if ((dbPublicID != null) && (dbSystemID != null)) {
1117

    
1118
                            out
1119
                                    .print("<!DOCTYPE " + dbDocName
1120
                                            + " PUBLIC \"" + dbPublicID
1121
                                            + "\" \"" + dbSystemID + "\">");
1122
                        } else {
1123

    
1124
                            out.print("<!DOCTYPE " + dbDocName + ">");
1125
                        }
1126
                    }
1127

    
1128
                    //reset these variable
1129
                    dbDocName = null;
1130
                    dbPublicID = null;
1131
                    dbSystemID = null;
1132
                }
1133

    
1134
                // Handle any other node type (do nothing)
1135
            } else {
1136
                // Any other types of nodes are not handled.
1137
                // Probably should throw an exception here to indicate this
1138
            }
1139
            out.flush();
1140
        }
1141

    
1142
        // Print the final end tag for the root element
1143
        while (!openElements.empty()) {
1144
            NodeRecord currentElement = (NodeRecord) openElements.pop();
1145
            util.debugMessage("\n POPPED: " + currentElement.nodename, 50);
1146
            if (currentElement.nodeprefix != null) {
1147
                out.print("</" + currentElement.nodeprefix + ":"
1148
                        + currentElement.nodename + ">");
1149
            } else {
1150
                out.print("</" + currentElement.nodename + ">");
1151
            }
1152
        }
1153
        out.flush();
1154
    }
1155

    
1156
    /**
1157
     * Build the index records for this document.  For each node, all absolute
1158
     * and relative paths to the root of the document are created and inserted
1159
     * into the xml_index table.  This requires that the DocumentImpl instance 
1160
     * exists, so first call the constructor that reads the document from the 
1161
     * database.
1162
     *
1163
     * @throws McdbException on error getting the node records for the document
1164
     */
1165
    public void buildIndex() throws McdbException
1166
    {
1167
        MetaCatUtil util = new MetaCatUtil();
1168
        TreeSet nodeRecordLists = getNodeRecordList(rootnodeid);
1169
        Stack openElements = new Stack();
1170
        boolean atRootElement = true;
1171
        long rootNodeId = -1;
1172

    
1173
        // Build a map of the same records that are present in the
1174
        // TreeSet so that any node can be easily accessed by nodeId
1175
        HashMap nodeRecordMap = new HashMap();
1176
        Iterator it = nodeRecordLists.iterator();
1177
        while (it.hasNext()) {
1178
            NodeRecord currentNode = (NodeRecord) it.next();
1179
            Long nodeId = new Long(currentNode.getNodeId());
1180
            nodeRecordMap.put(nodeId, currentNode);
1181
        }
1182
        
1183
        // Opening separate db connection for deleting and writing 
1184
        // XML Index -- be sure that it is all in one db transaction
1185
        int serialNumber = -1;
1186
        DBConnection dbConn = null;
1187
        try {
1188
            dbConn = DBConnectionPool.getDBConnection(
1189
                "DocumentImpl.buildIndex");
1190
            serialNumber = dbConn.getCheckOutSerialNumber();
1191
            dbConn.setAutoCommit(false);
1192
            //make sure record is done
1193
            //checkDocumentTable();
1194
            
1195
            // Delete the previous index entries for this document
1196
            deleteNodeIndex(dbConn);
1197
            
1198
            // Step through all of the node records we were given
1199
            // and build the new index and update the database
1200
            it = nodeRecordLists.iterator();
1201
            while (it.hasNext()) {
1202
                NodeRecord currentNode = (NodeRecord) it.next();
1203
                HashMap pathList = new HashMap();
1204
                if (currentNode.nodetype.equals("ELEMENT") ||
1205
                    currentNode.nodetype.equals("ATTRIBUTE") ) {
1206

    
1207
                    System.err.println("\nStarting Node: " + 
1208
                        currentNode.getNodeId() + " (" + 
1209
                        currentNode.getParentNodeId() + "): " + 
1210
                        currentNode.getNodeName() + " (" +
1211
                        currentNode.getNodeType() + ")");
1212
                    if (atRootElement) {
1213
                        rootNodeId = currentNode.getNodeId();
1214
                        atRootElement = false;
1215
                    }
1216
                    traverseParents(nodeRecordMap, rootNodeId, 
1217
                            currentNode.getNodeId(),
1218
                            currentNode.getNodeId(), "", pathList);
1219
                    updateNodeIndex(dbConn, pathList);
1220
                }
1221
            }
1222
            dbConn.commit();
1223
        } catch (SQLException e) {
1224
            MetaCatUtil.debugMessage(
1225
                "SQL Exception while inserting path index in " +
1226
                "DocumentImpl.buildIndex for document " + docid, 10);
1227
      		MetaCatUtil.debugMessage(e.getMessage(), 10);
1228
            e.printStackTrace();
1229
            try {
1230
                dbConn.rollback();
1231
            } catch (SQLException sqle) {
1232
            	MetaCatUtil.debugMessage(
1233
					"Error while rolling back commit in DocumentImpl.buildIndex"
1234
                    + "\n" + sqle.getMessage(), 10);
1235
            }
1236
        } finally {
1237
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1238
        }
1239
    }
1240

    
1241
    /**
1242
     * Recurse up the parent node hierarchy and add each node to the
1243
     * hashmap of paths to be indexed.
1244
     *
1245
     * @param records the set of records hashed by nodeId
1246
     * @param rootNodeId the id of the root element of the document
1247
     * @param leafNodeId the id of the leafNode being processed
1248
     * @param id the id of the current node to be processed
1249
     * @param children the string representation of all child nodes of this id
1250
     * @param pathList the hash to which paths are added
1251
     */
1252
    private void traverseParents(HashMap records, long rootNodeId, 
1253
            long leafNodeId, long id, 
1254
            String children, HashMap pathList) {
1255
        Long nodeId = new Long(id);
1256
        NodeRecord current = (NodeRecord)records.get(nodeId);
1257
        long parentId = current.getParentNodeId();
1258
        String currentName = current.getNodeName();
1259
        if (current.nodetype.equals("ELEMENT") ||
1260
            current.nodetype.equals("ATTRIBUTE") ) {
1261

    
1262
            if (children.equals("")) {
1263
                System.err.print("A: " + currentName +"\n");
1264
                pathList.put(currentName, new PathIndexEntry(
1265
                    leafNodeId, currentName, docid, doctype, parentId));
1266
            }
1267
            currentName = "/" + currentName;
1268
            currentName = currentName + children;
1269
            if (parentId != 0) {
1270
                traverseParents(records, rootNodeId, leafNodeId,
1271
                    parentId, currentName, pathList);
1272
            }
1273
            String path = current.getNodeName() + children;
1274
            if (!children.equals("")) {
1275
                System.err.print("B: " + path +"\n");
1276
                pathList.put(path, new PathIndexEntry(leafNodeId, path, docid, 
1277
                    doctype, parentId));
1278
            }
1279
            if (id == rootNodeId) {
1280
                String fullPath = '/' + path;
1281
                System.err.print("C: " + fullPath +"\n");
1282
                pathList.put(fullPath, new PathIndexEntry(leafNodeId, fullPath,
1283
                    docid, doctype, parentId));
1284
            }
1285
        }
1286
    }
1287

    
1288
    /**
1289
     * Delete the paths from the xml_index table on the database in preparation 
1290
     * of a subsequent update.
1291
     *
1292
     * @param conn the database connection to use, keeping a single transaction
1293
     * @throws SQLException if there is an error deleting from the db
1294
     */
1295
    public void deleteNodeIndex(DBConnection conn) throws SQLException
1296
    {
1297
        String familyId = MetaCatUtil.getDocIdFromString(docid);
1298
        String sql = "DELETE FROM xml_index WHERE docid LIKE ?";
1299
        MetaCatUtil.debugMessage(sql, 55);
1300
        System.err.println("SQL is: " + sql);
1301
        
1302
        PreparedStatement pstmt = conn.prepareStatement(sql);
1303

    
1304
        // Increase usage count for the connection
1305
        conn.increaseUsageCount(1);
1306
        
1307
        // Execute the delete and close the statement
1308
        pstmt.setString(1, familyId);
1309
        int rows = pstmt.executeUpdate();
1310
        pstmt.close();
1311
        MetaCatUtil.debugMessage("Deleted " + rows + " rows from xml_index " +
1312
            "for document " + docid, 55);
1313
    }
1314
    
1315
    /**
1316
	 * Insert the paths from the pathList into the xml_index table on the
1317
     * database.
1318
     *
1319
     * @param conn the database connection to use, keeping a single transaction
1320
     * @param pathList the hash of paths to insert
1321
     * @throws SQLException if there is an error inserting into the db
1322
     */
1323
    private void updateNodeIndex(DBConnection conn, HashMap pathList) 
1324
    	throws SQLException
1325
    {
1326
        // Create an insert statement to reuse for all of the path
1327
        // insertions
1328
        PreparedStatement pstmt = conn.prepareStatement(
1329
                "INSERT INTO xml_index (nodeid, path, docid, doctype, " +
1330
                "parentnodeid) " + "VALUES (?, ?, ?, ?, ?)");
1331
        // Increase usage count for the connection
1332
        conn.increaseUsageCount(1);
1333
        String familyId = MetaCatUtil.getDocIdFromString(docid);
1334
        pstmt.setString(3, familyId);
1335
        pstmt.setString(4, doctype);
1336
        
1337
        // Step through the hashtable and insert each of the path values
1338
        Iterator it = pathList.values().iterator();
1339
        while (it.hasNext()) {
1340
            PathIndexEntry entry = (PathIndexEntry)it.next();
1341
            System.err.println("Inserting: " + entry.nodeId +
1342
                " (" + entry.parentId + "): " + entry.path);
1343
            pstmt.setLong(1, entry.nodeId);
1344
            pstmt.setString(2, entry.path);
1345
            pstmt.setLong(5, entry.parentId);
1346
            pstmt.executeUpdate();
1347
        }
1348
        // Close the database statement
1349
        pstmt.close();
1350
    }
1351
    
1352
    private boolean isRevisionOnly(DocumentIdentifier docid) throws Exception
1353
    {
1354
        //System.out.println("inRevisionOnly");
1355
        DBConnection dbconn = null;
1356
        int serialNumber = -1;
1357
        PreparedStatement pstmt = null;
1358
        String rev = docid.getRev();
1359
        String newid = docid.getIdentifier();
1360
        try {
1361
            dbconn = DBConnectionPool
1362
                    .getDBConnection("DocumentImpl.isRevisionOnly");
1363
            serialNumber = dbconn.getCheckOutSerialNumber();
1364
            pstmt = dbconn.prepareStatement("select rev from xml_documents "
1365
                    + "where docid like '" + newid + "'");
1366
            pstmt.execute();
1367
            ResultSet rs = pstmt.getResultSet();
1368
            boolean tablehasrows = rs.next();
1369
            if (rev.equals("newest") || rev.equals("all")) { return false; }
1370

    
1371
            if (tablehasrows) {
1372
                int r = rs.getInt(1);
1373
                pstmt.close();
1374
                if (new Integer(rev).intValue() == r) { //the current revision
1375
                                                        // in in xml_documents
1376
                    //System.out.println("returning false");
1377
                    return false;
1378
                } else if (new Integer(rev).intValue() < r) { //the current
1379
                                                              // revision is in
1380
                                                              // xml_revisions.
1381
                    //System.out.println("returning true");
1382
                    return true;
1383
                } else if (new Integer(rev).intValue() > r) { //error, rev
1384
                                                              // cannot be
1385
                                                              // greater than r
1386
                throw new Exception(
1387
                        "requested revision cannot be greater than "
1388
                                + "the latest revision number."); }
1389
            }
1390
            // Get miss docid and rev, throw to McdDocNotFoundException
1391
            String missDocId = MetaCatUtil.getDocIdFromString(docid.toString());
1392
            String missRevision = MetaCatUtil.getRevisionStringFromString(docid
1393
                    .toString());
1394
            throw new McdbDocNotFoundException("the requested docid '"
1395
                    + docid.toString() + "' does not exist", missDocId,
1396
                    missRevision);
1397
        }//try
1398
        finally {
1399
            pstmt.close();
1400
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1401
        }//finally
1402
    }
1403

    
1404
    private void getDocumentInfo(String docid) throws McdbException,
1405
            AccessionNumberException, Exception
1406
    {
1407
        getDocumentInfo(new DocumentIdentifier(docid));
1408
    }
1409

    
1410
    /**
1411
     * Look up the document type information from the database
1412
     *
1413
     * @param docid
1414
     *            the id of the document to look up
1415
     */
1416
    private void getDocumentInfo(DocumentIdentifier docid)
1417
            throws McdbException, Exception
1418
    {
1419
        DBConnection dbconn = null;
1420
        int serialNumber = -1;
1421
        PreparedStatement pstmt = null;
1422
        String table = "xml_documents";
1423

    
1424
        try {
1425
            if (isRevisionOnly(docid)) { //pull the document from xml_revisions
1426
                                         // instead of from xml_documents;
1427
                table = "xml_revisions";
1428
            }
1429
        }
1430
        // catch a McdbDocNotFoundException throw it
1431
        catch (McdbDocNotFoundException notFound) {
1432
            throw notFound;
1433
        } catch (Exception e) {
1434

    
1435
            MetaCatUtil.debugMessage("error in DocumentImpl.getDocumentInfo: "
1436
                    + e.getMessage(), 30);
1437
            throw e;
1438
        }
1439

    
1440
        try {
1441
            dbconn = DBConnectionPool
1442
                    .getDBConnection("DocumentImpl.getDocumentInfo");
1443
            serialNumber = dbconn.getCheckOutSerialNumber();
1444
            StringBuffer sql = new StringBuffer();
1445
            sql.append("SELECT docname, doctype, rootnodeid, ");
1446
            sql.append("date_created, date_updated, user_owner, user_updated,");
1447
            sql.append(" server_location, public_access, rev");
1448
            sql.append(" FROM ").append(table);
1449
            sql.append(" WHERE docid LIKE '").append(docid.getIdentifier());
1450
            sql.append("' and rev like '").append(docid.getRev()).append("'");
1451

    
1452
            pstmt = dbconn.prepareStatement(sql.toString());
1453

    
1454
            pstmt.execute();
1455
            ResultSet rs = pstmt.getResultSet();
1456
            boolean tableHasRows = rs.next();
1457
            if (tableHasRows) {
1458
                this.docname = rs.getString(1);
1459
                this.doctype = rs.getString(2);
1460
                this.rootnodeid = rs.getLong(3);
1461
                this.createdate = rs.getString(4);
1462
                this.updatedate = rs.getString(5);
1463
                this.userowner = rs.getString(6);
1464
                this.userupdated = rs.getString(7);
1465
                this.serverlocation = rs.getInt(8);
1466
                this.publicaccess = rs.getString(9);
1467
                this.rev = rs.getInt(10);
1468
            }
1469
            pstmt.close();
1470

    
1471
            //get doc home server name
1472
            pstmt = dbconn.prepareStatement("select server "
1473
                    + "from xml_replication where serverid = ?");
1474
            //because connection use twice here, so we need to increase one
1475
            dbconn.increaseUsageCount(1);
1476
            pstmt.setInt(1, serverlocation);
1477
            pstmt.execute();
1478
            rs = pstmt.getResultSet();
1479
            tableHasRows = rs.next();
1480
            if (tableHasRows) {
1481

    
1482
                String server = rs.getString(1);
1483
                //get homeserver name
1484
                if (!server.equals("localhost")) {
1485
                    this.docHomeServer = server;
1486
                } else {
1487
                    this.docHomeServer = MetaCatUtil
1488
                            .getLocalReplicationServerName();
1489
                }
1490
                MetaCatUtil.debugMessage("server: " + docHomeServer, 50);
1491

    
1492
            }
1493
            pstmt.close();
1494
            if (this.doctype != null) {
1495
                pstmt = dbconn.prepareStatement("SELECT system_id, entry_type "
1496
                        + "FROM xml_catalog " + "WHERE public_id = ?");
1497
                //should increase usage count again
1498
                dbconn.increaseUsageCount(1);
1499
                // Bind the values to the query
1500
                pstmt.setString(1, doctype);
1501

    
1502
                pstmt.execute();
1503
                rs = pstmt.getResultSet();
1504
                tableHasRows = rs.next();
1505
                if (tableHasRows) {
1506
                    this.system_id = rs.getString(1);
1507
                    this.validateType = rs.getString(2);
1508

    
1509
                }
1510
                pstmt.close();
1511
            }
1512
        } catch (SQLException e) {
1513
            System.out.println("error in DocumentImpl.getDocumentInfo: "
1514
                    + e.getMessage());
1515
            e.printStackTrace(System.out);
1516
            throw new McdbException("Error accessing database connection in "
1517
                    + "DocumentImpl.getDocumentInfo: ", e);
1518
        } finally {
1519
            try {
1520
                pstmt.close();
1521
            } catch (SQLException ee) {
1522
                MetaCatUtil.debugMessage(
1523
                        "error in DocumentImple.getDocumentInfo: "
1524
                                + ee.getMessage(), 30);
1525
            } finally {
1526
                DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1527
            }
1528
        }
1529

    
1530
        if (this.docname == null) {
1531
            throw new McdbDocNotFoundException(
1532
                "Document not found: " + docid, docid.getIdentifier(), docid
1533
                        .getRev());
1534
        }
1535
    }
1536

    
1537
    /**
1538
     * Look up the node data from the database, but some node would be shown
1539
     * because of access control
1540
     *
1541
     * @param rootnodeid
1542
     *            the id of the root node of the node tree to look up
1543
     * @param accessControl
1544
     *            the hashtable has control info
1545
     */
1546
    private TreeSet getPartNodeRecordList(long rootnodeid,
1547
            Hashtable accessControl) throws McdbException
1548
    {
1549
        PreparedStatement pstmt = null;
1550
        DBConnection dbconn = null;
1551
        int serialNumber = -1;
1552
        TreeSet nodeRecordList = new TreeSet(new NodeComparator());
1553
        long nodeid = 0;
1554
        long parentnodeid = 0;
1555
        long nodeindex = 0;
1556
        String nodetype = null;
1557
        String nodename = null;
1558
        String nodeprefix = null;
1559
        String nodedata = null;
1560
        String quotechar = dbAdapter.getStringDelimiter();
1561
        String sql = "SELECT nodeid,parentnodeid,nodeindex, "
1562
                + "nodetype,nodename,nodeprefix,nodedata "
1563
                + "FROM xml_nodes WHERE rootnodeid = ?";
1564

    
1565
        // go through the access control for some nodes
1566
        Enumeration en = accessControl.elements();
1567
        while (en.hasMoreElements()) {
1568
            SubTree tree = (SubTree) en.nextElement();
1569
            long startId = tree.getStartNodeId();
1570
            long endId = tree.getEndNodeId();
1571
            sql = sql + " AND(nodeid < " + startId + " OR nodeid > " + endId
1572
                    + ")";
1573

    
1574
        }
1575
        MetaCatUtil.debugMessage("The final query to select part node tree: "
1576
                + sql, 25);
1577

    
1578
        try {
1579
            dbconn = DBConnectionPool
1580
                    .getDBConnection("DocumentImpl.getPartNodeRecordList");
1581
            serialNumber = dbconn.getCheckOutSerialNumber();
1582
            pstmt = dbconn.prepareStatement(sql);
1583

    
1584
            // Bind the values to the query
1585
            pstmt.setLong(1, rootnodeid);
1586
            pstmt.execute();
1587
            ResultSet rs = pstmt.getResultSet();
1588
            boolean tableHasRows = rs.next();
1589
            while (tableHasRows) {
1590
                nodeid = rs.getLong(1);
1591
                parentnodeid = rs.getLong(2);
1592
                nodeindex = rs.getLong(3);
1593
                nodetype = rs.getString(4);
1594
                nodename = rs.getString(5);
1595
                nodeprefix = rs.getString(6);
1596
                nodedata = rs.getString(7);
1597
                nodedata = MetaCatUtil.normalize(nodedata);
1598
                // add the data to the node record list hashtable
1599
                NodeRecord currentRecord = new NodeRecord(nodeid, parentnodeid,
1600
                        nodeindex, nodetype, nodename, nodeprefix, nodedata);
1601
                nodeRecordList.add(currentRecord);
1602

    
1603
                // Advance to the next node
1604
                tableHasRows = rs.next();
1605
            }
1606
            pstmt.close();
1607

    
1608
        } catch (SQLException e) {
1609
            throw new McdbException(
1610
                    "Error in DocumentImpl.getPartNodeRecordList "
1611
                            + e.getMessage());
1612
        } finally {
1613
            try {
1614
                pstmt.close();
1615
            } catch (SQLException ee) {
1616
                MetaCatUtil.debugMessage(
1617
                        "error in DocumentImpl.getPartNodeRecordList: "
1618
                                + ee.getMessage(), 30);
1619
            } finally {
1620
                DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1621
            }
1622
        }
1623

    
1624
        if (!nodeRecordList.isEmpty()) {
1625

    
1626
            return nodeRecordList;
1627
        } else {
1628

    
1629
            throw new McdbException("Error getting node data: " + docid);
1630
        }
1631
    }
1632

    
1633
    /**
1634
     * Look up the node data from the database
1635
     *
1636
     * @param rootnodeid
1637
     *            the id of the root node of the node tree to look up
1638
     */
1639
    private TreeSet getNodeRecordList(long rootnodeid) throws McdbException
1640
    {
1641
        PreparedStatement pstmt = null;
1642
        DBConnection dbconn = null;
1643
        int serialNumber = -1;
1644
        TreeSet nodeRecordList = new TreeSet(new NodeComparator());
1645
        long nodeid = 0;
1646
        long parentnodeid = 0;
1647
        long nodeindex = 0;
1648
        String nodetype = null;
1649
        String nodename = null;
1650
        String nodeprefix = null;
1651
        String nodedata = null;
1652
        String quotechar = dbAdapter.getStringDelimiter();
1653

    
1654
        try {
1655
            dbconn = DBConnectionPool
1656
                    .getDBConnection("DocumentImpl.getNodeRecordList");
1657
            serialNumber = dbconn.getCheckOutSerialNumber();
1658
            pstmt = dbconn
1659
                    .prepareStatement("SELECT nodeid,parentnodeid,nodeindex, "
1660
                            + "nodetype,nodename,nodeprefix,nodedata "
1661
                            + "FROM xml_nodes WHERE rootnodeid = ?");
1662

    
1663
            // Bind the values to the query
1664
            pstmt.setLong(1, rootnodeid);
1665

    
1666
            pstmt.execute();
1667
            ResultSet rs = pstmt.getResultSet();
1668
            boolean tableHasRows = rs.next();
1669
            while (tableHasRows) {
1670
                nodeid = rs.getLong(1);
1671
                parentnodeid = rs.getLong(2);
1672
                nodeindex = rs.getLong(3);
1673
                nodetype = rs.getString(4);
1674
                nodename = rs.getString(5);
1675
                nodeprefix = rs.getString(6);
1676
                nodedata = rs.getString(7);
1677
                nodedata = MetaCatUtil.normalize(nodedata);
1678
                // add the data to the node record list hashtable
1679
                NodeRecord currentRecord = new NodeRecord(nodeid, parentnodeid,
1680
                        nodeindex, nodetype, nodename, nodeprefix, nodedata);
1681
                nodeRecordList.add(currentRecord);
1682

    
1683
                // Advance to the next node
1684
                tableHasRows = rs.next();
1685
            }
1686
            pstmt.close();
1687

    
1688
        } catch (SQLException e) {
1689
            throw new McdbException("Error in DocumentImpl.getNodeRecordList "
1690
                    + e.getMessage());
1691
        } finally {
1692
            try {
1693
                pstmt.close();
1694
            } catch (SQLException ee) {
1695
                MetaCatUtil.debugMessage(
1696
                        "error in DocumentImpl.getNodeRecordList: "
1697
                                + ee.getMessage(), 30);
1698
            } finally {
1699
                DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1700
            }
1701
        }
1702

    
1703
        return nodeRecordList;
1704

    
1705
    }
1706

    
1707
    /** creates SQL code and inserts new document into DB connection */
1708
    private void writeDocumentToDB(String action, String user, String pub,
1709
            String catalogid, int serverCode) throws SQLException, Exception
1710
    {
1711
        String sysdate = dbAdapter.getDateTimeFunction();
1712

    
1713
        try {
1714
            PreparedStatement pstmt = null;
1715

    
1716
            if (action.equals("INSERT")) {
1717
                //AccessionNumber ac = new AccessionNumber();
1718
                //this.docid = ac.generate(docid, "INSERT");
1719

    
1720
                pstmt = connection
1721
                        .prepareStatement("INSERT INTO xml_documents "
1722
                        + "(docid, rootnodeid, docname, doctype, user_owner, "
1723
                        + "user_updated, date_created, date_updated, "
1724
                        + "public_access, catalog_id, server_location, rev) "
1725
                        + "VALUES (?, ?, ?, ?, ?, ?, " + sysdate + ", "
1726
                        + sysdate + ", ?, ?, ?, ?)");
1727
                // Increase dbconnection usage count
1728
                connection.increaseUsageCount(1);
1729

    
1730
                //note that the server_location is set to 1.
1731
                //this means that "localhost" in the xml_replication table must
1732
                //always be the first entry!!!!!
1733

    
1734
                // Bind the values to the query
1735
                pstmt.setString(1, this.docid);
1736
                pstmt.setLong(2, rootnodeid);
1737
                pstmt.setString(3, docname);
1738
                pstmt.setString(4, doctype);
1739
                pstmt.setString(5, user);
1740
                pstmt.setString(6, user);
1741
                //public access is usefulless, so set it to null
1742
                pstmt.setString(7, null);
1743
                /*
1744
                 * if ( pub == null ) { pstmt.setString(7, null); } else if (
1745
                 * pub.toUpperCase().equals("YES") || pub.equals("1") ) {
1746
                 * pstmt.setInt(7, 1); } else if (
1747
                 * pub.toUpperCase().equals("NO") || pub.equals("0") ) {
1748
                 * pstmt.setInt(7, 0); }
1749
                 */
1750
                pstmt.setString(8, catalogid);
1751
                pstmt.setInt(9, serverCode);
1752
                pstmt.setInt(10, Integer.parseInt(updatedVersion));
1753
            } else if (action.equals("UPDATE")) {
1754

    
1755
                // Save the old document publicaccessentry in a backup table
1756
                DocumentImpl.archiveDocRevision(connection, docid, user);
1757
                MetaCatUtil.debugMessage("after archiveDoc", 40);
1758
                DocumentImpl thisdoc = new DocumentImpl(docid, false);
1759
                int thisrev = thisdoc.getRev();
1760
                MetaCatUtil.debugMessage("this revsion is: " + thisrev, 40);
1761
                //if the updated vesion is not greater than current one,
1762
                //throw it into a exception
1763
                if (Integer.parseInt(updatedVersion) <= thisrev) {
1764
                    throw new Exception("Next revision number couldn't be less"
1765
                            + " than or equal " + thisrev);
1766
                } else {
1767
                    //set the user specified revision
1768
                    thisrev = Integer.parseInt(updatedVersion);
1769
                }
1770
                MetaCatUtil.debugMessage("final revsion is: " + thisrev, 40);
1771
                boolean useXMLIndex = (new Boolean(MetaCatUtil
1772
                        .getOption("usexmlindex"))).booleanValue();
1773
                if (useXMLIndex) {
1774
                    MetaCatUtil.debugMessage("before delete", 40);
1775
                    // Delete index for the old version of docid
1776
                    // The new index is inserting on the next calls to DBSAXNode
1777
                    pstmt = connection
1778
                            .prepareStatement("DELETE FROM xml_index WHERE docid='"
1779
                                    + this.docid + "'");
1780
                    MetaCatUtil.debugMessage("after delete", 40);
1781
                    // Increase dbconnection usage count
1782
                    connection.increaseUsageCount(1);
1783

    
1784
                    pstmt.execute();
1785
                    pstmt.close();
1786
                }
1787

    
1788
                // Update the new document to reflect the new node tree
1789
                pstmt = connection
1790
                        .prepareStatement("UPDATE xml_documents "
1791
                        + "SET rootnodeid = ?, docname = ?, doctype = ?, "
1792
                        + "user_updated = ?, date_updated = "
1793
                        + sysdate
1794
                        + ", "
1795
                        + "server_location = ?, rev = ?, public_access = ?, "
1796
                        + "catalog_id = ? "
1797
                        + "WHERE docid = ?");
1798
                // Increase dbconnection usage count
1799
                connection.increaseUsageCount(1);
1800
                // Bind the values to the query
1801
                pstmt.setLong(1, rootnodeid);
1802
                pstmt.setString(2, docname);
1803
                pstmt.setString(3, doctype);
1804
                pstmt.setString(4, user);
1805
                pstmt.setInt(5, serverCode);
1806
                pstmt.setInt(6, thisrev);
1807
                pstmt.setString(7, null);
1808
                /*
1809
                 * if ( pub == null ) { pstmt.setString(7, null); } else if (
1810
                 * pub.toUpperCase().equals("YES") || pub.equals("1") ) { pstmt
1811
                 * .setInt(7, 1); } else if ( pub.toUpperCase().equals("NO") ||
1812
                 * pub.equals("0") ) { pstmt.setInt(7, 0); }
1813
                 */
1814
                pstmt.setString(8, catalogid);
1815
                pstmt.setString(9, this.docid);
1816

    
1817
            } else {
1818
                System.err.println("Action not supported: " + action);
1819
            }
1820

    
1821
            // Do the insertion
1822
            pstmt.execute();
1823

    
1824
            pstmt.close();
1825

    
1826
        } catch (SQLException sqle) {
1827
            throw sqle;
1828
        } catch (Exception e) {
1829
            throw e;
1830
        }
1831
    }
1832

    
1833
    /**
1834
     * Write an XML file to the database, given a filename
1835
     *
1836
     * @param conn
1837
     *            the JDBC connection to the database
1838
     * @param filename
1839
     *            the filename to be loaded into the database
1840
     * @param pub
1841
     *            flag for public "read" access on document
1842
     * @param dtdfilename
1843
     *            the dtd to be uploaded on server's file system
1844
     * @param action
1845
     *            the action to be performed (INSERT OR UPDATE)
1846
     * @param docid
1847
     *            the docid to use for the INSERT OR UPDATE
1848
     * @param user
1849
     *            the user that owns the document
1850
     * @param groups
1851
     *            the groups to which user belongs
1852
     */
1853
    /*
1854
     * public static String write(DBConnection conn,String filename, String pub,
1855
     * String dtdfilename, String action, String docid, String user, String[]
1856
     * groups ) throws Exception {
1857
     *
1858
     * Reader dtd = null; if ( dtdfilename != null ) { dtd = new FileReader(new
1859
     * File(dtdfilename).toString()); } return write ( conn, new FileReader(new
1860
     * File(filename).toString()), pub, dtd, action, docid, user, groups,
1861
     * false); }
1862
     */
1863

    
1864
    public static String write(DBConnection conn, Reader xml, String pub,
1865
            Reader dtd, String action, String docid, String user,
1866
            String[] groups, String ruleBase, boolean needValidation)
1867
            throws Exception
1868
    {
1869
        //this method will be called in handleUpdateOrInsert method
1870
        //in MetacatServlet class and now is wrapper into documentImple
1871
        // get server location for this doc
1872
        int serverLocation = getServerLocationNumber(docid);
1873
        return write(conn, xml, pub, dtd, action, docid, user, groups,
1874
                serverLocation, false, ruleBase, needValidation);
1875
    }
1876

    
1877
    /**
1878
     * Write an XML file to the database, given a Reader
1879
     *
1880
     * @param conn
1881
     *            the JDBC connection to the database
1882
     * @param xml
1883
     *            the xml stream to be loaded into the database
1884
     * @param pub
1885
     *            flag for public "read" access on xml document
1886
     * @param dtd
1887
     *            the dtd to be uploaded on server's file system
1888
     * @param action
1889
     *            the action to be performed (INSERT or UPDATE)
1890
     * @param accnum
1891
     *            the docid + rev# to use on INSERT or UPDATE
1892
     * @param user
1893
     *            the user that owns the document
1894
     * @param groups
1895
     *            the groups to which user belongs
1896
     * @param serverCode
1897
     *            the serverid from xml_replication on which this document
1898
     *            resides.
1899
     * @param override
1900
     *            flag to stop insert replication checking. if override = true
1901
     *            then a document not belonging to the local server will not be
1902
     *            checked upon update for a file lock. if override = false then
1903
     *            a document not from this server, upon update will be locked
1904
     *            and version checked.
1905
     */
1906

    
1907
    public static String write(DBConnection conn, Reader xml, String pub,
1908
            Reader dtd, String action, String accnum, String user,
1909
            String[] groups, int serverCode, boolean override, String ruleBase,
1910
            boolean needValidation) throws Exception
1911
    {
1912
        // NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
1913
        //MetaCatUtil util = new MetaCatUtil();
1914
        MetaCatUtil.debugMessage("conn usage count before writting: "
1915
                + conn.getUsageCount(), 50);
1916
        AccessionNumber ac = new AccessionNumber(accnum, action);
1917
        String docid = ac.getDocid();
1918
        String rev = ac.getRev();
1919
        MetaCatUtil.debugMessage("action: " + action + " servercode: "
1920
                + serverCode + " override: " + override, 10);
1921

    
1922
        if ((serverCode != 1 && action.equals("UPDATE")) && !override) {
1923
            // if this document being written is not a resident of this server
1924
            // then we need to try to get a lock from it's resident server. If
1925
            // the resident server will not give a lock then we send the user
1926
            // a  message saying that he/she needs to download a new copy of
1927
            // the file and merge the differences manually.
1928
            int istreamInt;
1929
            char istreamChar;
1930

    
1931
            // check for 'write' permission for 'user' to update this document
1932
            if (!hasWritePermission(user, groups, accnum)) {
1933
                throw new Exception(
1934
                    "User " + user
1935
                    + " does not have permission to update XML Document #"
1936
                    + accnum);
1937
            }
1938

    
1939
            DocumentIdentifier id = new DocumentIdentifier(accnum);
1940
            String updaterev = id.getRev();
1941
            String server = MetacatReplication
1942
                    .getServerNameForServerCode(serverCode);
1943
            MetacatReplication.replLog("attempting to lock " + accnum);
1944
            URL u = new URL("https://" + server + "?server="
1945
                    + MetaCatUtil.getLocalReplicationServerName()
1946
                    + "&action=getlock&updaterev=" + updaterev + "&docid="
1947
                    + docid);
1948
            //System.out.println("sending message: " + u.toString());
1949
            String serverResStr = MetacatReplication.getURLContent(u);
1950
            String openingtag = serverResStr.substring(0, serverResStr
1951
                    .indexOf(">") + 1);
1952
            if (openingtag.equals("<lockgranted>")) {//the lock was granted go
1953
                                                     // ahead with the insert
1954
                XMLReader parser = null;
1955
                try {
1956
                    //System.out.println("In lockgranted");
1957
                    MetacatReplication.replLog("lock granted for " + accnum
1958
                            + " from " + server);
1959
                    /*
1960
                     * XMLReader parser = initializeParser(conn, action, docid,
1961
                     * updaterev, validate, user, groups, pub, serverCode, dtd);
1962
                     */
1963
                    parser = initializeParser(conn, action, docid, updaterev,
1964
                            user, groups, pub, serverCode, dtd, ruleBase,
1965
                            needValidation);
1966
                    conn.setAutoCommit(false);
1967
                    parser.parse(new InputSource(xml));
1968
                    conn.commit();
1969
                    conn.setAutoCommit(true);
1970
                } catch (Exception e) {
1971
                    conn.rollback();
1972
                    conn.setAutoCommit(true);
1973
                    //if it is a eml2 document, we need delete online data
1974
                    if (parser != null) {
1975
                        ContentHandler handler = parser.getContentHandler();
1976
                        if (handler instanceof Eml200SAXHandler) {
1977
                            Eml200SAXHandler eml = (Eml200SAXHandler) handler;
1978
                            eml.deleteInlineFiles();
1979
                        }
1980
                    }
1981
                    throw e;
1982
                }
1983
                // run write into access db base one relation table and access
1984
                // object
1985
                runRelationAndAccessHandler(accnum, user, groups, serverCode);
1986

    
1987
                // Force replication the docid
1988
                ForceReplicationHandler frh = new ForceReplicationHandler(
1989
                        accnum, true, null);
1990
                return (accnum);
1991

    
1992
            }
1993

    
1994
            else if (openingtag.equals("<filelocked>")) {
1995
                // the file is currently locked by another user notify our
1996
                // user to wait a few minutes, check out a new copy and try
1997
                // again.
1998
                MetacatReplication.replLog("lock denied for " + accnum + " on "
1999
                        + server + " reason: file already locked");
2000
                throw new Exception(
2001
                        "The file specified is already locked by another "
2002
                                + "user.  Please wait 30 seconds, checkout the "
2003
                                + "newer document, merge your changes and try "
2004
                                + "again.");
2005
            } else if (openingtag.equals("<outdatedfile>")) {
2006
                // our file is outdated. notify our user to check out a new
2007
                // copy of the file and merge his version with the new version.
2008
                //System.out.println("outdated file");
2009
                MetacatReplication.replLog("lock denied for " + accnum + " on "
2010
                        + server + " reason: local file outdated");
2011
                throw new Exception(
2012
                        "The file you are trying to update is an outdated"
2013
                        + " version.  Please checkout the newest document, "
2014
                        + "merge your changes and try again.");
2015
            }
2016
        }
2017

    
2018
        if (action.equals("UPDATE")) {
2019
            // check for 'write' permission for 'user' to update this document
2020
            if (!hasWritePermission(user, groups, accnum)) {
2021
                throw new Exception(
2022
                    "User " + user
2023
                    + " does not have permission to update XML Document #"
2024
                    + accnum); }
2025
        }
2026
        XMLReader parser = null;
2027
        try {
2028
            parser = initializeParser(conn, action, docid, rev, user, groups,
2029
                    pub, serverCode, dtd, ruleBase, needValidation);
2030

    
2031
            conn.setAutoCommit(false);
2032
            parser.parse(new InputSource(xml));
2033
            conn.commit();
2034
            conn.setAutoCommit(true);
2035
        } catch (Exception e) {
2036
            conn.rollback();
2037
            conn.setAutoCommit(true);
2038
            //if it is a eml2 document, we need delete online data
2039
            if (parser != null) {
2040
                ContentHandler handler = parser.getContentHandler();
2041
                if (handler instanceof Eml200SAXHandler) {
2042
                    Eml200SAXHandler eml = (Eml200SAXHandler) handler;
2043
                    eml.deleteInlineFiles();
2044
                }
2045
            }
2046
            throw e;
2047
        }
2048

    
2049
        // run access db base on relation table and access object
2050
        runRelationAndAccessHandler(accnum, user, groups, serverCode);
2051

    
2052
        // Force replicate out the new document to each server in our server
2053
        // list. Start the thread to replicate this new document out to the
2054
        // other servers true mean it is xml document null is because no
2055
        // metacat notify the force replication.
2056
        ForceReplicationHandler frh = new ForceReplicationHandler(accnum,
2057
                action, true, null);
2058

    
2059
        MetaCatUtil.debugMessage("Conn Usage count after writting: "
2060
                + conn.getUsageCount(), 50);
2061
        return (accnum);
2062
    }
2063

    
2064
    /**
2065
     * Write an XML file to the database during replication
2066
     *
2067
     * @param conn
2068
     *            the JDBC connection to the database
2069
     * @param xml
2070
     *            the xml stream to be loaded into the database
2071
     * @param pub
2072
     *            flag for public "read" access on xml document
2073
     * @param dtd
2074
     *            the dtd to be uploaded on server's file system
2075
     * @param action
2076
     *            the action to be performed (INSERT or UPDATE)
2077
     * @param accnum
2078
     *            the docid + rev# to use on INSERT or UPDATE
2079
     * @param user
2080
     *            the user that owns the document
2081
     * @param groups
2082
     *            the groups to which user belongs
2083
     * @param homeServer
2084
     *            the name of server which the document origanlly create
2085
     * @param validate,
2086
     *            if the xml document is valid or not
2087
     * @param notifyServer,
2088
     *            the server which notify local server the force replication
2089
     *            command
2090
     */
2091
    public static String writeReplication(DBConnection conn, Reader xml,
2092
            String pub, Reader dtd, String action, String accnum, String user,
2093
            String[] groups, String homeServer, String notifyServer,
2094
            String ruleBase, boolean needValidation) throws Exception
2095
    {
2096
        MetaCatUtil.debugMessage("user in replication" + user, 30);
2097
        // Docid without revision
2098
        String docid = MetaCatUtil.getDocIdFromAccessionNumber(accnum);
2099
        // Revision specified by user (int)
2100
        int userSpecifyRev = MetaCatUtil.getRevisionFromAccessionNumber(accnum);
2101
        MetaCatUtil.debugMessage("The user specifyRev: " + userSpecifyRev, 30);
2102
        // Revision for this docid in current database
2103
        int revInDataBase = getLatestRevisionNumber(docid);
2104
        MetaCatUtil.debugMessage("The rev in data base: " + revInDataBase, 30);
2105
        // String to store the revision
2106
        String rev = null;
2107

    
2108
        //revIndataBase=-1, there is no record in xml_documents table
2109
        //the document is a new one for local server, inert it into table
2110
        //user specified rev should be great than 0
2111
        if (revInDataBase == -1 && userSpecifyRev >= 0) {
2112
            // rev equals user specified
2113
            rev = (new Integer(userSpecifyRev)).toString();
2114
            // action should be INSERT
2115
            action = "INSERT";
2116
        }
2117
        //rev is greater the last revsion number and revInDataBase isn't -1
2118
        // it is a updated file
2119
        else if (userSpecifyRev > revInDataBase && revInDataBase >= 0) {
2120
            // rev equals user specified
2121
            rev = (new Integer(userSpecifyRev)).toString();
2122
            // action should be update
2123
            action = "UPDATE";
2124
        }
2125
        // local server has newer version, then notify the remote server
2126
        else if (userSpecifyRev < revInDataBase && revInDataBase > 0) {
2127
            throw new Exception("Local server: "
2128
                    + MetaCatUtil.getOption("server")
2129
                    + " has newer revision of doc: " + docid + "."
2130
                    + revInDataBase + ". Please notify it.");
2131
        }
2132
        //other situation
2133
        else {
2134

    
2135
            throw new Exception("The docid" + docid
2136
                    + "'s revision number couldn't be " + userSpecifyRev);
2137
        }
2138
        // Variable to store homeserver code
2139
        int serverCode = -2;
2140

    
2141
        // If server is not int the xml replication talbe, insert it into
2142
        // xml_replication table
2143
        //serverList.addToServerListIfItIsNot(homeServer);
2144
        insertServerIntoReplicationTable(homeServer);
2145
        // Get server code again
2146
        serverCode = getServerCode(homeServer);
2147

    
2148
        MetaCatUtil
2149
                .debugMessage("Document " + docid + "." + rev + " " + action
2150
                        + " into local" + " metacat with servercode: "
2151
                        + serverCode, 10);
2152

    
2153
        // insert into xml_nodes table
2154
        XMLReader parser = null;
2155
        try {
2156

    
2157
            parser = initializeParser(conn, action, docid, rev, user, groups,
2158
                    pub, serverCode, dtd, ruleBase, needValidation);
2159
            conn.setAutoCommit(false);
2160
            parser.parse(new InputSource(xml));
2161
            conn.commit();
2162
            conn.setAutoCommit(true);
2163
        } catch (Exception e) {
2164
            conn.rollback();
2165
            conn.setAutoCommit(true);
2166
            if (parser != null) {
2167
                ContentHandler handler = parser.getContentHandler();
2168
                if (handler instanceof Eml200SAXHandler) {
2169
                    Eml200SAXHandler eml = (Eml200SAXHandler) handler;
2170
                    eml.deleteInlineFiles();
2171
                }
2172
            }
2173
            throw e;
2174
        }
2175

    
2176
        // run write into access db base on relation table and access rule
2177
        try {
2178
            runRelationAndAccessHandler(accnum, user, groups, serverCode);
2179
        } catch (Exception ee) {
2180
            MetacatReplication.replErrorLog("Failed to " + "create access "
2181
                    + "rule for package: " + accnum + " because "
2182
                    + ee.getMessage());
2183
            MetaCatUtil.debugMessage("Failed to  " + "create access "
2184
                    + "rule for package: " + accnum + " because "
2185
                    + ee.getMessage(), 30);
2186
        }
2187
        //Force replication to other server
2188
        ForceReplicationHandler forceReplication = new ForceReplicationHandler(
2189
                accnum, action, true, notifyServer);
2190

    
2191
        return (accnum);
2192
    }
2193

    
2194
    /* Running write record to xml_relation and xml_access */
2195
    private static void runRelationAndAccessHandler(String accnumber,
2196
            String userName, String[] group, int servercode) throws Exception
2197
    {
2198
        DBConnection dbconn = null;
2199
        int serialNumber = -1;
2200
        PreparedStatement pstmt = null;
2201
        String documenttype = getDocTypeFromDBForCurrentDocument(accnumber);
2202
        try {
2203
            String packagedoctype = MetaCatUtil.getOption("packagedoctype");
2204
            Vector packagedoctypes = new Vector();
2205
            packagedoctypes = MetaCatUtil.getOptionList(packagedoctype);
2206
            String docIdWithoutRev = MetaCatUtil.getDocIdFromString(accnumber);
2207
            if (documenttype != null &&
2208
                    packagedoctypes.contains(documenttype)) {
2209
                dbconn = DBConnectionPool.getDBConnection(
2210
                        "DocumentImpl.runRelationAndAccessHandeler");
2211
                serialNumber = dbconn.getCheckOutSerialNumber();
2212
                dbconn.setAutoCommit(false);
2213
                // from the relations get the access file id for that package
2214
                String aclid = RelationHandler.getAccessFileID(docIdWithoutRev);
2215
                // if there are access file, write ACL for that package
2216
                if (aclid != null) {
2217
                    runAccessControlList(dbconn, aclid, userName, group,
2218
                            servercode);
2219
                }
2220
                dbconn.commit();
2221
                dbconn.setAutoCommit(true);
2222
            }
2223
            // if it is an access file
2224
            else if (documenttype != null
2225
                    && MetaCatUtil.getOptionList(
2226
                            MetaCatUtil.getOption("accessdoctype")).contains(
2227
                            documenttype)) {
2228
                dbconn = DBConnectionPool.getDBConnection(
2229
                        "DocumentImpl.runRelationAndAccessHandeler");
2230
                serialNumber = dbconn.getCheckOutSerialNumber();
2231
                dbconn.setAutoCommit(false);
2232
                // write ACL for the package
2233
                runAccessControlList(dbconn, docIdWithoutRev, userName, group,
2234
                        servercode);
2235
                dbconn.commit();
2236
                dbconn.setAutoCommit(true);
2237

    
2238
            }
2239

    
2240
        } catch (Exception e) {
2241
            if (dbconn != null) {
2242
                dbconn.rollback();
2243
                dbconn.setAutoCommit(true);
2244
            }
2245
            MetaCatUtil.debugMessage(
2246
                    "Error in DocumentImple.runRelationAndAccessHandler "
2247
                            + e.getMessage(), 30);
2248
            throw e;
2249
        } finally {
2250
            if (dbconn != null) {
2251
                DBConnectionPool.returnDBConnection(dbconn, serialNumber);
2252
            }
2253
        }
2254
    }
2255

    
2256
    // It runs in xmlIndex thread. It writes ACL for a package.
2257
    private static void runAccessControlList(DBConnection conn, String aclid,
2258
            String users, String[] group, int servercode) throws Exception
2259
    {
2260
        // read the access file from xml_nodes
2261
        // parse the access file and store the access info into xml_access
2262
        AccessControlList aclobj = new AccessControlList(conn, aclid, users,
2263
                group, servercode);
2264

    
2265
    }
2266

    
2267
    /* Method get document type from db */
2268
    private static String getDocTypeFromDBForCurrentDocument(String accnumber)
2269
            throws SQLException
2270
    {
2271
        String docoumentType = null;
2272
        String docid = null;
2273
        PreparedStatement pstate = null;
2274
        ResultSet rs = null;
2275
        String sql = "SELECT doctype FROM xml_documents where docid = ?";
2276
        DBConnection dbConnection = null;
2277
        int serialNumber = -1;
2278
        try {
2279
            //get rid of revision number
2280
            docid = MetaCatUtil.getDocIdFromString(accnumber);
2281
            dbConnection = DBConnectionPool.getDBConnection(
2282
                    "DocumentImpl.getDocTypeFromDBForCurrentDoc");
2283
            serialNumber = dbConnection.getCheckOutSerialNumber();
2284
            pstate = dbConnection.prepareStatement(sql);
2285
            //bind variable
2286
            pstate.setString(1, docid);
2287
            //excute query
2288
            pstate.execute();
2289
            //handle resultset
2290
            rs = pstate.getResultSet();
2291
            if (rs.next()) {
2292
                docoumentType = rs.getString(1);
2293
            }
2294
            rs.close();
2295
            pstate.close();
2296
        }//try
2297
        catch (SQLException e) {
2298
            MetaCatUtil.debugMessage("error in DocumentImpl."
2299
                    + "getDocTypeFromDBForCurrentDocument " + e.getMessage(),
2300
                    30);
2301
            throw e;
2302
        }//catch
2303
        finally {
2304
            pstate.close();
2305
            DBConnectionPool.returnDBConnection(dbConnection, serialNumber);
2306
        }//
2307
        MetaCatUtil.debugMessage("The current doctype from db is: "
2308
                + docoumentType, 35);
2309
        return docoumentType;
2310
    }
2311

    
2312
    /**
2313
     * Delete an XML file from the database (actually, just make it a revision
2314
     * in the xml_revisions table)
2315
     *
2316
     * @param docid
2317
     *            the ID of the document to be deleted from the database
2318
     */
2319
    public static void delete(String accnum, String user, String[] groups)
2320
            throws Exception
2321
    {
2322
        // OLD
2323
        //DocumentIdentifier id = new DocumentIdentifier(accnum);
2324
        //String docid = id.getIdentifier();
2325
        //String rev = id.getRev();
2326

    
2327
        // OLD
2328
        // Determine if the docid,rev are OK for DELETE
2329
        //AccessionNumber ac = new AccessionNumber(conn);
2330
        //docid = ac.generate(docid, rev, "DELETE");
2331
        DBConnection conn = null;
2332
        int serialNumber = -1;
2333
        PreparedStatement pstmt = null;
2334
        try {
2335
            //check out DBConnection
2336
            conn = DBConnectionPool.getDBConnection("DocumentImpl.delete");
2337
            serialNumber = conn.getCheckOutSerialNumber();
2338

    
2339
            // NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV
2340
            // IN IT
2341
            AccessionNumber ac = new AccessionNumber(accnum, "DELETE");
2342
            String docid = ac.getDocid();
2343
            String rev = ac.getRev();
2344

    
2345
            MetaCatUtil.debugMessage("Start deleting doc " + docid + "...", 20);
2346
            // check for 'write' permission for 'user' to delete this document
2347
            if (!hasAllPermission(user, groups, accnum)) { throw new Exception(
2348
                    "User " + user
2349
                    + " does not have permission to delete XML Document #"
2350
                    + accnum); }
2351

    
2352
            conn.setAutoCommit(false);
2353
            // Copy the record to the xml_revisions table
2354
            DocumentImpl.archiveDocRevision(conn, docid, user);
2355

    
2356
            // Now delete it from the xml_index table
2357
            boolean useXMLIndex = (new Boolean(MetaCatUtil
2358
                    .getOption("usexmlindex"))).booleanValue();
2359
            //if (useXMLIndex) {
2360
            pstmt = conn
2361
                    .prepareStatement("DELETE FROM xml_index WHERE docid = ?");
2362
            pstmt.setString(1, docid);
2363
            pstmt.execute();
2364
            pstmt.close();
2365
            conn.increaseUsageCount(1);
2366
            //}
2367

    
2368
            //stmt.execute("DELETE FROM xml_access WHERE docid = '" + docid +
2369
            // "'");
2370
            // Now delete it from xml_access table
2371
            pstmt = conn.prepareStatement(
2372
                    "DELETE FROM xml_access WHERE accessfileid = ?");
2373
            pstmt.setString(1, docid);
2374
            pstmt.execute();
2375
            pstmt.close();
2376
            conn.increaseUsageCount(1);
2377

    
2378
            pstmt = conn.prepareStatement(
2379
                    "DELETE FROM xml_access WHERE docid = ?");
2380
            pstmt.setString(1, docid);
2381
            pstmt.execute();
2382
            pstmt.close();
2383
            conn.increaseUsageCount(1);
2384

    
2385
            // Delete it from relation table
2386
            pstmt = conn.prepareStatement(
2387
                    "DELETE FROM xml_relation WHERE docid = ?");
2388
            //increase usage count
2389
            conn.increaseUsageCount(1);
2390
            pstmt.setString(1, docid);
2391
            pstmt.execute();
2392
            pstmt.close();
2393

    
2394
            // Delete it from xml_accesssubtree table
2395
            pstmt = conn.prepareStatement(
2396
                    "DELETE FROM xml_accesssubtree WHERE docid = ?");
2397
            //increase usage count
2398
            conn.increaseUsageCount(1);
2399
            pstmt.setString(1, docid);
2400
            pstmt.execute();
2401
            pstmt.close();
2402

    
2403
            // Delete it from xml_documents table
2404
            pstmt = conn.prepareStatement(
2405
                    "DELETE FROM xml_documents WHERE docid = ?");
2406
            pstmt.setString(1, docid);
2407
            pstmt.execute();
2408
            pstmt.close();
2409
            //Usaga count increase 1
2410
            conn.increaseUsageCount(1);
2411

    
2412
            conn.commit();
2413
            conn.setAutoCommit(true);
2414
        } catch (Exception e) {
2415
            MetaCatUtil.debugMessage("error in DocumentImpl.delete: "
2416
                    + e.getMessage(), 30);
2417
            throw e;
2418
        } finally {
2419

    
2420
            try {
2421
                // close preparedStatement
2422
                if (pstmt != null) {
2423
                    pstmt.close();
2424
                }
2425
            }
2426
            finally {
2427
                //check in DBonnection
2428
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2429
            }
2430
        }
2431
        //IF this is a package document:
2432
        //delete all of the relations that this document created.
2433
        //if the deleted document is a package document its relations should
2434
        //no longer be active if it has been deleted from the system.
2435
    }
2436

    
2437
    /**
2438
     * Check for "WRITE" permission on @docid for @user and/or @groups
2439
     * from DB connection
2440
     */
2441
    private static boolean hasWritePermission(String user, String[] groups,
2442
            String docid) throws SQLException, Exception
2443
    {
2444
        // Check for WRITE permission on @docid for @user and/or @groups
2445
        PermissionController controller = new PermissionController(docid);
2446
        return controller.hasPermission(user, groups,
2447
                AccessControlInterface.WRITESTRING);
2448
    }
2449

    
2450
    /**
2451
     * Check for "READ" permission base on docid, user and group
2452
     *
2453
     * @param docid, the document
2454
     * @param user, user name
2455
     * @param groups, user's group
2456
     */
2457
    public static boolean hasReadPermission(String user, String[] groups,
2458
            String docId) throws SQLException, Exception
2459
    {
2460
        // Check for READ permission on @docid for @user and/or @groups
2461
        PermissionController controller = new PermissionController(docId);
2462
        return controller.hasPermission(user, groups,
2463
                AccessControlInterface.READSTRING);
2464
    }
2465

    
2466
    /**
2467
     * Check for "WRITE" permission on @docid for @user and/or @groups
2468
     * from DB connection
2469
     */
2470
    private static boolean hasAllPermission(String user, String[] groups,
2471
            String docid) throws SQLException, Exception
2472
    {
2473
        // Check for WRITE permission on @docid for @user and/or @groups
2474
        PermissionController controller = new PermissionController(docid);
2475
        return controller.hasPermission(user, groups,
2476
                AccessControlInterface.ALLSTRING);
2477
    }
2478

    
2479
    /**
2480
     * Set up the parser handlers for writing the document to the database
2481
     */
2482
    private static XMLReader initializeParser(DBConnection dbconn,
2483
            String action, String docid, String rev, String user,
2484
            String[] groups, String pub, int serverCode, Reader dtd,
2485
            String ruleBase, boolean needValidation) throws Exception
2486
    {
2487
        XMLReader parser = null;
2488
        try {
2489
            // handler
2490
            ContentHandler chandler;
2491
            EntityResolver eresolver;
2492
            DTDHandler dtdhandler;
2493
            // Get an instance of the parser
2494
            String parserName = MetaCatUtil.getOption("saxparser");
2495
            parser = XMLReaderFactory.createXMLReader(parserName);
2496
            if (ruleBase != null && ruleBase.equals(EML200)) {
2497
                MetaCatUtil.debugMessage("eml 2.0.0 parser", 20);
2498
                chandler = new Eml200SAXHandler(dbconn, action, docid, rev,
2499
                        user, groups, pub, serverCode);
2500
                parser.setContentHandler((ContentHandler) chandler);
2501
                parser.setErrorHandler((ErrorHandler) chandler);
2502
                parser.setProperty(DECLARATIONHANDLERPROPERTY, chandler);
2503
                parser.setProperty(LEXICALPROPERTY, chandler);
2504
                // turn on schema validation feature
2505
                parser.setFeature(VALIDATIONFEATURE, true);
2506
                parser.setFeature(NAMESPACEFEATURE, true);
2507
                //parser.setFeature(NAMESPACEPREFIXESFEATURE, true);
2508
                parser.setFeature(SCHEMAVALIDATIONFEATURE, true);
2509
                // From DB to find the register external schema location
2510
                String externalSchemaLocation = null;
2511
                SchemaLocationResolver resolver = new SchemaLocationResolver();
2512
                externalSchemaLocation = resolver
2513
                        .getNameSpaceAndLocationString();
2514
                // Set external schemalocation.
2515
                if (externalSchemaLocation != null
2516
                        && !(externalSchemaLocation.trim()).equals("")) {
2517
                    parser.setProperty(EXTERNALSCHEMALOCATIONPROPERTY,
2518
                            externalSchemaLocation);
2519
                }
2520
            } else if (ruleBase != null && ruleBase.equals(EML210)) {
2521
                MetaCatUtil.debugMessage("eml 2.1.0 parser", 20);
2522
                chandler = new Eml210SAXHandler(dbconn, action, docid, rev,
2523
                        user, groups, pub, serverCode);
2524
                parser.setContentHandler((ContentHandler) chandler);
2525
                parser.setErrorHandler((ErrorHandler) chandler);
2526
                parser.setProperty(DECLARATIONHANDLERPROPERTY, chandler);
2527
                parser.setProperty(LEXICALPROPERTY, chandler);
2528
                // turn on schema validation feature
2529
                parser.setFeature(VALIDATIONFEATURE, true);
2530
                parser.setFeature(NAMESPACEFEATURE, true);
2531
                //parser.setFeature(NAMESPACEPREFIXESFEATURE, true);
2532
                parser.setFeature(SCHEMAVALIDATIONFEATURE, true);
2533
                // From DB to find the register external schema location
2534
                String externalSchemaLocation = null;
2535
                SchemaLocationResolver resolver = new SchemaLocationResolver();
2536
                externalSchemaLocation = resolver
2537
                        .getNameSpaceAndLocationString();
2538
                // Set external schemalocation.
2539
                if (externalSchemaLocation != null
2540
                        && !(externalSchemaLocation.trim()).equals("")) {
2541
                    parser.setProperty(EXTERNALSCHEMALOCATIONPROPERTY,
2542
                            externalSchemaLocation);
2543
                }
2544
            } else {
2545
                //create a DBSAXHandler object which has the revision
2546
                // specification
2547
                chandler = new DBSAXHandler(dbconn, action, docid, rev, user,
2548
                        groups, pub, serverCode);
2549
                parser.setContentHandler((ContentHandler) chandler);
2550
                parser.setErrorHandler((ErrorHandler) chandler);
2551
                parser.setProperty(DECLARATIONHANDLERPROPERTY, chandler);
2552
                parser.setProperty(LEXICALPROPERTY, chandler);
2553

    
2554
                if (ruleBase != null && ruleBase.equals(SCHEMA)
2555
                        && needValidation) {
2556
                    MetaCatUtil.debugMessage("General schema parser", 20);
2557
                    // turn on schema validation feature
2558
                    parser.setFeature(VALIDATIONFEATURE, true);
2559
                    parser.setFeature(NAMESPACEFEATURE, true);
2560
                    //parser.setFeature(NAMESPACEPREFIXESFEATURE, true);
2561
                    parser.setFeature(SCHEMAVALIDATIONFEATURE, true);
2562
                    // From DB to find the register external schema location
2563
                    String externalSchemaLocation = null;
2564
                    SchemaLocationResolver resolver =
2565
                        new SchemaLocationResolver();
2566
                    externalSchemaLocation = resolver
2567
                            .getNameSpaceAndLocationString();
2568
                    // Set external schemalocation.
2569
                    if (externalSchemaLocation != null
2570
                            && !(externalSchemaLocation.trim()).equals("")) {
2571
                        parser.setProperty(EXTERNALSCHEMALOCATIONPROPERTY,
2572
                                externalSchemaLocation);
2573
                    }
2574

    
2575
                } else if (ruleBase != null && ruleBase.equals(DTD)
2576
                        && needValidation) {
2577
                    MetaCatUtil.debugMessage("dtd parser", 20);
2578
                    // turn on dtd validaton feature
2579
                    parser.setFeature(VALIDATIONFEATURE, true);
2580
                    eresolver = new DBEntityResolver(dbconn,
2581
                            (DBSAXHandler) chandler, dtd);
2582
                    dtdhandler = new DBDTDHandler(dbconn);
2583
                    parser.setEntityResolver((EntityResolver) eresolver);
2584
                    parser.setDTDHandler((DTDHandler) dtdhandler);
2585
                } else {
2586
                    MetaCatUtil.debugMessage("other parser", 20);
2587
                    // non validation
2588
                    parser.setFeature(VALIDATIONFEATURE, false);
2589
                    eresolver = new DBEntityResolver(dbconn,
2590
                            (DBSAXHandler) chandler, dtd);
2591
                    dtdhandler = new DBDTDHandler(dbconn);
2592
                    parser.setEntityResolver((EntityResolver) eresolver);
2593
                    parser.setDTDHandler((DTDHandler) dtdhandler);
2594
                }
2595
            }//else
2596
        } catch (Exception e) {
2597
            throw e;
2598
        }
2599
        return parser;
2600
    }
2601

    
2602
    /**
2603
     * Save a document entry in the xml_revisions table Connection use as a
2604
     * paramter is in order to rollback feature
2605
     */
2606
    private static void archiveDocRevision(DBConnection dbconn, String docid,
2607
            String user)
2608
    {
2609
        String sysdate = dbAdapter.getDateTimeFunction();
2610
        //DBConnection conn = null;
2611
        //int serialNumber = -1;
2612
        PreparedStatement pstmt = null;
2613

    
2614
        // create a record in xml_revisions table
2615
        // for that document as selected from xml_documents
2616

    
2617
        try {
2618
            //check out DBConnection
2619
            /*
2620
             * conn=DBConnectionPool.
2621
             * getDBConnection("DocumentImpl.archiveDocRevision");
2622
             * serialNumber=conn.getCheckOutSerialNumber();
2623
             */
2624
            pstmt = dbconn.prepareStatement("INSERT INTO xml_revisions "
2625
                    + "(docid, rootnodeid, docname, doctype, "
2626
                    + "user_owner, user_updated, date_created, date_updated, "
2627
                    + "server_location, rev, public_access, catalog_id) "
2628
                    + "SELECT ?, rootnodeid, docname, doctype, "
2629
                    + "user_owner, ?, " + sysdate + ", " + sysdate + ", "
2630
                    + "server_location, rev, public_access, catalog_id "
2631
                    + "FROM xml_documents " + "WHERE docid = ?");
2632
            // Increase dbconnection usage count
2633
            dbconn.increaseUsageCount(1);
2634
            // Bind the values to the query and execute it
2635
            pstmt.setString(1, docid);
2636
            pstmt.setString(2, user);
2637
            pstmt.setString(3, docid);
2638
            pstmt.execute();
2639
            pstmt.close();
2640
        } catch (SQLException e) {
2641
            MetaCatUtil.debugMessage(
2642
                    "Error in DocumentImpl.archiveDocRevision : "
2643
                            + e.getMessage(), 30);
2644
        } finally {
2645
            try {
2646
                pstmt.close();
2647
            } catch (SQLException ee) {
2648
                MetaCatUtil.debugMessage(
2649
                        "Error in DocumnetImpl.archiveDocRevision: "
2650
                                + ee.getMessage(), 50);
2651
            }
2652
        }
2653
    }
2654

    
2655
    /** Save a document entry in the xml_revisions table */
2656
    private static void archiveDocRevision(String docid, String user)
2657
    {
2658
        String sysdate = dbAdapter.getDateTimeFunction();
2659
        DBConnection conn = null;
2660
        int serialNumber = -1;
2661
        PreparedStatement pstmt = null;
2662

    
2663
        // create a record in xml_revisions table
2664
        // for that document as selected from xml_documents
2665

    
2666
        try {
2667
            //check out DBConnection
2668
            conn = DBConnectionPool
2669
                    .getDBConnection("DocumentImpl.archiveDocRevision");
2670
            serialNumber = conn.getCheckOutSerialNumber();
2671
            pstmt = conn.prepareStatement("INSERT INTO xml_revisions "
2672
                    + "(docid, rootnodeid, docname, doctype, "
2673
                    + "user_owner, user_updated, date_created, date_updated, "
2674
                    + "server_location, rev, public_access, catalog_id) "
2675
                    + "SELECT ?, rootnodeid, docname, doctype, "
2676
                    + "user_owner, ?, " + sysdate + ", " + sysdate + ", "
2677
                    + "server_location, rev, public_access, catalog_id "
2678
                    + "FROM xml_documents " + "WHERE docid = ?");
2679
            // Bind the values to the query and execute it
2680
            pstmt.setString(1, docid);
2681
            pstmt.setString(2, user);
2682
            pstmt.setString(3, docid);
2683
            pstmt.execute();
2684
            pstmt.close();
2685
        } catch (SQLException e) {
2686
            MetaCatUtil.debugMessage(
2687
                    "Error in DocumentImpl.archiveDocRevision : "
2688
                            + e.getMessage(), 30);
2689
        } finally {
2690
            try {
2691
                pstmt.close();
2692
            } catch (SQLException ee) {
2693
                MetaCatUtil.debugMessage(
2694
                        "Error in DocumnetImpl.archiveDocRevision: "
2695
                                + ee.getMessage(), 50);
2696
            } finally {
2697
                //check in DBConnection
2698
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2699
            }
2700
        }
2701
    }
2702

    
2703
    /**
2704
     * delete a entry in xml_table for given docid
2705
     *
2706
     * @param docId,
2707
     *            the id of the document need to be delete
2708
     */
2709
    private static void deleteXMLDocuments(String docId) throws SQLException
2710
    {
2711
        DBConnection conn = null;
2712
        int serialNumber = -1;
2713
        PreparedStatement pStmt = null;
2714
        try {
2715
            //check out DBConnection
2716
            conn = DBConnectionPool
2717
                    .getDBConnection("DocumentImpl.deleteXMLDocuments");
2718
            serialNumber = conn.getCheckOutSerialNumber();
2719
            //delete a record
2720
            pStmt = conn.prepareStatement(
2721
                    "DELETE FROM xml_documents WHERE docid = '" + docId + "'");
2722
            pStmt.execute();
2723
        } finally {
2724
            try {
2725
                pStmt.close();
2726
            } catch (SQLException e) {
2727
                MetaCatUtil.debugMessage(
2728
                        "error in DocumentImpl.deleteXMLDocuments: "
2729
                                + e.getMessage(), 50);
2730
            } finally {
2731
                //return back DBconnection
2732
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2733
            }
2734
        }
2735
    }
2736

    
2737
    /**
2738
     * Get last revision number from database for a docid If couldn't find an
2739
     * entry, -1 will return The return value is integer because we want compare
2740
     * it to there new one
2741
     *
2742
     * @param docid
2743
     *            <sitecode>. <uniqueid>part of Accession Number
2744
     */
2745
    public static int getLatestRevisionNumber(String docId) throws SQLException
2746
    {
2747
        int rev = 1;
2748
        PreparedStatement pStmt = null;
2749
        DBConnection dbConn = null;
2750
        int serialNumber = -1;
2751
        // get rid of rev
2752
        docId = MetaCatUtil.getDocIdFromString(docId);
2753
        try {
2754
            //check out DBConnection
2755
            dbConn = DBConnectionPool
2756
                    .getDBConnection("DocumentImpl.getLatestRevisionNumber");
2757
            serialNumber = dbConn.getCheckOutSerialNumber();
2758

    
2759
            pStmt = dbConn
2760
                    .prepareStatement("SELECT rev FROM xml_documents WHERE docid='"
2761
                            + docId + "'");
2762
            pStmt.execute();
2763

    
2764
            ResultSet rs = pStmt.getResultSet();
2765
            boolean hasRow = rs.next();
2766
            if (hasRow) {
2767
                rev = rs.getInt(1);
2768
                pStmt.close();
2769
            } else {
2770
                rev = -1;
2771
                pStmt.close();
2772
            }
2773
        }//try
2774
        finally {
2775
            try {
2776
                pStmt.close();
2777
            } catch (Exception ee) {
2778
                MetaCatUtil.debugMessage("Error in DocumentImpl."
2779
                        + "getLatestRevisionNumber: " + ee.getMessage(), 50);
2780
            } finally {
2781
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2782
            }
2783
        }//finally
2784

    
2785
        return rev;
2786
    }//getLatestRevisionNumber
2787

    
2788
    /**
2789
     * Get server location form database for a accNum
2790
     *
2791
     * @param accum
2792
     *            <sitecode>. <uniqueid>. <rev>
2793
     */
2794
    private static int getServerLocationNumber(String accNum)
2795
            throws SQLException
2796
    {
2797
        //get rid of revNum part
2798
        String docId = MetaCatUtil.getDocIdFromString(accNum);
2799
        PreparedStatement pStmt = null;
2800
        int serverLocation = 1;
2801
        DBConnection conn = null;
2802
        int serialNumber = -1;
2803

    
2804
        try {
2805
            //check out DBConnection
2806
            conn = DBConnectionPool
2807
                    .getDBConnection("DocumentImpl.getServerLocationNumber");
2808
            serialNumber = conn.getCheckOutSerialNumber();
2809

    
2810
            pStmt = conn
2811
                    .prepareStatement("SELECT server_location FROM xml_documents WHERE docid='"
2812
                            + docId + "'");
2813
            pStmt.execute();
2814

    
2815
            ResultSet rs = pStmt.getResultSet();
2816
            boolean hasRow = rs.next();
2817
            //if there is entry in xml_documents, get the serverlocation
2818
            if (hasRow) {
2819
                serverLocation = rs.getInt(1);
2820
                pStmt.close();
2821
            } else {
2822
                //if htere is no entry in xml_documents, we consider it is new
2823
                // document
2824
                //the server location is local host and value is 1
2825
                serverLocation = 1;
2826
                pStmt.close();
2827
            }
2828
        }//try
2829
        finally {
2830
            try {
2831
                pStmt.close();
2832
            }//try
2833
            catch (Exception ee) {
2834
                MetaCatUtil.debugMessage(
2835
                        "Error in DocumentImpl.getServerLocationNu(): "
2836
                                + ee.getMessage(), 50);
2837
            }//catch
2838
            finally {
2839
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2840
            }//finally
2841
        }//finally
2842

    
2843
        return serverLocation;
2844
    }
2845

    
2846
    /**
2847
     * Given a server name, return its servercode in xml_replication table. If
2848
     * no server is found, -1 will return
2849
     *
2850
     * @param serverName,
2851
     */
2852
    private static int getServerCode(String serverName)
2853
    {
2854
        PreparedStatement pStmt = null;
2855
        int serverLocation = -2;
2856
        DBConnection dbConn = null;
2857
        int serialNumber = -1;
2858
        //MetaCatUtil util = new MetaCatUtil();
2859

    
2860
        //we should consider about local host too
2861
        if (serverName.equals(MetaCatUtil.getLocalReplicationServerName())) {
2862
            serverLocation = 1;
2863
            return serverLocation;
2864
        }
2865

    
2866
        try {
2867
            //check xml_replication table
2868
            //dbConn=util.openDBConnection();
2869
            //check out DBConnection
2870
            dbConn = DBConnectionPool
2871
                    .getDBConnection("DocumentImpl.getServerCode");
2872
            serialNumber = dbConn.getCheckOutSerialNumber();
2873
            pStmt = dbConn
2874
                    .prepareStatement("SELECT serverid FROM xml_replication WHERE server='"
2875
                            + serverName + "'");
2876
            pStmt.execute();
2877

    
2878
            ResultSet rs = pStmt.getResultSet();
2879
            boolean hasRow = rs.next();
2880
            //if there is entry in xml_replication, get the serverid
2881
            if (hasRow) {
2882
                serverLocation = rs.getInt(1);
2883
                pStmt.close();
2884
            } else {
2885
                // if htere is no entry in xml_replication, -1 will return
2886
                serverLocation = -1;
2887
                pStmt.close();
2888
            }
2889
        } catch (Exception e) {
2890
            MetaCatUtil.debugMessage("Error in DocumentImpl.getServerCode(): "
2891
                    + e.getMessage(), 30);
2892
        } finally {
2893
            try {
2894
                pStmt.close();
2895
            } catch (Exception ee) {
2896
                MetaCatUtil.debugMessage(
2897
                        "Error in DocumentImpl.getServerCode(): "
2898
                                + ee.getMessage(), 50);
2899
            } finally {
2900
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2901
            }
2902
        }
2903

    
2904
        return serverLocation;
2905
    }
2906

    
2907
    /**
2908
     * Insert a server into xml_replcation table
2909
     *
2910
     * @param server,
2911
     *            the name of server
2912
     */
2913
    private static synchronized void insertServerIntoReplicationTable(
2914
            String server)
2915
    {
2916
        PreparedStatement pStmt = null;
2917
        DBConnection dbConn = null;
2918
        int serialNumber = -1;
2919

    
2920
        // Initial value for the server
2921
        int replicate = 0;
2922
        int dataReplicate = 0;
2923
        int hub = 0;
2924

    
2925
        try {
2926
            // Get DBConnection
2927
            dbConn = DBConnectionPool
2928
                    .getDBConnection("DocumentImpl.insertServIntoReplicationTable");
2929
            serialNumber = dbConn.getCheckOutSerialNumber();
2930

    
2931
            // Compare the server to dabase
2932
            pStmt = dbConn
2933
                    .prepareStatement("SELECT serverid FROM xml_replication WHERE server='"
2934
                            + server + "'");
2935
            pStmt.execute();
2936
            ResultSet rs = pStmt.getResultSet();
2937
            boolean hasRow = rs.next();
2938
            // Close preparedstatement and result set
2939
            pStmt.close();
2940
            rs.close();
2941

    
2942
            // If the server is not in the table, and server is not local host,
2943
            // insert it
2944
            if (!hasRow
2945
                    && !server.equals(MetaCatUtil
2946
                            .getLocalReplicationServerName())) {
2947
                // Set auto commit false
2948
                dbConn.setAutoCommit(false);
2949
                /*
2950
                 * pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
2951
                 * "(server, last_checked, replicate, datareplicate, hub) " +
2952
                 * "VALUES ('" + server + "', to_date(" + "'01/01/00',
2953
                 * 'MM/DD/YY'), '" + replicate +"', '"+dataReplicate+"','"+ hub +
2954
                 * "')");
2955
                 */
2956
                pStmt = dbConn
2957
                        .prepareStatement("INSERT INTO xml_replication "
2958
                                + "(server, last_checked, replicate, datareplicate, hub) "
2959
                                + "VALUES ('" + server + "', "
2960
                                + dbAdapter.toDate("01/01/1980", "MM/DD/YYYY")
2961
                                + ", '" + replicate + "', '" + dataReplicate
2962
                                + "','" + hub + "')");
2963

    
2964
                pStmt.execute();
2965
                dbConn.commit();
2966
                // Increase usage number
2967
                dbConn.increaseUsageCount(1);
2968
                pStmt.close();
2969

    
2970
            }
2971
        }//try
2972
        catch (Exception e) {
2973
            MetaCatUtil.debugMessage(
2974
                    "Error in DocumentImpl.insertServerIntoRepli(): "
2975
                            + e.getMessage(), 30);
2976
        }//catch
2977
        finally {
2978

    
2979
            try {
2980
                // Set auto commit true
2981
                dbConn.setAutoCommit(true);
2982
                pStmt.close();
2983

    
2984
            }//try
2985
            catch (Exception ee) {
2986
                MetaCatUtil.debugMessage(
2987
                        "Error in DocumentImpl.insetServerIntoRepl(): "
2988
                                + ee.getMessage(), 50);
2989
            }//catch
2990
            finally {
2991
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2992
            }
2993

    
2994
        }//finally
2995

    
2996
    }
2997

    
2998
    /**
2999
     * the main routine used to test the DBWriter utility.
3000
     * <p>
3001
     * Usage: java DocumentImpl <-f filename -a action -d docid>
3002
     *
3003
     * @param filename
3004
     *            the filename to be loaded into the database
3005
     * @param action
3006
     *            the action to perform (READ, INSERT, UPDATE, DELETE)
3007
     * @param docid
3008
     *            the id of the document to process
3009
     */
3010
    static public void main(String[] args)
3011
    {
3012
        DBConnection dbconn = null;
3013
        int serialNumber = -1;
3014
        try {
3015
            String filename = null;
3016
            String dtdfilename = null;
3017
            String action = null;
3018
            String docid = null;
3019
            boolean showRuntime = false;
3020
            boolean useOldReadAlgorithm = false;
3021

    
3022
            // Parse the command line arguments
3023
            for (int i = 0; i < args.length; ++i) {
3024
                if (args[i].equals("-f")) {
3025
                    filename = args[++i];
3026
                } else if (args[i].equals("-r")) {
3027
                    dtdfilename = args[++i];
3028
                } else if (args[i].equals("-a")) {
3029
                    action = args[++i];
3030
                } else if (args[i].equals("-d")) {
3031
                    docid = args[++i];
3032
                } else if (args[i].equals("-t")) {
3033
                    showRuntime = true;
3034
                } else if (args[i].equals("-old")) {
3035
                    useOldReadAlgorithm = true;
3036
                } else {
3037
                    System.err.println("   args[" + i + "] '" + args[i]
3038
                            + "' ignored.");
3039
                }
3040
            }
3041

    
3042
            // Check if the required arguments are provided
3043
            boolean argsAreValid = false;
3044
            if (action != null) {
3045
                if (action.equals("INSERT")) {
3046
                    if (filename != null) {
3047
                        argsAreValid = true;
3048
                    }
3049
                } else if (action.equals("UPDATE")) {
3050
                    if ((filename != null) && (docid != null)) {
3051
                        argsAreValid = true;
3052
                    }
3053
                } else if (action.equals("DELETE")) {
3054
                    if (docid != null) {
3055
                        argsAreValid = true;
3056
                    }
3057
                } else if (action.equals("READ")) {
3058
                    if (docid != null) {
3059
                        argsAreValid = true;
3060
                    }
3061
                }
3062
            }
3063

    
3064
            // Print usage message if the arguments are not valid
3065
            if (!argsAreValid) {
3066
                System.err.println("Wrong number of arguments!!!");
3067
                System.err
3068
                        .println("USAGE: java DocumentImpl [-t] <-a INSERT> [-d docid] <-f filename> "
3069
                                + "[-r dtdfilename]");
3070
                System.err
3071
                        .println("   OR: java DocumentImpl [-t] <-a UPDATE -d docid -f filename> "
3072
                                + "[-r dtdfilename]");
3073
                System.err
3074
                        .println("   OR: java DocumentImpl [-t] <-a DELETE -d docid>");
3075
                System.err
3076
                        .println("   OR: java DocumentImpl [-t] [-old] <-a READ -d docid>");
3077
                return;
3078
            }
3079

    
3080
            // Time the request if asked for
3081
            double startTime = System.currentTimeMillis();
3082

    
3083
            // Open a connection to the database
3084
            MetaCatUtil util = new MetaCatUtil();
3085

    
3086
            dbconn = DBConnectionPool.getDBConnection("DocumentImpl.main");
3087
            serialNumber = dbconn.getCheckOutSerialNumber();
3088

    
3089
            double connTime = System.currentTimeMillis();
3090
            // Execute the action requested (READ, INSERT, UPDATE, DELETE)
3091
            if (action.equals("READ")) {
3092
                DocumentImpl xmldoc = new DocumentImpl(docid);
3093
                if (useOldReadAlgorithm) {
3094
                    System.out.println(xmldoc.readUsingSlowAlgorithm());
3095
                } else {
3096
                    xmldoc.toXml(new PrintWriter(System.out), null, null, true);
3097
                }
3098
            } else if (action.equals("DELETE")) {
3099
                DocumentImpl.delete(docid, null, null);
3100
                System.out.println("Document deleted: " + docid);
3101
            } else {
3102
                /*
3103
                 * String newdocid = DocumentImpl.write(dbconn, filename, null,
3104
                 * dtdfilename, action, docid, null, null); if ((docid != null) &&
3105
                 * (!docid.equals(newdocid))) { if (action.equals("INSERT")) {
3106
                 * System.out.println("New document ID generated!!! "); } else
3107
                 * if (action.equals("UPDATE")) { System.out.println("ERROR:
3108
                 * Couldn't update document!!! "); } } else if ((docid == null) &&
3109
                 * (action.equals("UPDATE"))) { System.out.println("ERROR:
3110
                 * Couldn't update document!!! "); }
3111
                 * System.out.println("Document processing finished for: " +
3112
                 * filename + " (" + newdocid + ")");
3113
                 */
3114
            }
3115

    
3116
            double stopTime = System.currentTimeMillis();
3117
            double dbOpenTime = (connTime - startTime) / 1000;
3118
            double insertTime = (stopTime - connTime) / 1000;
3119
            double executionTime = (stopTime - startTime) / 1000;
3120
            if (showRuntime) {
3121
                System.out.println("\n\nTotal Execution time was: "
3122
                        + executionTime + " seconds.");
3123
                System.out.println("Time to open DB connection was: "
3124
                        + dbOpenTime + " seconds.");
3125
                System.out.println("Time to insert document was: " + insertTime
3126
                        + " seconds.");
3127
            }
3128
            dbconn.close();
3129
        } catch (McdbException me) {
3130
            me.toXml(new PrintWriter(System.err));
3131
        } catch (AccessionNumberException ane) {
3132
            System.out.println(ane.getMessage());
3133
        } catch (Exception e) {
3134
            System.err.println("EXCEPTION HANDLING REQUIRED");
3135
            System.err.println(e.getMessage());
3136
            e.printStackTrace(System.err);
3137
        } finally {
3138
            // Return db connection
3139
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
3140
        }
3141
    }
3142
}
(31-31/63)