Project

General

Profile

« Previous | Next » 

Revision 779

Added by bojilova about 23 years ago

changes according to bug# 234 - metacat accession # handling, done

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
831 831
                              boolean validate)
832 832
                throws Exception
833 833
  {
834
    int rev = 1;
835
    String docid = null;
836
    MetaCatUtil util = new MetaCatUtil();
837
    String sep = util.getOption("accNumSeparator");
838
    
834
//    int rev = 1;
835
//    String docid = null;
836
//    MetaCatUtil util = new MetaCatUtil();
837
//    String sep = util.getOption("accNumSeparator");
838
/*    
839 839
    if ( accnum != null ) {
840 840
      // check the correctness of accnum;
841 841
      // split accnum in docid and rev in order to
......
846 846
      rev = (new Integer(id.getRev())).intValue();
847 847
      sep = id.getSeparator();
848 848
    }
849
    
849
*/    
850
    // OLD
850 851
    // Determine if the docid,rev are OK for INSERT or UPDATE
851 852
    // Generate new docid on INSERT, if one is not provided.
852
    AccessionNumber ac = new AccessionNumber(conn);
853
    docid = ac.generate(docid, java.lang.String.valueOf(rev), action); 
853
    //AccessionNumber ac = new AccessionNumber(conn);
854
    //docid = ac.generate(docid, java.lang.String.valueOf(rev), action); 
854 855
    
856
    // NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
857
    AccessionNumber ac = new AccessionNumber(conn, accnum, action);
858
    String docid = ac.getDocid();
859
    String rev = ac.getRev();
860
    
855 861
    MetaCatUtil.debugMessage("action: " + action + " servercode: " + 
856 862
                             serverCode + " override: " + override);
857 863
                        
......
863 869
      //merge the differences manually.
864 870
      int istreamInt; 
865 871
      char istreamChar;
866
      // NOT NEEDED
867
      //DocumentImpl newdoc = new DocumentImpl(conn, docid);
868
      //updaterev = newdoc.getRev();
869
      //String updaterev = rev;
872
      DocumentIdentifier id = new DocumentIdentifier(accnum);
873
      String updaterev = id.getRev();
870 874
      String server = MetacatReplication.getServer(serverCode);
871 875
      MetacatReplication.replLog("attempting to lock " + accnum);
872 876
      URL u = new URL("http://" + server + "?action=getlock&updaterev=" + 
873
                      rev + "&docid=" + docid);
877
                      updaterev + "&docid=" + docid);
874 878
      System.out.println("sending message: " + u.toString());
875 879
      String serverResStr = MetacatReplication.getURLContent(u);
876 880
      String openingtag = serverResStr.substring(0, serverResStr.indexOf(">")+1);
......
899 903
        //to come get a copy from here.
900 904
        ForceReplicationHandler frh = new ForceReplicationHandler(docid);
901 905
        
902
        rev++;
903
        return (docid + sep + rev);
906
        // OLD
907
        //rev++;
908
        //return (docid + sep + rev);
909
        return (accnum);
904 910
      }
905 911

  
906 912
      else if(openingtag.equals("<filelocked>"))
......
934 940
        throw new Exception("User " + user + 
935 941
              " does not have permission to update XML Document #" + accnum);
936 942
      }          
937
      rev++;
943
      //rev++;
938 944
    }
939 945

  
940 946
    try 
......
959 965
      ForceReplicationHandler frh = new ForceReplicationHandler(docid, action);
960 966
    }
961 967
      
962
    return (docid + sep + rev);
968
    //return (docid + sep + rev);
969
    return(accnum);
963 970
  }
964 971

  
965 972
  /**
......
972 979
                                 String user, String group )
973 980
                throws Exception 
974 981
  {
975
    DocumentIdentifier id = new DocumentIdentifier(accnum);
976
    String docid = id.getIdentifier();
977
    String rev = id.getRev();
982
    // OLD
983
    //DocumentIdentifier id = new DocumentIdentifier(accnum);
984
    //String docid = id.getIdentifier();
985
    //String rev = id.getRev();
978 986
    
987
    // OLD
979 988
    // Determine if the docid,rev are OK for DELETE
980
    AccessionNumber ac = new AccessionNumber(conn);
981
    docid = ac.generate(docid, rev, "DELETE");
989
    //AccessionNumber ac = new AccessionNumber(conn);
990
    //docid = ac.generate(docid, rev, "DELETE");
982 991

  
992
    // NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
993
    AccessionNumber ac = new AccessionNumber(conn, accnum, "DELETE");
994
    String docid = ac.getDocid();
995
    String rev = ac.getRev();
996
    
997

  
983 998
    // check for 'write' permission for 'user' to delete this document
984 999
    if ( !hasPermission(conn, user, group, docid) ) {
985 1000
      throw new Exception("User " + user + 
src/edu/ucsb/nceas/metacat/AccessionNumber.java
48 48
  private Connection conn = null;
49 49
  private String sitecode = null;
50 50
  private String sep = null;
51
  private String docid = null;
52
  private String rev = null;
51 53
    
52 54
  /** 
53 55
   * Construct an AccessionNumber 
......
55 57
  public AccessionNumber () 
56 58
         throws SQLException, ClassNotFoundException  
57 59
  {
58
    MetaCatUtil util = new MetaCatUtil();
59

  
60
    this.sitecode = util.getOption("sitecode");
61
    this.sep = util.getOption("accNumSeparator");
62

  
63
    // Open a connection to the database
64
    this.conn = util.openDBConnection();
60
    // Open a connection to the database from here
61
    this((new MetaCatUtil()).openDBConnection());
65 62
  }  
66 63

  
67 64
  /** 
......
71 68
   */
72 69
  public AccessionNumber (Connection conn) 
73 70
  {
74
    MetaCatUtil util = new MetaCatUtil();
75

  
76
    this.sitecode = util.getOption("sitecode");
77
    this.sep = util.getOption("accNumSeparator");
71
    this.sitecode = MetaCatUtil.getOption("sitecode");
72
    this.sep = MetaCatUtil.getOption("accNumSeparator");
78 73
    this.conn = conn;
79 74
  }
80 75

  
76
  /** NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
77
   * Construct an AccessionNumber
78
   *
79
   * @param conn the db connection to read Accession number from
80
   * @param accnum the accession number to be checked for validness
81
   */
82
  public AccessionNumber (Connection conn, String accnum, String action) 
83
                         throws AccessionNumberException, SQLException 
84
  {
85
    this(conn);
86

  
87
    this.rev = null;
88
    this.docid = accnum;
89
    if ( accnum != null ) {
90
      int firstIndex = accnum.indexOf(this.sep);
91
      int lastIndex = accnum.lastIndexOf(this.sep);
92
      if ( firstIndex != lastIndex ) {
93
        //this docid contains a revision number
94
        this.rev = accnum.substring(lastIndex + 1);
95
        this.docid = accnum.substring(0, lastIndex);
96
      }
97
    }
98
    
99
    // INSERT
100
    if ( action.equals("INSERT")) {
101

  
102
      // check accession number for validness
103
      if ( docid == null ) {
104
        throw new AccessionNumberException("Accession number is required.");
105

  
106
      // rev is not provided; throw an exception to prevent the insertion
107
      } else if ( rev == null ) {
108
        throw new AccessionNumberException
109
                  ("Revision number is required and must be 1.");
110

  
111
      // docid is used; throw an exception to prevent the insertion
112
      } else if ( accNumberUsed(docid) ) {
113
        throw new AccessionNumberException
114
                  ("Accession number " + docid + " is already in use.");
115
                    
116
      // rev is <> 1; throw an exception to prevent the insertion
117
      } else if ( !rev.equals("1") ) {
118
        throw new AccessionNumberException("Revision number must be 1.");
119
      }
120
          
121
    // UPDATE or DELETE
122
    } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
123
      String l_rev = "";
124

  
125
      // Accession# is not provided; throw an exception to prevent the action
126
      if ( docid == null ) {
127
        throw new AccessionNumberException("Accession number is required.");
128

  
129
      // rev is not provided; throw an exception to prevent the action
130
      } else if ( rev == null ) {
131
        throw new AccessionNumberException
132
                  ("Revision number is required.");
133

  
134
      // Accession# is not current (not in xml_documents); throw an exception
135
      } else if ( !accNumberIsCurrent(docid) ) {
136
        throw new AccessionNumberException
137
                  ("Document not found for Accession number " + docid);
138

  
139
      // Revision number is not the recent one; throw an exception
140
      } else if ( action.equals("UPDATE") &&
141
                  !rev.equals(l_rev = getNextRevision(docid)) ) {
142
        throw new AccessionNumberException
143
                  ("Next revision number must be "+ l_rev);
144

  
145
      // Revision number is not the recent one; throw an exception
146
      } else if ( action.equals("DELETE") && 
147
                  !rev.equals(l_rev = getLastRevision(docid)) ) {
148
        throw new AccessionNumberException
149
                  ("Last revision number is "+ l_rev);
150
      }
151
    }
152
  }
153

  
81 154
  /**
82 155
   * Generate an Accession Number of form <sidecode>.<uniqueid>.<revisionid>
83 156
   * where <revisionid> is always 1.
......
267 340
    return rev;
268 341
  }
269 342

  
343
  // get the next revision number for docid
344
  private String getNextRevision(String docid) throws SQLException
345
  {
346
    String rev = "";
347
    
348
    try {
349
      PreparedStatement pstmt;
350
      pstmt = conn.prepareStatement
351
              ("SELECT rev+1 FROM xml_documents WHERE docid='" + docid + "'");
352
      pstmt.execute();
353

  
354
      ResultSet rs = pstmt.getResultSet();
355
      boolean hasRow = rs.next();
356
      rev = rs.getString(1);
357
      pstmt.close();
358
      
359
    } catch (SQLException e) {
360
      throw new SQLException(
361
      "Error on AccessionNumber.getNextRevision(): " + e.getMessage());
362
    }
363

  
364
    return rev;
365
  }
366
  
367
  /**
368
   * returns the docid encoded in this accession number
369
   */
370
  public String getDocid() {
371

  
372
    return this.docid;
373
  }
374

  
375
  /**
376
   * returns the revision number encoded in this accession number
377
   */
378
  public String getRev()
379
  {
380
    return this.rev;
381
  }
382
  
270 383
}

Also available in: Unified diff