Repository: oodt
Updated Branches:
  refs/heads/master c101b01da -> 64b200a0c


fix about 1000 code quality issues


Project: http://git-wip-us.apache.org/repos/asf/oodt/repo
Commit: http://git-wip-us.apache.org/repos/asf/oodt/commit/c212b1a5
Tree: http://git-wip-us.apache.org/repos/asf/oodt/tree/c212b1a5
Diff: http://git-wip-us.apache.org/repos/asf/oodt/diff/c212b1a5

Branch: refs/heads/master
Commit: c212b1a53d6ac19d657951d972005b5287eba271
Parents: c101b01
Author: Tom Barber <[email protected]>
Authored: Sat Oct 24 14:27:03 2015 +0100
Committer: Tom Barber <[email protected]>
Committed: Sat Oct 24 14:27:03 2015 +0100

----------------------------------------------------------------------
 .../cas/filemgr/catalog/DataSourceCatalog.java  | 576 +++++++++----------
 1 file changed, 265 insertions(+), 311 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/oodt/blob/c212b1a5/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/DataSourceCatalog.java
----------------------------------------------------------------------
diff --git 
a/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/DataSourceCatalog.java
 
b/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/DataSourceCatalog.java
index 5a01e1f..896dbcd 100644
--- 
a/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/DataSourceCatalog.java
+++ 
b/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/DataSourceCatalog.java
@@ -123,11 +123,11 @@ public class DataSourceCatalog implements Catalog {
     /**
      * Constructor that assumes productIdString=false
      * to support current subclasses.
-     * @param ds
-     * @param valLayer
-     * @param fieldId
-     * @param pageSize
-     * @param cacheUpdateMin
+     * @param ds the datasource.
+     * @param valLayer the validation layer
+     * @param fieldId the feildid flag
+     * @param pageSize the page size
+     * @param cacheUpdateMin the min cache update.
      */
     public DataSourceCatalog(DataSource ds, ValidationLayer valLayer,
         boolean fieldId, int pageSize, long cacheUpdateMin) {
@@ -155,40 +155,37 @@ public class DataSourceCatalog implements Catalog {
                             + ": Message: " + e.getMessage());
         }
 
-        for (Iterator<Element> i = metadataTypes.iterator(); i.hasNext();) {
-            Element element = i.next();
-            List<String> values = m.getAllMetadata(element.getElementName());
+      for (Element element : metadataTypes) {
+        List<String> values = m.getAllMetadata(element.getElementName());
 
-            if (values == null) {
-                LOG.log(Level.WARNING, "No Metadata specified for product ["
-                        + product.getProductName() + "] for required field ["
-                        + element.getElementName()
-                        + "]: Attempting to continue processing metadata");
-                continue;
-            }
-
-            for (Iterator<String> j = values.iterator(); j.hasNext();) {
-                String value = j.next();
+        if (values == null) {
+          LOG.log(Level.WARNING, "No Metadata specified for product ["
+                                 + product.getProductName() + "] for required 
field ["
+                                 + element.getElementName()
+                                 + "]: Attempting to continue processing 
metadata");
+          continue;
+        }
 
-                try {
-                    addMetadataValue(element, product, value);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                    LOG
-                            .log(
-                                    Level.WARNING,
-                                    "Exception ingesting metadata. Error 
inserting field: ["
-                                            + element.getElementId()
-                                            + "=>"
-                                            + value
-                                            + "]: for product: ["
-                                            + product.getProductName()
-                                            + "]: Message: "
-                                            + e.getMessage()
-                                            + ": Attempting to continue 
processing metadata");
-                }
-            }
+        for (String value : values) {
+          try {
+            addMetadataValue(element, product, value);
+          } catch (Exception e) {
+            e.printStackTrace();
+            LOG
+                .log(
+                    Level.WARNING,
+                    "Exception ingesting metadata. Error inserting field: ["
+                    + element.getElementId()
+                    + "=>"
+                    + value
+                    + "]: for product: ["
+                    + product.getProductName()
+                    + "]: Message: "
+                    + e.getMessage()
+                    + ": Attempting to continue processing metadata");
+          }
         }
+      }
 
     }
 
@@ -200,7 +197,7 @@ public class DataSourceCatalog implements Catalog {
      */
     public synchronized void removeMetadata(Metadata m, Product product)
             throws CatalogException {
-        List<Element> metadataTypes = null;
+        List<Element> metadataTypes;
 
         try {
             metadataTypes = validationLayer.getElements(product
@@ -213,34 +210,31 @@ public class DataSourceCatalog implements Catalog {
                             + ": Message: " + e.getMessage());
         }
 
-        for (Iterator<Element> i = metadataTypes.iterator(); i.hasNext();) {
-            Element element = i.next();
-            List<String> values = m.getAllMetadata(element.getElementName());
-
-            if (values != null) {
-                for (Iterator<String> j = values.iterator(); j.hasNext();) {
-                    String value = j.next();
-
-                    try {
-                        removeMetadataValue(element, product, value);
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                        LOG
-                                .log(
-                                        Level.WARNING,
-                                        "Exception removing metadata. Error 
deleting field: ["
-                                                + element.getElementId()
-                                                + "=>"
-                                                + value
-                                                + "]: for product: ["
-                                                + product.getProductName()
-                                                + "]: Message: "
-                                                + e.getMessage()
-                                                + ": Attempting to continue 
processing metadata");
-                    }
-                }
-            }
+      for (Element element : metadataTypes) {
+        List<String> values = m.getAllMetadata(element.getElementName());
+
+        if (values != null) {
+          for (String value : values) {
+            try {
+              removeMetadataValue(element, product, value);
+            } catch (Exception e) {
+              e.printStackTrace();
+              LOG
+                  .log(
+                      Level.WARNING,
+                      "Exception removing metadata. Error deleting field: ["
+                      + element.getElementId()
+                      + "=>"
+                      + value
+                      + "]: for product: ["
+                      + product.getProductName()
+                      + "]: Message: "
+                      + e.getMessage()
+                      + ": Attempting to continue processing metadata");
+            }
+          }
         }
+      }
     }
 
     /*
@@ -259,8 +253,8 @@ public class DataSourceCatalog implements Catalog {
             conn.setAutoCommit(false);
             statement = conn.createStatement();
 
-            String addProductSql = null;
-            String productTypeIdStr = null;
+            String addProductSql;
+            String productTypeIdStr;
 
             if (fieldIdStringFlag) {
                 productTypeIdStr = "'"
@@ -269,7 +263,7 @@ public class DataSourceCatalog implements Catalog {
                 productTypeIdStr = product.getProductType().getProductTypeId();
             }
 
-                                               if (productIdString==false) {
+                                               if (!productIdString) {
                                                        
                    addProductSql = "INSERT INTO products (product_name, 
product_structure, product_transfer_status, product_type_id) "
                        + "VALUES ('"
@@ -286,7 +280,7 @@ public class DataSourceCatalog implements Catalog {
                                        statement.execute(addProductSql);
                                
                                        // read "product_id" value that was 
automatically assigned by the database
-                                       String productId = new String();
+                                       String productId = "";
                                
                                        String getProductIdSql = "SELECT 
MAX(product_id) AS max_id FROM products";
                                
@@ -333,7 +327,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception adding product. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback addProduct transaction. Message: "
@@ -348,7 +344,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -357,7 +352,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -367,7 +361,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -395,9 +388,8 @@ public class DataSourceCatalog implements Catalog {
                     + product.getTransferStatus() + "' "
                     + "WHERE product_id = " + quoteIt(product.getProductId());
 
-            LOG
-                    .log(Level.FINE, "modifyProduct: Executing: "
-                            + modifyProductSql);
+            LOG.log(Level.FINE, "modifyProduct: Executing: "
+                                + modifyProductSql);
             statement.execute(modifyProductSql);
             conn.commit();
 
@@ -409,7 +401,8 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception modifying product. Message: "
                     + e.getMessage());
             try {
-                conn.rollback();
+              assert conn != null;
+              conn.rollback();
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback modifyProduct transaction. 
Message: "
@@ -424,7 +417,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -434,7 +426,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -483,7 +474,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception removing product. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback removeProduct transaction. 
Message: "
@@ -497,7 +490,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -507,7 +499,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -543,7 +534,9 @@ public class DataSourceCatalog implements Catalog {
                     "Exception setting transfer status for product. Message: "
                             + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback setProductTransferStatus 
transaction. Message: "
@@ -583,7 +576,6 @@ public class DataSourceCatalog implements Catalog {
             throws CatalogException {
         Connection conn = null;
         Statement statement = null;
-        ResultSet rs = null;
 
         String productRefTable = product.getProductType().getName()
                 + "_reference";
@@ -626,7 +618,9 @@ public class DataSourceCatalog implements Catalog {
                     "Exception adding product references. Message: "
                             + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback addProductReferences transaction. 
Message: "
@@ -635,22 +629,12 @@ public class DataSourceCatalog implements Catalog {
             throw new CatalogException(e.getMessage());
         } finally {
 
-            if (rs != null) {
-                try {
-                    rs.close();
-                } catch (SQLException ignore) {
-                }
-
-                rs = null;
-            }
-
             if (statement != null) {
                 try {
                     statement.close();
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -660,7 +644,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -697,7 +680,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception getting product. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback getProductById transaction. 
Message: "
@@ -712,7 +697,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -721,7 +705,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -731,7 +714,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -771,7 +753,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception getting product. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback getProductByName transaction. 
Message: "
@@ -786,7 +770,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -795,7 +778,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -805,7 +787,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -849,7 +830,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception getting product type. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback getProductTypeById transaction. 
Message: "
@@ -864,7 +847,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -873,7 +855,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -883,7 +864,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -927,7 +907,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception getting products. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback getProductstransaction. Message: "
@@ -942,7 +924,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -951,7 +932,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -961,7 +941,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -1016,7 +995,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception getting products. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback getProductsByProductType 
transaction. Message: "
@@ -1031,7 +1012,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -1040,7 +1020,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1050,7 +1029,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -1077,7 +1055,7 @@ public class DataSourceCatalog implements Catalog {
             rs = statement.executeQuery(metadataSql.toString());
             
             m = new Metadata();
-            List<Element> elements = null;
+            List<Element> elements;
 
             try {
                 elements = 
validationLayer.getElements(product.getProductType());
@@ -1090,18 +1068,16 @@ public class DataSourceCatalog implements Catalog {
             }
 
             while (rs.next()) {
-                for (Iterator<Element> i = elements.iterator(); i.hasNext();) {
-                    Element e = i.next();
-
-                    // right now, we just support STRING
-                    String elemValue = rs.getString("metadata_value");
-                    String elemId = rs.getString("element_id");
-
-                    if (elemId.equals(e.getElementId())) {
-                        elemValue = (elemValue != null ? elemValue : "");
-                        m.addMetadata(e.getElementName(), elemValue);
-                    }
+              for (Element e : elements) {
+                // right now, we just support STRING
+                String elemValue = rs.getString("metadata_value");
+                String elemId = rs.getString("element_id");
+
+                if (elemId.equals(e.getElementId())) {
+                  elemValue = (elemValue != null ? elemValue : "");
+                  m.addMetadata(e.getElementName(), elemValue);
                 }
+              }
             }
 
         } catch (Exception e) {
@@ -1117,7 +1093,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -1126,7 +1101,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1136,7 +1110,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -1172,7 +1145,7 @@ public class DataSourceCatalog implements Catalog {
             rs = statement.executeQuery(metadataSql.toString());
 
             m = new Metadata();
-            List<Element> elements = null;
+            List<Element> elements;
 
             try {
                 elements = 
validationLayer.getElements(product.getProductType());
@@ -1185,18 +1158,16 @@ public class DataSourceCatalog implements Catalog {
             }
 
             while (rs.next()) {
-                for (Iterator<Element> i = elements.iterator(); i.hasNext();) {
-                    Element e = i.next();
-
-                    // right now, we just support STRING
-                    String elemValue = rs.getString("metadata_value");
-                    String elemId = rs.getString("element_id");
-
-                    if (elemId.equals(e.getElementId())) {
-                        elemValue = (elemValue != null ? elemValue : "");
-                        m.addMetadata(e.getElementName(), elemValue);
-                    }
+              for (Element e : elements) {
+                // right now, we just support STRING
+                String elemValue = rs.getString("metadata_value");
+                String elemId = rs.getString("element_id");
+
+                if (elemId.equals(e.getElementId())) {
+                  elemValue = (elemValue != null ? elemValue : "");
+                  m.addMetadata(e.getElementName(), elemValue);
                 }
+              }
             }
 
         } catch (Exception e) {
@@ -1212,7 +1183,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -1221,7 +1191,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1231,7 +1200,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -1312,7 +1280,9 @@ public class DataSourceCatalog implements Catalog {
                     "Exception getting top N products. Message: "
                             + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback get top N products. Message: "
@@ -1327,7 +1297,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -1336,7 +1305,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1346,7 +1314,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -1403,7 +1370,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception adding metadata value. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback add metadata value. Message: "
@@ -1417,7 +1386,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1427,7 +1395,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
     }
@@ -1467,7 +1434,9 @@ public class DataSourceCatalog implements Catalog {
                     "Exception removing metadata value. Message: "
                             + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback remove metadata value. Message: "
@@ -1481,7 +1450,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1491,7 +1459,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
     }
@@ -1535,7 +1502,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception getting num products. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback get num products. Message: "
@@ -1550,7 +1519,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -1559,7 +1527,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1569,7 +1536,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -1697,11 +1663,10 @@ public class DataSourceCatalog implements Catalog {
         if (productIds != null && productIds.size() > 0) {
             List<Product> products = new Vector<Product>(productIds.size());
 
-            for (Iterator<String> i = productIds.iterator(); i.hasNext();) {
-                String productId = i.next();
-                Product p = getProductById(productId);
-                products.add(p);
-            }
+          for (String productId : productIds) {
+            Product p = getProductById(productId);
+            products.add(p);
+          }
 
             retPage.setPageProducts(products);
         }
@@ -1725,9 +1690,7 @@ public class DataSourceCatalog implements Catalog {
             String tableName = type.getName() + "_metadata";
             String subSelectQueryBase = "SELECT product_id FROM " + tableName
                     + " ";
-            StringBuilder selectClause = new StringBuilder(
-                    "SELECT COUNT(DISTINCT p.product_id) AS numResults ");
-            StringBuilder fromClause = new StringBuilder("FROM " + tableName
+          StringBuilder fromClause = new StringBuilder("FROM " + tableName
                     + " p ");
             StringBuilder whereClause = new StringBuilder("WHERE ");
 
@@ -1735,110 +1698,113 @@ public class DataSourceCatalog implements Catalog {
             int clauseNum = 0;
 
             if (query.getCriteria() != null && query.getCriteria().size() > 0) 
{
-                for (Iterator<QueryCriteria> i = 
query.getCriteria().iterator(); i.hasNext();) {
-                    QueryCriteria criteria = i.next();
-                    clauseNum++;
+              for (QueryCriteria criteria : query.getCriteria()) {
+                clauseNum++;
 
-                    String elementIdStr = null;
+                String elementIdStr;
 
-                    if (fieldIdStringFlag) {
-                        elementIdStr = "'" + 
this.validationLayer.getElementByName(criteria.getElementName()).getElementId() 
+ "'";
-                    } else {
-                        elementIdStr = 
this.validationLayer.getElementByName(criteria.getElementName()).getElementId();
-                    }
+                if (fieldIdStringFlag) {
+                  elementIdStr =
+                      "'" + 
this.validationLayer.getElementByName(criteria.getElementName()).getElementId() 
+ "'";
+                } else {
+                  elementIdStr = 
this.validationLayer.getElementByName(criteria.getElementName()).getElementId();
+                }
 
-                    StringBuilder clause = new StringBuilder();
-
-                    if (!gotFirstClause) {
-                        clause.append("(p.element_id = 
").append(elementIdStr).append(" AND ");
-                        if (criteria instanceof TermQueryCriteria) {
-                            clause.append(" metadata_value LIKE 
'%").append(((TermQueryCriteria) criteria).getValue())
-                                  .append("%') ");
-                        } else if (criteria instanceof RangeQueryCriteria) {
-                            String startVal = ((RangeQueryCriteria) criteria)
-                                    .getStartValue();
-                            String endVal = ((RangeQueryCriteria) criteria)
-                                    .getEndValue();
-                            boolean inclusive = ((RangeQueryCriteria) criteria)
-                                    .getInclusive();
-
-                            if ((startVal != null && !startVal.equals(""))
-                                    || (endVal != null && !endVal.equals(""))) 
{
-                                clause.append(" metadata_value ");
-
-                                boolean gotStart = false;
-
-                                if (startVal != null && !startVal.equals("")) {
-                                    if (inclusive)
-                                        clause.append(">= '" + startVal + "'");
-                                    else
-                                        clause.append("> '" + startVal + "'");
-                                    gotStart = true;
-                                }
-
-                                if (endVal != null && !endVal.equals("")) {
-                                    if (gotStart) {
-                                        if (inclusive)
-                                            clause.append(" AND metadata_value 
<= '").append(endVal).append("'");
-                                        else
-                                            clause.append(" AND metadata_value 
< '").append(endVal).append("'");
-                                    } else if (inclusive)
-                                        clause.append("<= 
'").append(endVal).append("'");
-                                    else
-                                        clause.append("< 
'").append(endVal).append("'");
-                                }
-
-                                clause.append(") ");
-                            }
+                StringBuilder clause = new StringBuilder();
+
+                if (!gotFirstClause) {
+                  clause.append("(p.element_id = 
").append(elementIdStr).append(" AND ");
+                  if (criteria instanceof TermQueryCriteria) {
+                    clause.append(" metadata_value LIKE 
'%").append(((TermQueryCriteria) criteria).getValue())
+                          .append("%') ");
+                  } else if (criteria instanceof RangeQueryCriteria) {
+                    String startVal = ((RangeQueryCriteria) criteria)
+                        .getStartValue();
+                    String endVal = ((RangeQueryCriteria) criteria)
+                        .getEndValue();
+                    boolean inclusive = ((RangeQueryCriteria) criteria)
+                        .getInclusive();
+
+                    if ((startVal != null && !startVal.equals(""))
+                        || (endVal != null && !endVal.equals(""))) {
+                      clause.append(" metadata_value ");
+
+                      boolean gotStart = false;
+
+                      if (startVal != null && !startVal.equals("")) {
+                        if (inclusive) {
+                          clause.append(">= '").append(startVal).append("'");
+                        } else {
+                          clause.append("> '").append(startVal).append("'");
                         }
-                        whereClause.append(clause);
-                        gotFirstClause = true;
-                    } else {
-                        String subSelectTblName = "p" + clauseNum;
-                        StringBuilder subSelectQuery = new 
StringBuilder(subSelectQueryBase
-                                + "WHERE (element_id = " + elementIdStr
-                                + " AND ");
-                        if (criteria instanceof TermQueryCriteria) {
-                            subSelectQuery.append(" metadata_value LIKE '%")
-                                          .append(((TermQueryCriteria) 
criteria).getValue()).append("%')");
-                        } else if (criteria instanceof RangeQueryCriteria) {
-                            String startVal = ((RangeQueryCriteria) criteria)
-                                    .getStartValue();
-                            String endVal = ((RangeQueryCriteria) criteria)
-                                    .getEndValue();
-
-                            if (startVal != null || endVal != null) {
-                                subSelectQuery.append(" metadata_value ");
-
-                                boolean gotStart = false;
-
-                                if (startVal != null && !startVal.equals("")) {
-                                    subSelectQuery.append(">= 
'").append(startVal).append("'");
-                                    gotStart = true;
-                                }
-
-                                if (endVal != null && !endVal.equals("")) {
-                                    if (gotStart) {
-                                        subSelectQuery.append(" AND 
metadata_value <= '").append(endVal).append("'");
-                                    } else
-                                        subSelectQuery.append("<= 
'").append(endVal).append("'");
-                                }
-
-                                subSelectQuery.append(") ");
-
-                            }
+                        gotStart = true;
+                      }
+
+                      if (endVal != null && !endVal.equals("")) {
+                        if (gotStart) {
+                          if (inclusive) {
+                            clause.append(" AND metadata_value <= 
'").append(endVal).append("'");
+                          } else {
+                            clause.append(" AND metadata_value < 
'").append(endVal).append("'");
+                          }
+                        } else if (inclusive) {
+                          clause.append("<= '").append(endVal).append("'");
+                        } else {
+                          clause.append("< '").append(endVal).append("'");
+                        }
+                      }
+
+                      clause.append(") ");
+                    }
+                  }
+                  whereClause.append(clause);
+                  gotFirstClause = true;
+                } else {
+                  String subSelectTblName = "p" + clauseNum;
+                  StringBuilder subSelectQuery = new 
StringBuilder(subSelectQueryBase
+                                                                   + "WHERE 
(element_id = " + elementIdStr
+                                                                   + " AND ");
+                  if (criteria instanceof TermQueryCriteria) {
+                    subSelectQuery.append(" metadata_value LIKE '%")
+                                  .append(((TermQueryCriteria) 
criteria).getValue()).append("%')");
+                  } else if (criteria instanceof RangeQueryCriteria) {
+                    String startVal = ((RangeQueryCriteria) criteria)
+                        .getStartValue();
+                    String endVal = ((RangeQueryCriteria) criteria)
+                        .getEndValue();
+
+                    if (startVal != null || endVal != null) {
+                      subSelectQuery.append(" metadata_value ");
+
+                      boolean gotStart = false;
+
+                      if (startVal != null && !startVal.equals("")) {
+                        subSelectQuery.append(">= 
'").append(startVal).append("'");
+                        gotStart = true;
+                      }
+
+                      if (endVal != null && !endVal.equals("")) {
+                        if (gotStart) {
+                          subSelectQuery.append(" AND metadata_value <= 
'").append(endVal).append("'");
+                        } else {
+                          subSelectQuery.append("<= 
'").append(endVal).append("'");
                         }
+                      }
 
-                        fromClause.append("INNER JOIN (" + 
subSelectQuery.toString()
-                                + ") " + subSelectTblName + " ON "
-                                + subSelectTblName
-                                + ".product_id = p.product_id ");
+                      subSelectQuery.append(") ");
 
                     }
+                  }
+
+                  fromClause.append("INNER JOIN 
(").append(subSelectQuery.toString()).append(") ")
+                            .append(subSelectTblName).append(" ON 
").append(subSelectTblName)
+                            .append(".product_id = p.product_id ");
+
                 }
+              }
             }
 
-            getProductSql.append(selectClause.toString() + 
fromClause.toString());
+            getProductSql.append("SELECT COUNT(DISTINCT p.product_id) AS 
numResults ").append(fromClause.toString());
             if (gotFirstClause) {
                 getProductSql.append(whereClause.toString());
             }
@@ -1859,7 +1825,9 @@ public class DataSourceCatalog implements Catalog {
                     "Exception performing get num results. Message: "
                             + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback get num results transaction. 
Message: "
@@ -1874,7 +1842,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                rs = null;
             }
 
             if (statement != null) {
@@ -1883,7 +1850,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
             }
 
             if (conn != null) {
@@ -1893,7 +1859,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -1910,21 +1875,16 @@ public class DataSourceCatalog implements Catalog {
                     .get(productTypeId);
             String lastUpdateTime = (String) productListAndUpdateTime
                     .get("lastUpdateTime");
-            Date lastUpdateTimeDate = null;
 
             try {
-                lastUpdateTimeDate = DateConvert.isoParse(lastUpdateTime);
+                Date lastUpdateTimeDate = DateConvert.isoParse(lastUpdateTime);
 
                 long timeDifferenceMilis = currentTime.getTime()
                         - lastUpdateTimeDate.getTime();
                 long timeDifferenceSeconds = timeDifferenceMilis * 1000;
                 long timeDifferenceMinutes = timeDifferenceSeconds / 60;
 
-                if (timeDifferenceMinutes >= cacheUpdateMinutes) {
-                    return false;
-                } else {
-                    return true;
-                }
+              return timeDifferenceMinutes < cacheUpdateMinutes;
             } catch (Exception e) {
                 LOG.log(Level.WARNING,
                         "Unable to parse last update time for product type: ["
@@ -1938,7 +1898,7 @@ public class DataSourceCatalog implements Catalog {
 
     @SuppressWarnings("unchecked")
        private List<Product> getProductsFromCache(String productTypeId) {
-        List<Product> products = null;
+        List<Product> products;
 
         if (PRODUCT_CACHE.get(productTypeId) == null) {
             return null;
@@ -1962,7 +1922,7 @@ public class DataSourceCatalog implements Catalog {
 
     @SuppressWarnings("unused")
        private List<Product> getProductsByProductTypeCached(ProductType type) {
-        List<Product> products = null;
+        List<Product> products;
         // check the product cache first
         if (stillFresh(type.getProductTypeId())) {
             products = getProductsFromCache(type.getProductTypeId());
@@ -1976,7 +1936,7 @@ public class DataSourceCatalog implements Catalog {
                         "CatalogException getting cached products for type: ["
                                 + type.getProductTypeId() + "]: Message: "
                                 + e.getMessage());
-                return products;
+                return null;
             }
         }
 
@@ -2009,7 +1969,7 @@ public class DataSourceCatalog implements Catalog {
             if (!productIdString) {
                
                    if (query.getCriteria().size() == 0) {
-                       getProductSql.append("SELECT DISTINCT product_id FROM " 
+ type.getName() + "_metadata");
+                       getProductSql.append("SELECT DISTINCT product_id FROM 
").append(type.getName()).append("_metadata");
                    }else if (query.getCriteria().size() == 1) {
                        
getProductSql.append(this.getSqlQuery(query.getCriteria().get(0), type));
                    }else {
@@ -2021,8 +1981,9 @@ public class DataSourceCatalog implements Catalog {
             } else {
                
               if (query.getCriteria().size() == 0) {
-                getProductSql.append("SELECT DISTINCT products.product_id FROM 
products, " + type.getName() + "_metadata"
-                                                     + " WHERE 
products.product_id="+type.getName() + "_metadata.product_id");
+                getProductSql.append("SELECT DISTINCT products.product_id FROM 
products, ").append(type.getName())
+                             .append("_metadata").append(" WHERE 
products.product_id=").append(type.getName())
+                             .append("_metadata.product_id");
               }        else if (query.getCriteria().size() == 1) {
                 
getProductSql.append(this.getSqlQuery(query.getCriteria().get(0), type));
               }        else {
@@ -2048,7 +2009,7 @@ public class DataSourceCatalog implements Catalog {
                 // must call next first, or else no relative cursor
                 if (rs.next()) {
                     // grab the first one
-                    int numGrabbed = -1;
+                    int numGrabbed;
 
                     if (pageNum == 1) {
                         numGrabbed = 1;
@@ -2084,7 +2045,9 @@ public class DataSourceCatalog implements Catalog {
             LOG.log(Level.WARNING, "Exception performing query. Message: "
                     + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback query transaction. Message: "
@@ -2130,19 +2093,21 @@ public class DataSourceCatalog implements Catalog {
             BooleanQueryCriteria bqc = (BooleanQueryCriteria) queryCriteria;
             if (bqc.getOperator() == BooleanQueryCriteria.NOT) {               
                        if (!this.productIdString) {
-                               sqlQuery.append("SELECT DISTINCT product_id 
FROM " + type.getName() + "_metadata WHERE product_id "
-                                 + "NOT IN (" + 
this.getSqlQuery(bqc.getTerms().get(0), type) + ")");
+                               sqlQuery.append("SELECT DISTINCT product_id 
FROM ").append(type.getName())
+                                .append("_metadata WHERE product_id 
").append("NOT IN (")
+                                
.append(this.getSqlQuery(bqc.getTerms().get(0), type)).append(")");
                        } else {
-                               sqlQuery.append("SELECT DISTINCT 
products.product_id FROM products," + type.getName() + "_metadata"
-                                                                + " WHERE 
products.product_id="+type.getName() + "_metadata.product_id" 
-                                                                + " AND 
products.product_id NOT IN (" + this.getSqlQuery(bqc.getTerms().get
-                            (0), type) + ")");
+                               sqlQuery.append("SELECT DISTINCT 
products.product_id FROM products,").append(type.getName())
+                                .append("_metadata").append(" WHERE 
products.product_id=").append(type.getName())
+                                .append("_metadata.product_id").append(" AND 
products.product_id NOT IN (")
+                                .append(this.getSqlQuery(bqc.getTerms().get
+                                    (0), type)).append(")");
                        }
             }else {
-                sqlQuery.append("(" + this.getSqlQuery(bqc.getTerms().get(0), 
type));
+                
sqlQuery.append("(").append(this.getSqlQuery(bqc.getTerms().get(0), type));
                 String op = bqc.getOperator() == BooleanQueryCriteria.AND ? 
"INTERSECT" : "UNION";
                 for (int i = 1; i < bqc.getTerms().size(); i++) 
-                    sqlQuery.append(") " + op + " (" + 
this.getSqlQuery(bqc.getTerms().get(i), type));
+                    sqlQuery.append(") ").append(op).append(" 
(").append(this.getSqlQuery(bqc.getTerms().get(i), type));
                 sqlQuery.append(")");
             }
         }else {
@@ -2150,15 +2115,17 @@ public class DataSourceCatalog implements Catalog {
             if (fieldIdStringFlag) 
                 elementIdStr = "'" + elementIdStr + "'";
             if (!this.productIdString) {
-               sqlQuery.append("SELECT DISTINCT product_id FROM " + 
type.getName() + "_metadata WHERE element_id = " +
-                         elementIdStr + " AND ");
+               sqlQuery.append("SELECT DISTINCT product_id FROM 
").append(type.getName())
+                        .append("_metadata WHERE element_id = 
").append(elementIdStr).append(" AND ");
             } else {
-               sqlQuery.append("SELECT DISTINCT products.product_id FROM 
products," + type.getName() + "_metadata"
-                        + " WHERE products.product_id="+type.getName() + 
"_metadata.product_id" 
-                                    + " AND element_id = " + elementIdStr + " 
AND ");
+               sqlQuery.append("SELECT DISTINCT products.product_id FROM 
products,").append(type.getName())
+                        .append("_metadata").append(" WHERE 
products.product_id=").append(type.getName())
+                        .append("_metadata.product_id").append(" AND 
element_id = ").append(elementIdStr)
+                        .append(" AND ");
             }
             if (queryCriteria instanceof TermQueryCriteria) {
-                sqlQuery.append("metadata_value = '" + ((TermQueryCriteria) 
queryCriteria).getValue() + "'");
+                sqlQuery.append("metadata_value = 
'").append(((TermQueryCriteria) queryCriteria).getValue())
+                        .append("'");
             } else if (queryCriteria instanceof RangeQueryCriteria) {
                 RangeQueryCriteria rqc = (RangeQueryCriteria) queryCriteria;
                 String rangeSubQuery = null;
@@ -2183,7 +2150,6 @@ public class DataSourceCatalog implements Catalog {
             throws CatalogException {
         Connection conn = null;
         Statement statement = null;
-        ResultSet rs = null;
 
         String productRefTable = product.getProductType().getName()
                 + "_reference";
@@ -2202,24 +2168,21 @@ public class DataSourceCatalog implements Catalog {
             statement.execute(deleteProductSql);
 
             // now add the new ones back in
-            for (Iterator<Reference> i = 
product.getProductReferences().iterator(); i
-                    .hasNext();) {
-                Reference r = i.next();
+          for (Reference r : product.getProductReferences()) {
+            String addRefSql = "INSERT INTO "
+                               + productRefTable
+                               + " "
+                               + "(product_id, product_orig_reference, 
product_datastore_reference, product_reference_filesize,"
+                               + "product_reference_mimetype) " + "VALUES ("
+                               + product.getProductId() + ", '" + 
r.getOrigReference()
+                               + "', '" + r.getDataStoreReference() + "', "
+                               + r.getFileSize() + ",'" + 
r.getMimeType().getName()
+                               + "')";
 
-                String addRefSql = "INSERT INTO "
-                        + productRefTable
-                        + " "
-                        + "(product_id, product_orig_reference, 
product_datastore_reference, product_reference_filesize,"
-                        + "product_reference_mimetype) " + "VALUES ("
-                        + product.getProductId() + ", '" + r.getOrigReference()
-                        + "', '" + r.getDataStoreReference() + "', "
-                        + r.getFileSize() + ",'" + r.getMimeType().getName()
-                        + "')";
-
-                LOG.log(Level.FINE, "updateProductReferences: Executing: "
-                        + addRefSql);
-                statement.execute(addRefSql);
-            }
+            LOG.log(Level.FINE, "updateProductReferences: Executing: "
+                                + addRefSql);
+            statement.execute(addRefSql);
+          }
 
             conn.commit();
 
@@ -2229,7 +2192,9 @@ public class DataSourceCatalog implements Catalog {
                     "Exception updating product references. Message: "
                             + e.getMessage());
             try {
+              if (conn != null) {
                 conn.rollback();
+              }
             } catch (SQLException e2) {
                 LOG.log(Level.SEVERE,
                         "Unable to rollback updateProductReferences 
transaction. Message: "
@@ -2238,23 +2203,13 @@ public class DataSourceCatalog implements Catalog {
             throw new CatalogException(e.getMessage());
         } finally {
 
-            if (rs != null) {
-                try {
-                    rs.close();
-                } catch (SQLException ignore) {
-                }
-
-                rs = null;
-            }
-
-            if (statement != null) {
+          if (statement != null) {
                 try {
                     statement.close();
                 } catch (SQLException ignore) {
                 }
 
-                statement = null;
-            }
+          }
 
             if (conn != null) {
                 try {
@@ -2263,7 +2218,6 @@ public class DataSourceCatalog implements Catalog {
                 } catch (SQLException ignore) {
                 }
 
-                conn = null;
             }
         }
 
@@ -2272,8 +2226,8 @@ public class DataSourceCatalog implements Catalog {
     /**
      * Utility method to quote the "productId" value 
      * if the column type is "string".
-     * @param productId
-     * @return
+     * @param productId the product id
+     * @return the quoted productId
      */
     protected String quoteIt(String productId) {
        if (this.productIdString) {

Reply via email to