Project

General

Profile

Revision 5895

Added by berkley over 10 years ago

added code to do database query for listObjects

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
522 522
	        ObjectFormat of2 = ObjectFormat.convert("eml://ecoinformatics.org/eml-2.0.0");
523 523
	        //create docs at different times
524 524
	        Date d1 = new Date();
525
	        Identifier id1 = createDoc(token, getTestDoc(), of1);
525
	        Identifier id1 = createDoc(token, getTestDoc(EML2_1_0), of1);
526 526
            SystemMetadata sm1 = getSystemMetadata(token, id1);
527 527
            
528 528
            Date d2 = new Date();
529
            Identifier id2 = createDoc(token, getTestDoc(), of2);
529
            Identifier id2 = createDoc(token, getTestDoc(EML2_0_0), of2);
530 530
            makeDocPublic(token, id2, true);
531 531
            SystemMetadata sm2 = getSystemMetadata(token, id2);
532 532
            
533 533
            Date d3 = new Date();
534
            Identifier id3 = createDoc(token, getTestDoc(), of1);
534
            Identifier id3 = createDoc(token, getTestDoc(EML2_1_0), of1);
535 535
            makeDocPublic(token, id3, true);
536 536
            SystemMetadata sm3 = getSystemMetadata(token, id3);
537 537
              
538 538
            Date d4 = new Date();
539
            Identifier id4 = createDoc(token, getTestDoc(), of2);
539
            Identifier id4 = createDoc(token, getTestDoc(EML2_0_0), of2);
540 540
            makeDocPublic(token, id4, true);
541 541
            SystemMetadata sm4 = getSystemMetadata(token, id4);
542 542
            
543 543
            Date d5 = new Date();
544
            Identifier id5 = createDoc(token, getTestDoc(), of1);
544
            Identifier id5 = createDoc(token, getTestDoc(EML2_1_0), of1);
545 545
            makeDocPublic(token, id5, true);
546 546
            SystemMetadata sm5 = getSystemMetadata(token, id5);  
547 547
             
......
602 602
            token = new AuthToken("public");
603 603
            ObjectList list7 = cs.listObjects(token, d1, d6, null);
604 604
            //System.out.println("list7 size: " + list7.sizeObjectInfoList());
605
            assertTrue(list7.sizeObjectInfoList() == 4);
605
            assertTrue(list7.sizeObjectInfoList() == 5);
606 606
            
607 607
            //test paging
608 608
            ObjectList list8 = cs.listObjects(token, d1, d6, null, false, 2, 2);
......
799 799
        return IOUtils.toString(gotDocStream);
800 800
	}
801 801
	
802
	private String getTestDoc()
803
	{
804
	    return getTestDoc(null);
805
	}
806
	
802 807
	/**
803
	 * return a test document
808
	 * return a test document.  objectFormat should come from MCTestCase
804 809
	 */
805
	private String getTestDoc()
810
	private String getTestDoc(String objectFormat)
806 811
	{
807
	    return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
812
	    if(objectFormat == null)
813
	    {
814
	        return "<?xml version=\"1.0\"?><test><somecontent>This is some content XXX</somecontent></test>\n";
815
	    }
816
	    
817
	    String accessBlock = getAccessBlock("public", true, true,
818
                false, false, false);
819
	    
820
	    
821
	    return getTestEmlDoc("Test identifier manager", objectFormat, null,
822
	                null, "http://fake.example.com/somedata", null,
823
	                accessBlock, null, null,
824
	                null, null);
808 825
	}
809 826
	
810 827
	/**
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
575 575
        ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
576 576
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
577 577
    {
578
      ObjectList ol = new ObjectList();
579
      final SessionData sessionData = getSessionData(token);
580
      int totalAfterQuery = 0;
578
      //ObjectList ol = new ObjectList();
579
      //final SessionData sessionData = getSessionData(token);
580
      //int totalAfterQuery = 0;
581 581
      
582
      try
583
      {
584
          if (PropertyService.getProperty("database.queryCacheOn").equals("true"))
585
          {
586
              //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
587
              DBQuery.clearQueryResultCache();
588
          }
589
      }
590
      catch (PropertyNotFoundException e1)
591
      {
592
          //just don't do anything
593
      }
594 582
      
595
      try
596
      {
597
          //TODO: Milliseconds need to be added to the dateFormat
598
          System.out.println("=========== Listing Objects =============");
599
          System.out.println("Current server time is: " + new Date());
600
          if(startTime != null)
601
          {
602
              System.out.println("query start time is " + startTime);
603
          }
604
          if(endTime != null)
605
          {
606
              System.out.println("query end time is " + endTime);
607
          }
608
          params.clear();
609
          params.put("qformat", new String[] {PropertyService.getProperty("crudService.listObjects.QFormat")});
610
          params.put("action", new String[] {"squery"});
611
          params.put("query", new String[] {createListObjectsPathQueryDocument()});
612
          
613
          /*System.out.println("query is: metacatUrl: " + metacatUrl + " user: " + sessionData.getUserName() +
614
                  " sessionid: " + sessionData.getId() + " params: ");
615
          String url = metacatUrl + "/metacat?action=query&sessionid=" + sessionData.getId();
616
          Enumeration keys = params.keys();
617
          while(keys.hasMoreElements())
618
          {
619
              String key = (String)keys.nextElement();
620
              String[] parr = params.get(key);
621
              for(int i=0; i<parr.length; i++)
622
              {
623
                  System.out.println("param " + key + ": " + parr[i]);
624
                  url += "&" + key + "=" + parr[i] ;
625
              }
626
          }
627
          System.out.println("query url: " + url);
628
          */
629
          String username = "public";
630
          String[] groups = null;
631
          String sessionid = "";
632
          if(sessionData != null)
633
          {
634
              username = sessionData.getUserName();
635
              groups = sessionData.getGroupNames();
636
              sessionid = sessionData.getId();
637
          }
638
          
639
          MetacatResultSet rs = handler.query(metacatUrl, params, username, 
640
                  groups, sessionid);
641
          List docs = rs.getDocuments();
642
          
643
          System.out.println("query returned " + docs.size() + " documents.");
644
          Vector<Document> docCopy = new Vector<Document>();
645
          
646
          //preparse the list to remove any that don't match the query params
647
          /* TODO: this type of query/subquery processing is probably not scalable
648
           * to larger object stores.  This code should be revisited.  The metacat
649
           * query handler should probably be altered to handle the type of query 
650
           * done here.
651
           */ 
652
          for(int i=0; i<docs.size(); i++)
653
          {
654
              Document d = (Document)docs.get(i);
655
              
656
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
657
              
658
              if(returnedObjectFormat != null && 
659
                 objectFormat != null && 
660
                 !objectFormat.toString().trim().equals(returnedObjectFormat.toString().trim()))
661
              { //make sure the objectFormat is the one specified
662
                  continue;
663
              }
664
              
665
              String dateSMM = d.getField("dateSysMetadataModified");
666
              if((startTime != null || endTime != null) && dateSMM == null)
667
              {  //if startTime or endTime are not null, we need a date to compare to
668
                  continue;
669
              }
670
              
671
              //date parse
672
              Date dateSysMetadataModified = null;
673
              if(dateSMM != null)
674
              {
675

  
676
                  /*                  
677
                  if(dateSMM.indexOf(".") != -1)
678
                  {  //strip the milliseconds
679
                      //TODO: don't do this. we need milliseconds now.
680
                      //TODO: explore ISO 8601 to figure out milliseconds
681
                      dateSMM = dateSMM.substring(0, dateSMM.indexOf(".")) + 'Z';
682
                  }
683
                  */
684
                  //System.out.println("dateSMM: " + dateSMM);
685
                  //dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-0"));
686
                  try
687
                  {   //the format we want
688
                      dateSysMetadataModified = dateFormat.parse(dateSMM);
689
                  }
690
                  catch(java.text.ParseException pe)
691
                  {   //try another legacy format
692
                      try
693
                      {
694
                          DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S'Z'");
695
                          dateFormat2.setTimeZone(TimeZone.getTimeZone("GMT-0"));
696
                          dateSysMetadataModified = dateFormat2.parse(dateSMM);
697
                      }
698
                      catch(java.text.ParseException pe2)
699
                      {
700
                          //try another legacy format
701
                          DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
702
                          dateFormat3.setTimeZone(TimeZone.getTimeZone("GMT-0"));
703
                          dateSysMetadataModified = dateFormat3.parse(dateSMM);
704
                      }
705
                      
706
                  }                  
707
              }
708
              
709
              /*System.out.println("====================================");
710
              System.out.println("doc number " + i);
711
              System.out.println("docid: " + d.docid);
712
              System.out.println("guid: " + d.getField("identifier").trim());
713
              System.out.println("dateSMM: " + dateSMM);
714
              System.out.println("dateSysMetadataModified: " + dateSysMetadataModified);
715
              System.out.println("startTime: " + startTime);
716
              System.out.println("endtime: " + endTime);*/
717
              
718
              int startDateComparison = 0;
719
              int endDateComparison = 0;
720
              if(startTime != null)
721
              {
722
                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
723
                  zTime.setTime(startTime);
724
                  startTime = zTime.getTime();
725
                  
726
                  if(dateSysMetadataModified == null)
727
                  {
728
                      startDateComparison = -1;
729
                  }
730
                  else
731
                  {
732
                      startDateComparison = dateSysMetadataModified.compareTo(startTime);
733
                  }
734
                  //System.out.println("startDateCom: " + startDateComparison);
735
              }
736
              else
737
              {
738
                  startDateComparison = 1;
739
              }
740
              
741
              if(endTime != null)
742
              {
743
                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
744
                  zTime.setTime(endTime);
745
                  endTime = zTime.getTime();
746
                  
747
                  if(dateSysMetadataModified == null)
748
                  {
749
                      endDateComparison = 1;
750
                  }
751
                  else
752
                  {
753
                      endDateComparison = dateSysMetadataModified.compareTo(endTime);
754
                  }
755
                  //System.out.println("endDateCom: " + endDateComparison);
756
              }
757
              else
758
              {
759
                  endDateComparison = -1;
760
              }
761
              
762
              
763
              if(startDateComparison < 0 || endDateComparison > 0)
764
              { 
765
                  continue;                  
766
              }
767
              
768
              docCopy.add((Document)docs.get(i));
769
          } //end pre-parse
770
          
771
          docs = docCopy;
772
          totalAfterQuery = docs.size();
773
          //System.out.println("total after subquery: " + totalAfterQuery);
774
          
775
          //make sure we don't run over the end
776
          int end = start + count;
777
          if(end > docs.size())
778
          {
779
              end = docs.size();
780
          }
781
          
782
          for(int i=start; i<end; i++)
783
          {
784
              //get the document from the result
785
              Document d = (Document)docs.get(i);
786
              //System.out.println("processing doc " + d.docid);
787
              
788
              String dateSMM = d.getField("dateSysMetadataModified");
789
              //System.out.println("dateSMM: " + dateSMM);
790
              //System.out.println("parsed date: " + parseDate(dateSMM));
791
              Date dateSysMetadataModified = null;
792
              if(dateSMM != null)
793
              {
794
                  try
795
                  {
796
                      dateSysMetadataModified = parseDate(dateSMM);
797
                  }
798
                  catch(Exception e)
799
                  { //if we fail to parse the date, just ignore the value
800
                      dateSysMetadataModified = null;
801
                  }
802
              }
803
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
804
                
805
              
806
              ObjectInfo info = new ObjectInfo();
807
              //add the fields to the info object
808
              Checksum cs = new Checksum();
809
              cs.setValue(d.getField("checksum"));
810
              String csalg = d.getField("algorithm");
811
              if(csalg == null)
812
              {
813
                  csalg = "MD5";
814
              }
815
              ChecksumAlgorithm ca = ChecksumAlgorithm.convert(csalg);
816
              cs.setAlgorithm(ca);
817
              info.setChecksum(cs);
818
              info.setDateSysMetadataModified(dateSysMetadataModified);
819
              Identifier id = new Identifier();
820
              id.setValue(d.getField("identifier").trim());
821
              info.setIdentifier(id);
822
              info.setObjectFormat(returnedObjectFormat);
823
              String size = d.getField("size");
824
              if(size != null)
825
              {
826
                  info.setSize(new Long(size.trim()).longValue());
827
              }
828
              //add the ObjectInfo to the ObjectList
829
              //logCrud.info("objectFormat: " + info.getObjectFormat().toString());
830
              //logCrud.info("id: " + info.getIdentifier().getValue());
831
              
832
              if(info.getIdentifier().getValue() != null)
833
              { //id can be null from tests.  should not happen in production.
834
                  if((info.getObjectFormat() != null && !info.getObjectFormat().toString().trim().equals("")))
835
                  { //objectFormat needs to not be null and not be an empty string
836
                    ol.addObjectInfo(info);
837
                  }
838
                  else
839
                  {
840
                      logCrud.info("Not adding object with null objectFormat" + info.getIdentifier().getValue().toString());
841
                  }
842
              }
843
             
844
          }
845
      }
846
      catch(Exception e)
847
      {
848
          e.printStackTrace();
849
          logCrud.error("Error creating ObjectList: " + e.getMessage() + " cause: " + e.getCause());
850
          throw new ServiceFailure("1580", "Error retrieving ObjectList: " + e.getMessage());
851
      }
852
      String username = "public";
853
      if(sessionData != null)
854
      {
855
          username = sessionData.getUserName();
856
      }
857
      EventLog.getInstance().log(metacatUrl,
858
              username, null, "read");
859
      logCrud.info("listObjects");
860
      if(totalAfterQuery < count)
861
      {
862
          count = totalAfterQuery;
863
      }
864
      ol.setCount(count);
865
      ol.setStart(start);
866
      ol.setTotal(totalAfterQuery);
867
      return ol;
583
      return IdentifierManager.getInstance().querySystemMetadata(startTime, endTime,
584
              objectFormat, replicaStatus, start, count);
585
      
586
      
587
      /////////////////////////////////////////////////////////////////////////
588
      ///////////////// OLD CODE //////////////////////////////////////////////
589
      /////////////////////////////////////////////////////////////////////////
590
      
591
//      try
592
//      {
593
//          if (PropertyService.getProperty("database.queryCacheOn").equals("true"))
594
//          {
595
//              //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
596
//              DBQuery.clearQueryResultCache();
597
//          }
598
//      }
599
//      catch (PropertyNotFoundException e1)
600
//      {
601
//          //just don't do anything
602
//      }
603
//      
604
//      try
605
//      {
606
//          //TODO: Milliseconds need to be added to the dateFormat
607
//          System.out.println("=========== Listing Objects =============");
608
//          System.out.println("Current server time is: " + new Date());
609
//          if(startTime != null)
610
//          {
611
//              System.out.println("query start time is " + startTime);
612
//          }
613
//          if(endTime != null)
614
//          {
615
//              System.out.println("query end time is " + endTime);
616
//          }
617
//          params.clear();
618
//          params.put("qformat", new String[] {PropertyService.getProperty("crudService.listObjects.QFormat")});
619
//          params.put("action", new String[] {"squery"});
620
//          params.put("query", new String[] {createListObjectsPathQueryDocument()});
621
//          
622
//          /*System.out.println("query is: metacatUrl: " + metacatUrl + " user: " + sessionData.getUserName() +
623
//                  " sessionid: " + sessionData.getId() + " params: ");
624
//          String url = metacatUrl + "/metacat?action=query&sessionid=" + sessionData.getId();
625
//          Enumeration keys = params.keys();
626
//          while(keys.hasMoreElements())
627
//          {
628
//              String key = (String)keys.nextElement();
629
//              String[] parr = params.get(key);
630
//              for(int i=0; i<parr.length; i++)
631
//              {
632
//                  System.out.println("param " + key + ": " + parr[i]);
633
//                  url += "&" + key + "=" + parr[i] ;
634
//              }
635
//          }
636
//          System.out.println("query url: " + url);
637
//          */
638
//          String username = "public";
639
//          String[] groups = null;
640
//          String sessionid = "";
641
//          if(sessionData != null)
642
//          {
643
//              username = sessionData.getUserName();
644
//              groups = sessionData.getGroupNames();
645
//              sessionid = sessionData.getId();
646
//          }
647
//          
648
//          MetacatResultSet rs = handler.query(metacatUrl, params, username, 
649
//                  groups, sessionid);
650
//          List docs = rs.getDocuments();
651
//          
652
//          System.out.println("query returned " + docs.size() + " documents.");
653
//          Vector<Document> docCopy = new Vector<Document>();
654
//          
655
//          //preparse the list to remove any that don't match the query params
656
//          /* TODO: this type of query/subquery processing is probably not scalable
657
//           * to larger object stores.  This code should be revisited.  The metacat
658
//           * query handler should probably be altered to handle the type of query 
659
//           * done here.
660
//           */ 
661
//          for(int i=0; i<docs.size(); i++)
662
//          {
663
//              Document d = (Document)docs.get(i);
664
//              
665
//              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
666
//              
667
//              if(returnedObjectFormat != null && 
668
//                 objectFormat != null && 
669
//                 !objectFormat.toString().trim().equals(returnedObjectFormat.toString().trim()))
670
//              { //make sure the objectFormat is the one specified
671
//                  continue;
672
//              }
673
//              
674
//              String dateSMM = d.getField("dateSysMetadataModified");
675
//              if((startTime != null || endTime != null) && dateSMM == null)
676
//              {  //if startTime or endTime are not null, we need a date to compare to
677
//                  continue;
678
//              }
679
//              
680
//              //date parse
681
//              Date dateSysMetadataModified = null;
682
//              if(dateSMM != null)
683
//              {
684
//
685
//                  /*                  
686
//                  if(dateSMM.indexOf(".") != -1)
687
//                  {  //strip the milliseconds
688
//                      //TODO: don't do this. we need milliseconds now.
689
//                      //TODO: explore ISO 8601 to figure out milliseconds
690
//                      dateSMM = dateSMM.substring(0, dateSMM.indexOf(".")) + 'Z';
691
//                  }
692
//                  */
693
//                  //System.out.println("dateSMM: " + dateSMM);
694
//                  //dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-0"));
695
//                  try
696
//                  {   //the format we want
697
//                      dateSysMetadataModified = dateFormat.parse(dateSMM);
698
//                  }
699
//                  catch(java.text.ParseException pe)
700
//                  {   //try another legacy format
701
//                      try
702
//                      {
703
//                          DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S'Z'");
704
//                          dateFormat2.setTimeZone(TimeZone.getTimeZone("GMT-0"));
705
//                          dateSysMetadataModified = dateFormat2.parse(dateSMM);
706
//                      }
707
//                      catch(java.text.ParseException pe2)
708
//                      {
709
//                          //try another legacy format
710
//                          DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
711
//                          dateFormat3.setTimeZone(TimeZone.getTimeZone("GMT-0"));
712
//                          dateSysMetadataModified = dateFormat3.parse(dateSMM);
713
//                      }
714
//                      
715
//                  }                  
716
//              }
717
//              
718
//              /*System.out.println("====================================");
719
//              System.out.println("doc number " + i);
720
//              System.out.println("docid: " + d.docid);
721
//              System.out.println("guid: " + d.getField("identifier").trim());
722
//              System.out.println("dateSMM: " + dateSMM);
723
//              System.out.println("dateSysMetadataModified: " + dateSysMetadataModified);
724
//              System.out.println("startTime: " + startTime);
725
//              System.out.println("endtime: " + endTime);*/
726
//              
727
//              int startDateComparison = 0;
728
//              int endDateComparison = 0;
729
//              if(startTime != null)
730
//              {
731
//                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
732
//                  zTime.setTime(startTime);
733
//                  startTime = zTime.getTime();
734
//                  
735
//                  if(dateSysMetadataModified == null)
736
//                  {
737
//                      startDateComparison = -1;
738
//                  }
739
//                  else
740
//                  {
741
//                      startDateComparison = dateSysMetadataModified.compareTo(startTime);
742
//                  }
743
//                  //System.out.println("startDateCom: " + startDateComparison);
744
//              }
745
//              else
746
//              {
747
//                  startDateComparison = 1;
748
//              }
749
//              
750
//              if(endTime != null)
751
//              {
752
//                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
753
//                  zTime.setTime(endTime);
754
//                  endTime = zTime.getTime();
755
//                  
756
//                  if(dateSysMetadataModified == null)
757
//                  {
758
//                      endDateComparison = 1;
759
//                  }
760
//                  else
761
//                  {
762
//                      endDateComparison = dateSysMetadataModified.compareTo(endTime);
763
//                  }
764
//                  //System.out.println("endDateCom: " + endDateComparison);
765
//              }
766
//              else
767
//              {
768
//                  endDateComparison = -1;
769
//              }
770
//              
771
//              
772
//              if(startDateComparison < 0 || endDateComparison > 0)
773
//              { 
774
//                  continue;                  
775
//              }
776
//              
777
//              docCopy.add((Document)docs.get(i));
778
//          } //end pre-parse
779
//          
780
//          docs = docCopy;
781
//          totalAfterQuery = docs.size();
782
//          //System.out.println("total after subquery: " + totalAfterQuery);
783
//          
784
//          //make sure we don't run over the end
785
//          int end = start + count;
786
//          if(end > docs.size())
787
//          {
788
//              end = docs.size();
789
//          }
790
//          
791
//          for(int i=start; i<end; i++)
792
//          {
793
//              //get the document from the result
794
//              Document d = (Document)docs.get(i);
795
//              //System.out.println("processing doc " + d.docid);
796
//              
797
//              String dateSMM = d.getField("dateSysMetadataModified");
798
//              //System.out.println("dateSMM: " + dateSMM);
799
//              //System.out.println("parsed date: " + parseDate(dateSMM));
800
//              Date dateSysMetadataModified = null;
801
//              if(dateSMM != null)
802
//              {
803
//                  try
804
//                  {
805
//                      dateSysMetadataModified = parseDate(dateSMM);
806
//                  }
807
//                  catch(Exception e)
808
//                  { //if we fail to parse the date, just ignore the value
809
//                      dateSysMetadataModified = null;
810
//                  }
811
//              }
812
//              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
813
//                
814
//              
815
//              ObjectInfo info = new ObjectInfo();
816
//              //add the fields to the info object
817
//              Checksum cs = new Checksum();
818
//              cs.setValue(d.getField("checksum"));
819
//              String csalg = d.getField("algorithm");
820
//              if(csalg == null)
821
//              {
822
//                  csalg = "MD5";
823
//              }
824
//              ChecksumAlgorithm ca = ChecksumAlgorithm.convert(csalg);
825
//              cs.setAlgorithm(ca);
826
//              info.setChecksum(cs);
827
//              info.setDateSysMetadataModified(dateSysMetadataModified);
828
//              Identifier id = new Identifier();
829
//              id.setValue(d.getField("identifier").trim());
830
//              info.setIdentifier(id);
831
//              info.setObjectFormat(returnedObjectFormat);
832
//              String size = d.getField("size");
833
//              if(size != null)
834
//              {
835
//                  info.setSize(new Long(size.trim()).longValue());
836
//              }
837
//              //add the ObjectInfo to the ObjectList
838
//              //logCrud.info("objectFormat: " + info.getObjectFormat().toString());
839
//              //logCrud.info("id: " + info.getIdentifier().getValue());
840
//              
841
//              if(info.getIdentifier().getValue() != null)
842
//              { //id can be null from tests.  should not happen in production.
843
//                  if((info.getObjectFormat() != null && !info.getObjectFormat().toString().trim().equals("")))
844
//                  { //objectFormat needs to not be null and not be an empty string
845
//                    ol.addObjectInfo(info);
846
//                  }
847
//                  else
848
//                  {
849
//                      logCrud.info("Not adding object with null objectFormat" + info.getIdentifier().getValue().toString());
850
//                  }
851
//              }
852
//             
853
//          }
854
//      }
855
//      catch(Exception e)
856
//      {
857
//          e.printStackTrace();
858
//          logCrud.error("Error creating ObjectList: " + e.getMessage() + " cause: " + e.getCause());
859
//          throw new ServiceFailure("1580", "Error retrieving ObjectList: " + e.getMessage());
860
//      }
861
//      String username = "public";
862
//      if(sessionData != null)
863
//      {
864
//          username = sessionData.getUserName();
865
//      }
866
//      EventLog.getInstance().log(metacatUrl,
867
//              username, null, "read");
868
//      logCrud.info("listObjects");
869
//      if(totalAfterQuery < count)
870
//      {
871
//          count = totalAfterQuery;
872
//      }
873
//      ol.setCount(count);
874
//      ol.setStart(start);
875
//      ol.setTotal(totalAfterQuery);
876
//      return ol;
868 877
    }
869 878
    
870 879
    /**
src/edu/ucsb/nceas/metacat/IdentifierManager.java
29 29
import java.sql.PreparedStatement;
30 30
import java.sql.ResultSet;
31 31
import java.sql.SQLException;
32
import java.sql.Timestamp;
32 33

  
33 34
import org.apache.log4j.Logger;
35
import org.dataone.service.types.Checksum;
36
import org.dataone.service.types.ChecksumAlgorithm;
37
import org.dataone.service.types.ObjectFormat;
38
import org.dataone.service.types.ObjectInfo;
39
import org.dataone.service.types.ObjectList;
34 40
import org.dataone.service.types.SystemMetadata;
41
import org.dataone.service.types.Identifier;
35 42

  
36 43
import edu.ucsb.nceas.metacat.database.DBConnection;
37 44
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
38 45
import edu.ucsb.nceas.metacat.properties.PropertyService;
46
import edu.ucsb.nceas.metacat.shared.ServiceException;
39 47
import edu.ucsb.nceas.metacat.util.DocumentUtil;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
40 49

  
41 50
/**
42 51
 * Manage the relationship between Metacat local identifiers (LocalIDs) that are
......
92 101
    }
93 102
    
94 103
    /**
104
     * return a hash of all of the info that is in the systemmetadata table
105
     * @param localId
106
     * @return
107
     */
108
    public Hashtable<String, String> getSystemMetadataInfo(String localId)
109
    throws McdbDocNotFoundException
110
    {
111
        try
112
        {
113
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
114
            localId = acc.getDocid();
115
        }
116
        catch(Exception e)
117
        {
118
            //do nothing. just try the localId as it is
119
        }
120
        Hashtable<String, String> h = new Hashtable<String, String>();
121
        String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
122
          "origin_member_node, authoritative_member_node, date_modified, submitter " +
123
          "from systemmetadata where docid = ?";
124
        DBConnection dbConn = null;
125
        int serialNumber = -1;
126
        try 
127
        {
128
            // Get a database connection from the pool
129
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getDocumentInfo");
130
            serialNumber = dbConn.getCheckOutSerialNumber();
131

  
132
            // Execute the insert statement
133
            PreparedStatement stmt = dbConn.prepareStatement(sql);
134
            stmt.setString(1, localId);
135
            ResultSet rs = stmt.executeQuery();
136
            if (rs.next()) 
137
            {
138
                String guid = rs.getString(1);
139
                Timestamp dateUploaded = rs.getTimestamp(2);
140
                String rightsHolder = rs.getString(3);
141
                String checksum = rs.getString(4);
142
                String checksumAlgorithm = rs.getString(5);
143
                String originMemberNode = rs.getString(6);
144
                String authoritativeMemberNode = rs.getString(7);
145
                Timestamp dateModified = rs.getTimestamp(8);
146
                String submitter = rs.getString(9);
147
                
148
                h.put("guid", guid);
149
                h.put("date_uploaded", new Long(dateUploaded.getTime()).toString());
150
                h.put("rights_holder", rightsHolder);
151
                h.put("checksum", checksum);
152
                h.put("checksum_algorithm", checksumAlgorithm);
153
                h.put("origin_member_node", originMemberNode);
154
                h.put("authoritative_member_node", authoritativeMemberNode);
155
                h.put("date_modified", new Long(dateModified.getTime()).toString());
156
                h.put("submitter", submitter);
157
                
158
                stmt.close();
159
            } 
160
            else
161
            {
162
                stmt.close();
163
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
164
                throw new McdbDocNotFoundException("2Could not find document " + localId);
165
            }
166
            
167
        } 
168
        catch (SQLException e) 
169
        {
170
            e.printStackTrace();
171
            logMetacat.error("Error while getting system metadata info for localid " + localId + " : "  
172
                    + e.getMessage());
173
        } 
174
        finally 
175
        {
176
            // Return database connection to the pool
177
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
178
        }
179
        return h;
180
    }
181
    
182
    /**
95 183
     * return information on the document with localId.  These are the fields
96 184
     * from the xml_documents table.  They can be used to contstruct metadata 
97 185
     * about the object that is stored.
......
708 796
    }
709 797
    
710 798
    /**
799
     * query the systemmetadata table based on the given parameters
800
     * @param startTime
801
     * @param endTime
802
     * @param objectFormat
803
     * @param replicaStatus
804
     * @param start
805
     * @param count
806
     * @return ObjectList
807
     */
808
    public ObjectList querySystemMetadata(Date startTime, Date endTime, 
809
            ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
810
    {
811
        ObjectList ol = new ObjectList();
812
        int total = 0;
813
        
814
        try
815
        {
816
            String sql = "select guid, date_uploaded, rights_holder, checksum, " +
817
                "checksum_algorithm, origin_member_node, authoritive_member_node, " +
818
                "date_modified, submitter, xml_documents.doctype from systemmetadata, " +
819
                "xml_documents where xml_documents.docid in " +
820
                "(select docid from identifier where guid = systemmetadata.guid)";
821
            
822
            boolean f1 = false;
823
            boolean f2 = false;
824
            boolean f3 = false;
825
            
826
            if(startTime != null)
827
            {
828
                sql += " and systemmetadata.date_modified > ?";
829
                f1 = true;
830
            }
831
            
832
            if(endTime != null)
833
            {
834
                sql += " and systemmetadata.date_modified < ?";
835
                f2 = true;
836
            }
837
            
838
            if(objectFormat != null)
839
            {
840
                sql += " and xml_documents.doctype = ?";
841
                f3 = true;
842
            }
843
            
844
            if(replicaStatus)
845
            {
846
                String currentNodeId = PropertyService.getInstance().getProperty("dataone.memberNodeId");
847
                sql += " and authoritive_member_node != '" + currentNodeId.trim() + "'";
848
            }
849
            
850
            DBConnection dbConn = DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
851
            int serialNumber = dbConn.getCheckOutSerialNumber();
852
            PreparedStatement stmt = dbConn.prepareStatement(sql);
853
            
854
            if(f1 && f2 && f3)
855
            {
856
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
857
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
858
                stmt.setString(3, objectFormat.toString());
859
            }
860
            else if(f1 && f2 && !f3)
861
            {
862
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
863
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
864
            }
865
            else if(f1 && !f2 && f3)
866
            {
867
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
868
                stmt.setString(2, objectFormat.toString());
869
            }
870
            else if(f1 && !f2 && !f3)
871
            {
872
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
873
            }
874
            else if(!f1 && f2 && f3)
875
            {
876
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
877
                stmt.setString(2, objectFormat.toString());
878
            }
879
            else if(!f1 && !f2 && f3)
880
            {
881
                stmt.setString(1, objectFormat.toString());
882
            }
883
            else if(!f1 && f2 && !f3)
884
            {
885
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
886
            }
887
            
888
            //System.out.println("LISTOBJECTS QUERY: " + stmt.toString());
889
            
890
            ResultSet rs = stmt.executeQuery();
891
            for(int i=0; i<start - 1; i++)
892
            {
893
                rs.next();
894
            }
895
            int countIndex = 0;
896
                        
897
            while(rs.next()) 
898
            {
899
                total++;
900
                if(countIndex >= count)
901
                {
902
                    break;
903
                }
904
                String guid = rs.getString(1);
905
                //System.out.println("query found doc with guid " + guid);
906
                //Timestamp dateUploaded = rs.getTimestamp(2);
907
                //String rightsHolder = rs.getString(3);
908
                String checksum = rs.getString(4);
909
                String checksumAlgorithm = rs.getString(5);
910
                //String originMemberNode = rs.getString(6);
911
                String authoritiveMemberNode = rs.getString(7);
912
                Timestamp dateModified = rs.getTimestamp(8);
913
                //String submitter = rs.getString(9);
914
                String format = rs.getString(10);
915
                
916
                ObjectInfo oi = new ObjectInfo();
917
                
918
                Identifier id = new Identifier();
919
                id.setValue(guid);
920
                oi.setIdentifier(id);
921
                
922
                oi.setDateSysMetadataModified(new Date(dateModified.getTime()));
923
                
924
                Checksum cs = new Checksum();
925
                cs.setValue(checksum);
926
                cs.setAlgorithm(ChecksumAlgorithm.convert(checksumAlgorithm));
927
                oi.setChecksum(cs);
928
                
929
                ObjectFormat oFormat = ObjectFormat.convert(format);
930
                if(oFormat != null)
931
                {
932
                    oi.setObjectFormat(oFormat);
933
                }
934
                
935
                ol.addObjectInfo(oi);
936
                countIndex++;
937
            }
938
            
939
            while(rs.next())
940
            { //expend the resultset to get the total count of possible rows
941
                total++;
942
            }
943
        }
944
        catch(Exception e)
945
        {
946
           e.printStackTrace();
947
           System.out.println("Error querying system metadata: " + e.getMessage());
948
        }
949
        
950
        System.out.println("listObjects total: " + total);
951
        ol.setStart(start);
952
        ol.setCount(count);
953
        ol.setTotal(total);
954
        
955
        return ol;
956
    }
957
    
958
    /**
711 959
     * create a mapping in the systemmetadata or identifier table
712 960
     * @param guid
713 961
     * @param localId
src/edu/ucsb/nceas/metacat/replication/ReplicationHandler.java
427 427
        {
428 428
            System.out.println("creating mapping for systemMetadata: guid: " + guid + " localId: " + docinfoHash.get("docid"));
429 429
            idman.createSystemMetadataMapping(guid, docinfoHash.get("docid"));
430
            Long dateUploadedLong = new Long(docinfoHash.get("date_uploaded"));
431
            Long dateModifiedLong = new Long(docinfoHash.get("modified_date"));
432
            idman.insertAdditionalSystemMetadataFields(
433
                    dateUploadedLong.longValue(), 
434
                    docinfoHash.get("rights_holder"),
435
                    docinfoHash.get("checksum"), 
436
                    docinfoHash.get("checksum_algorithm"), 
437
                    docinfoHash.get("origin_member_node"),
438
                    docinfoHash.get("authoritative_member_node"), 
439
                    dateModifiedLong.longValue(),
440
                    docinfoHash.get("submitter"),
441
                    docinfoHash.get("guid"));
430 442
        }
431 443
        else
432 444
        {
src/edu/ucsb/nceas/metacat/replication/ReplicationService.java
45 45
import java.sql.SQLException;
46 46
import java.text.SimpleDateFormat;
47 47
import java.util.Date;
48
import java.util.Enumeration;
48 49
import java.util.Hashtable;
49 50
import java.util.Timer;
50 51
import java.util.Vector;
......
966 967
			  String guid = idman.getGUID(doc.getDocID(), doc.getRev());
967 968
			  sb.append("<guid>").append(guid).append("</guid>");
968 969
			  String smLocalId = idman.getSystemMetadataLocalId(guid);
970
			  Hashtable<String, String> sysmetaInfo = idman.getSystemMetadataInfo(smLocalId);
969 971
			  if(smLocalId != null && !smLocalId.trim().equals(""))
970 972
			  {
971 973
			      sb.append("<systemmetadatalocalid>").append(smLocalId).append("</systemmetadatalocalid>");
972 974
			  }
975
			  
976
			  Enumeration<String> sysmetaKeys = sysmetaInfo.keys();
977
			  while(sysmetaKeys.hasMoreElements())
978
			  {
979
			      String key = sysmetaKeys.nextElement();
980
			      sb.append("<" + key + ">" + sysmetaInfo.get(key).toString() + "</" + key + ">");
981
			  }
982
			  
973 983
			}
974 984
			catch(McdbDocNotFoundException e)
975 985
			{

Also available in: Unified diff