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: 2001-01-19 15:20:58 -0800 (Fri, 19 Jan 2001) $'
11
 * '$Revision: 683 $'
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
      try
113
      { //this connection is prone to error for some reason so we 
114
        //try to connect it three times before quiting.
115
        conn = util.openDBConnection();
116
      }
117
      catch(SQLException sqle)
118
      {
119
        try
120
        {
121
          conn = util.openDBConnection();
122
        }
123
        catch(SQLException sqlee)
124
        {
125
          try
126
          {
127
            conn = util.openDBConnection();
128
          }
129
          catch(SQLException sqleee)
130
          {
131
            System.out.println("error getting db connection in " + 
132
                               "MetacatReplication.handleGetDocumentRequest: " +
133
                               sqleee.getMessage());
134
          }
135
        }
136
      }
137
      pstmt = conn.prepareStatement("select rev from xml_documents where " +
138
                                    "docid like '" + docid + "'");
139
      pstmt.execute();
140
      ResultSet rs = pstmt.getResultSet();
141
      boolean tablehasrows = rs.next();
142
      if(tablehasrows)
143
      {
144
        String retStr = rs.getString(1);
145
        pstmt.close();
146
        conn.close();
147
        return retStr;
148
      }
149
      conn.close();
150
    }
151
    catch(Exception e)
152
    {
153
      System.out.println("error in DocumentIdentifier.getNewestRev(): " +
154
                          e.getMessage());
155
      try
156
      {
157
        conn.close();
158
      }
159
      catch(Exception e2)
160
      {}
161
    }
162
    return "1";
163
  }
164
  
165
  private int countSeparator()
166
  {
167
    int count = 0;
168
    for(int i=0; i<docid.length(); i++)
169
    {
170
      if(docid.charAt(i) == '.')
171
      {  
172
        count++;
173
      }
174
    }
175
    return count;
176
  }
177
  
178
  /**
179
   * returns the revision number encoded in this docid
180
   */
181
  public String getRev()
182
  {
183
    return rev;
184
  }
185
  
186
  /**
187
   * returns the uniqueId encoded in this docid
188
   */
189
  public String getUniqueId()
190
  {
191
    return uniqueId;
192
  }
193
  
194
  /**
195
   * returns the siteCode encoded in this docid
196
   */
197
  public String getSiteCode()
198
  {
199
    return siteCode;
200
  }
201
  
202
  /**
203
   * returns the separator used in the accession number
204
   */
205
  public String getSeparator()
206
  {
207
    return separator;
208
  }
209
  
210
  /**
211
   * returns <siteCode><sepatator><uniqueId>
212
   */
213
  public String getIdentifier()
214
  {
215
    return siteCode + separator + uniqueId;
216
  }
217
  
218
  /**
219
   * returns the whole docid as a string
220
   */
221
  public String toString()
222
  {
223
    return docid;
224
  }
225
  
226
  /**
227
   * Test driver.  The first command line argument is the docid you want to 
228
   * create an object for.  For instance ">java DocumentIdentifer nceas.1.2"
229
   * will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
230
   */
231
  public static void main(String args[]) 
232
  {
233
    try
234
    {
235
      DocumentIdentifier d = new DocumentIdentifier(args[0]);
236
      System.out.println("rev: " + d.getRev());
237
      System.out.println("uniqueId: " + d.getUniqueId());
238
      System.out.println("siteCode: " + d.getSiteCode());
239
    }
240
    catch(Exception e)
241
    {
242
      System.out.println("error: " + e.getMessage());
243
    }
244
  }
245
}
(25-25/43)