Project

General

Profile

« Previous | Next » 

Revision 622

Added by berkley over 23 years ago

changed accession number schema to <sitecode>.<serialnumber>.<revisionnumber>. DocumentIdentifier.java parses docids into parts for easy manipulation. You can now specifiy a specific revision by asking for document <sitecode>.<serialnumber>.<revisionnumber> or you can get the newest revision by asking for <sitecode>.<serialnumber> or by asking for <sitecode>.<serialnumber>.newest.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
81 81
    try { 
82 82
      this.conn = conn;
83 83
      this.docid = docid;
84
      
85
      DocumentIdentifier id = new DocumentIdentifier(docid);
86
      
84 87
  
85 88
      // Look up the document information
86 89
      getDocumentInfo(docid);
......
390 393
    out.print("</" + currentElement.nodename + ">" );
391 394
    out.flush();
392 395
  }
396
  
397
  private boolean isRevisionOnly(DocumentIdentifier docid) throws Exception
398
  {
399
    //System.out.println("inRevisionOnly");
400
    PreparedStatement pstmt;
401
    String rev = docid.getRev();
402
    String newid = docid.getIdentifier();
403
    pstmt = conn.prepareStatement("select rev from xml_documents " +
404
                                  "where docid like '" + newid + "'");
405
    pstmt.execute();
406
    ResultSet rs = pstmt.getResultSet();
407
    boolean tablehasrows = rs.next();
408
    if(rev.equals("newest") || rev.equals("all"))
409
    {
410
      return false;
411
    }
412
    
413
    if(tablehasrows)
414
    {
415
      int r = rs.getInt(1);
416
      if(new Integer(rev).intValue() == r)
417
      { //the current revision in in xml_documents
418
        //System.out.println("returning false");
419
        return false;
420
      }
421
      else if(new Integer(rev).intValue() < r)
422
      { //the current revision is in xml_revisions.
423
        //System.out.println("returning true");
424
        return true;
425
      }
426
      else if(new Integer(rev).intValue() > r)
427
      { //error, rev cannot be greater than r
428
        throw new Exception("requested revision cannot be greater than " +
429
                            "the latest revision number.");
430
      }
431
    }
432
    throw new Exception("the requested docid '" + docid.toString() + 
433
                        "' does not exist");
434
  }
393 435

  
436
  private void getDocumentInfo(String docid) throws McdbException, 
437
                                                    AccessionNumberException
438
  {
439
    getDocumentInfo(new DocumentIdentifier(docid));
440
  }
441
  
394 442
  /**
395 443
   * Look up the document type information from the database
396 444
   *
397 445
   * @param docid the id of the document to look up
398 446
   */
399
  private void getDocumentInfo(String docid) throws McdbException 
447
  private void getDocumentInfo(DocumentIdentifier docid) throws McdbException 
400 448
  {
401 449
    PreparedStatement pstmt;
402

  
450
    String table = "xml_documents";
451
    
452
    try
453
    {
454
      if(isRevisionOnly(docid))
455
      { //pull the document from xml_revisions instead of from xml_documents;
456
        table = "xml_revisions";
457
      }
458
    }
459
    catch(Exception e)
460
    {
461
      System.out.println("error in getDocumentInfo: " + e.getMessage());
462
    }
463
    
464
    //deal with the key words here.
465
    
466
    if(docid.getRev().equals("all"))
467
    {
468
      
469
    }
470
    
403 471
    try {
472
      StringBuffer sql = new StringBuffer();
473
      sql.append("SELECT docname, doctype, rootnodeid,doctitle, ");
474
      sql.append("date_created, date_updated, ");
475
      sql.append("user_owner, user_updated, server_location, ");
476
      sql.append("rev FROM ").append(table);
477
      sql.append(" WHERE docid LIKE '").append(docid.getIdentifier());
478
      sql.append("' and rev like '").append(docid.getRev()).append("'");
479
      //System.out.println(sql.toString());
404 480
      pstmt =
405
        conn.prepareStatement("SELECT docname, doctype, rootnodeid,doctitle, " +
406
                              "date_created, date_updated, " + 
407
                              "user_owner, user_updated, server_location, " +
408
                              "public_access, rev " +
409
                               "FROM xml_documents " +
410
                               "WHERE docid LIKE ?");
481
        conn.prepareStatement(sql.toString());
411 482
      // Bind the values to the query
412
      pstmt.setString(1, docid);
483
      //pstmt.setString(1, docid.getIdentifier());
484
      //pstmt.setString(2, docid.getRev());
413 485

  
414 486
      pstmt.execute();
415 487
      ResultSet rs = pstmt.getResultSet();
......
424 496
        this.userowner      = rs.getString(7);
425 497
        this.userupdated    = rs.getString(8);
426 498
        this.serverlocation = rs.getInt(9);
427
        this.publicaccess   = rs.getInt(10);
428
        this.rev            = rs.getInt(11);
499
        //this.publicaccess   = rs.getInt(10);
500
        this.rev            = rs.getInt(10);
429 501
      } 
430 502
      pstmt.close();
431 503

  
......
446 518
        pstmt.close();
447 519
      }
448 520
    } catch (SQLException e) {
521
      System.out.println("error in getDocumentInfo: " + e.getMessage());
522
      e.printStackTrace(System.out);
449 523
      throw new McdbException("Error accessing database connection.", e);
450 524
    }
451 525

  
src/edu/ucsb/nceas/metacat/DocumentIdentifier.java
1
package edu.ucsb.nceas.metacat;
2

  
3
import java.io.*;
4
import java.util.*;
5
import java.lang.*;
6
import java.sql.*;
7

  
8
/**
9
 * A class to parse document ids
10
 * The docid is of the form siteCode.uniqueId.rev 
11
 */
12
public class DocumentIdentifier
13
{
14
  private String docid = null;
15
  private String rev = null;
16
  private String uniqueId = null;
17
  private String siteCode = null;
18
  private String separator = null;
19
  private MetaCatUtil util = new MetaCatUtil();
20
  
21
  /** 
22
   * Constructor to build a docid object and parse an incoming string.
23
   */
24
  public DocumentIdentifier(String docid) throws AccessionNumberException
25
  {
26
    this.docid = docid;
27
    //int numSeps = countSeparator();
28
    //if(numSeps > 2 || numSeps < 1)
29
    //{
30
    //  throw new AccessionNumberException("Accession number can contain " +
31
    //            "no more than 2 and no fewer than 1 separators.");
32
    //}
33
    if(docid.endsWith("."))
34
    {
35
      throw new AccessionNumberException("Accession number cannot end with " + 
36
                "a seperator.");
37
    }
38
    if(docid.startsWith("."))
39
    {
40
      throw new AccessionNumberException("Accession number cannot begin with " + 
41
                "a seperator.");
42
    }
43
    
44
    parseDocid();
45
  }
46
  
47
  /**
48
   * parses the docid into its parts
49
   */
50
  private void parseDocid()
51
  {
52
    separator = util.getOption("accNumSeparator");
53
    
54
    int firstIndex = docid.indexOf(separator);
55
    int lastIndex = docid.lastIndexOf(separator);
56
    if(firstIndex != lastIndex)
57
    { //this docid contains a revision number
58
      rev = docid.substring(lastIndex + 1);
59
      uniqueId = docid.substring(firstIndex + 1, lastIndex);
60
      siteCode = docid.substring(0, firstIndex);
61
    }
62
    else
63
    {
64
      uniqueId = docid.substring(firstIndex + 1);
65
      siteCode = docid.substring(0, firstIndex);
66
      rev = getNewestRev();
67
    }
68
    
69
    if(rev.equals("newest"))
70
    {
71
      rev = getNewestRev();
72
    }
73
  }
74
  
75
  /**
76
   * returns the newest revision number for a document
77
   */
78
  private String getNewestRev()
79
  {
80
    PreparedStatement pstmt;
81
    Connection conn = null;
82
    
83
    try
84
    {
85
      conn = util.openDBConnection();
86
      pstmt = conn.prepareStatement("select rev from xml_documents where " +
87
                                    "docid like '" + docid + "'");
88
      pstmt.execute();
89
      ResultSet rs = pstmt.getResultSet();
90
      boolean tablehasrows = rs.next();
91
      if(tablehasrows)
92
      {
93
        String retStr = rs.getString(1);
94
        conn.close();
95
        return retStr;
96
      }
97
    }
98
    catch(Exception e)
99
    {
100
      try
101
      {
102
        conn.close();
103
      }
104
      catch(Exception e2)
105
      {}
106
      System.out.println("error in DocumentIdentifier.getNewestRev " + 
107
                          e.getMessage());
108
    }
109
    return "1";
110
  }
111
  
112
  private int countSeparator()
113
  {
114
    int count = 0;
115
    for(int i=0; i<docid.length(); i++)
116
    {
117
      if(docid.charAt(i) == '.')
118
      {  
119
        count++;
120
      }
121
    }
122
    return count;
123
  }
124
  
125
  /**
126
   * returns the revision number encoded in this docid
127
   */
128
  public String getRev()
129
  {
130
    return rev;
131
  }
132
  
133
  /**
134
   * returns the uniqueId encoded in this docid
135
   */
136
  public String getUniqueId()
137
  {
138
    return uniqueId;
139
  }
140
  
141
  /**
142
   * returns the siteCode encoded in this docid
143
   */
144
  public String getSiteCode()
145
  {
146
    return siteCode;
147
  }
148
  
149
  /**
150
   * returns the separator used in the accession number
151
   */
152
  public String getSeparator()
153
  {
154
    return separator;
155
  }
156
  
157
  /**
158
   * returns <siteCode><sepatator><uniqueId>
159
   */
160
  public String getIdentifier()
161
  {
162
    return siteCode + separator + uniqueId;
163
  }
164
  
165
  /**
166
   * returns the whole docid as a string
167
   */
168
  public String toString()
169
  {
170
    return docid;
171
  }
172
  
173
  /**
174
   * Test driver.  The first command line argument is the docid you want to 
175
   * create an object for.  For instance ">java DocumentIdentifer nceas.1.2"
176
   * will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
177
   */
178
  public static void main(String args[]) 
179
  {
180
    try
181
    {
182
      DocumentIdentifier d = new DocumentIdentifier(args[0]);
183
      System.out.println("rev: " + d.getRev());
184
      System.out.println("uniqueId: " + d.getUniqueId());
185
      System.out.println("siteCode: " + d.getSiteCode());
186
    }
187
    catch(Exception e)
188
    {
189
      System.out.println("error: " + e.getMessage());
190
    }
191
  }
192
}
0 193

  

Also available in: Unified diff