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: jones $'
10
 *     '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
11
 * '$Revision: 669 $'
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.*;
31
import java.util.*;
32
import java.lang.*;
33
import java.sql.*;
34

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