cmlenz      2002/08/22 05:16:33

  Modified:    src/stores/org/apache/slide/store/impl/rdbms JDBCStore.java
  Log:
  More cleanup.
  The utility ID retrieval functions now close their statements in a finally 
  block.
  
  Revision  Changes    Path
  1.11      +471 -383  
jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/JDBCStore.java
  
  Index: JDBCStore.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/JDBCStore.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- JDBCStore.java    22 Aug 2002 08:55:54 -0000      1.10
  +++ JDBCStore.java    22 Aug 2002 12:16:32 -0000      1.11
  @@ -1203,7 +1203,7 @@
                   while (rs2.next()) {
                       predecessors.addElement(
                           new NodeRevisionNumber(
  -                            getXNumber(rs2.getLong("PREDECESSOR_ID"), uriId,
  +                            getRevisionNumber(rs2.getLong("PREDECESSOR_ID"), uriId,
                                          branch)));
                   }
                   branches.put(nrn, predecessors);
  @@ -1270,7 +1270,7 @@
               }
   
               // for now, only the latest revision from the main branch is stored
  -            long branchId = getBranchID(NodeRevisionDescriptors.MAIN_BRANCH);
  +            long branchId = getBranchId(NodeRevisionDescriptors.MAIN_BRANCH);
               NodeRevisionNumber nrn =
                   revisionDescriptors.getLatestRevision();
               sql.setLength(0);
  @@ -1488,10 +1488,10 @@
               stmt = connection.createStatement();
   
               long uriId = getUriId(uri.toString());
  -            long branchId = getBranchID(revisionDescriptor.getBranchName());
  +            long branchId = getBranchId(revisionDescriptor.getBranchName());
               if (branchId == 0) {
  -                setBranchID(revisionDescriptor.getBranchName());
  -                branchId = getBranchID(revisionDescriptor.getBranchName());
  +                setBranchId(revisionDescriptor.getBranchName());
  +                branchId = getBranchId(revisionDescriptor.getBranchName());
               }
               
               // TODO: this automatically creates the corresponding 
  @@ -1527,17 +1527,17 @@
               }
               
               long versionId =
  -                getVersionID(uriId, revisionDescriptor.getBranchName(),
  -                             revisionDescriptor.getRevisionNumber().toString());
  +                getRevisionId(uriId, revisionDescriptor.getBranchName(),
  +                             revisionDescriptor.getRevisionNumber());
               
               // create revision labels
               Enumeration labels = revisionDescriptor.enumerateLabels();
               while (labels.hasMoreElements()) {
                   String label =(String)labels.nextElement();
  -                long labelId = getLabelID(label);
  +                long labelId = getLabelId(label);
                   if (labelId == 0) {
  -                    setLabelID(label);
  -                    labelId = getLabelID(label);
  +                    setLabelId(label);
  +                    labelId = getLabelId(label);
                   }
                   sql.setLength(0);
                   sql.append("INSERT INTO VERSION_LABELS (VERSION_ID,LABEL_ID) ")
  @@ -1628,9 +1628,7 @@
                   branchStr = "main";
               }
               
  -            long versionId =
  -                getVersionID(uriId, branchStr,
  -                             (nrn != null) ? nrn.toString() : "NULL");
  +            long versionId = getRevisionId(uriId, branchStr, nrn);
               
               // remove revision labels
               sql.setLength(0);
  @@ -1664,7 +1662,7 @@
       
       
       /**
  -     * Retrive revision content.
  +     * Retrieve revision content.
        *
        * @param uri Uri
        * @param revisionNumber Node revision number
  @@ -1674,34 +1672,28 @@
           throws ServiceAccessException, RevisionNotFoundException {
   
           NodeRevisionContent result = null;
  -        String revisionUri = uri.toString();
  -        String revisionNumber =
  -                (revisionDescriptor.getRevisionNumber() == null)
  -                ? "NULL" : revisionDescriptor.getRevisionNumber().toString();
  -        Statement selectStatement=null;
  +        NodeRevisionNumber nrn = revisionDescriptor.getRevisionNumber();
  +        Statement stmt = null;
           try {
  -
  -            long versionId = getVersionID(getUriId(revisionUri),
  +            StringBuffer sql = new StringBuffer();
  +            stmt = connection.createStatement();
  +            
  +            long versionId = getRevisionId(getUriId(uri.toString()),
                                             revisionDescriptor.getBranchName(),
  -                                          revisionNumber);
  -            selectStatement = connection.createStatement();
  -            StringBuffer sql = new StringBuffer
  -                ("select CONTENT from VERSION_CONTENT where VERSION_ID = ");
  -            sql.append(versionId);
  -            ResultSet rs = selectStatement.executeQuery(sql.toString());
  -
  +                                          nrn);
  +            
  +            sql.setLength(0);
  +            sql.append("SELECT CONTENT FROM VERSION_CONTENT ")
  +               .append("WHERE VERSION_ID = ").append(versionId);
  +            ResultSet rs = stmt.executeQuery(sql.toString());
               if (!rs.next()) {
  -                rs.close();
  -                selectStatement.close();
                   throw new RevisionNotFoundException
                       (uri.toString(),
                        revisionDescriptor.getRevisionNumber());
               }
   
  -            InputStream is = rs.getBinaryStream("CONTENT");
  -            if (is == null) {
  -                rs.close();
  -                selectStatement.close();
  +            InputStream in = rs.getBinaryStream("CONTENT");
  +            if (in == null) {
                   throw new RevisionNotFoundException
                       (uri.toString(),
                        revisionDescriptor.getRevisionNumber());
  @@ -1709,31 +1701,39 @@
               // Uncompress the retrieved data.
               result = new NodeRevisionContent();
               if (compressContent) {
  -                getLogger().log
  -                    ("DeCompressing the data",LOG_CHANNEL,Logger.INFO);
  -                StoreContentZip ziputil = new StoreContentZip();
  -                ziputil.UnZip(is);
  -                revisionDescriptor.setContentLength(ziputil.getContentLength());
  -                is = ziputil.getInputStream();
  +                getLogger().log("Decompressing content", LOG_CHANNEL,
  +                                Logger.INFO);
  +                StoreContentZip zip = new StoreContentZip();
  +                zip.UnZip(in);
  +                revisionDescriptor.setContentLength(zip.getContentLength());
  +                in = zip.getInputStream();
               }
  -            result.setContent(is);                          
  -
  +            result.setContent(in);
  +            
               // this input stream passes on the closure of itself onto the
               // jdbc stmt and resultSet
  -            result.setContent( new JDBCAwareInputStream(is,selectStatement) );
  -            selectStatement.close();
  +            result.setContent(new JDBCAwareInputStream(in, stmt));
   
           } catch (SQLException e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
               throw new ServiceAccessException(this, e.getMessage());
           } catch (RevisionNotFoundException e) {
               getLogger().log("RevisionNotFoundException encountered for " +
  -                            revisionUri + " revision " + revisionNumber,
  -                            LOG_CHANNEL,Logger.WARNING);
  +                            uri + " revision " + nrn, LOG_CHANNEL,
  +                            Logger.WARNING);
               throw e; // we do NOT want this caught by next clause.
           } catch (Exception e) {
               getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
               throw new ServiceAccessException(this, e.getMessage());
  +        } finally {
  +            try {
  +                if (stmt != null) {
  +                    stmt.close();
  +                }
  +            } catch (SQLException e) {
  +                getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
  +                throw new ServiceAccessException(this, e);
  +            }
           }
   
           return result;
  @@ -1754,32 +1754,28 @@
           throws ServiceAccessException, RevisionAlreadyExistException {
   
           String revisionUri = uri.toString();
  -        String revisionNumber =
  -            revisionDescriptor.getRevisionNumber().toString();
  +        NodeRevisionNumber nrn = revisionDescriptor.getRevisionNumber();
           long contentLength = revisionDescriptor.getContentLength();
  -        Statement selectStatement = null;
  -
  +        Statement stmt = null;
           try {
  -
  -            long versionId = getVersionID(getUriId(revisionUri),
  +            StringBuffer sql = new StringBuffer();
  +            stmt = connection.createStatement();
  +            
  +            long versionId = getRevisionId(getUriId(revisionUri),
                                             revisionDescriptor.getBranchName(),
  -                                          revisionNumber);
  +                                          nrn);
   
  -            selectStatement = connection.createStatement();
  -            StringBuffer sql = new StringBuffer
  -                    ("select 1 from VERSION_CONTENT where VERSION_ID = ");
  -            sql.append(versionId);
  -            ResultSet rs = selectStatement.executeQuery(sql.toString());
  +            sql.setLength(0);
  +            sql.append("SELECT 1 FROM VERSION_CONTENT ")
  +               .append("WHERE VERSION_ID = ").append(versionId);
  +            ResultSet rs = stmt.executeQuery(sql.toString());
               if (rs.next()) {
  -                rs.close();
                   throw new RevisionAlreadyExistException
                       (uri.toString(),
                        revisionDescriptor.getRevisionNumber());
               }
   
  -            rs.close();
  -
  -            storeContent(revisionUri, revisionNumber, revisionDescriptor,
  +            storeContent(revisionUri, nrn.toString(), revisionDescriptor,
                            revisionContent);
   
           } catch (SQLException e) {
  @@ -1790,7 +1786,7 @@
               throw new ServiceAccessException(this, e.getMessage());
           } catch(RevisionAlreadyExistException e) {
               getLogger().log("RevisionAlreadyExistException encountered for " +
  -                            revisionUri + " revision " + revisionNumber,
  +                            revisionUri + " revision " + nrn,
                               LOG_CHANNEL,Logger.WARNING);
               throw e;
           } catch (Exception e) {
  @@ -1798,7 +1794,7 @@
               throw new ServiceAccessException(this, e.getMessage());
           } finally {
               try {
  -                selectStatement.close();
  +                stmt.close();
               } catch (SQLException e) {
                   getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
                   throw new ServiceAccessException(this, e);
  @@ -1821,15 +1817,14 @@
           throws ServiceAccessException, RevisionNotFoundException {
   
           String revisionUri = uri.toString();
  -        String revisionNumber =
  -            revisionDescriptor.getRevisionNumber().toString();
  +        NodeRevisionNumber nrn = revisionDescriptor.getRevisionNumber();
           Statement selectStatement = null;
   
           try {
  -
  -            long versionId = getVersionID(getUriId(revisionUri),
  +            
  +            long versionId = getRevisionId(getUriId(revisionUri),
                                             revisionDescriptor.getBranchName(),
  -                                          revisionNumber);
  +                                          nrn);
   
               selectStatement = connection.createStatement();
               StringBuffer sql = new StringBuffer
  @@ -1838,16 +1833,13 @@
               ResultSet rs = selectStatement.executeQuery(sql.toString());
   
               if (!rs.next()) {
  -                rs.close();
                   throw new RevisionNotFoundException
                       (uri.toString(),
                        revisionDescriptor.getRevisionNumber());
               }
   
  -            rs.close();
  -
               removeRevisionContent(uri, revisionDescriptor);
  -            storeContent(revisionUri, revisionNumber, revisionDescriptor,
  +            storeContent(revisionUri, nrn.toString(), revisionDescriptor,
                           revisionContent);
   
           } catch (SQLException e) {
  @@ -1858,7 +1850,7 @@
               throw new ServiceAccessException(this, e.getMessage());
           } catch(RevisionNotFoundException e) {
               getLogger().log("RevisionNotFoundException encountered for "+
  -                            revisionUri + " revision " + revisionNumber,
  +                            revisionUri + " revision " + nrn,
                               LOG_CHANNEL,Logger.WARNING);
               throw e;
           } catch (Exception e) {
  @@ -1886,14 +1878,11 @@
           (Uri uri, NodeRevisionDescriptor revisionDescriptor)
           throws ServiceAccessException {
   
  -        String revisionUri = uri.toString();
  -        String revisionNumber =
  -            revisionDescriptor.getRevisionNumber().toString();
  -
           try {
  -            long versionId = getVersionID(getUriId(revisionUri),
  -                                          revisionDescriptor.getBranchName(),
  -                                          revisionNumber);
  +            long versionId =
  +                getRevisionId(getUriId(uri.toString()),
  +                                      revisionDescriptor.getBranchName(),
  +                                      revisionDescriptor.getRevisionNumber());
               removeContent(versionId);
   
           } catch (Exception e) {
  @@ -1908,46 +1897,39 @@
       
       
       /**
  -     * Close specified stmt.
  -     */
  -    private void closeStatement(Statement stmt) {
  -        if (stmt != null) {
  -            try {
  -                stmt.close();
  -            } catch (SQLException e) {
  -                connectIfNeededAndPossible();
  -            }
  -        }
  -    }
  -    
  -    
  -    /**
        * Returns the URI ID corresponding to an URI.
        */
       protected long getUriId(String uri)
           throws SQLException {
   
  -        Long uriIdLong = (Long)uriIdLookup.get(uri);
  -        if (uriIdLong == null) {
  -            StringBuffer sql = new StringBuffer()
  -                .append("SELECT URI_ID FROM URI WHERE URI_STRING = '")
  -                .append(uri).append("'");
  -            Statement stmt = connection.createStatement();
  -            ResultSet rs = stmt.executeQuery(sql.toString());
  -            if (rs.next()) {
  -                long uriId = rs.getLong("URI_ID");
  -                if (uriIdLookup.size() == CACHE_SIZE) {
  -                    uriIdLookup.clear();
  -                    uriLookup.clear();
  +        Statement stmt = null;
  +        try {
  +            Long uriIdLong = (Long)uriIdLookup.get(uri);
  +            if (uriIdLong == null) {
  +                StringBuffer sql = new StringBuffer()
  +                    .append("SELECT URI_ID FROM URI WHERE URI_STRING = '")
  +                    .append(uri).append("'");
  +                stmt = connection.createStatement();
  +                ResultSet rs = stmt.executeQuery(sql.toString());
  +                if (rs.next()) {
  +                    long uriId = rs.getLong("URI_ID");
  +                    if (uriIdLookup.size() == CACHE_SIZE) {
  +                        uriIdLookup.clear();
  +                        uriLookup.clear();
  +                    }
  +                    uriIdLong = new Long(uriId);
  +                    uriIdLookup.put(uri, uriIdLong);
  +                    uriLookup.put(uriIdLong, uri);
                   }
  -                rs.close();
  -                uriIdLong = new Long(uriId);
  -                uriIdLookup.put(uri, uriIdLong);
  -                uriLookup.put(uriIdLong, uri);
  +            }
  +            return (uriIdLong != null) ? uriIdLong.longValue() : 0;
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
               }
           }
  -        
  -        return (uriIdLong != null) ? uriIdLong.longValue() : 0;
       }
   
   
  @@ -1957,27 +1939,34 @@
       protected String getUri(long uriId)
           throws SQLException {
   
  -        Long uriIdLong = new Long(uriId);
  -        String uri = (String)uriLookup.get(uriIdLong);
  -        if (uri ==  null) {
  -            StringBuffer sql = new StringBuffer()
  -                .append("SELECT URI_STRING FROM URI WHERE URI_ID = ")
  -                .append(uriId);
  -            Statement stmt = connection.createStatement();
  -            ResultSet rslt = stmt.executeQuery(sql.toString());
  -            if (rslt.next()) {
  -                uri = rslt.getString("URI_STRING");
  -                if (uriLookup.size() == CACHE_SIZE) {
  -                    uriLookup.clear();
  -                    uriIdLookup.clear();
  +        Statement stmt = null;
  +        try {
  +            Long uriIdLong = new Long(uriId);
  +            String uri = (String)uriLookup.get(uriIdLong);
  +            if (uri ==  null) {
  +                StringBuffer sql = new StringBuffer()
  +                    .append("SELECT URI_STRING FROM URI WHERE URI_ID = ")
  +                    .append(uriId);
  +                stmt = connection.createStatement();
  +                ResultSet rs = stmt.executeQuery(sql.toString());
  +                if (rs.next()) {
  +                    uri = rs.getString("URI_STRING");
  +                    if (uriLookup.size() == CACHE_SIZE) {
  +                        uriLookup.clear();
  +                        uriIdLookup.clear();
  +                    }
  +                    uriLookup.put(uriIdLong, uri);
  +                    uriIdLookup.put(uri, uriIdLong);
                   }
  -                rslt.close();
  -                uriLookup.put(uriIdLong, uri);
  -                uriIdLookup.put(uri, uriIdLong);
  +            }
  +            return uri;
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
               }
           }
  -        
  -        return uri;
       }
   
   
  @@ -1987,13 +1976,21 @@
       protected void setUriId(String uri)
           throws SQLException {
           
  -        if (getUriId(uri) == 0) {
  -            StringBuffer sql = new StringBuffer()
  -                .append("INSERT INTO URI (URI_STRING) VALUES ('")
  -                .append(uri).append("')");
  -            Statement stmt = connection.createStatement();
  -            stmt.execute(sql.toString());
  -            stmt.close();
  +        Statement stmt = null;
  +        try {
  +            if (getUriId(uri) == 0) {
  +                StringBuffer sql = new StringBuffer()
  +                    .append("INSERT INTO URI (URI_STRING) VALUES ('")
  +                    .append(uri).append("')");
  +                stmt = connection.createStatement();
  +                stmt.executeUpdate(sql.toString());
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
       
  @@ -2004,16 +2001,23 @@
       protected void removeUri(String uri)
           throws SQLException {
           
  -        long uriId = getUriId(uri);
  -        if (uriId != 0) {
  -            StringBuffer sql = new StringBuffer()
  -                .append("DELETE FROM URI WHERE URI_ID = ").append(uriId);
  -            Statement stmt = connection.createStatement();
  -            stmt.execute(sql.toString());
  -            stmt.close();
  -            
  -            uriIdLookup.remove(uri);
  -            uriLookup.remove(new Long(uriId));
  +        Statement stmt = null;
  +        try {
  +            long uriId = getUriId(uri);
  +            if (uriId != 0) {
  +                StringBuffer sql = new StringBuffer()
  +                    .append("DELETE FROM URI WHERE URI_ID = ").append(uriId);
  +                stmt = connection.createStatement();
  +                stmt.executeUpdate(sql.toString());
  +                uriIdLookup.remove(uri);
  +                uriLookup.remove(new Long(uriId));
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
       
  @@ -2021,19 +2025,28 @@
       /**
        * Retrieve the Branch ID For the given Branch.
        */
  -
  -    protected long getBranchID(String branchName)
  +    protected long getBranchId(String branchName)
           throws SQLException {
   
  -        Statement getStatement = connection.createStatement();
  -        StringBuffer sql = new StringBuffer
  -                ("select BRANCH_ID from BRANCH where BRANCH_STRING = '");
  -        sql.append(branchName).append("'");
  -        ResultSet rslt = getStatement.executeQuery(sql.toString());
  -        if (rslt.next()) {
  -            return rslt.getLong("BRANCH_ID");
  -        } else {
  -            return 0;
  +        Statement stmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            sql.append("SELECT BRANCH_ID FROM BRANCH ")
  +               .append("WHERE BRANCH_STRING = '").append(branchName)
  +               .append("'");
  +            stmt = connection.createStatement();
  +            ResultSet rs = stmt.executeQuery(sql.toString());
  +            if (rs.next()) {
  +                return rs.getLong("BRANCH_ID");
  +            } else {
  +                return 0;
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
   
  @@ -2042,79 +2055,112 @@
        * Add the branchname and id
        */
   
  -    protected void setBranchID(String branchName)
  +    protected void setBranchId(String branchName)
           throws SQLException {
   
  -        if (getBranchID(branchName) == 0 ) {
  -            Statement addStatement = connection.createStatement();
  -            StringBuffer sql = new StringBuffer
  -                        ("insert into BRANCH (BRANCH_STRING) values ('");
  -            sql.append(branchName).append("')");
  -            addStatement.execute(sql.toString());
  -            addStatement.close();
  +        Statement stmt = null;
  +        try {
  +            if (getBranchId(branchName) == 0 ) {
  +                StringBuffer sql = new StringBuffer();
  +                sql.append("INSERT INTO BRANCH (BRANCH_STRING) ")
  +                   .append("VALUES ('").append(branchName).append("')");
  +                stmt = connection.createStatement();
  +                stmt.execute(sql.toString());
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
  -
  +    
  +    
       /**
        * Retrieve the label ID For the labelName.
        */
  -
  -    protected long getLabelID(String labelName)
  +    protected long getLabelId(String label)
           throws SQLException {
   
  -        Statement getStatement = connection.createStatement();
  -        StringBuffer sql = new StringBuffer
  -                    ("select LABEL_ID from LABEL where LABEL_STRING = '");
  -        sql.append(labelName).append("'");
  -        ResultSet rslt = getStatement.executeQuery(sql.toString());
  -        if (rslt.next()) {
  -            return rslt.getLong("LABEL_ID");
  -        } else {
  -            return 0;
  +        Statement stmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            sql.append("SELECT LABEL_ID FROM LABEL ")
  +               .append("WHERE LABEL_STRING = '").append(label).append("'");
  +            stmt = connection.createStatement();
  +            ResultSet rs = stmt.executeQuery(sql.toString());
  +            if (rs.next()) {
  +                return rs.getLong("LABEL_ID");
  +            } else {
  +                return 0;
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
  -
  +    
  +    
       /**
  -     * Retrieve the label NAME For the labelid.
  +     * Retrieve the label name for the corresponding ID.
        */
  -
       protected String getLabelName(long labelID)
           throws SQLException {
   
  -        Statement getStatement = connection.createStatement();
  -        StringBuffer sql = new StringBuffer
  -                    ("select LABEL_STRING from LABEL where LABEL_ID= ");
  -        sql.append(labelID);
  -        ResultSet rslt = getStatement.executeQuery(sql.toString());
  -        if (rslt.next()) {
  -            return rslt.getString("LABEL_STRING");
  -        } else {
  -            return null;
  +        Statement stmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            sql.append("SELECT LABEL_STRING FROM LABEL ")
  +               .append("WHERE LABEL_ID = ").append(labelID);
  +            stmt = connection.createStatement();
  +            ResultSet rs = stmt.executeQuery(sql.toString());
  +            if (rs.next()) {
  +                return rs.getString("LABEL_STRING");
  +            } else {
  +                return null;
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
  -
  +    
  +    
       /**
        * Add the labelName and id
        */
  -
  -    protected void setLabelID(String labelName)
  +    protected void setLabelId(String labelName)
           throws SQLException {
   
  -        if (getBranchID(labelName) == 0 ) {
  -            Statement addStatement = connection.createStatement();
  -            StringBuffer sql = new StringBuffer
  -                        ("insert into LABEL (LABEL_STRING) values ('");
  -            sql.append(labelName).append("')");
  -            addStatement.execute(sql.toString());
  -            addStatement.close();
  +        Statement stmt = null;
  +        try {
  +            if (getBranchId(labelName) == 0) {
  +                StringBuffer sql = new StringBuffer();
  +                sql.append("INSERT INTO LABEL (LABEL_STRING) ")
  +                   .append("VALUES ('").append(labelName).append("')");
  +                stmt = connection.createStatement();
  +                stmt.executeUpdate(sql.toString());
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
  -
  -
  -     /**
  +    
  +    
  +    /**
        * Filter for all the new children
        */
  -
       protected Enumeration getNewChildren( long parent, Enumeration childlist)
           throws SQLException {
   
  @@ -2142,11 +2188,11 @@
           hshtempHash.clear();
           return hshnewChild.elements();
       }
  -
  -     /**
  +    
  +    
  +    /**
        * Check if the Link exist.
        */
  -
       protected boolean isLinkExist(long parent, long child)
           throws SQLException {
   
  @@ -2157,11 +2203,11 @@
           ResultSet rslt = getStatement.executeQuery(sql.toString());
           return rslt.next();
       }
  -
  -     /**
  +    
  +    
  +    /**
        * Check if the Lock exist.
        */
  -
       protected boolean isLockExist(long parent)
           throws SQLException {
   
  @@ -2172,210 +2218,252 @@
           ResultSet rslt = getStatement.executeQuery(sql.toString());
           return rslt.next();
       }
  -
  -     /**
  +    
  +    
  +    /**
        * Get the VersionID for the current URI
        */
  -
  -    protected long getVersionID(long uri_id,
  -                                String branch_name,
  -                                String sXNumber)
  +    protected long getRevisionId(long uriId, String branch,
  +                                NodeRevisionNumber nrn)
           throws SQLException {
  -
  -        Statement getStatement = connection.createStatement();
  -        StringBuffer sql = new StringBuffer
  -            ("select A.VERSION_ID from VERSION_HISTORY A, BRANCH B ");
  -        sql.append("where  A.URI_ID= " );
  -        sql.append(uri_id).append(" and A.BRANCH_ID = B.BRANCH_ID and");
  -        sql.append(" B.BRANCH_STRING = '").append(branch_name);
  -        sql.append("' and A.REVISION_NO= '").append(sXNumber).append("'");
  -        ResultSet rslt = getStatement.executeQuery(sql.toString());
  -        if (rslt.next()) {
  -            return rslt.getLong("VERSION_ID");
  -        } else {
  -            return 0;
  +        
  +        Statement stmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            stmt = connection.createStatement();
  +            
  +            sql.setLength(0);
  +            sql.append("SELECT A.VERSION_ID FROM VERSION_HISTORY A, BRANCH B ")
  +               .append("WHERE A.URI_ID = " ).append(uriId)
  +               .append(" AND A.BRANCH_ID = B.BRANCH_ID ")
  +               .append(" AND B.BRANCH_STRING = '").append(branch)
  +               .append("' AND A.REVISION_NO ")
  +               .append((nrn != null) ? "= '" + nrn + "'" : "IS NULL");
  +            ResultSet rs = stmt.executeQuery(sql.toString());
  +            if (rs.next()) {
  +                return rs.getLong("VERSION_ID");
  +            } else {
  +                return 0;
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
  -
  -     /**
  -     * Set the VersionID for the current URI
  +    
  +    
  +    /**
  +     * Set the Version ID for the current URI
        */
  -
  -    protected void setVersionID(long uri_id, long branch_id, String sXNumber)
  -        throws SQLException {
  -        Statement insStatement = connection.createStatement();
  -        StringBuffer sql = new StringBuffer
  -                        ("SELECT 1 FROM VERSION WHERE URI_ID =");
  -        sql.append(uri_id);
  -        getLogger().log
  -                ("Inside VersionID =="
  -                + sql.toString(),
  -                LOG_CHANNEL,Logger.DEBUG);
  -        ResultSet rs = insStatement.executeQuery(sql.toString());
  -        if (!rs.next()) {
  -            sql = new StringBuffer
  -                    ("insert into VERSION (URI_ID,IS_VERSIONED) values( ");
  -            sql.append(uri_id).append(", ").append(0).append(")");
  -            insStatement.execute(sql.toString());
  -        }
  -        sql = new StringBuffer
  -            ("insert into VERSION_HISTORY (URI_ID,BRANCH_ID,REVISION_NO)");
  -        sql.append("values(");
  -        sql.append(uri_id).append(", ")
  -                .append(branch_id).append(", '")
  -                .append(sXNumber).append("')");
  -        insStatement.execute(sql.toString());
  -        insStatement.close();
  -    }
  -
  -     /**
  -     * Get the XNUMBER for the current URI
  -     */
  -
  -    protected String getXNumber(long version_id,
  -                                long uri_id,
  -                                String branch_name)
  +    protected void setRevisionId(long uriId, long branchId, String sXNumber)
           throws SQLException {
   
  -        Statement getStatement = connection.createStatement();
  -        StringBuffer sql = new StringBuffer
  -                ("select A.REVISION_NO from VERSION_HISTORY A, ");
  -        sql.append("BRANCH B where A.VERSION_ID = " );
  -        sql.append(version_id).append(" and A.URI_ID= ");
  -        sql.append(uri_id).append(" and A.BRANCH_ID = B.BRANCH_ID");
  -        sql.append(" and B.BRANCH_STRING = '");
  -        sql.append(branch_name).append("'");
  -        ResultSet rslt = getStatement.executeQuery(sql.toString());
  -        if (rslt.next()) {
  -            return rslt.getString("REVISION_NO");
  -        } else {
  -            return null;
  +        Statement stmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            stmt = connection.createStatement();
  +            sql.setLength(0);
  +            sql.append("SELECT 1 FROM VERSION WHERE URI_ID = ").append(uriId);
  +            ResultSet rs = stmt.executeQuery(sql.toString());
  +            if (!rs.next()) {
  +                sql.setLength(0);
  +                sql.append("INSERT INTO VERSION (URI_ID, IS_VERSIONED) ")
  +                   .append("VALUES (").append(uriId).append(", ").append(0)
  +                   .append(")");
  +                stmt.execute(sql.toString());
  +            }
  +            sql.setLength(0);
  +            sql.append("INSERT INTO VERSION_HISTORY ")
  +               .append("(URI_ID, BRANCH_ID, REVISION_NO) ")
  +               .append("VALUES (").append(uriId).append(", ").append(branchId)
  +               .append(", '").append(sXNumber).append("')");
  +            stmt.execute(sql.toString());
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
           }
       }
  +    
  +    
  +    /**
  +     * Get the revision number string for the current URI
  +     */
  +    protected String getRevisionNumber(long versionId, long uriId,
  +                                       String branch)
  +        throws SQLException {
   
  +        Statement stmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            stmt = connection.createStatement();
  +            sql.append("SELECT A.REVISION_NO ")
  +               .append("FROM VERSION_HISTORY A, BRANCH B ")
  +               .append("WHERE A.VERSION_ID = ").append(versionId)
  +               .append(" AND A.URI_ID = ").append(uriId)
  +               .append(" AND A.BRANCH_ID = B.BRANCH_ID")
  +               .append(" AND B.BRANCH_STRING = '").append(branch).append("'");
  +            ResultSet rs = stmt.executeQuery(sql.toString());
  +            if (rs.next()) {
  +                return rs.getString("REVISION_NO");
  +            } else {
  +                return null;
  +            }
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
  +        }
  +    }
  +    
  +    
       /**
        * Store a revision.
        */
  -    protected void storeContent(String revisionUri, String revisionNumber,
  +    private void storeContent(String revisionUri, String revisionNumber,
                                   NodeRevisionDescriptor revisionDescriptor,
                                   NodeRevisionContent revisionContent)
           throws IOException, SQLException {
  -
  -        long versionId = getVersionID(getUriId(revisionUri),
  -                                      revisionDescriptor.getBranchName(),
  -                                      revisionNumber);
  -        if (versionId == 0 ) {
  -            setVersionID(getUriId(revisionUri),
  -                         getBranchID(revisionDescriptor.getBranchName()),
  -                         revisionNumber);
  -            versionId = getVersionID(getUriId(revisionUri),
  -                                     revisionDescriptor.getBranchName(),
  -                                     revisionNumber);
  -        }
  -        PreparedStatement insertStatement = connection.prepareStatement
  -            ("insert into VERSION_CONTENT values(?, ?)");
  -        insertStatement.setLong(1, versionId);
  -
  -        InputStream is = revisionContent.streamContent();
  -
  -        if (is != null) {
  -            // Compress the recieved data.
  -            long contentLength = 0;
  -            if (compressContent) {
  -                getLogger().log("Compressing the data",LOG_CHANNEL,Logger.INFO);
  -                StoreContentZip ziputil = new StoreContentZip();
  -                ziputil.Zip(is);
  -                is = ziputil.getInputStream();
  -                contentLength = ziputil.getContentLength();
  -            } else {
  -                contentLength = revisionDescriptor.getContentLength();
  +        
  +        PreparedStatement pstmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            
  +            // find or add the version ID
  +            long versionId = getRevisionId(getUriId(revisionUri),
  +                                          revisionDescriptor.getBranchName(),
  +                                          revisionDescriptor.getRevisionNumber());
  +            if (versionId == 0) {
  +                setRevisionId(getUriId(revisionUri),
  +                             getBranchId(revisionDescriptor.getBranchName()),
  +                             revisionNumber);
  +                versionId = getRevisionId(getUriId(revisionUri),
  +                                         revisionDescriptor.getBranchName(),
  +                                         revisionDescriptor.getRevisionNumber());
               }
  -            OutputStream os = null;
  -            // We copy 8 ko with each read
  -            byte[] buffer = new byte[BUFFER_SIZE];
  -            long position = 0;
  -
  -            //long contentLength = revisionDescriptor.getContentLength();
  -            File tempFile = null;
  -            String tempFileName = null;
  -
  -            if (contentLength == -1) {
  -                // If content length is unspecified, we have to buffer
  -                // to a temp file.
  -                try {
  -                    tempFileName = revisionUri + "-" + revisionNumber;
  -                    tempFileName = tempFileName.replace('/', '.');
  -                    int tempFileNameLength = tempFileName.length();
  -                    if (tempFileNameLength > 200)
  -                        tempFileName = tempFileName.substring
  -                            (tempFileNameLength - 200, tempFileNameLength);
  -                    tempFile = File.createTempFile(tempFileName, null);
  -
  -                    FileOutputStream fos = new FileOutputStream(tempFile);
  -                    while (true) {
  -                        int nChar = is.read(buffer);
  -                        if (nChar == -1) {
  -                            break;
  +            
  +            sql.append("INSERT INTO VERSION_CONTENT VALUES (?, ?)");
  +            pstmt = connection.prepareStatement(sql.toString());
  +            pstmt.setLong(1, versionId);
  +            
  +            InputStream is = revisionContent.streamContent();
  +            if (is != null) {
  +                // Compress the recieved data.
  +                long contentLength = 0;
  +                if (compressContent) {
  +                    getLogger().log("Compressing the content", LOG_CHANNEL,
  +                                    Logger.INFO);
  +                    StoreContentZip zip = new StoreContentZip();
  +                    zip.Zip(is);
  +                    is = zip.getInputStream();
  +                    contentLength = zip.getContentLength();
  +                } else {
  +                    contentLength = revisionDescriptor.getContentLength();
  +                }
  +                OutputStream os = null;
  +                // We copy 8 ko with each read
  +                byte[] buffer = new byte[BUFFER_SIZE];
  +                long position = 0;
  +    
  +                //long contentLength = revisionDescriptor.getContentLength();
  +                File tempFile = null;
  +                String tempFileName = null;
  +    
  +                if (contentLength == -1) {
  +                    // If content length is unspecified, we have to buffer
  +                    // to a temp file.
  +                    try {
  +                        tempFileName = revisionUri + "-" + revisionNumber;
  +                        tempFileName = tempFileName.replace('/', '.');
  +                        int tempFileNameLength = tempFileName.length();
  +                        if (tempFileNameLength > 200)
  +                            tempFileName = tempFileName.substring
  +                                (tempFileNameLength - 200, tempFileNameLength);
  +                        tempFile = File.createTempFile(tempFileName, null);
  +    
  +                        FileOutputStream fos = new FileOutputStream(tempFile);
  +                        while (true) {
  +                            int nChar = is.read(buffer);
  +                            if (nChar == -1) {
  +                                break;
  +                            }
  +                            fos.write(buffer, 0, nChar);
  +                            position = position + nChar;
                           }
  -                        fos.write(buffer, 0, nChar);
  -                        position = position + nChar;
  +                        fos.close();
  +    
  +                        is = new FileInputStream(tempFile);
  +                        contentLength = tempFile.length();
  +                    }
  +                    catch (IOException ex) {
  +                        getLogger().log
  +                            (ex.toString()
  +                            + " during the calculation of the content length.",
  +                            LOG_CHANNEL,Logger.ERROR);
  +                        getLogger().log
  +                            ("tempFileName: "
  +                            + tempFileName,
  +                            LOG_CHANNEL,Logger.ERROR);
  +                        getLogger().log
  +                            ("tempFile: "
  +                            + tempFile.getAbsolutePath(),
  +                            LOG_CHANNEL,Logger.ERROR);
  +                        throw ex;
                       }
  -                    fos.close();
  -
  -                    is = new FileInputStream(tempFile);
  -                    contentLength = tempFile.length();
                   }
  -                catch (IOException ex) {
  -                    getLogger().log
  -                        (ex.toString()
  -                        + " during the calculation of the content length.",
  -                        LOG_CHANNEL,Logger.ERROR);
  -                    getLogger().log
  -                        ("tempFileName: "
  -                        + tempFileName,
  -                        LOG_CHANNEL,Logger.ERROR);
  -                    getLogger().log
  -                        ("tempFile: "
  -                        + tempFile.getAbsolutePath(),
  -                        LOG_CHANNEL,Logger.ERROR);
  -                    throw ex;
  +    
  +                // FIXME ? Cast from long to int won't allow files > 4GB.
  +                pstmt.setBinaryStream(2, is, (int)contentLength - 1);
  +                pstmt.executeUpdate();
  +    
  +                revisionDescriptor.setContentLength(contentLength);
  +    
  +                if (tempFile != null) {
  +                    is.close();
  +                    tempFile.delete();
                   }
  +    
               }
  -
  -            // FIXME ? Cast from long to int won't allow files > 4GB.
  -            //ORIGINAL SLIDE COMMENT
  -            insertStatement.setBinaryStream(2, is, (int) contentLength - 1);
  -            insertStatement.executeUpdate();
  -
  -            revisionDescriptor.setContentLength(contentLength);
  -
  -            if (tempFile != null) {
  -                is.close();
  -                tempFile.delete();
  +    
  +        } finally {
  +            if (pstmt != null) {
  +                try {
  +                    pstmt.close();
  +                } catch (SQLException sqle) { }
               }
  -
           }
  -
  -        insertStatement.close();
  -
       }
  -
  -
  +    
  +    
       /**
        * Remove content.
        */
  -    protected void removeContent(long version_id)
  +    private void removeContent(long versionId)
           throws SQLException {
   
  -        Statement deleteStatement = connection.createStatement();
  -        StringBuffer sql = new StringBuffer
  -                ("delete from VERSION_CONTENT where VERSION_ID = ");
  -        sql.append(version_id);
  -        deleteStatement.execute(sql.toString());
  -        deleteStatement.close();
  -
  +        Statement stmt = null;
  +        try {
  +            StringBuffer sql = new StringBuffer();
  +            sql.append("DELETE FROM VERSION_CONTENT ")
  +               .append("WHERE VERSION_ID = ").append(versionId);
  +            stmt = connection.createStatement();
  +            stmt.executeUpdate(sql.toString());
  +        } finally {
  +            if (stmt != null) {
  +                try {
  +                    stmt.close();
  +                } catch (SQLException sqle) { }
  +            }
  +        }
       }
  -
  +    
       
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to