Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents a docid
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Chad Berkley
7
 *    Release: @release@
8
 *
9
 *   '$Author: berkley $'
10
 *     '$Date: 2000-12-19 13:47:31 -0800 (Tue, 19 Dec 2000) $'
11
 * '$Revision: 623 $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

    
16
import java.io.*;
17
import java.util.*;
18
import java.lang.*;
19
import java.sql.*;
20

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