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: tao $'
10
 *     '$Date: 2002-06-13 11:54:51 -0700 (Thu, 13 Jun 2002) $'
11
 * '$Revision: 1217 $'
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 = null;
128
    DBConnection dbConn = null;
129
    int serialNumber = -1;
130
    
131
    try {
132
      /*try {
133
        //this connection is prone to error for some reason so we 
134
        //try to connect it three times before quiting.
135
        conn = util.openDBConnection();
136
      } catch(SQLException sqle) {
137
        try {
138
          conn = util.openDBConnection();
139
        } catch(SQLException sqlee) {
140
          try {
141
            conn = util.openDBConnection();
142
          } catch(SQLException sqleee) {
143
            System.out.println("error getting db connection in " + 
144
                               "MetacatReplication.handleGetDocumentRequest: " +
145
                               sqleee.getMessage());
146
          }
147
        }
148
      }*/
149
      dbConn=DBConnectionPool.
150
                  getDBConnection("DocumentIdentifier.getNewestRev");
151
      serialNumber=dbConn.getCheckOutSerialNumber();
152
      pstmt = dbConn.prepareStatement("select rev from xml_documents where " +
153
                                    "docid like '" + docid + "'");
154
      pstmt.execute();
155
      ResultSet rs = pstmt.getResultSet();
156
      boolean tablehasrows = rs.next();
157
      if (tablehasrows) {
158
        String retStr = rs.getString(1);
159
        pstmt.close();
160
        //conn.close();
161
        return retStr;
162
      }
163
      //conn.close();
164
    } catch(SQLException e) {
165
      System.out.println("error in DocumentIdentifier.getNewestRev(): " +
166
                          e.getMessage());
167
      /*try {
168
       conn.close();
169
      } catch(SQLException e2) { throw e2; }*/
170
      throw e;
171
    }
172
    finally
173
    {
174
      try
175
      {
176
        pstmt.close();
177
      }//try
178
      finally
179
      {
180
        DBConnectionPool.returnDBConnection(dbConn,serialNumber);
181
      }//finally
182
    }//finally
183
        
184
    
185
    
186
    return "1";
187
  }
188
  
189
  private int countSeparator()
190
  {
191
    int count = 0;
192
    for(int i=0; i<docid.length(); i++)
193
    {
194
      if(docid.charAt(i) == '.')
195
      {  
196
        count++;
197
      }
198
    }
199
    return count;
200
  }
201
  
202
  /**
203
   * returns the revision number encoded in this docid
204
   */
205
  public String getRev()
206
  {
207
    return rev;
208
  }
209
  
210
  /**
211
   * returns the uniqueId encoded in this docid
212
   */
213
  public String getUniqueId()
214
  {
215
    return uniqueId;
216
  }
217
  
218
  /**
219
   * returns the siteCode encoded in this docid
220
   */
221
  public String getSiteCode()
222
  {
223
    return siteCode;
224
  }
225
  
226
  /**
227
   * returns the separator used in the accession number
228
   */
229
  public String getSeparator()
230
  {
231
    return separator;
232
  }
233
  
234
  /**
235
   * returns <siteCode><sepatator><uniqueId>
236
   */
237
  public String getIdentifier()
238
  {
239
    return siteCode + separator + uniqueId;
240
  }
241
  
242
  /**
243
   * returns the whole docid as a string
244
   */
245
  public String toString()
246
  {
247
    return docid;
248
  }
249
  
250
  /**
251
   * Test driver.  The first command line argument is the docid you want to 
252
   * create an object for.  For instance ">java DocumentIdentifer nceas.1.2"
253
   * will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
254
   */
255
  public static void main(String args[]) 
256
  {
257
    try
258
    {
259
      DocumentIdentifier d = new DocumentIdentifier(args[0]);
260
      System.out.println("rev: " + d.getRev());
261
      System.out.println("uniqueId: " + d.getUniqueId());
262
      System.out.println("siteCode: " + d.getSiteCode());
263
    }
264
    catch(Exception e)
265
    {
266
      System.out.println("error: " + e.getMessage());
267
    }
268
  }
269
}
(30-30/65)