Project

General

Profile

« Previous | Next » 

Revision 734

Added by bojilova almost 23 years ago

- changes to expect revisionid to come from the client on INSERT/UPDATE/DELETE;
now the client should send accession# as:
sitecode.uniqueid.revisionid
INSERT checks if sitecode.uniqueid part does not exist in xml_documents and xml_revisions and revisionid=1
UPDATE/DELETE check if sitecode.uniqueid part is in xml_document and revisionid is the recent number
- keep the old implementation where the client can send accession# without revisionid

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
722 722
    }
723 723
  }
724 724

  
725
// DOCTITLE attr cleared from the db
726
//  /**
727
//   * Get the document title
728
//   */
729
//  public String getTitle() {
730
//    return doctitle;
731
//  }
732

  
733
// DOCTITLE attr cleared from the db
734
//  /**
735
//   * Set the document title
736
//   *
737
//   * @param title the new title for the document
738
//   */
739
//  public void setTitle( String title ) {
740
//    this.doctitle = title;
741
//    try {
742
//      PreparedStatement pstmt;
743
//      pstmt = conn.prepareStatement(
744
//            "UPDATE xml_documents " +
745
//            " SET doctitle = ? " +
746
//            "WHERE docid = ?");
747
//
748
//      // Bind the values to the query
749
//      pstmt.setString(1, doctitle);
750
//      pstmt.setString(2, docid);
751
//
752
//      // Do the insertion
753
//      pstmt.execute();
754
//      pstmt.close();
755
//    } catch (SQLException e) {
756
//      System.out.println("Error in DocumentImpl.setTitle: " + e.getMessage());
757
//    }
758
//  }
759

  
760 725
  /**
761 726
   * Write an XML file to the database, given a filename
762 727
   *
......
840 805
   * @param xml the xml stream to be loaded into the database
841 806
   * @param pub flag for public "read" access on xml document
842 807
   * @param dtd the dtd to be uploaded on server's file system
843
   * @param action the action to be performed (INSERT OR UPDATE)
844
   * @param docid the docid to use for the INSERT OR UPDATE
808
   * @param action the action to be performed (INSERT or UPDATE)
809
   * @param accnum the docid + rev# to use on INSERT or UPDATE
845 810
   * @param user the user that owns the document
846 811
   * @param group the group to which user belongs
847 812
   * @param serverCode the serverid from xml_replication on which this document
......
854 819
   */
855 820

  
856 821
  public static String write( Connection conn,Reader xml,String pub,Reader dtd,
857
                              String action, String docid, String user,
822
                              String action, String accnum, String user,
858 823
                              String group, int serverCode, boolean override,
859 824
                              boolean validate)
860 825
                throws Exception
861 826
  {
862
    //MOVED DOWN IN RelationHandler obj before write of relations
863
    //if(action.equals("UPDATE"))
864
    //{
865
    //  RelationHandler.deleteRelations(docid);
866
    //}
827
    String rev = "1";
828
    String docid = null;
829
    MetaCatUtil util = new MetaCatUtil();
830
    String sep = util.getOption("accNumSeparator");
867 831
    
868
    int updaterev;
869
    MetaCatUtil util = new MetaCatUtil();
870
        // Determine if the docid is OK for INSERT or UPDATE
832
    if ( accnum != null ) {
833
      // check the correctness of accnum;
834
      // split accnum in docid and rev in order to
835
      // preserve the current implementation of processing and storing,
836
      // but show the whole accnum to the client.
837
      DocumentIdentifier id = new DocumentIdentifier(accnum);
838
      docid = id.getIdentifier();
839
      rev = id.getRev();
840
      sep = id.getSeparator();
841
    }
842
    
843
    // Determine if the docid,rev are OK for INSERT or UPDATE
844
    // Generate new docid on INSERT, if one is not provided.
871 845
    AccessionNumber ac = new AccessionNumber(conn);
872
    String newdocid = ac.generate(docid, action);
846
    docid = ac.generate(docid, rev, action); 
873 847
    
874 848
    MetaCatUtil.debugMessage("action: " + action + " servercode: " + 
875 849
                             serverCode + " override: " + override);
......
882 856
      //merge the differences manually.
883 857
      int istreamInt; 
884 858
      char istreamChar;
885
      DocumentImpl newdoc = new DocumentImpl(conn, docid);
886
      updaterev = newdoc.getRev();
859
      // NOT NEEDED
860
      //DocumentImpl newdoc = new DocumentImpl(conn, docid);
861
      //updaterev = newdoc.getRev();
862
      String updaterev = rev;
887 863
      String server = MetacatReplication.getServer(serverCode);
888
      MetacatReplication.replLog("attempting to lock " + docid);
864
      MetacatReplication.replLog("attempting to lock " + accnum);
889 865
      URL u = new URL("http://" + server + "?action=getlock&updaterev=" + 
890 866
                      updaterev + "&docid=" + docid);
891 867
      System.out.println("sending message: " + u.toString());
......
896 872
      {//the lock was granted go ahead with the insert
897 873
        try 
898 874
        {
899
          MetacatReplication.replLog("lock granted for " + docid + " from " +
875
          MetacatReplication.replLog("lock granted for " + accnum + " from " +
900 876
                                      server);
901
          XMLReader parser = initializeParser(conn, action, newdocid, validate,
877
          XMLReader parser = initializeParser(conn, action, docid, validate,
902 878
                                              user, group, pub, serverCode, dtd);
903 879
          conn.setAutoCommit(false);
904 880
          parser.parse(new InputSource(xml)); 
......
916 892
        //to come get a copy from here.
917 893
        ForceReplicationHandler frh = new ForceReplicationHandler(docid);
918 894
        
919
        if ( (docid != null) && !(newdocid.equals(docid)) ) 
920
        {
921
          return new String("New document ID generated:" + newdocid);
922
        } 
923
        else 
924
        {
925
          return newdocid;
926
        }
895
        return (docid + sep + rev);
927 896
      }
897

  
928 898
      else if(openingtag.equals("<filelocked>"))
929 899
      {//the file is currently locked by another user
930 900
       //notify our user to wait a few minutes, check out a new copy and try
931 901
       //again.
932 902
        //System.out.println("file locked");
933
        MetacatReplication.replLog("lock denied for " + docid + " on " +
903
        MetacatReplication.replLog("lock denied for " + accnum + " on " +
934 904
                                   server + " reason: file already locked");
935 905
        throw new Exception("The file specified is already locked by another " +
936 906
                            "user.  Please wait 30 seconds, checkout the " +
......
941 911
      {//our file is outdated.  notify our user to check out a new copy of the
942 912
       //file and merge his version with the new version.
943 913
        //System.out.println("outdated file");
944
        MetacatReplication.replLog("lock denied for " + docid + " on " +
914
        MetacatReplication.replLog("lock denied for " + accnum + " on " +
945 915
                                    server + " reason: local file outdated");
946 916
        throw new Exception("The file you are trying to update is an outdated" +
947 917
                            " version.  Please checkout the newest document, " +
......
954 924

  
955 925
      if ( !hasPermission(conn, user, group, docid) ) {
956 926
        throw new Exception("User " + user + 
957
              " does not have permission to update XML Document #" + docid);
927
              " does not have permission to update XML Document #" + accnum);
958 928
      }          
959 929
    }
960 930

  
961 931
    try 
962 932
    { 
963
      XMLReader parser = initializeParser(conn, action, newdocid, validate,
933
      XMLReader parser = initializeParser(conn, action, docid, validate,
964 934
                                          user, group, pub, serverCode, dtd);
965 935
      conn.setAutoCommit(false);
966 936
      parser.parse(new InputSource(xml));
......
977 947
    //force replicate out the new document to each server in our server list.
978 948
    if(serverCode == 1)
979 949
    { //start the thread to replicate this new document out to the other servers
980
      ForceReplicationHandler frh = new ForceReplicationHandler(newdocid, 
981
                                                                action);
950
      ForceReplicationHandler frh = new ForceReplicationHandler(docid, action);
982 951
    }
983 952
      
984
    if ( (docid != null) && !(newdocid.equals(docid)) ) 
985
    {
986
      return new String("New document ID generated:" + newdocid);
987
    } 
988
    else 
989
    {
990
      return newdocid;
991
    }
953
    return (docid + sep + rev);
992 954
  }
993 955

  
994 956
  /**
......
997 959
   *
998 960
   * @param docid the ID of the document to be deleted from the database
999 961
   */
1000
  public static void delete( Connection conn, String docid,
962
  public static void delete( Connection conn, String accnum,
1001 963
                                 String user, String group )
1002
                throws Exception {
1003
    DocumentIdentifier id = new DocumentIdentifier(docid);
1004
    docid = id.getSiteCode() + id.getSeparator() + id.getUniqueId();
964
                throws Exception 
965
  {
966
    DocumentIdentifier id = new DocumentIdentifier(accnum);
967
    String docid = id.getIdentifier();
968
    String rev = id.getRev();
1005 969
    
1006
    // Determine if the docid is OK for DELETE
970
    // Determine if the docid,rev are OK for DELETE
1007 971
    AccessionNumber ac = new AccessionNumber(conn);
1008
    String newdocid = ac.generate(docid, "DELETE");
972
    docid = ac.generate(docid, rev, "DELETE");
1009 973

  
1010 974
    // check for 'write' permission for 'user' to delete this document
1011 975
    if ( !hasPermission(conn, user, group, docid) ) {
1012 976
      throw new Exception("User " + user + 
1013
              " does not have permission to delete XML Document #" + docid);
977
              " does not have permission to delete XML Document #" + accnum);
1014 978
    }
1015 979

  
1016 980
    conn.setAutoCommit(false);
......
1033 997
    //if the deleted document is a package document its relations should 
1034 998
    //no longer be active if it has been deleted from the system.
1035 999
    
1036
//    MOVED UP IN THE TRANSACTION
1037
//    RelationHandler.deleteRelations(docid);
1038 1000
  }
1039 1001

  
1040 1002
  /** 
src/edu/ucsb/nceas/metacat/AccessionNumber.java
49 49
   * Construct an AccessionNumber 
50 50
   */
51 51
  public AccessionNumber () 
52
         throws SQLException, ClassNotFoundException  {
53
        
52
         throws SQLException, ClassNotFoundException  
53
  {
54 54
    MetaCatUtil util = new MetaCatUtil();
55 55

  
56 56
    this.sitecode = util.getOption("sitecode");
......
65 65
   *
66 66
   * @param conn the db connection to read from and write Accession# to
67 67
   */
68
  public AccessionNumber (Connection conn) {
69
        
68
  public AccessionNumber (Connection conn) 
69
  {
70 70
    MetaCatUtil util = new MetaCatUtil();
71 71

  
72 72
    this.sitecode = util.getOption("sitecode");
73 73
    this.sep = util.getOption("accNumSeparator");
74 74
    this.conn = conn;
75

  
76 75
  }
77 76

  
78 77
  /**
79 78
   * Generate an Accession Number of form <sidecode>.<createdate>
80 79
   */
81
  public String generate(String accNumber, String action) 
80
  public String generate(String docid, String rev, String action) 
82 81
                throws AccessionNumberException, SQLException 
83 82
  {
84 83
    try {
85 84
      // INSERT
86 85
      if ( action.equals("INSERT")) {
87 86

  
88
        // get a new Accession#
89
        if ( accNumber == null ) {
87
        // get a new docid
88
        if ( docid == null ) {
90 89
          String sitecode = getSitecode();
91 90
          String uniqueid = getUniqueID();
92 91

  
93 92
          return sitecode + sep + uniqueid;
94 93

  
95
        // Accession# is not used; return it
96
        } else if ( !accNumberUsed(accNumber) ) {
97
          return accNumber;
98

  
99
        // Accession# is used; throw an exception to prevent the insertion
94
        // docid is used; throw an exception to prevent the insertion
95
        } else if ( accNumberUsed(docid) ) {
96
          throw new AccessionNumberException
97
                    ("Accession number " + docid + " is already in use.");
98
        // rev is <> 1; throw an exception to prevent the insertion
99
        } else if ( !rev.equals("1") ) {
100
          throw new AccessionNumberException
101
                    ("Revision number must be 1.");
102
        // docid is not used and rev=1; return it
100 103
        } else {
101
          throw new AccessionNumberException
102
                    ("Accession number " + accNumber + " is already in use.");
104
          return docid;
103 105
        }
104 106

  
105 107
      // UPDATE or DELETE
106 108
      } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
107 109

  
108 110
        // Accession# is not provided; throw an exception to prevent the action
109
        if ( accNumber == null ) {
111
        if ( docid == null ) {
110 112
          throw new AccessionNumberException("Accession number is required.");
111 113

  
112 114
        // Accession# is not current (not in xml_documents); throw an exception
113
        } else if ( !accNumberIsCurrent(accNumber) ) {
115
        } else if ( !accNumberIsCurrent(docid) ) {
114 116
          throw new AccessionNumberException
115
          ("Document not found for Accession number " + accNumber);
117
          ("Document not found for Accession number " + docid);
116 118

  
119
        // Revision number is not the recent one; throw an exception
120
        } else if ( !rev.equals(getLastRevision(docid)) ) {
121
          throw new AccessionNumberException
122
          ("Revision number must be the recent.");
123

  
117 124
        // Accession# is current (present in xml_documents); return it
118 125
        } else {
119
          return accNumber;
126
          return docid;
120 127
        }
121 128
      }
122 129
 
123 130
    } catch (SQLException e) {
124 131
      throw new SQLException
125
      ("Error on AccessionNumber.generate(accNumber, action): "+e.getMessage());
132
      ("Error on AccessionNumber.generate(): " + e.getMessage());
126 133
    }    
127 134
 
128 135
    // never comes here
......
189 196
    return hasAccNumber;
190 197
  }    
191 198
    
192
  /** check for existence of Accesssion Number in xml_documents table */
199
  // check for existence of Accesssion Number in xml_documents table
193 200
  private boolean accNumberIsCurrent(String accNumber) throws SQLException {
194 201
        
195 202
    boolean hasCurrentAccNumber = false;
......
214 221
    return hasCurrentAccNumber;
215 222
  }    
216 223

  
224
  // get the recent revision number for docid
225
  private String getLastRevision(String docid) throws SQLException
226
  {
227
    String rev = "";
228
    
229
    try {
230
      PreparedStatement pstmt;
231
      pstmt = conn.prepareStatement
232
              ("SELECT rev FROM xml_documents WHERE docid='" + docid + "'");
233
      pstmt.execute();
234

  
235
      ResultSet rs = pstmt.getResultSet();
236
      boolean hasRow = rs.next();
237
      rev = rs.getString(1);
238
      pstmt.close();
239
      
240
    } catch (SQLException e) {
241
      throw new SQLException(
242
      "Error on AccessionNumber.getLastRevision(): " + e.getMessage());
243
    }
244

  
245
    return rev;
246
  }
247

  
217 248
}
src/edu/ucsb/nceas/metacat/DocumentIdentifier.java
51 51
  public DocumentIdentifier(String docid) throws AccessionNumberException
52 52
  {
53 53
    this.docid = docid;
54
    //int numSeps = countSeparator();
55
    //if(numSeps > 2 || numSeps < 1)
56
    //{
57
    //  throw new AccessionNumberException("Accession number can contain " +
58
    //            "no more than 2 and no fewer than 1 separators.");
59
    //}
60
    if(docid.endsWith("."))
54
    this.separator = util.getOption("accNumSeparator");
55

  
56
    if(docid.endsWith(this.separator))
61 57
    {
62 58
      throw new AccessionNumberException("Accession number cannot end with " + 
63 59
                "a seperator.");
64 60
    }
65
    if(docid.startsWith("."))
61
    if(docid.startsWith(this.separator))
66 62
    {
67 63
      throw new AccessionNumberException("Accession number cannot begin with " + 
68 64
                "a seperator.");
......
76 72
   */
77 73
  private void parseDocid()
78 74
  {
79
    separator = util.getOption("accNumSeparator");
80
    
81 75
    int firstIndex = docid.indexOf(separator);
82 76
    int lastIndex = docid.lastIndexOf(separator);
83 77
    if(firstIndex != lastIndex)

Also available in: Unified diff