Project

General

Profile

« Previous | Next » 

Revision 6020

use the jaxb date parser for ISO 8601 formats. the numeric and date node values are now calculated after the document has been successfully inserted in the db so any sql exceptions do not prevent the raw node data from being saved.
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=2084

View differences:

DBSAXNode.java
26 26

  
27 27
package edu.ucsb.nceas.metacat;
28 28

  
29
import java.sql.*;
30
import java.text.ParseException;
31
import java.text.SimpleDateFormat;
32
import java.util.Date;
29
import java.sql.PreparedStatement;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.Statement;
33
import java.util.Enumeration;
33 34
import java.util.Hashtable;
34
import java.util.Enumeration;
35

  
35 36
import org.apache.log4j.Logger;
36 37
import org.xml.sax.SAXException;
37 38

  
......
176 177
    {
177 178

  
178 179
      PreparedStatement pstmt;
179
      Timestamp timestampData = null;
180 180
      
181 181
      if (nodetype == "DOCUMENT") {
182 182
        pstmt = connection.prepareStatement(
......
184 184
            "(nodetype, nodename, nodeprefix, docid) " +
185 185
            "VALUES (?, ?, ?, ?)");
186 186

  
187
        // Increase DBConnection usage count
188
        connection.increaseUsageCount(1);
189 187
        logMetacat.debug("DBSAXNode.writeChildNodeToDBDataLimited - inserting doc name: " + nodename);
190 188
      } else {
191
          if(data != null && !data.trim().equals("")
192
             && !data.trim().equals("NaN") && !data.trim().equalsIgnoreCase("Infinity")){
193
        	  try{
194
        		  // try some common ISO 8601 formats
195
        		  SimpleDateFormat sdf = null;
196
        		  if (data.length() == 10) {
197
        			  sdf = new SimpleDateFormat("yyyy-MM-dd");
198
        		  } else if (data.length() == 19) {
199
        			  sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
200
        		  } else {
201
        			  // throw this so we continue with parsing as numeric or string.
202
        			  throw new ParseException("String length will not match date/dateTime patterns", -1);
203
        		  }
204
        		  Date dateData = sdf.parse(data);
205
        		  timestampData = new Timestamp(dateData.getTime());
206
                  pstmt = connection.prepareStatement(
207
                      "INSERT INTO xml_nodes " +
208
                      "(nodetype, nodename, nodeprefix, docid, " +
209
                      "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatadate) " +
210
                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
211
              } catch (ParseException pe) {
212
	        	  try{
213
	                  double numberData = Double.parseDouble(data);
214
	                  pstmt = connection.prepareStatement(
215
	                      "INSERT INTO xml_nodes " +
216
	                      "(nodetype, nodename, nodeprefix, docid, " +
217
	                      "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatanumerical) " +
218
	                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?, "+ numberData +")");
219
	              } catch (NumberFormatException nfe) {
220
	                  pstmt = connection.prepareStatement(
221
	                      "INSERT INTO xml_nodes " +
222
	                      "(nodetype, nodename, nodeprefix, docid, " +
223
	                      "rootnodeid, parentnodeid, nodedata, nodeindex) " +
224
	                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
225
	              }
226
              }
227
          } else {
228
              pstmt = connection.prepareStatement(
229
                  "INSERT INTO xml_nodes " +
230
                  "(nodetype, nodename, nodeprefix, docid, " +
231
                  "rootnodeid, parentnodeid, nodedata, nodeindex) " +
232
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
233
          }
234
        // Increase DBConnection usage count
235
        connection.increaseUsageCount(1);
189
          pstmt = connection.prepareStatement(
190
              "INSERT INTO xml_nodes " +
191
              "(nodetype, nodename, nodeprefix, docid, " +
192
              "rootnodeid, parentnodeid, nodedata, nodeindex) " +
193
              "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
236 194
      }
237

  
195
      
196
      // Increase DBConnection usage count
197
      connection.increaseUsageCount(1);
198
      
238 199
      // Bind the values to the query
239 200
      pstmt.setString(1, nodetype);
240 201
      int idx;
......
259 220
          pstmt.setInt(8, incChildNum());
260 221
        }
261 222
      }
262
      if (timestampData != null) {
263
    	  pstmt.setTimestamp(9, timestampData);
264
      }
265 223
      // Do the insertion
266 224
      logMetacat.debug("DBSAXNode.writeChildNodeToDBDataLimited - SQL insert: " + pstmt.toString());
267 225
      pstmt.execute();
......
271 229
      nid = DatabaseService.getInstance().getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
272 230
      //should increase connection usage!!!!!!
273 231

  
274

  
275 232
      if (nodetype.equals("DOCUMENT")) {
276 233
        // Record the root node id that was generated from the database
277 234
        setRootNodeID(nid);
......
356 313
    {
357 314

  
358 315
      PreparedStatement pstmt;
359
      Timestamp timestampData = null;
360 316
      logMetacat.info("DBSAXNode.writeDTDNodeToDB - Insert dtd into db: "+nodename +" "+data);
361
      if(data != null && !data.trim().equals("")){
362
    	  try{
363
    		  // try some common ISO 8601 formats
364
    		  SimpleDateFormat sdf = null;
365
    		  if (data.length() == 10) {
366
    			  sdf = new SimpleDateFormat("yyyy-MM-dd");
367
    		  } else if (data.length() == 19) {
368
    			  sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
369
    		  }
370
    		  Date dateData = sdf.parse(data);
371
    		  timestampData = new Timestamp(dateData.getTime());
372
              pstmt = connection.prepareStatement(
373
                  "INSERT INTO xml_nodes " +
374
                  "(nodetype, nodename, docid, " +
375
                  "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatadate) " +
376
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
377
          } catch (ParseException pe) {
378
    	  	try{
379
                double numberData = Double.parseDouble(data);
380
                pstmt = connection.prepareStatement(
381
                    "INSERT INTO xml_nodes " +
382
                    "(nodetype, nodename, docid, " +
383
                    "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatanumerical) " +
384
                    "VALUES (?, ?, ?, ?, ?, ?, ?, "+ numberData +")");
385
            } catch (NumberFormatException nfe) {
386
                pstmt = connection.prepareStatement(
387
                    "INSERT INTO xml_nodes " +
388
                    "(nodetype, nodename,  docid, " +
389
                    "rootnodeid, parentnodeid, nodedata, nodeindex) " +
390
                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
391
            }
392
          }
393
        } else {
394
            pstmt = connection.prepareStatement(
395
                  "INSERT INTO xml_nodes " +
396
                  "(nodetype, nodename, docid, " +
397
                  "rootnodeid, parentnodeid, nodedata, nodeindex) " +
398
                  "VALUES (?, ?, ?, ?, ?, ?, ?)");
399
        }
317
  
318
      pstmt = connection.prepareStatement(
319
              "INSERT INTO xml_nodes " +
320
              "(nodetype, nodename, docid, " +
321
              "rootnodeid, parentnodeid, nodedata, nodeindex) " +
322
              "VALUES (?, ?, ?, ?, ?, ?, ?)");
400 323

  
401 324
       // Increase DBConnection usage count
402 325
      connection.increaseUsageCount(1);
......
409 332
      pstmt.setLong(5, getParentID());
410 333
      pstmt.setString(6, data);
411 334
      pstmt.setInt(7, getNodeIndex());
412
      if (timestampData != null) {
413
    	  pstmt.setTimestamp(8, timestampData);
414
      }
415 335

  
416 336
      // Do the insertion
417 337
      pstmt.execute();
......
419 339

  
420 340
      // get the generated unique id afterward
421 341
      nid = DatabaseService.getInstance().getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
422
      //should incease connection usage!!!!!!
423 342

  
424 343
    } catch (SQLException e) {
425 344
      System.out.println("Error in DBSaxNode.writeDTDNodeToDB");

Also available in: Unified diff