Changeset 6250


Ignore:
Timestamp:
Mar 6, 2013, 1:51:33 PM (9 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1745: BioMaterial?.getParent() return null after changing the (single) parent of a biomaterial

Added fix to update script.

Location:
branches/3.2-stable/src/core/net/sf/basedb/core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2-stable/src/core/net/sf/basedb/core/BioMaterialEvent.java

    r6249 r6250  
    693693    }
    694694
    695     ensureProductParentCorrectness(bioMaterial.getType());
     695    ensureProductParentCorrectness(getData());
    696696
    697697    return new BioMaterialEventSource(getDbControl(), src);
     
    722722    if (src == null) return;
    723723   
    724     ensureProductParentCorrectness(bioMaterial.getType());
     724    ensureProductParentCorrectness(getData());
    725725   
    726726    if (src.getId() != 0) getDbControl().getHibernateSession().delete(src);
     
    740740    sources collection.
    741741  */
    742   private void ensureProductParentCorrectness(Item itemType)
    743   {
    744     MeasuredBioMaterialData product = getData().getBioMaterial();
     742  static void ensureProductParentCorrectness(BioMaterialEventData event)
     743  {
     744    MeasuredBioMaterialData product = event.getBioMaterial();
    745745    if (product != null)
    746746    {
    747747      // If the product is a biomaterial we may need to set parent + parent type
    748       Map<BioMaterialData, BioMaterialEventSourceData> sources = getData().getSources();
     748      Map<BioMaterialData, BioMaterialEventSourceData> sources = event.getSources();
    749749      int numSources = sources.size();
    750750     
     
    759759        // This is the only parent
    760760        BioMaterialData parent = sources.keySet().iterator().next();
    761         product.setParentType(itemType.getValue());
     761        product.setParentType(Item.fromDataObject(parent).getValue());
    762762        product.setParent(parent);
    763763      }
     
    766766        // Clear if we get more than one parent
    767767        product.setParent(null);
    768         product.setParentType(itemType.getValue());
    769768      }
    770769    }
  • branches/3.2-stable/src/core/net/sf/basedb/core/Install.java

    r6152 r6250  
    119119    method.
    120120  */
    121   public static final int NEW_SCHEMA_VERSION = Integer.valueOf(111).intValue();
     121  public static final int NEW_SCHEMA_VERSION = Integer.valueOf(112).intValue();
    122122 
    123123  public static synchronized int createTables(SchemaGenerator.Mode mode, ProgressReporter progress,
  • branches/3.2-stable/src/core/net/sf/basedb/core/Update.java

    r6113 r6250  
    4646import net.sf.basedb.core.data.AnyToAnyData;
    4747import net.sf.basedb.core.data.ArraySlideData;
     48import net.sf.basedb.core.data.BioMaterialEventData;
    4849import net.sf.basedb.core.data.BioMaterialEventSourceData;
    4950import net.sf.basedb.core.data.BioPlateData;
     
    5960import net.sf.basedb.core.data.ItemParameterValueData;
    6061import net.sf.basedb.core.data.ItemSubtypeData;
     62import net.sf.basedb.core.data.MeasuredBioMaterialData;
    6163import net.sf.basedb.core.data.PhysicalBioAssayData;
    6264import net.sf.basedb.core.data.ProjectData;
     
    167169    </td>
    168170  </tr>
     171  <tr>
     172    <td>112</td>
     173    <td>
     174      Not an actual database schema change, but we need to fix the parent_id and parent_type
     175      columns in the BioMaterials table to really reflect what is in the BioMaterialEventSources2
     176      table. For more info see BASE ticket: http://base.thep.lu.se/ticket/1745
     177    </td>
     178  </tr>
    169179  </table>
    170180
     
    273283        if (progress != null) progress.display((int)(progress_current), "--Updating schema version: " + schemaVersion + " -> 111...");
    274284        schemaVersion = updateToSchemaVersion111(session, progress);
     285        progress_current += progress_step;
     286      }
     287     
     288      if (schemaVersion < 112)
     289      {
     290        if (progress != null) progress.display((int)(progress_current), "--Updating schema version: " + schemaVersion + " -> 112...");
     291        schemaVersion = updateToSchemaVersion112(session, progress);
    275292        progress_current += progress_step;
    276293      }
     
    776793  }
    777794
     795  /**
     796    Ensure that parent_id and parent_type in BioMaterials table is correct
     797    when compared to BioMaterialEventSources2 table.
     798    @return The new schema version (=112)
     799  */
     800  private static int updateToSchemaVersion112(org.hibernate.Session session, final ProgressReporter progress)
     801    throws BaseException
     802  {
     803    final int schemaVersion = 112;
     804    org.hibernate.Transaction tx = null;
     805    try
     806    {
     807      tx = HibernateUtil.newTransaction(session);
     808     
     809      // Get all BioMaterial that has null in parent_id and/or parent_type
     810      String hql = "select bm.creationEvent from MeasuredBioMaterialData bm" +
     811          " where bm.parent is null or bm.parentType is null";
     812     
     813      org.hibernate.Query query = HibernateUtil.createQuery(session, hql);
     814      List<BioMaterialEventData> events = HibernateUtil.loadList(BioMaterialEventData.class, query, null);
     815      int i = 0;
     816      for (BioMaterialEventData event : events)
     817      {
     818        BioMaterialEvent.ensureProductParentCorrectness(event);
     819        i++;
     820      }
     821     
     822      // Update the schema version number
     823      setSchemaVersion(session, schemaVersion);
     824 
     825      // Commit the changes
     826      HibernateUtil.commit(tx);
     827      log.info("updateToSchemaVersion112: OK");
     828    }
     829    catch (RuntimeException ex)
     830    {
     831      if (tx != null) HibernateUtil.rollback(tx);
     832      log.error("updateToSchemaVersion112: FAILED", ex);
     833      throw ex;
     834    }
     835    return schemaVersion;
     836  }
     837
     838 
    778839  // Item type codes for item types that was removed since BASE 2
    779840  private static final int LABELEDEXTRACT = 204;
Note: See TracChangeset for help on using the changeset viewer.