Project

General

Profile

« Previous | Next » 

Revision 1031

Added by Jing Tao over 22 years ago

Using DocumentImpl.writeDataFile method in handleReplicateDataFileRequest. Make code simple.

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
514 514
      String docType = (String)docinfoHash.get("doctype");
515 515
     
516 516
      
517
      conn = util.openDBConnection();
517
      conn = util.getConnection();
518 518
      
519 519
      //if action is delete, we don't delete the data file. Just archieve
520 520
      //the xml_documents
......
522 522
      {
523 523
        DocumentImpl.delete(conn,docid,user,null);
524 524
      }
525
      else if (dbaction.equals("insert"))
525
      //To data file insert or update is same
526
      else if (dbaction.equals("insert")||dbaction.equals("update"))
526 527
      {
527
        //register data information into xml_documents table
528
        DocumentImpl.registerDocument(docName,docType,docid,user,serverCode);
529
      
530 528
        //Get data file and store it into local file system.
531 529
        // sending back readdata request to server
532 530
        URL url = new URL("https://" + server + "?server="
533 531
                +util.getLocalReplicationServerName()
534 532
                +"&action=readdata&docid=" + docid);
535
        //Create file to store the data.
536
        String datafilepath = util.getOption("datafilepath");
537
        File dataDirectory = new File(datafilepath);
538
        //dataDirectory.mkdirs();
539
        File newFile = new File(dataDirectory, docid); 
540
       
541
        // create a buffered byte output stream
542
        // that uses a default-sized output buffer
543
        FileOutputStream fos = new FileOutputStream(newFile);
544
        BufferedOutputStream outPut = new BufferedOutputStream(fos);
545

  
546
        BufferedInputStream bis = null;
547
        bis = new BufferedInputStream(url.openStream());
548
        byte[] buf = new byte[4 * 1024]; // 4K buffer
549
        int b = bis.read(buf);
550
       
551
        while (b != -1) 
552
        {
553
          
554
          outPut.write(buf, 0, b);
555
          b = bis.read(buf);
556
        }
557
        bis.close();
558
	      outPut.close();
559
	      fos.close();
560
      
533
        String datafilePath = util.getOption("datafilepath");
534
        //register data file into xml_documents table and wite data file
535
        //into file system
536
        DocumentImpl.writeDataFile(url.openStream(), datafilePath, docName,
537
                              docType, docid, user,serverCode);
561 538
     }
562
     else if (dbaction.equals("update"))
563
     {
564
        //archieve old entry
565
        DocumentImpl.unRegisterDocument(conn,docid,user,null);
566
        //register new one
567
        DocumentImpl.registerDocument(docName,docType,docid,user,serverCode);
568
        // sending back readdata request to server
569
        URL url = new URL("https://" + server + "?server="
570
                +util.getLocalReplicationServerName()
571
                +"&action=readdata&docid=" + docid);
572
        //Create file to store the data.
573
        String datafilepath = util.getOption("datafilepath");
574
        File dataDirectory = new File(datafilepath);
575
        //dataDirectory.mkdirs();
576
        File newFile = new File(dataDirectory, docid);
577
        
578
        // create a buffered byte output stream
579
        // that uses a default-sized output buffer
580
        FileOutputStream fos = new FileOutputStream(newFile);
581
        BufferedOutputStream outPut = new BufferedOutputStream(fos);
582

  
583
        BufferedInputStream bis = null;
584
       
585
        bis = new BufferedInputStream(url.openStream());
586
        byte[] buf = new byte[4 * 1024]; // 4K buffer
587
        int b = bis.read(buf);
588
        System.out.println("before the while loop");
589
        while (b != -1) 
590
        {
591
          outPut.write(buf, 0, b);
592
          b = bis.read(buf);
593
        }
594
        bis.close();
595
	      outPut.close();
596
	      fos.close();
597
        
598
      
599
    }
600
    conn.close();
601 539
    
540
    util.returnConnection(conn);
541
    
602 542
    MetacatReplication.replLog("document " + docid + " added to DB with " +
603 543
                                 "action " + dbaction);
604 544
    } catch(Exception e) 

Also available in: Unified diff