Project

General

Profile

« Previous | Next » 

Revision 2249

Added by Matt Jones over 19 years ago

Reformatted code for readability. It was crazy. Still has problems, but its
better. Will be working on some new methods on monday.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
60 60

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

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

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

  
101
  public static final String DOCNAME = "docname";
102
  public static final String PUBLICID = "publicid";
103
  public static final String SYSTEMID = "systemid";
104
  static final int ALL = 1;
105
  static final int WRITE = 2;
106
  static final int READ = 4;
107
  private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
144
            // Look up the document information
145
            getDocumentInfo(docid);
108 146

  
109
  private DBConnection connection = null;
110
  private String docid = null;
111
  private String updatedVersion=null;
112
  private String docname = null;
113
  private String doctype = null;
114
  private String validateType = null; //base on dtd or schema
115
// DOCTITLE attr cleared from the db
116
//  private String doctitle = null;
117
  private String createdate = null;
118
  private String updatedate = null;
119
  private String system_id = null;
120
  private String userowner = null;
121
  private String userupdated = null;
122
  private int rev;
123
  private int serverlocation;
124
  private String docHomeServer;
125
  private String publicaccess;
126
  private long rootnodeid;
127
  private ElementNode rootNode = null;
128
  private TreeSet nodeRecordList = null;
129
  //private static
130
  //ReplicationServerList serverList = new ReplicationServerList();
147
            if (readNodes) {
148
                // Download all of the document nodes using a single SQL query
149
                // The sort order of the records is determined by the
150
                // NodeComparator
151
                // class, and needs to represent a depth-first traversal for the
152
                // toXml() method to work properly
153
                nodeRecordList = getNodeRecordList(rootnodeid);
154
            }
131 155

  
132
  /**
133
   * Constructor used to create a document and read the document information
134
   * from the database.  If readNodes is false, then the node data is not
135
   * read at this time, but is deferred until it is needed (such as when a
136
   * call to toXml() is made).
137
   *
138
   * @param conn the database connection from which to read the document
139
   * @param docid the identifier of the document to be created
140
   * @param readNodes flag indicating whether the xmlnodes should be read
141
   */
142
  public DocumentImpl(String docid, boolean readNodes)
143
         throws McdbException
144
  {
145
    try {
146
      //this.conn = conn;
147
      this.docid = docid;
156
        } catch (McdbException ex) {
157
            throw ex;
158
        } catch (Throwable t) {
159
            throw new McdbException("Error reading document from "
160
                    + "DocumentImpl.DocumentImpl: " + docid);
161
        }
162
    }
148 163

  
149
      // Look up the document information
150
      getDocumentInfo(docid);
151

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

  
159
      }
160

  
161
    } catch (McdbException ex) {
162
      throw ex;
163
    } catch (Throwable t) {
164
      throw new McdbException("Error reading document from " +
165
                              "DocumentImpl.DocumentImpl: " + docid);
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);
166 176
    }
167
  }
168 177

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

  
181

  
182

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

  
227
  /**
228
   * This method will be call in handleUploadRequest in MetacatServlet class
229
   */
230
  public static void registerDocument(
231
                     String docname, String doctype, String accnum, String user)
232
                     throws SQLException, AccessionNumberException, Exception
233
  {
234

  
235
    try
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
236 221
    {
237
       // get server location for this doc
238
      int serverLocation=getServerLocationNumber(accnum);
239
      registerDocument(docname, doctype,accnum, user, serverLocation);
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);
240 229
    }
241
    catch (Exception e)
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) throws SQLException,
236
            AccessionNumberException, Exception
242 237
    {
243
      throw e;
238
        try {
239
            // get server location for this doc
240
            int serverLocation = getServerLocationNumber(accnum);
241
            registerDocument(docname, doctype, accnum, user, serverLocation);
242
        } catch (Exception e) {
243
            throw e;
244
        }
244 245
    }
245 246

  
247
    /**
248
     * Register a document that resides on the filesystem with the database.
249
     * (ie, just an entry in xml_documents, nothing in xml_nodes). Creates a
250
     * reference to a filesystem document (used for non-xml data files). This
251
     * class only be called in MetaCatServerlet.
252
     * 
253
     * @param conn
254
     *            the JDBC Connection to which all information is written
255
     * @param docname -
256
     *            the name of DTD, i.e. the name immediately following the
257
     *            DOCTYPE keyword ( should be the root element name ) or the
258
     *            root element name if no DOCTYPE declaration provided (Oracle's
259
     *            and IBM parsers are not aware if it is not the root element
260
     *            name)
261
     * @param doctype -
262
     *            Public ID of the DTD, i.e. the name immediately following the
263
     *            PUBLIC keyword in DOCTYPE declaration or the docname if no
264
     *            Public ID provided or null if no DOCTYPE declaration provided
265
     * @param accnum
266
     *            the accession number to use for the INSERT OR UPDATE, which
267
     *            includes a revision number for this revision of the document
268
     *            (e.g., knb.1.1)
269
     * @param user
270
     *            the user that owns the document
271
     * @param serverCode
272
     *            the serverid from xml_replication on which this document
273
     *            resides.
274
     */
275
    public static void registerDocument(String docname, String doctype,
276
            String accnum, String user, int serverCode) throws SQLException,
277
            AccessionNumberException, Exception
278
    {
279
        DBConnection dbconn = null;
280
        int serialNumber = -1;
281
        PreparedStatement pstmt = null;
282
        //MetaCatUtil util = new MetaCatUtil();
283
        AccessionNumber ac;
284
        String action = null;
285
        try {
286
            //dbconn = util.openDBConnection();
287
            //check out DBConnection
288
            dbconn = DBConnectionPool
289
                    .getDBConnection("DocumentImpl.registerDocument");
290
            serialNumber = dbconn.getCheckOutSerialNumber();
291
            String docIdWithoutRev = MetaCatUtil.getDocIdFromString(accnum);
292
            int userSpecifyRev = MetaCatUtil.getVersionFromString(accnum);
293
            int revInDataBase = getLatestRevisionNumber(docIdWithoutRev);
294
            //revIndataBase=-1, there is no record in xml_documents table
295
            //the data file is a new one, inert it into table
296
            //user specified rev should be great than 0
297
            if (revInDataBase == -1 && userSpecifyRev > 0) {
298
                ac = new AccessionNumber(accnum, "insert");
299
                action = "insert";
300
            }
301
            //rev is greater the last revsion number and revInDataBase isn't -1
302
            // it is a updated data file
303
            else if (userSpecifyRev > revInDataBase && revInDataBase > 0) {
246 304

  
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).
251
   * Creates a reference to a filesystem document (used for non-xml data files).
252
   * This class only be called in MetaCatServerlet.
253
   * @param conn the JDBC Connection to which all information is written
254
   * @param docname - the name of DTD, i.e. the name immediately following
255
   *        the DOCTYPE keyword ( should be the root element name ) or
256
   *        the root element name if no DOCTYPE declaration provided
257
   *        (Oracle's and IBM parsers are not aware if it is not the
258
   *        root element name)
259
   * @param doctype - Public ID of the DTD, i.e. the name immediately
260
   *                  following the PUBLIC keyword in DOCTYPE declaration or
261
   *                  the docname if no Public ID provided or
262
   *                  null if no DOCTYPE declaration provided
263
   * @param accnum the accession number to use for the INSERT OR UPDATE, which
264
   *               includes a revision number for this revision of the document
265
   *               (e.g., knb.1.1)
266
   * @param user the user that owns the document
267
   * @param serverCode the serverid from xml_replication on which this document
268
   *        resides.
269
   */
270
  public static void registerDocument(
271
                     String docname, String doctype, String accnum,
272
                     String user, int serverCode)
273
                     throws SQLException, AccessionNumberException, Exception
274
  {
275
    DBConnection dbconn = null;
276
    int serialNumber = -1;
277
    PreparedStatement pstmt = null;
278
    //MetaCatUtil util = new MetaCatUtil();
279
    AccessionNumber ac;
280
    String action = null;
281
    try {
282
      //dbconn = util.openDBConnection();
283
      //check out DBConnection
284
      dbconn=DBConnectionPool.
285
                    getDBConnection("DocumentImpl.registerDocument");
286
      serialNumber=dbconn.getCheckOutSerialNumber();
287
      String docIdWithoutRev=MetaCatUtil.getDocIdFromString(accnum);
288
      int userSpecifyRev=MetaCatUtil.getVersionFromString(accnum);
289
      int revInDataBase=getLatestRevisionNumber(docIdWithoutRev);
290
      //revIndataBase=-1, there is no record in xml_documents table
291
      //the data file is a new one, inert it into table
292
      //user specified rev should be great than 0
293
      if (revInDataBase==-1 && userSpecifyRev>0 )
294
      {
295
        ac = new AccessionNumber(accnum, "insert");
296
        action = "insert";
297
      }
298
      //rev is greater the last revsion number and revInDataBase isn't -1
299
      // it is a updated data file
300
      else if (userSpecifyRev>revInDataBase && revInDataBase>0)
301
      {
305
                //archive the old entry
306
                archiveDocRevision(docIdWithoutRev, user);
307
                //delete the old entry in xml_documents
308
                //deleteXMLDocuments(docIdWithoutRev);
309
                ac = new AccessionNumber(accnum, "update");
310
                action = "update";
311
            }
312
            //other situation
313
            else {
302 314

  
303
        //archive the old entry
304
        archiveDocRevision(docIdWithoutRev, user);
305
        //delete the old entry in xml_documents
306
        //deleteXMLDocuments(docIdWithoutRev);
307
        ac = new AccessionNumber(accnum, "update");
308
        action = "update";
309
      }
310
      //other situation
311
      else
312
      {
315
                throw new Exception("Revision number couldn't be "
316
                        + userSpecifyRev);
317
            }
318
            String docid = ac.getDocid();
319
            String rev = ac.getRev();
320
            /*
321
             * SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy
322
             * HH:mm:ss"); Date localtime = new Date(); String dateString =
323
             * formatter.format(localtime); String
324
             * sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY
325
             * HH24:MI:SS");
326
             */
327
            String sqlDateString = dbAdapter.getDateTimeFunction();
313 328

  
314
        throw new Exception("Revision number couldn't be "
315
                    +userSpecifyRev);
316
      }
317
      String docid = ac.getDocid();
318
      String rev = ac.getRev();
319
      /*SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy HH:mm:ss");
320
      Date localtime = new Date();
321
      String dateString = formatter.format(localtime);
322
      String sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY HH24:MI:SS");*/
323
      String  sqlDateString = dbAdapter.getDateTimeFunction();
324

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

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

  
425
        ac = new AccessionNumber(accnum, "insert");
426
        action = "insert";
427
      }
428
      //rev is greater the last revsion number and revInDataBase isn't -1
429
      // it is a updated data file
430
      else if (userSpecifyRev>revInDataBase && revInDataBase>=0)
431
      {
433
                //archive the old entry
434
                archiveDocRevision(docIdWithoutRev, user);
435
                //delete the old entry in xml_documents
436
                //deleteXMLDocuments(docIdWithoutRev);
437
                ac = new AccessionNumber(accnum, "update");
438
                action = "update";
439
            }
440
            // local server has newer version, then notify the remote server
441
            else if (userSpecifyRev < revInDataBase && revInDataBase > 0) {
442
                throw new Exception("Local server: "
443
                        + MetaCatUtil.getOption("server")
444
                        + " has newer revision of doc: " + docIdWithoutRev
445
                        + "." + revInDataBase + ". Please notify it.");
446
            }
447
            //other situation
448
            else {
432 449

  
433
        //archive the old entry
434
        archiveDocRevision(docIdWithoutRev, user);
435
        //delete the old entry in xml_documents
436
        //deleteXMLDocuments(docIdWithoutRev);
437
        ac = new AccessionNumber(accnum, "update");
438
        action = "update";
439
      }
440
      // local server has newer version, then notify the remote server
441
      else if ( userSpecifyRev < revInDataBase && revInDataBase > 0)
442
      {
443
        throw new Exception("Local server: "+MetaCatUtil.getOption("server")+
444
                 " has newer revision of doc: "+docIdWithoutRev+"."
445
                  +revInDataBase+". Please notify it.");
446
      }
447
      //other situation
448
      else
449
      {
450
                throw new Exception("Revision number couldn't be "
451
                        + userSpecifyRev);
452
            }
453
            String docid = ac.getDocid();
454
            String rev = ac.getRev();
455
            /*
456
             * SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy
457
             * HH:mm:ss"); Date localtime = new Date(); String dateString =
458
             * formatter.format(localtime); String
459
             * sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY
460
             * HH24:MI:SS");
461
             */
462
            String sqlDateString = dbAdapter.getDateTimeFunction();
450 463

  
451
        throw new Exception("Revision number couldn't be "
452
                    +userSpecifyRev);
453
      }
454
      String docid = ac.getDocid();
455
      String rev = ac.getRev();
456
      /*SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy HH:mm:ss");
457
      Date localtime = new Date();
458
      String dateString = formatter.format(localtime);
459
      String sqlDateString=dbAdapter.toDate(dateString, "MM/DD/YY HH24:MI:SS");*/
460
      String sqlDateString = dbAdapter.getDateTimeFunction();
464
            StringBuffer sql = new StringBuffer();
465
            if (action != null && action.equals("insert")) {
466
                sql.append("insert into xml_documents (docid, docname, " +
467
                           "doctype, ");
468
                sql.append("user_owner, user_updated, server_location, " + 
469
                           "rev,date_created");
470
                sql.append(", date_updated, public_access) values ('");
471
                sql.append(docid).append("','");
472
                sql.append(docname).append("','");
473
                sql.append(doctype).append("','");
474
                sql.append(user).append("','");
475
                sql.append(user).append("','");
476
                sql.append(serverCode).append("','");
477
                sql.append(rev).append("',");
478
                sql.append(sqlDateString).append(",");
479
                sql.append(sqlDateString).append(",");
480
                sql.append("'0')");
481
            } else if (action != null && action.equals("update")) {
482
                sql.append("update xml_documents set docname ='");
483
                sql.append(docname).append("', ");
484
                sql.append("user_updated='");
485
                sql.append(user).append("', ");
486
                sql.append("server_location='");
487
                sql.append(serverCode).append("',");
488
                sql.append("rev='");
489
                sql.append(rev).append("',");
490
                sql.append("date_updated=");
491
                sql.append(sqlDateString);
492
                sql.append(" where docid='");
493
                sql.append(docid).append("'");
494
            }
495
            // Set auto commit fasle
496
            dbconn.setAutoCommit(false);
497
            pstmt = dbconn.prepareStatement(sql.toString());
461 498

  
462
      StringBuffer sql = new StringBuffer();
463
      if (action != null && action.equals("insert"))
464
      {
465
        sql.append("insert into xml_documents (docid, docname, doctype, ");
466
        sql.append("user_owner, user_updated, server_location, rev,date_created");
467
        sql.append(", date_updated, public_access) values ('");
468
        sql.append(docid).append("','");
469
        sql.append(docname).append("','");
470
        sql.append(doctype).append("','");
471
        sql.append(user).append("','");
472
        sql.append(user).append("','");
473
        sql.append(serverCode).append("','");
474
        sql.append(rev).append("',");
475
        sql.append(sqlDateString).append(",");
476
        sql.append(sqlDateString).append(",");
477
        sql.append("'0')");
478
      }
479
      else if (action != null && action.equals("update"))
480
      {
481
        sql.append("update xml_documents set docname ='");
482
        sql.append(docname).append("', ");
483
        sql.append("user_updated='");
484
        sql.append(user).append("', ");
485
        sql.append("server_location='");
486
        sql.append(serverCode).append("',");
487
        sql.append("rev='");
488
        sql.append(rev).append("',");
489
        sql.append("date_updated=");
490
        sql.append(sqlDateString);
491
        sql.append(" where docid='");
492
        sql.append(docid).append("'");
493
      }
494
      // Set auto commit fasle
495
      dbconn.setAutoCommit(false);
496
      pstmt = dbconn.prepareStatement(sql.toString());
497

  
498
      pstmt.execute();
499
      // Commit the insert
500
      dbconn.commit();
501
      pstmt.close();
502
      //dbconn.close();
499
            pstmt.execute();
500
            // Commit the insert
501
            dbconn.commit();
502
            pstmt.close();
503
            //dbconn.close();
504
        } finally {
505
            // Set DBConnection auto commit true
506
            dbconn.setAutoCommit(true);
507
            pstmt.close();
508
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
509
        }
503 510
    }
504
    finally
505
    {
506
      // Set DBConnection auto commit true
507
      dbconn.setAutoCommit(true);
508
      pstmt.close();
509
      DBConnectionPool.returnDBConnection(dbconn, serialNumber);
510
    }
511
  }
512 511

  
513
 /**
514
   * This method will register a data file entry in xml_documents and save a
515
   * data file input Stream into file system.. It is only used in replication
516
   *
517
   * @param  input, the input stream which contain the file content.
518
   * @param  , the input stream which contain the file content
519
   * @param docname - the name of DTD, for data file, it is a docid number.
520
   * @param doctype - "BIN" for data file
521
   * @param accnum the accession number to use for the INSERT OR UPDATE, which
522
   *               includes a revision number for this revision of the document
523
   *               (e.g., knb.1.1)
524
   * @param user the user that owns the document
525
   * @param docHomeServer, the home server of the docid
526
   * @param notificationServer, the server to notify force replication info to
527
   *                            local metacat
528
   */
529
 public static void writeDataFileInReplication(InputStream input,
530
                 String filePath, String docname, String doctype, String accnum,
531
                   String user, String docHomeServer, String notificationServer)
532
                     throws SQLException, AccessionNumberException, Exception
533
 {
534
    int serverCode=-2;
535

  
536

  
537
    if (filePath==null||filePath.equals(""))
512
    /**
513
     * This method will register a data file entry in xml_documents and save a
514
     * data file input Stream into file system.. It is only used in replication
515
     * 
516
     * @param input,
517
     *            the input stream which contain the file content.
518
     * @param ,
519
     *            the input stream which contain the file content
520
     * @param docname -
521
     *            the name of DTD, for data file, it is a docid number.
522
     * @param doctype -
523
     *            "BIN" for data file
524
     * @param accnum
525
     *            the accession number to use for the INSERT OR UPDATE, which
526
     *            includes a revision number for this revision of the document
527
     *            (e.g., knb.1.1)
528
     * @param user
529
     *            the user that owns the document
530
     * @param docHomeServer,
531
     *            the home server of the docid
532
     * @param notificationServer,
533
     *            the server to notify force replication info to local metacat
534
     */
535
    public static void writeDataFileInReplication(InputStream input,
536
            String filePath, String docname, String doctype, String accnum,
537
            String user, String docHomeServer, String notificationServer)
538
            throws SQLException, AccessionNumberException, Exception
538 539
    {
539
      throw new
540
            Exception("Please specify the directory where file will be store");
541
    }
542
    if (accnum==null||accnum.equals(""))
543
    {
544
      throw new Exception("Please specify the stored file name");
545
    }
540
        int serverCode = -2;
546 541

  
542
        if (filePath == null || filePath.equals("")) { throw new Exception(
543
                "Please specify the directory where file will be store"); }
544
        if (accnum == null || accnum.equals("")) { throw new Exception(
545
                "Please specify the stored file name"); }
547 546

  
547
        // If server is not int the xml replication talbe, insert it into
548
        // xml_replication table
549
        //serverList.addToServerListIfItIsNot(docHomeServer);
550
        insertServerIntoReplicationTable(docHomeServer);
548 551

  
549
    // If server is not int the xml replication talbe, insert it into
550
    // xml_replication table
551
    //serverList.addToServerListIfItIsNot(docHomeServer);
552
    insertServerIntoReplicationTable(docHomeServer);
552
        // Get server code again
553
        serverCode = getServerCode(docHomeServer);
553 554

  
554
    // Get server code again
555
    serverCode = getServerCode(docHomeServer);
555
        //register data file into xml_documents table
556
        registerDocumentInReplication(docname, doctype, accnum, user,
557
                serverCode);
558
        //write inputstream into file system.
559
        File dataDirectory = new File(filePath);
560
        File newFile = new File(dataDirectory, accnum);
556 561

  
562
        // create a buffered byte output stream
563
        // that uses a default-sized output buffer
564
        FileOutputStream fos = new FileOutputStream(newFile);
565
        BufferedOutputStream outPut = new BufferedOutputStream(fos);
557 566

  
558
    //register data file into xml_documents table
559
    registerDocumentInReplication(docname, doctype, accnum, user, serverCode);
560
    //write inputstream into file system.
561
    File dataDirectory = new File(filePath);
562
    File newFile = new File(dataDirectory, accnum);
567
        BufferedInputStream bis = null;
568
        bis = new BufferedInputStream(input);
569
        byte[] buf = new byte[4 * 1024]; // 4K buffer
570
        int b = bis.read(buf);
563 571

  
564
    // create a buffered byte output stream
565
    // that uses a default-sized output buffer
566
    FileOutputStream fos = new FileOutputStream(newFile);
567
    BufferedOutputStream outPut = new BufferedOutputStream(fos);
572
        while (b != -1) {
573
            outPut.write(buf, 0, b);
574
            b = bis.read(buf);
575
        }
576
        bis.close();
577
        outPut.close();
578
        fos.close();
568 579

  
569
    BufferedInputStream bis = null;
570
    bis = new BufferedInputStream(input);
571
    byte[] buf = new byte[4 * 1024]; // 4K buffer
572
    int b = bis.read(buf);
580
        // Force replicate data file
581
        ForceReplicationHandler forceReplication = new ForceReplicationHandler(
582
                accnum, false, notificationServer);
583
    }
573 584

  
574
    while (b != -1)
585
    /**
586
     * Get a lock for a given document.
587
     */
588
    public static boolean getDataFileLockGrant(String accnum) throws Exception
575 589
    {
576
        outPut.write(buf, 0, b);
577
        b = bis.read(buf);
590
        try {
591
            int serverLocation = getServerLocationNumber(accnum);
592
            return getDataFileLockGrant(accnum, serverLocation);
593
        } catch (Exception e) {
594
            throw e;
595
        }
578 596
    }
579
    bis.close();
580
          outPut.close();
581
          fos.close();
582 597

  
583
    // Force replicate data file
584
    ForceReplicationHandler forceReplication = new ForceReplicationHandler
585
                                    (accnum, false, notificationServer);
598
    /**
599
     * The method will check if metacat can get data file lock grant If server
600
     * code is 1, it get. If server code is not 1 but call replication getlock
601
     * successfully, it get else, it didn't get
602
     * 
603
     * @param accnum,
604
     *            the ID of the document
605
     * @param action,
606
     *            the action to the document
607
     * @param serverCode,
608
     *            the server location code
609
     */
610
    public static boolean getDataFileLockGrant(String accnum, int serverCode)
611
            throws Exception
612
    {
613
        boolean flag = true;
614
        //MetaCatUtil util = new MetaCatUtil();
615
        String docid = MetaCatUtil.getDocIdFromString(accnum);
616
        int rev = MetaCatUtil.getVersionFromString(accnum);
586 617

  
587
 }
618
        if (serverCode == 1) {
619
            flag = true;
620
            return flag;
621
        }
588 622

  
623
        //if((serverCode != 1 && action.equals("UPDATE")) )
624
        if (serverCode != 1) { //if this document being written is not a
625
                               // resident of this server then
626
            //we need to try to get a lock from it's resident server. If the
627
            //resident server will not give a lock then we send the user a
628
            // message
629
            //saying that he/she needs to download a new copy of the file and
630
            //merge the differences manually.
589 631

  
632
            String server = MetacatReplication
633
                    .getServerNameForServerCode(serverCode);
634
            MetacatReplication.replLog("attempting to lock " + accnum);
635
            URL u = new URL("https://" + server + "?server="
636
                    + MetaCatUtil.getLocalReplicationServerName()
637
                    + "&action=getlock&updaterev=" + rev + "&docid=" + docid);
638
            //System.out.println("sending message: " + u.toString());
639
            String serverResStr = MetacatReplication.getURLContent(u);
640
            String openingtag = serverResStr.substring(0, serverResStr
641
                    .indexOf(">") + 1);
642
            if (openingtag.equals("<lockgranted>")) {
643
                //the lock was granted go ahead with the insert
644
                //System.out.println("In lockgranted");
645
                MetacatReplication.replLog("lock granted for " + accnum
646
                        + " from " + server);
647
                flag = true;
648
                return flag;
649
            }//if
590 650

  
591
  public static boolean getDataFileLockGrant(String accnum)
592
                                                  throws Exception
593
  {
651
            else if (openingtag.equals("<filelocked>")) {//the file is
652
                                                         // currently locked by
653
                                                         // another user
654
                //notify our user to wait a few minutes, check out a new copy
655
                // and try
656
                //again.
657
                //System.out.println("file locked");
658
                MetacatReplication.replLog("lock denied for " + accnum + " on "
659
                        + server + " reason: file already locked");
660
                throw new Exception(
661
                        "The file specified is already locked by another "
662
                                + "user.  Please wait 30 seconds, checkout the "
663
                                + "newer document, merge your changes and try "
664
                                + "again.");
665
            } else if (openingtag.equals("<outdatedfile>")) {//our file is
666
                                                             // outdated. notify
667
                                                             // our user to
668
                                                             // check out a new
669
                                                             // copy of the
670
                //file and merge his version with the new version.
671
                //System.out.println("outdated file");
672
                MetacatReplication.replLog("lock denied for " + accnum + " on "
673
                        + server + " reason: local file outdated");
674
                throw new Exception(
675
                        "The file you are trying to update is an outdated"
676
                         + " version.  Please checkout the newest document, "
677
                         + "merge your changes and try again.");
678
            }
679
        }
680
        return flag;
681
    }
594 682

  
595
    try
683
    /**
684
     * get the document name
685
     */
686
    public String getDocname()
596 687
    {
688
        return docname;
689
    }
597 690

  
598
      int serverLocation=getServerLocationNumber(accnum);
691
    /**
692
     * get the document type (which is the PublicID)
693
     */
694
    public String getDoctype()
695
    {
696
        return doctype;
697
    }
599 698

  
600
      return getDataFileLockGrant(accnum,serverLocation);
699
    /**
700
     * get the system identifier
701
     */
702
    public String getSystemID()
703
    {
704
        return system_id;
601 705
    }
602
    catch (Exception e)
706

  
707
    /**
708
     * get the root node identifier
709
     */
710
    public long getRootNodeID()
603 711
    {
712
        return rootnodeid;
713
    }
604 714

  
605
      throw e;
715
    /**
716
     * get the creation date
717
     */
718
    public String getCreateDate()
719
    {
720
        return createdate;
606 721
    }
607
  }
608 722

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

  
627
    if (serverCode == 1)
731
    /**
732
     * Get the document identifier (docid)
733
     */
734
    public String getDocID()
628 735
    {
629
      flag=true;
630
      return flag;
736
        return docid;
631 737
    }
632 738

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

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

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

  
683
   return flag;
684

  
685
  }//getDataFileLockGrant
686

  
687
  /**
688
   * get the document name
689
   */
690
  public String getDocname() {
691
    return docname;
692
  }
693

  
694
  /**
695
   * get the document type (which is the PublicID)
696
   */
697
  public String getDoctype() {
698
    return doctype;
699
  }
700

  
701
  /**
702
   * get the system identifier
703
   */
704
  public String getSystemID() {
705
    return system_id;
706
  }
707

  
708
  /**
709
   * get the root node identifier
710
   */
711
  public long getRootNodeID() {
712
    return rootnodeid;
713
  }
714

  
715
  /**
716
   * get the creation date
717
   */
718
  public String getCreateDate() {
719
    return createdate;
720
  }
721

  
722
  /**
723
   * get the update date
724
   */
725
  public String getUpdateDate() {
726
    return updatedate;
727
  }
728

  
729
  /**
730
   * Get the document identifier (docid)
731
   */
732
  public String getDocID() {
733
    return docid;
734
  }
735

  
736
// DOCTITLE attr cleared from the db
737
//  /**
738
//   *get the document title
739
//   */
740
//  public String getDocTitle() {
741
//    return doctitle;
742
//  }
743

  
744
  public String getUserowner() {
745
    return userowner;
746
  }
747

  
748
  public String getUserupdated() {
749
    return userupdated;
750
  }
751

  
752
  public int getServerlocation() {
753
    return serverlocation;
754
  }
755

  
756
  public String getDocHomeServer() {
757
    return docHomeServer;
758
  }
759

  
760

  
761

  
762
  public String getPublicaccess() {
763
    return publicaccess;
764
  }
765

  
766
  public int getRev() {
767
    return rev;
768
  }
769

  
770
  public String getValidateType()
771
  {
772
    return validateType;
773
  }
774

  
775
  /**
776
   * Print a string representation of the XML document
777
   */
778
  public String toString(String user, String[] groups, boolean withInlinedata)
779
  {
780
    StringWriter docwriter = new StringWriter();
781
    try
739
    public String getUserowner()
782 740
    {
783
      this.toXml(docwriter, user, groups, withInlinedata);
741
        return userowner;
784 742
    }
785
    catch (McdbException mcdbe)
743

  
744
    public String getUserupdated()
786 745
    {
787
      return null;
746
        return userupdated;
788 747
    }
789
    String document = docwriter.toString();
790
    return document;
791
  }
792 748

  
793
   /**
794
   * Print a string representation of the XML document
795
   */
796
  public String toString()
797
  {
798
    StringWriter docwriter = new StringWriter();
799
    String userName = null;
800
    String[] groupNames = null;
801
    boolean withInlineData = true;
802
    try
749
    public int getServerlocation()
803 750
    {
804
      this.toXml(docwriter, userName, groupNames, withInlineData);
751
        return serverlocation;
805 752
    }
806
    catch (McdbException mcdbe)
753

  
754
    public String getDocHomeServer()
807 755
    {
808
      return null;
756
        return docHomeServer;
809 757
    }
810
    String document = docwriter.toString();
811
    return document;
812
  }
813 758

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

  
828
    // First, check that we have the needed node data, and get it if not
829
    if (nodeRecordList == null) {
830
      nodeRecordList = getNodeRecordList(rootnodeid);
759
    public String getPublicaccess()
760
    {
761
        return publicaccess;
831 762
    }
832 763

  
833
    // Create the elements from the downloaded data in the TreeSet
834
    rootNode = new ElementNode(nodeRecordList, rootnodeid);
835

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

  
839
    if (docname != null) {
840
      if ((doctype != null) && (system_id != null)) {
841
        doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
842
                   "\" \"" + system_id + "\">\n");
843
      } else {
844
        doc.append("<!DOCTYPE " + docname + ">\n");
845
      }
764
    public int getRev()
765
    {
766
        return rev;
846 767
    }
847
    doc.append(rootNode.toString());
848 768

  
849
    return (doc.toString());
850
  }
851

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

  
871
    if (doctype != null && (doctype.equals(EML2_0_0NAMESPACE) || doctype.equals(EML2_0_1NAMESPACE) ||doctype.equals(EML2_1_0NAMESPACE)))
769
    public String getValidateType()
872 770
    {
873
      proccessEml2 = true;
771
        return validateType;
874 772
    }
875
    // flag for process inline data
876
    boolean prcocessInlineData = false;
877 773

  
878
    TreeSet nodeRecordLists = null;
879
    PrintWriter out = null;
880
    if (pw instanceof PrintWriter) {
881
      out = (PrintWriter)pw;
882
    } else {
883
      out = new PrintWriter(pw);
774
    /**
775
     * Print a string representation of the XML document
776
     */
777
    public String toString(String user, String[] groups, boolean withInlinedata)
778
    {
779
        StringWriter docwriter = new StringWriter();
780
        try {
781
            this.toXml(docwriter, user, groups, withInlinedata);
782
        } catch (McdbException mcdbe) {
783
            return null;
784
        }
785
        String document = docwriter.toString();
786
        return document;
884 787
    }
885 788

  
886
    MetaCatUtil util = new MetaCatUtil();
887

  
888
    // Here add code to handle subtree access control
889
    /*PermissionController control = new PermissionController(docid);
890
    Hashtable unaccessableSubTree =control.hasUnaccessableSubTree(user, groups,
891
                                             AccessControlInterface.READSTRING);
892

  
893
    if (!unaccessableSubTree.isEmpty())
789
    /**
790
     * Print a string representation of the XML document
791
     */
792
    public String toString()
894 793
    {
794
        StringWriter docwriter = new StringWriter();
795
        String userName = null;
796
        String[] groupNames = null;
797
        boolean withInlineData = true;
798
        try {
799
            this.toXml(docwriter, userName, groupNames, withInlineData);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff