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