Changeset 3086
- Timestamp:
- Jan 29, 2007, 2:45:24 PM (16 years ago)
- Location:
- trunk/src/core
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/common-queries.xml
r3074 r3086 1959 1959 </sql> 1960 1960 <description> 1961 A Hibernate query that updates the remaining quantity on a1961 A Hibernate query that adds/removes the remaining quantity on a 1962 1962 measuered biomaterial. 1963 1963 </description> 1964 1964 </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 1965 1978 1966 1979 <query id="GET_ANYTOANY_FOR_NAME" type="HQL"> … … 2499 2512 </query> 2500 2513 2501 2502 2514 <query id="SET_MAX_MAPPING_ON_DATACUBE" type="HQL"> 2503 2515 <sql> … … 2511 2523 </query> 2512 2524 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 2513 2551 </predefined-queries> -
trunk/src/core/net/sf/basedb/core/DbControl.java
r3076 r3086 34 34 import net.sf.basedb.util.ClassUtil; 35 35 36 import java.util.IdentityHashMap; 36 37 import java.util.Iterator; 37 38 import java.util.LinkedList; … … 133 134 hSession = HibernateUtil.newSession(); 134 135 hTransaction = HibernateUtil.newTransaction(hSession); 135 itemCache = new HashMap<BasicData,BasicItem>();136 itemCache = new IdentityHashMap<BasicData, BasicItem>(); 136 137 commitQueue = new LinkedHashMap<BasicItem,Transactional.Action>(); 137 138 isClosed = false; … … 368 369 { 369 370 HibernateUtil.saveData(hSession, data); 370 itemCache.put(data, item);371 371 item.onAfterInsert(); 372 372 } … … 377 377 // Need flush so directories can be deleted recursiveley 378 378 if (item instanceof Directory || item instanceof File) HibernateUtil.flush(hSession); 379 itemCache.remove(data);380 379 } 381 380 if (action != Transactional.Action.UPDATE || transactional) afterCommitQueue.put(item, action); … … 652 651 assert itemConstructor != null : "itemConstructor == null"; 653 652 I item = itemConstructor.newInstance(constructorParams); 654 item.setDbControl(this); 653 item.setDbControl(this); 654 itemCache.put(data, item); 655 655 return item; 656 656 } … … 776 776 { 777 777 item = c.newInstance(constructorParams); 778 itemCache.put(data, item); 778 779 } 779 780 catch (Exception ex) … … 791 792 item.initPermissions(0, 0); 792 793 item.checkPermission(Permission.READ); 793 itemCache.put(data, item);794 794 if (item instanceof Controlled) 795 795 { … … 886 886 } 887 887 items.add(new SaveIf(item, before)); 888 itemCache.put(item.getData(), item);889 888 } 890 889 } … … 978 977 /** 979 978 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 o r 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). 982 981 */ 983 982 boolean isAttached(BasicItem item) 984 983 { 985 984 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()); 987 986 } 988 987 -
trunk/src/core/net/sf/basedb/core/Install.java
r2993 r3086 102 102 method. 103 103 */ 104 public static final int NEW_SCHEMA_VERSION = 2 8;104 public static final int NEW_SCHEMA_VERSION = 29; 105 105 106 106 public static synchronized void createTables(boolean update, final ProgressReporter progress) -
trunk/src/core/net/sf/basedb/core/MeasuredBioMaterial.java
r3079 r3086 219 219 // For unsaved, objects we must use the object itself 220 220 Float remain = bioMaterial.getRemainingQuantity(); 221 if (remain == null) re turn;221 if (remain == null) remain = 0.0f; 222 222 bioMaterial.setRemainingQuantity(remain-usedQuantity); 223 223 } -
trunk/src/core/net/sf/basedb/core/Update.java
r2993 r3086 29 29 import java.sql.Statement; 30 30 import java.util.Collections; 31 import java.util.HashMap; 31 32 import java.util.List; 33 import java.util.Map; 32 34 33 35 import org.hibernate.mapping.Table; … … 35 37 import net.sf.basedb.core.data.DataCubeData; 36 38 import net.sf.basedb.core.data.FormulaData; 39 import net.sf.basedb.core.data.MeasuredBioMaterialData; 37 40 import net.sf.basedb.core.data.PlateData; 38 41 import net.sf.basedb.core.data.PlateMappingData; … … 338 341 The update must query the dynamic database and set this value for 339 342 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. 340 352 </td> 341 353 </tr> … … 484 496 { 485 497 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); 487 505 } 488 506 489 507 /* 490 if (schemaVersion < 29)491 { 492 if (progress != null) progress.display((int)(2 8*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); 494 512 - or - 495 schemaVersion = updateToSchemaVersion 29(session);513 schemaVersion = updateToSchemaVersion30(session); 496 514 } 497 515 ... etc... … … 1051 1069 } 1052 1070 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 1054 1156 /** 1055 1157 Adjust the existing items in the database to be compatible with the latest mappings.
Note: See TracChangeset
for help on using the changeset viewer.