Project

General

Profile

« Previous | Next » 

Revision 2167

Added by Jing Tao over 20 years ago

Fixed a bug that couldn't find eml2 data format.

View differences:

src/edu/ucsb/nceas/metacat/ContentTypeProvider.java
24 24
 * along with this program; if not, write to the Free Software
25 25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 26
 */
27
 
27

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

  
30 30
import java.io.StringReader;
......
67 67
  private String contentType = null;
68 68
  private String packageType = null;
69 69
  private Hashtable contentTypeHash = new Hashtable();
70
  
70

  
71 71
  //Constant
72 72
  private String BETA = "beta";
73 73
  private String EML2 = "eml2";
......
92 92
  private String ZIPTYPE    = "application/x-zip-compressed";
93 93
  private String BINARY     = "binary";
94 94
  private String BINARYTYPE = "application/octet-stream";
95
  
95

  
96 96
  private String ENTITYDOCTYPE = "entitydoctype";
97 97
  private String PHYSICALDOCTYPE = "physicaldoctype";
98 98
  private String EML2DOCTYPE = "eml2namespace";
......
101 101
  private String EXTENALFORMAT = "externallyDefinedFormat";
102 102
  private String FORMATNAME = "formatName";
103 103
  private String BINARYRASTERFORMAT = "binaryRasterFormat";
104
  
104

  
105 105
  private String DATAFILEPATH ="//physical/distribution/online/url";
106
 
106

  
107 107
  /**
108 108
   * Constructor of ContentTypeProvider
109 109
   */
......
113 113
    //get relative doclist for data file and package type
114 114
    Vector docLists = null;
115 115
    docLists = getRelativeDocIdList(dataFileId);
116
       
116

  
117 117
    if ( packageType == null)
118 118
    {
119 119
      // other situation, contenetype is default value
......
129 129
      // if no physical docid assign to this data file, content type is default
130 130
      if (physicalDocId == null)
131 131
      {
132
        
132

  
133 133
        contentType = DEFAULTCONTENTTYPE;
134 134
      }
135 135
      else
136 136
      {
137
        
137

  
138 138
        parsePhysicalDocumentForBeta(physicalDocId);
139 139
      }
140 140
    }
......
142 142
    {
143 143
      // for eml2 package
144 144
      // get eml document for data file
145
      String eml2Docid = getTargetDocIdForBeta(docLists, EML2DOCTYPE);
145
      //String eml2Docid = getTargetDocIdForBeta(docLists, EML2DOCTYPE);
146
      String eml2Docid = (String)docLists.elementAt(0);
146 147
      findContentTypeInEML2(eml2Docid);
147
      
148

  
148 149
    }
149
 
150

  
150 151
  }
151
  
152

  
152 153
  /** Method to get content type */
153 154
  public String getContentType()
154 155
  {
155 156
    return contentType;
156 157
  }//getContentType
157
  
158

  
158 159
  /* Method to find content type base on data format*/
159 160
  private void findContentTypeInEML2(String eml2DocId)
160 161
  {
162
    if (eml2DocId == null)
163
    {
164
      contentType = DEFAULTCONTENTTYPE;
165
      return;
166
    }
161 167
    DocumentImpl xmlDoc = null;
162 168
    String xmlString = null;
163 169
    StringReader read = null;
......
184 190
                         "findContentTypeInEML2()" + e.getMessage(), 30);
185 191
      return;
186 192
    }
187
    Node dataFormatNode = findDataFormatNodeInEML2(doc, DATAFILEPATH, 
193
    Node dataFormatNode = findDataFormatNodeInEML2(doc, DATAFILEPATH,
188 194
                                                   dataFileId);
195
    if (dataFormatNode == null)
196
    {
197
      contentType = DEFAULTCONTENTTYPE;
198
      MetaCatUtil.debugMessage("Couldn't find data format node", 30);
199
      return;
200

  
201
    }
189 202
    NodeList childList  = dataFormatNode.getChildNodes();
190 203
    // go through childList
191 204
    for (int i = 0; i<childList.getLength(); i++)
192 205
    {
193 206
      Node child = childList.item(i);
194
      
207

  
195 208
      // if has text format child set to text/plain
196 209
      if (child.getNodeName() != null && child.getNodeName().equals(TEXTFORMAT))
197 210
      {
198 211
        MetaCatUtil.debugMessage("in text format", 35);
199 212
        contentType = TEXTYPE;
200 213
      }
201
      
214

  
202 215
      //external format
203 216
      if (child.getNodeName() != null && child.getNodeName().equals(EXTENALFORMAT))
204 217
      {
......
212 225
          contentType = BINARYTYPE;
213 226
        }
214 227
      }
215
      
228

  
216 229
      // binaryRasterFormat
217 230
      if (child.getNodeName() != null && child.getNodeName().
218 231
          equals(BINARYRASTERFORMAT))
......
226 239
      contentType = DEFAULTCONTENTTYPE;
227 240
    }
228 241
  }
229
  
242

  
230 243
  /* Method get text value of given child tagname*/
231
  private String getTextValueForGivenChildTag(Node parentNode, 
244
  private String getTextValueForGivenChildTag(Node parentNode,
232 245
                                              String childTagName)
233 246
  {
234 247
    String textValue = null;
......
250 263
                             " is " + textValue, 30);
251 264
    return textValue;
252 265
  }//getTExtValueForGivenChildTag
253
  
266

  
254 267
  /* Find the data format node in eml2 document */
255
  private Node findDataFormatNodeInEML2(Document xml, String xPath, 
268
  private Node findDataFormatNodeInEML2(Document xml, String xPath,
256 269
                                       String targetDocId)
257 270
  {
258 271
    Node targetNode = null;
259 272
    Node node = findDataFileNodeInEML2(xml, xPath, targetDocId);
260
    // get the phycial the prent is online, grandparent is distribution
261
    // the grand'parent is physical
262
    Node phyicalNode = node.getParentNode().getParentNode().getParentNode();
263
    NodeList list = phyicalNode.getChildNodes();
264
    for (int i = 0; i<list.getLength(); i++)
273
    if (node != null)
265 274
    {
266
      Node kid = list.item(i);
267
      // find dataFormat node
268
      if (kid.getNodeType() == node.ELEMENT_NODE && 
269
          kid.getNodeName().equals(DATAFORMAT))
275
      // get the phycial the prent is online, grandparent is distribution
276
      // the grand'parent is physical
277
      Node phyicalNode = node.getParentNode().getParentNode().getParentNode();
278
      NodeList list = phyicalNode.getChildNodes();
279
      for (int i = 0; i < list.getLength(); i++)
270 280
      {
271
        targetNode = kid;
272
        break;
273
      }//if
274
    }//for
275
    MetaCatUtil.debugMessage("dataFormat node'name: "+ 
276
                             targetNode.getNodeName(), 35);
281
        Node kid = list.item(i);
282
        // find dataFormat node
283
        if (kid.getNodeType() == node.ELEMENT_NODE &&
284
            kid.getNodeName().equals(DATAFORMAT))
285
        {
286
          targetNode = kid;
287
          break;
288
        } //if
289
      } //for
290
      if (targetNode != null)
291
      {
292
        MetaCatUtil.debugMessage("dataFormat node'name: " +
293
                                 targetNode.getNodeName(), 35);
294
      }
295
    }//if
277 296
    return targetNode;
278 297
  }
279 298
  /* Find the datafile node */
280
  private Node findDataFileNodeInEML2(Document xml, String xPath, 
299
  private Node findDataFileNodeInEML2(Document xml, String xPath,
281 300
                                String targetDocId)
282 301
  {
283 302
    Node dataFileNode = null;
......
293 312
      return dataFileNode;
294 313
    }
295 314
    // go through the list and find target docid in online/url
296
    for (int i = 0; i<list.getLength(); i++)
315
    if (list != null)
297 316
    {
298
      Node node = list.item(i);
299
      Node textNode = node.getFirstChild();
300
      if (textNode.getNodeType() == node.TEXT_NODE)
317
      for (int i = 0; i < list.getLength(); i++)
301 318
      {
302
        String URLData = textNode.getNodeValue();
303
        MetaCatUtil.debugMessage("online/url text data: " + URLData, 30);
304
        //Only handle data file in local metacat server
305
        if (URLData.indexOf(MetaCatUtil.getOption("httpserver")) != -1 || 
306
            URLData.indexOf(MetaCatUtil.getOption("server")) != -1)
319
        Node node = list.item(i);
320
        Node textNode = node.getFirstChild();
321
        if (textNode.getNodeType() == node.TEXT_NODE)
307 322
        {
308
          // Get docid from url
309
          String docId =MetaCatUtil.getDocIdWithRevFromOnlineURL(URLData);
310
          // Get rid of revision
311
          docId = MetaCatUtil.getDocIdFromString(docId);
312
          MetaCatUtil.debugMessage("docid from url element in xml is: "+
313
                                   docId, 30);
314
          //if this docid equals target one, we find it
315
          if (docId != null && docId.equals(targetDocId))
323
          String URLData = textNode.getNodeValue();
324
          MetaCatUtil.debugMessage("online/url text data: " + URLData, 30);
325
          //Only handle ecogrid data file
326
          if (URLData.indexOf(DBSAXHandler.ECOGRID) != -1 )
316 327
          {
317
            MetaCatUtil.debugMessage("Find target docid in online/url: "+
318
                                      docId, 30);
319
            dataFileNode = node;
320
            break;
321
          }
322
        }//if
323
        
324
      }//if
325
    }//for
326
    MetaCatUtil.debugMessage("online/url node's name: " +
327
                             dataFileNode.getNodeName(), 35);
328
            // Get docid from url
329
            String docId = MetaCatUtil.
330
                               getAccessionNumberFromEcogridIdentifier(URLData);
331
            // Get rid of revision
332
            docId = MetaCatUtil.getDocIdFromAccessionNumber(docId);
333
            MetaCatUtil.debugMessage("docid from url element in xml is: " +
334
                                     docId, 30);
335
            //if this docid equals target one, we find it
336
            if (docId != null && docId.equals(targetDocId))
337
            {
338
              MetaCatUtil.debugMessage("Find target docid in online/url: " +
339
                                       docId, 30);
340
              dataFileNode = node;
341
              break;
342
            }
343
          } //if
344

  
345
        } //if
346
      } //for
347
    }//if
348

  
328 349
    return dataFileNode;
329 350
  }//findDataFileNode
330
  
351

  
331 352
  /* Get relative docid list and packagetype */
332
  private Vector getRelativeDocIdList(String id) 
353
  private Vector getRelativeDocIdList(String id)
333 354
  {
334 355
    Vector docList = new Vector();
335
    String sql = "SELECT packagetype, subject from xml_relation " + 
356
    String sql = "SELECT packagetype, subject from xml_relation " +
336 357
                 "where object = ?";
337 358
    ResultSet rs = null;
338 359
    PreparedStatement pStmt=null;
......
356 377
      {
357 378
        packType = rs.getString(1);
358 379
        String subject = rs.getString(2);
359
       
380

  
360 381
        // get rid of duplicate record and add the docid into vector
361 382
        if (!docList.contains(subject))
362 383
        {
363
          
384

  
364 385
          docList.add(subject);
365 386
        }
366 387
      }//while
367
      
388

  
368 389
      // set up data package type
369 390
      if ((MetaCatUtil.getOptionList(MetaCatUtil.getOption("packagedoctype"))).
370 391
                                     contains(packType))
......
380 401
        MetaCatUtil.debugMessage("This is EML2 package", 30);
381 402
        packageType = EML2;
382 403
      }
383
     
384
        
404

  
405

  
385 406
    }//try
386 407
    catch(SQLException e)
387 408
    {
388
       
409

  
389 410
      MetaCatUtil.debugMessage("ContenTypProvider.getRelativeDoclist1 " +
390 411
                             e.getMessage(), 30);
391 412
    }//catch
......
405 426
        DBConnectionPool.returnDBConnection(conn, serialNumber);
406 427
      }
407 428
    }//finally
408
      
429

  
409 430
    return docList;
410 431
  }// getRelativeDocIdList
411
  
432

  
412 433
  /* Method to get physical document for data file in xml_documents table for
413 434
   * beta eml package
414 435
   */
......
418 439
    // make sure list is not empty
419 440
    if (list.isEmpty())
420 441
    {
421
      
442

  
422 443
      return docId;
423 444
    }
424 445
    // get sql command
......
461 482
         break;
462 483
       }
463 484
      }//while
464
    
485

  
465 486
    }//try
466 487
    catch(SQLException e)
467 488
    {
468
       
489

  
469 490
      MetaCatUtil.debugMessage("ContenTypProvider.setPhysicalDocIdForBeta1 " +
470 491
                             e.getMessage(), 30);
471 492
    }//catch
......
489 510
                             "for target doctype: "+targetType, 25);
490 511
    return docId;
491 512
  }
492
  
493 513

  
494
  
495
  
496
  /* Parser the beta physical document and find the value in format element*/ 
514

  
515

  
516

  
517
  /* Parser the beta physical document and find the value in format element*/
497 518
  private void parsePhysicalDocumentForBeta(String physicalDocid)
498 519
  {
499 520
    String xmlDoc = null;
......
511 532
    }
512 533
      // get format element's text value
513 534
    String format = getTextValueFromPath(new StringReader(xmlDoc), FORMATPATH);
514
    
535

  
515 536
    if (format == null)
516 537
    {
517 538
      // if couldn't find the format, set contentype default value;
......
529 550
      }//if
530 551
    }//else
531 552
  }//parsePhysicalDocumentForBeta
532
  
553

  
533 554
  private String getTextValueFromPath(StringReader xml, String xPath)
534 555
  {
535 556
    String textValue = null;
......
543 564
      {
544 565
        textValue = textNode.getNodeValue();// get value
545 566
      }
546
     
567

  
547 568
    }
548 569
    catch (Exception e)
549 570
    {
550 571
      MetaCatUtil.debugMessage("error in ContentTypeProvider."+
551 572
                               "getTextValueFromPath: "+e.getMessage(), 30);
552 573
    }
553
    MetaCatUtil.debugMessage("The text value for " + xPath + " is: "+ 
574
    MetaCatUtil.debugMessage("The text value for " + xPath + " is: "+
554 575
                              textValue, 30);
555 576
    return textValue;
556 577
  }//getTextValueFromPath
557
  
578

  
558 579
  /* A method to look up contentype */
559 580
  private String lookUpContentType(String format)
560 581
  {
......
567 588
                              type, 30);
568 589
    return type;
569 590
  }// lookupcontentypes
570
  
591

  
571 592
  /* Construct content type hashtable */
572 593
  private void constructContentHashTable()
573 594
  {
......
580 601
    contentTypeHash.put(TAR, TARTYPE);
581 602
    contentTypeHash.put(ZIP, ZIPTYPE);
582 603
    contentTypeHash.put(BINARY, BINARYTYPE);
583
    
604

  
584 605
  }//constructrContentHashTable();
585
  
586
  
587
  
606

  
607

  
608

  
588 609
  public static void main(String[] argus)
589 610
  {
590 611
     try
......
597 618
     }
598 619
     catch(Exception e)
599 620
     {
600
       MetaCatUtil.debugMessage("erorr in Schemalocation.main: " + 
621
       MetaCatUtil.debugMessage("erorr in Schemalocation.main: " +
601 622
                                e.getMessage(), 30);
602 623
     }
603 624
  }

Also available in: Unified diff