Project

General

Profile

« Previous | Next » 

Revision 5350

Added by berkley almost 14 years ago

amost have update working. still need to get unit test squared away.

View differences:

CrudService.java
35 35
import java.util.Enumeration;
36 36
import java.util.Hashtable;
37 37
import java.util.Timer;
38
import java.util.List;
38 39

  
39 40
import javax.servlet.ServletContext;
40 41
import javax.servlet.http.HttpServletRequest;
......
211 212
        if (isScienceMetadata) {
212 213
            // CASE METADATA:
213 214
            try {
214
                String xml = IOUtils.toString(object);
215
                this.insertDocument(xml, guid, sessionData);
215
                this.insertDocument(object, guid, sessionData);
216 216
            } catch (IOException e) {
217 217
                String msg = "Could not create string from XML stream: " +
218 218
                    " " + e.getMessage();
......
372 372
        }                
373 373
    }
374 374

  
375
    /**
376
     * update an existing object with a new object.  Change the system metadata
377
     * to reflect the changes and update it as well.
378
     */
375 379
    public Identifier update(AuthToken token, Identifier guid, 
376 380
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
377 381
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
378 382
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
379 383
            NotImplemented {
380
        throw new NotImplemented(1000, "This method not yet implemented.");
384
        try
385
        {
386
            SessionData sessionData = getSessionData(token);
387
            
388
            //find the old systemmetadata (sm.old) document id (the one linked to obsoletedGuid)
389
            SystemMetadata sm = getSystemMetadata(token, obsoletedGuid);
390
            System.out.println("CS: got sm for obsoleteGuid: " + obsoletedGuid.getValue());
391
            //change sm.old's obsoletedBy field 
392
            List l = sm.getObsoletedByList();
393
            l.add(guid);
394
            sm.setObsoletedByList(l);
395
            System.out.println("CS: set obsoletedBy to id: " + ((Identifier)l.get(0)).getValue());
396
            //update sm.old
397
            System.out.println("CS: updating system metadata...");
398
            updateSystemMetadata(sm, sessionData);
399
            System.out.println("CS: updated system metadata.");
400
            //change the obsoletes field of the new systemMetadata (sm.new) to point to the id of the old one
401
            System.out.println("CS: Adding obsoleteGuid to new sysmeta");
402
            sysmeta.addObsolete(obsoletedGuid);
403
            //insert sm.new
404
            System.out.println("CS: inserting new sysmeta");
405
            insertSystemMetadata(sysmeta, sessionData);
406
            //call update with sm.new.object.id for object
407
            //System.out.println("CS: updating old sm");
408
            //updateSystemMetadata(sm, sessionData);
409
            System.out.println("CS: updating doc");
410
            updateDocument(object, obsoletedGuid, guid, sessionData);
411
            System.out.println("CS: Done updating document.");
412
            return guid;
413
        }
414
        catch(Exception e)
415
        {
416
            throw new ServiceFailure(1030, "Error updating document in CrudService: " + e.getMessage());
417
        }
381 418
    }
382 419

  
383 420
    /*
......
531 568
        return newFile;
532 569
    }
533 570

  
571
    /**
572
     * insert a systemMetadata doc
573
     */
534 574
    private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) 
535 575
        throws ServiceFailure {
536 576
        logMetacat.debug("Starting to insert SystemMetadata...");
......
549 589
    }
550 590
    
551 591
    /**
592
     * update a systemMetadata doc
593
     */
594
    private void updateSystemMetadata(SystemMetadata sm, SessionData sessionData)
595
      throws ServiceFailure
596
    {
597
        try
598
        {
599
            System.out.println("Updating SystemMetadata with id " + sm.getIdentifier().getValue());
600
            String smId = IdentifierManager.getInstance().getSystemMetadataId(sm.getIdentifier().getValue());
601
            System.out.println("smId: " + smId);
602
            sm.setDateSysMetadataModified(new Date());
603
            String xml = new String(serializeSystemMetadata(sm).toByteArray());
604
            Identifier id = new Identifier();
605
            id.setValue(smId);
606
            System.out.println("updating sysmeta doc with new id " + id.getValue());
607
            String localId = updateDocument(xml, id, null, sessionData);
608
            System.out.println("new updated localId: " + localId);
609
            System.out.println("CS: updateDocument returned localId: " + localId);
610
            IdentifierManager.getInstance().updateSystemMetadataMapping(sm.getIdentifier().getValue(), localId);
611
        }
612
        catch(Exception e)
613
        {
614
            throw new ServiceFailure(1030, "Error updating system metadata: " + e.getMessage());
615
        }
616
    }
617
    
618
    /**
619
     * insert a document
620
     * NOTE: this method shouldn't be used from the update or create() methods.  
621
     * we shouldn't be putting the science metadata or data objects into memory.
622
     */
623
    private String insertDocument(String xml, Identifier guid, SessionData sessionData)
624
        throws ServiceFailure
625
    {
626
        return insertOrUpdateDocument(xml, guid, sessionData, "insert");
627
    }
628
    
629
    /**
630
     * insert a document from a stream
631
     */
632
    private String insertDocument(InputStream is, Identifier guid, SessionData sessionData)
633
      throws IOException, ServiceFailure
634
    {
635
        //HACK: change this eventually.  we should not be converting the stream to a string
636
        String xml = IOUtils.toString(is);
637
        return insertDocument(xml, guid, sessionData);
638
    }
639
    
640
    /**
641
     * update a document
642
     * NOTE: this method shouldn't be used from the update or create() methods.  
643
     * we shouldn't be putting the science metadata or data objects into memory.
644
     */
645
    private String updateDocument(String xml, Identifier obsoleteGuid, Identifier guid, SessionData sessionData)
646
        throws ServiceFailure
647
    {
648
        return insertOrUpdateDocument(xml, obsoleteGuid, sessionData, "update");
649
    }
650
    
651
    /**
652
     * update a document from a stream
653
     */
654
    private String updateDocument(InputStream is, Identifier obsoleteGuid, Identifier guid, SessionData sessionData)
655
      throws IOException, ServiceFailure
656
    {
657
        //HACK: change this eventually.  we should not be converting the stream to a string
658
        String xml = IOUtils.toString(is);
659
        String localId = updateDocument(xml, obsoleteGuid, guid, sessionData);
660
        IdentifierManager im = IdentifierManager.getInstance();
661
        if(guid != null)
662
        {
663
          im.createMapping(guid.getValue(), localId);
664
        }
665
        return localId;
666
    }
667
    
668
    /**
552 669
     * insert a document, return the id of the document that was inserted
553 670
     */
554
    private String insertDocument(String xml, Identifier guid, SessionData sessionData) 
671
    private String insertOrUpdateDocument(String xml, Identifier guid, SessionData sessionData, String insertOrUpdate) 
555 672
        throws ServiceFailure {
556 673
        logMetacat.debug("Starting to insert xml document...");
557 674
        IdentifierManager im = IdentifierManager.getInstance();
558 675

  
559 676
        // generate guid/localId pair for sysmeta
560
        String localId = im.generateLocalId(guid.getValue(), 1);
677
        String localId = null;
678
        if(insertOrUpdate.equals("insert"))
679
        {
680
            localId = im.generateLocalId(guid.getValue(), 1);
681
        }
682
        else
683
        {
684
            //localid should already exist in the identifier table, so just find it
685
            try
686
            {
687
                localId = im.getLocalId(guid.getValue());
688
                //increment the revision
689
                String docid = localId.substring(0, localId.lastIndexOf("."));
690
                String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
691
                int rev = new Integer(revS).intValue();
692
                rev++;
693
                docid = docid + "." + rev;
694
                localId = docid;
695
            }
696
            catch(McdbDocNotFoundException e)
697
            {
698
                throw new ServiceFailure(1030, "CrudService.insertOrUpdateDocument(): " +
699
                    "guid " + guid.getValue() + " should have been in the identifier table, but it wasn't: " + e.getMessage());
700
            }
701
        }
561 702
        logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
562 703
                localId);
563 704

  
564 705
        String[] action = new String[1];
565
        action[0] = "insert";
706
        action[0] = insertOrUpdate;
566 707
        params.put("action", action);
567 708
        String[] docid = new String[1];
709
        System.out.println(insertOrUpdate + "ing with localId " + localId);
568 710
        docid[0] = localId;
569 711
        params.put("docid", docid);
570 712
        String[] doctext = new String[1];

Also available in: Unified diff