Project

General

Profile

1 623 berkley
/**
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
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11 669 jones
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 623 berkley
 */
26
27 622 berkley
package edu.ucsb.nceas.metacat;
28
29
import java.io.*;
30
import java.util.*;
31
import java.lang.*;
32
import java.sql.*;
33
34 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
35
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
36 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
37 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38
39 622 berkley
/**
40
 * A class to parse document ids
41
 * The docid is of the form siteCode.uniqueId.rev
42
 */
43
public class DocumentIdentifier
44
{
45
  private String docid = null;
46
  private String rev = null;
47
  private String uniqueId = null;
48
  private String siteCode = null;
49
  private String separator = null;
50
51 4080 daigle
  /**
52
	 * Constructor to build a docid object and parse an incoming string.
53
	 */
54
	public DocumentIdentifier(String docid) throws AccessionNumberException {
55
		this.docid = docid;
56
		try {
57 4212 daigle
			this.separator = PropertyService.getProperty("document.accNumSeparator");
58 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
59
			throw new AccessionNumberException(
60
					"Could not get property 'accNumSeparator' :" + pnfe.getMessage());
61
		}
62 734 bojilova
63 4080 daigle
		if (docid.endsWith(this.separator)) {
64
			throw new AccessionNumberException("Accession number cannot end with "
65
					+ "a seperator.");
66
		}
67
		if (docid.startsWith(this.separator)) {
68
			throw new AccessionNumberException("Accession number cannot begin with "
69
					+ "a seperator.");
70
		}
71
72
		parseDocid();
73
	}
74 622 berkley
75
  /**
76 4080 daigle
	 * parses the docid into its parts
77
	 */
78 736 bojilova
  private void parseDocid() throws AccessionNumberException
79 622 berkley
  {
80 736 bojilova
    try {
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
    } catch (StringIndexOutOfBoundsException e) {
96
      throw new
97
      AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
98
                               "Use accession number format as: " +
99
                               "sitecode" + separator + "uniqueid" +
100
                               separator + "revisionid");
101
    } catch (SQLException e) {
102
      throw new
103
      AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
104
                               "DB Error when reading revisionid");
105
    } catch (ClassNotFoundException e) {
106
      throw new
107
      AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
108
                                e.getMessage());
109 622 berkley
    }
110
111 736 bojilova
    try {
112
      if(rev.equals("newest")) {
113
        rev = getNewestRev();
114
      }
115
    } catch (SQLException e) {
116
      throw new
117
      AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
118
                               "DB Error when reading revisionid");
119
    } catch (ClassNotFoundException e) {
120
      throw new
121
      AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
122
                                e.getMessage());
123 622 berkley
    }
124 736 bojilova
125 622 berkley
  }
126
127
  /**
128
   * returns the newest revision number for a document
129
   */
130 736 bojilova
  private String getNewestRev() throws SQLException, ClassNotFoundException
131 622 berkley
  {
132 1217 tao
    PreparedStatement pstmt = null;
133
    DBConnection dbConn = null;
134
    int serialNumber = -1;
135 622 berkley
136 736 bojilova
    try {
137 1217 tao
      /*try {
138 736 bojilova
        //this connection is prone to error for some reason so we
139 683 berkley
        //try to connect it three times before quiting.
140
        conn = util.openDBConnection();
141 736 bojilova
      } catch(SQLException sqle) {
142
        try {
143 683 berkley
          conn = util.openDBConnection();
144 736 bojilova
        } catch(SQLException sqlee) {
145
          try {
146 683 berkley
            conn = util.openDBConnection();
147 736 bojilova
          } catch(SQLException sqleee) {
148 683 berkley
            System.out.println("error getting db connection in " +
149
                               "MetacatReplication.handleGetDocumentRequest: " +
150
                               sqleee.getMessage());
151
          }
152
        }
153 1217 tao
      }*/
154
      dbConn=DBConnectionPool.
155
                  getDBConnection("DocumentIdentifier.getNewestRev");
156
      serialNumber=dbConn.getCheckOutSerialNumber();
157
      pstmt = dbConn.prepareStatement("select rev from xml_documents where " +
158 622 berkley
                                    "docid like '" + docid + "'");
159
      pstmt.execute();
160
      ResultSet rs = pstmt.getResultSet();
161
      boolean tablehasrows = rs.next();
162 736 bojilova
      if (tablehasrows) {
163 622 berkley
        String retStr = rs.getString(1);
164 667 berkley
        pstmt.close();
165 1217 tao
        //conn.close();
166 622 berkley
        return retStr;
167
      }
168 1217 tao
      //conn.close();
169 736 bojilova
    } catch(SQLException e) {
170 683 berkley
      System.out.println("error in DocumentIdentifier.getNewestRev(): " +
171
                          e.getMessage());
172 1217 tao
      /*try {
173
       conn.close();
174
      } catch(SQLException e2) { throw e2; }*/
175 736 bojilova
      throw e;
176 622 berkley
    }
177 1217 tao
    finally
178
    {
179
      try
180
      {
181
        pstmt.close();
182
      }//try
183
      finally
184
      {
185
        DBConnectionPool.returnDBConnection(dbConn,serialNumber);
186
      }//finally
187
    }//finally
188
189 736 bojilova
190 1217 tao
191 622 berkley
    return "1";
192
  }
193
194
  private int countSeparator()
195
  {
196
    int count = 0;
197
    for(int i=0; i<docid.length(); i++)
198
    {
199
      if(docid.charAt(i) == '.')
200
      {
201
        count++;
202
      }
203
    }
204
    return count;
205
  }
206
207
  /**
208
   * returns the revision number encoded in this docid
209
   */
210
  public String getRev()
211
  {
212
    return rev;
213
  }
214
215
  /**
216
   * returns the uniqueId encoded in this docid
217
   */
218
  public String getUniqueId()
219
  {
220
    return uniqueId;
221
  }
222
223
  /**
224
   * returns the siteCode encoded in this docid
225
   */
226
  public String getSiteCode()
227
  {
228
    return siteCode;
229
  }
230
231
  /**
232
   * returns the separator used in the accession number
233
   */
234
  public String getSeparator()
235
  {
236
    return separator;
237
  }
238
239
  /**
240
   * returns <siteCode><sepatator><uniqueId>
241
   */
242
  public String getIdentifier()
243
  {
244
    return siteCode + separator + uniqueId;
245
  }
246
247
  /**
248
   * returns the whole docid as a string
249
   */
250
  public String toString()
251
  {
252
    return docid;
253
  }
254
255
  /**
256
   * Test driver.  The first command line argument is the docid you want to
257
   * create an object for.  For instance ">java DocumentIdentifer nceas.1.2"
258
   * will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
259
   */
260
  public static void main(String args[])
261
  {
262
    try
263
    {
264
      DocumentIdentifier d = new DocumentIdentifier(args[0]);
265
      System.out.println("rev: " + d.getRev());
266
      System.out.println("uniqueId: " + d.getUniqueId());
267
      System.out.println("siteCode: " + d.getSiteCode());
268
    }
269
    catch(Exception e)
270
    {
271
      System.out.println("error: " + e.getMessage());
272
    }
273
  }
274
}