Changeset 3086


Ignore:
Timestamp:
Jan 29, 2007, 2:45:24 PM (15 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #466: Remaining Quantity not display in extracts

Location:
trunk/src/core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/common-queries.xml

    r3074 r3086  
    19591959    </sql>
    19601960    <description>
    1961       A Hibernate query that updates the remaining quantity on a
     1961      A Hibernate query that adds/removes the remaining quantity on a
    19621962      measuered biomaterial.
    19631963    </description>
    19641964  </query>
     1965
     1966  <query id="SET_REMAINING_QUANTITY" type="HQL">
     1967    <sql>
     1968      UPDATE MeasuredBioMaterialData mbm
     1969      SET mbm.remainingQuantity = :remain
     1970      WHERE mbm = :bioMaterial
     1971    </sql>
     1972    <description>
     1973      A Hibernate query that sets the remaining quantity on a
     1974      measuered biomaterial.
     1975    </description>
     1976  </query>
     1977
    19651978
    19661979  <query id="GET_ANYTOANY_FOR_NAME" type="HQL">
     
    24992512  </query>
    25002513
    2501 
    25022514  <query id="SET_MAX_MAPPING_ON_DATACUBE" type="HQL">
    25032515    <sql>
     
    25112523  </query>
    25122524
     2525  <query id="GET_SUM_USED_QUANTITY_EVENTS" type="HQL">
     2526    <sql>
     2527      SELECT evt.bioMaterial, SUM(evt.usedQuantity)
     2528      FROM BioMaterialEventData evt
     2529      GROUP BY evt.bioMaterial
     2530    </sql>
     2531    <description>
     2532      A HQL query that loads the sum of the used quantity
     2533      grouped by the biomaterial for all BioMaterialEvent:s.
     2534    </description>
     2535  </query>
     2536 
     2537  <query id="GET_SUM_USED_QUANTITY_SOURCES" type="HQL">
     2538    <sql>
     2539      SELECT index(src), SUM(src.usedQuantity)
     2540      FROM BioMaterialEventData evt
     2541      JOIN evt.sources src
     2542      GROUP BY index(src)
     2543    </sql>
     2544    <description>
     2545      A HQL query that loads the sum of the used quantity
     2546      grouped by the biomaterial for all sources to
     2547      BioMaterialEvent:s.
     2548    </description>
     2549  </query>
     2550
    25132551</predefined-queries>
  • trunk/src/core/net/sf/basedb/core/DbControl.java

    r3076 r3086  
    3434import net.sf.basedb.util.ClassUtil;
    3535
     36import java.util.IdentityHashMap;
    3637import java.util.Iterator;
    3738import java.util.LinkedList;
     
    133134    hSession = HibernateUtil.newSession();
    134135    hTransaction = HibernateUtil.newTransaction(hSession);
    135     itemCache = new HashMap<BasicData,BasicItem>();
     136    itemCache = new IdentityHashMap<BasicData, BasicItem>();
    136137    commitQueue = new LinkedHashMap<BasicItem,Transactional.Action>();
    137138    isClosed = false;
     
    368369          {
    369370            HibernateUtil.saveData(hSession, data);
    370             itemCache.put(data, item);
    371371            item.onAfterInsert();
    372372          }
     
    377377            // Need flush so directories can be deleted recursiveley
    378378            if (item instanceof Directory || item instanceof File) HibernateUtil.flush(hSession);
    379             itemCache.remove(data);
    380379          }
    381380          if (action != Transactional.Action.UPDATE || transactional) afterCommitQueue.put(item, action);
     
    652651      assert itemConstructor != null : "itemConstructor == null";     
    653652      I item = itemConstructor.newInstance(constructorParams);
    654       item.setDbControl(this);         
     653      item.setDbControl(this); 
     654      itemCache.put(data, item);
    655655      return item;     
    656656    }
     
    776776        {
    777777          item = c.newInstance(constructorParams);
     778          itemCache.put(data, item);
    778779        }
    779780        catch (Exception ex)
     
    791792    item.initPermissions(0, 0);
    792793    item.checkPermission(Permission.READ);
    793     itemCache.put(data, item);
    794794    if (item instanceof Controlled)
    795795    {
     
    886886      }
    887887      items.add(new SaveIf(item, before));
    888       itemCache.put(item.getData(), item);
    889888    }
    890889  }
     
    978977  /**
    979978    Check if an item is attached to this <code>DbControl</code>.
    980     An item is attached if it is found in either the item cache
    981     or commit queue.
     979    An item is attached if it is found in either the item cache (existing items
     980    only) or commit queue (new or existing items).
    982981  */
    983982  boolean isAttached(BasicItem item)
    984983  {
    985984    assert item != null : "item == null";
    986     return itemCache.containsKey(item.getData()) || commitQueue.containsKey(item);
     985    return commitQueue.containsKey(item) || (itemCache.containsKey(item.getData()) && item.isInDatabase());
    987986  }
    988987 
  • trunk/src/core/net/sf/basedb/core/Install.java

    r2993 r3086  
    102102    method.
    103103  */
    104   public static final int NEW_SCHEMA_VERSION = 28;
     104  public static final int NEW_SCHEMA_VERSION = 29;
    105105
    106106  public static synchronized void createTables(boolean update, final ProgressReporter progress)
  • trunk/src/core/net/sf/basedb/core/MeasuredBioMaterial.java

    r3079 r3086  
    219219      // For unsaved, objects we must use the object itself
    220220      Float remain = bioMaterial.getRemainingQuantity();
    221       if (remain == null) return;
     221      if (remain == null) remain = 0.0f;
    222222      bioMaterial.setRemainingQuantity(remain-usedQuantity);
    223223    }
  • trunk/src/core/net/sf/basedb/core/Update.java

    r2993 r3086  
    2929import java.sql.Statement;
    3030import java.util.Collections;
     31import java.util.HashMap;
    3132import java.util.List;
     33import java.util.Map;
    3234
    3335import org.hibernate.mapping.Table;
     
    3537import net.sf.basedb.core.data.DataCubeData;
    3638import net.sf.basedb.core.data.FormulaData;
     39import net.sf.basedb.core.data.MeasuredBioMaterialData;
    3740import net.sf.basedb.core.data.PlateData;
    3841import net.sf.basedb.core.data.PlateMappingData;
     
    338341      The update must query the dynamic database and set this value for
    339342      all existing data cubes.
     343    </td>
     344  </tr>
     345
     346  <tr>
     347    <td>29</td>
     348    <td>
     349      No schema change as such, but we need to verify and possibly update the
     350      {@link net.sf.basedb.core.data.MeasuredBioMaterialData#getRemainingQuantity()}
     351      since a bug may have caused it to have an incorrect value.
    340352    </td>
    341353  </tr>
     
    484496      {
    485497        if (progress != null) progress.display((int)(27*progress_factor), "--Updating schema version: " + schemaVersion + " -> 28...");
    486         updateToSchemaVersion28(sc);
     498        schemaVersion = updateToSchemaVersion28(sc);
     499      }
     500     
     501      if (schemaVersion < 29)
     502      {
     503        if (progress != null) progress.display((int)(28*progress_factor), "--Updating schema version: " + schemaVersion + " -> 29...");
     504        schemaVersion = updateToSchemaVersion29(session);
    487505      }
    488506     
    489507      /*
    490       if (schemaVersion < 29)
    491       {
    492         if (progress != null) progress.display((int)(28*progress_factor), "--Updating schema version: " + schemaVersion + " -> 29...");
    493         schemaVersion = setSchemaVersionInTransaction(session, 29);
     508      if (schemaVersion < 30)
     509      {
     510        if (progress != null) progress.display((int)(29*progress_factor), "--Updating schema version: " + schemaVersion + " -> 30...");
     511        schemaVersion = setSchemaVersionInTransaction(session, 30);
    494512        - or -
    495         schemaVersion = updateToSchemaVersion29(session);
     513        schemaVersion = updateToSchemaVersion30(session);
    496514      }
    497515      ... etc...
     
    10511069  }
    10521070
    1053 
     1071  /**
     1072    Vefify and update the remaining quantity of all biomaterials.
     1073    @return The new schema version (=29)
     1074  */
     1075  private static int updateToSchemaVersion29(org.hibernate.Session session)
     1076    throws BaseException
     1077  {
     1078    final int schemaVersion = 29;
     1079   
     1080    org.hibernate.Transaction tx = null;
     1081    try
     1082    {
     1083      tx = HibernateUtil.newTransaction(session);
     1084     
     1085      // For temporary storing the new remaining quantities
     1086      Map<MeasuredBioMaterialData, Float> remaining = new HashMap<MeasuredBioMaterialData, Float>();
     1087     
     1088      // Load sum of used quantity for all events
     1089      org.hibernate.Query eventQuery = HibernateUtil.getPredefinedQuery(session,
     1090        "GET_SUM_USED_QUANTITY_EVENTS");
     1091      /*
     1092        SELECT evt.bioMaterial, SUM(evt.usedQuantity)
     1093        FROM BioMaterialEventData evt
     1094        GROUP BY evt.bioMaterial
     1095      */
     1096      // Load sum of used quantity for sources to all events
     1097      org.hibernate.Query sourcesQuery = HibernateUtil.getPredefinedQuery(session,
     1098        "GET_SUM_USED_QUANTITY_SOURCES");
     1099        /*
     1100          SELECT index(src), SUM(src.usedQuantity)
     1101          FROM BioMaterialEventData evt
     1102          JOIN evt.sources src
     1103          GROUP BY index(src)
     1104         */
     1105      List<Object[]> events = HibernateUtil.loadList(Object[].class, eventQuery);
     1106      events.addAll(HibernateUtil.loadList(Object[].class, sourcesQuery));
     1107     
     1108      // Calculate the remaining quantity for each biomaterial
     1109      // o[0] = biomaterial, o[1] = sum of used quantity
     1110      for (Object[] o : events)
     1111      {
     1112        MeasuredBioMaterialData bioMaterial = (MeasuredBioMaterialData)o[0];
     1113        Double sumUsedQuantity = (Double)o[1];
     1114        if (sumUsedQuantity != null)
     1115        {
     1116          Float currentRemaining = remaining.get(bioMaterial);
     1117          if (currentRemaining == null) currentRemaining = 0.0f;
     1118          currentRemaining = currentRemaining - sumUsedQuantity.floatValue();
     1119          remaining.put(bioMaterial, currentRemaining);
     1120        }
     1121      }
     1122     
     1123      // Update the remaining quantity
     1124      org.hibernate.Query updateQuery = HibernateUtil.getPredefinedQuery(session,
     1125        "SET_REMAINING_QUANTITY");
     1126      /*
     1127        UPDATE MeasuredBioMaterialData mbm
     1128        SET mbm.remainingQuantity = :remain
     1129        WHERE mbm = :bioMaterial
     1130      */
     1131      for (Map.Entry<MeasuredBioMaterialData, Float> entry : remaining.entrySet())
     1132      {
     1133        MeasuredBioMaterialData bioMaterial = entry.getKey();
     1134        Float remainingQuantity = entry.getValue();
     1135        updateQuery.setEntity("bioMaterial", bioMaterial);
     1136        updateQuery.setFloat("remain", remainingQuantity);
     1137        updateQuery.executeUpdate();
     1138      }
     1139     
     1140      // Update the shcema version number
     1141      setSchemaVersion(session, schemaVersion);
     1142 
     1143      // Commit the changes
     1144      HibernateUtil.commit(tx);
     1145      log.info("updateToSchemaVersion29: OK");
     1146    }
     1147    catch (BaseException ex)
     1148    {
     1149      if (tx != null) HibernateUtil.rollback(tx);
     1150      log.error("updateToSchemaVersion29: FAILED", ex);
     1151      throw ex;
     1152    }
     1153    return schemaVersion;
     1154  }
     1155 
    10541156  /**
    10551157    Adjust the existing items in the database to be compatible with the latest mappings.
Note: See TracChangeset for help on using the changeset viewer.