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: bojilova $'
10
 *     '$Date: 2001-05-03 17:12:39 -0700 (Thu, 03 May 2001) $'
11
 * '$Revision: 734 $'
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
    this.separator = util.getOption("accNumSeparator");
55

    
56
    if(docid.endsWith(this.separator))
57
    {
58
      throw new AccessionNumberException("Accession number cannot end with " + 
59
                "a seperator.");
60
    }
61
    if(docid.startsWith(this.separator))
62
    {
63
      throw new AccessionNumberException("Accession number cannot begin with " + 
64
                "a seperator.");
65
    }
66
    
67
    parseDocid();
68
  }
69
  
70
  /**
71
   * parses the docid into its parts
72
   */
73
  private void parseDocid()
74
  {
75
    int firstIndex = docid.indexOf(separator);
76
    int lastIndex = docid.lastIndexOf(separator);
77
    if(firstIndex != lastIndex)
78
    { //this docid contains a revision number
79
      rev = docid.substring(lastIndex + 1);
80
      uniqueId = docid.substring(firstIndex + 1, lastIndex);
81
      siteCode = docid.substring(0, firstIndex);
82
    }
83
    else
84
    {
85
      uniqueId = docid.substring(firstIndex + 1);
86
      siteCode = docid.substring(0, firstIndex);
87
      rev = getNewestRev();
88
    }
89
    
90
    if(rev.equals("newest"))
91
    {
92
      rev = getNewestRev();
93
    }
94
  }
95
  
96
  /**
97
   * returns the newest revision number for a document
98
   */
99
  private String getNewestRev()
100
  {
101
    PreparedStatement pstmt;
102
    Connection conn = null;
103
    
104
    try
105
    {
106
      try
107
      { //this connection is prone to error for some reason so we 
108
        //try to connect it three times before quiting.
109
        conn = util.openDBConnection();
110
      }
111
      catch(SQLException sqle)
112
      {
113
        try
114
        {
115
          conn = util.openDBConnection();
116
        }
117
        catch(SQLException sqlee)
118
        {
119
          try
120
          {
121
            conn = util.openDBConnection();
122
          }
123
          catch(SQLException sqleee)
124
          {
125
            System.out.println("error getting db connection in " + 
126
                               "MetacatReplication.handleGetDocumentRequest: " +
127
                               sqleee.getMessage());
128
          }
129
        }
130
      }
131
      pstmt = conn.prepareStatement("select rev from xml_documents where " +
132
                                    "docid like '" + docid + "'");
133
      pstmt.execute();
134
      ResultSet rs = pstmt.getResultSet();
135
      boolean tablehasrows = rs.next();
136
      if(tablehasrows)
137
      {
138
        String retStr = rs.getString(1);
139
        pstmt.close();
140
        conn.close();
141
        return retStr;
142
      }
143
      conn.close();
144
    }
145
    catch(Exception e)
146
    {
147
      System.out.println("error in DocumentIdentifier.getNewestRev(): " +
148
                          e.getMessage());
149
      try
150
      {
151
        conn.close();
152
      }
153
      catch(Exception e2)
154
      {}
155
    }
156
    return "1";
157
  }
158
  
159
  private int countSeparator()
160
  {
161
    int count = 0;
162
    for(int i=0; i<docid.length(); i++)
163
    {
164
      if(docid.charAt(i) == '.')
165
      {  
166
        count++;
167
      }
168
    }
169
    return count;
170
  }
171
  
172
  /**
173
   * returns the revision number encoded in this docid
174
   */
175
  public String getRev()
176
  {
177
    return rev;
178
  }
179
  
180
  /**
181
   * returns the uniqueId encoded in this docid
182
   */
183
  public String getUniqueId()
184
  {
185
    return uniqueId;
186
  }
187
  
188
  /**
189
   * returns the siteCode encoded in this docid
190
   */
191
  public String getSiteCode()
192
  {
193
    return siteCode;
194
  }
195
  
196
  /**
197
   * returns the separator used in the accession number
198
   */
199
  public String getSeparator()
200
  {
201
    return separator;
202
  }
203
  
204
  /**
205
   * returns <siteCode><sepatator><uniqueId>
206
   */
207
  public String getIdentifier()
208
  {
209
    return siteCode + separator + uniqueId;
210
  }
211
  
212
  /**
213
   * returns the whole docid as a string
214
   */
215
  public String toString()
216
  {
217
    return docid;
218
  }
219
  
220
  /**
221
   * Test driver.  The first command line argument is the docid you want to 
222
   * create an object for.  For instance ">java DocumentIdentifer nceas.1.2"
223
   * will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
224
   */
225
  public static void main(String args[]) 
226
  {
227
    try
228
    {
229
      DocumentIdentifier d = new DocumentIdentifier(args[0]);
230
      System.out.println("rev: " + d.getRev());
231
      System.out.println("uniqueId: " + d.getUniqueId());
232
      System.out.println("siteCode: " + d.getSiteCode());
233
    }
234
    catch(Exception e)
235
    {
236
      System.out.println("error: " + e.getMessage());
237
    }
238
  }
239
}
(25-25/43)