Changeset 6084


Ignore:
Timestamp:
Aug 16, 2012, 11:06:41 AM (11 years ago)
Author:
Nicklas Nordborg
Message:

References #1707: Make it possible for a derived bioassay to have multiple physical bioassays as parents

Implemented update script. Fixed queries used by the table exporter so that exporting the child and parent items work.

Location:
trunk
Files:
5 edited

Legend:

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

    r6082 r6084  
    33903390    </description>
    33913391  </query>
     3392
     3393  <query id="COPY_PARENT_PHYSICAL_BIOASSAYS" type="SQL">
     3394    <sql>
     3395      INSERT INTO [ParentPhysicalBioAssays] ([physicalbioassay_id], [derivedbioassay_id])
     3396      SELECT [bioassay_id], [id] FROM [DerivedBioAssays]
     3397    </sql>
     3398    <description>
     3399      An SQL query that copy the parent physical bioassays
     3400      to the new (in BASE 3.2) ParentPhysicalBioAssays table
     3401    </description>
     3402  </query>
     3403
     3404
     3405  <query id="COPY_PARENT_DERIVED_BIOASSAYS" type="SQL">
     3406    <sql>
     3407      INSERT INTO [ParentDerivedBioAssays] ([parentbioassay_id], [derivedbioassay_id])
     3408      SELECT [parent_id], [id] FROM [DerivedBioAssays] WHERE NOT [parent_id] IS NULL
     3409    </sql>
     3410    <description>
     3411      An SQL query that copy the parent derived bioassays
     3412      to the new (in BASE 3.2) ParentDerivedBioAssays table
     3413    </description>
     3414  </query>
     3415
     3416  <query id="SET_ISROOT_ON_DERIVED_BIOASSAYS" type="SQL">
     3417    <sql>
     3418      UPDATE [DerivedBioAssays] SET [is_root] = ([parent_id] IS NULL)
     3419    </sql>
     3420    <description>
     3421      An SQL query that set the is_root property on DerivedBioAssays table.
     3422    </description>
     3423  </query>
     3424
    33923425 
    33933426</predefined-queries>
  • trunk/src/core/net/sf/basedb/core/Install.java

    r6082 r6084  
    119119    method.
    120120  */
    121   public static final int NEW_SCHEMA_VERSION = Integer.valueOf(109).intValue();
     121  public static final int NEW_SCHEMA_VERSION = Integer.valueOf(110).intValue();
    122122 
    123123  public static synchronized int createTables(SchemaGenerator.Mode mode, ProgressReporter progress,
  • trunk/src/core/net/sf/basedb/core/Update.java

    r6082 r6084  
    151151  </tr>
    152152  <tr>
    153     <td>109</td>
     153    <td>109 and 110</td>
    154154    <td>
    155155      Added {@link DerivedBioAssayData#getPhysicalBioAssays()} and {@link DerivedBioAssayData#getParents()}
    156156      and removed <code>DerivedBioAssayData.getPhysicalBioAssay()</code> and
    157       <code>DerivedBioAssayData.getParent()</code>. Existing derived bioassays are updated.
     157      <code>DerivedBioAssayData.getParent()</code>. Existing derived bioassays are updated in a two-step
     158      procedure. The first step is to copy existing information to the new tables and the second step is
     159      to drop columns that are no longer needed.
    158160    </td>
    159161  </tr>
     
    250252      if (schemaVersion < 109)
    251253      {
    252         if (progress != null) progress.display((int)(progress_current), "--Updating schema version: " + schemaVersion + " -> 109...");
    253         schemaVersion = updateToSchemaVersion109(session);
     254        if (progress != null) progress.display((int)(progress_current), "--Updating schema version: " + schemaVersion + " -> 110...");
     255        schemaVersion = updateToSchemaVersion110(session, schemaVersion);
    254256        progress_current += progress_step;
    255257      }
     
    626628    old columns in the DerivedBioAssays table.
    627629   
    628     @return The new schema version (=109)
     630    @return The new schema version (=110)
    629631  */
    630   private static int updateToSchemaVersion109(org.hibernate.Session session)
     632  private static int updateToSchemaVersion110(org.hibernate.Session session, int currentSchemaVersion)
    631633    throws BaseException
    632634  {
    633     final int schemaVersion = 109;
    634     try
    635     {
    636  
    637       // TODO - 1707
    638      
     635    final int schemaVersion = 110;
     636    try
     637    {
     638      if (currentSchemaVersion < 109)
     639      {
     640        // First step is to copy existing information to the new tables
     641        org.hibernate.Transaction tx = null;
     642        try
     643        {
     644          tx = HibernateUtil.newTransaction(session);
     645         
     646          // Copy parent physical and derived bioassay information to the
     647          // new tables: ParentPhysicalBioAssays and ParentDerivedBioAssays
     648          org.hibernate.Query query = HibernateUtil.getPredefinedSQLQuery(session, "COPY_PARENT_PHYSICAL_BIOASSAYS");
     649          /*
     650            INSERT INTO ParentPhysicalBioAssays (physicalbioassay_id, derivedbioassay_id)
     651            SELECT bioassay_id, id FROM DerivedBioAssays
     652          */
     653          query.executeUpdate();
     654          query = HibernateUtil.getPredefinedSQLQuery(session, "COPY_PARENT_DERIVED_BIOASSAYS");
     655          /*
     656            INSERT INTO ParentDerivedBioAssays (parentbioassay_id, derivedbioassay_id)
     657            SELECT parent_id, id FROM DerivedBioAssays WHERE NOT parent_id IS NULL
     658          */
     659          query.executeUpdate();
     660         
     661          // Set is_root flag on derived bioassays
     662          query = HibernateUtil.getPredefinedSQLQuery(session, "SET_ISROOT_ON_DERIVED_BIOASSAYS");
     663          /*
     664            UPDATE DerivedBioAssays SET is_root = (parent_id IS NULL)
     665          */
     666          query.executeUpdate();
     667         
     668          // Remove table filters, etc. that use the old properties
     669          cleanContextFromProperty(session, Item.DERIVEDBIOASSAY, "physicalBioAssay", null, true);
     670          cleanContextFromProperty(session, Item.DERIVEDBIOASSAY, "parent", null, true);
     671         
     672          setSchemaVersion(session, 109);
     673 
     674          // Commit the changes
     675          HibernateUtil.commit(tx);
     676        }
     677        finally
     678        {
     679          if (tx != null && !tx.wasCommitted()) HibernateUtil.rollback(tx);
     680        }
     681      }
     682     
     683      // Second step is to drop the old columns that are no longer used in BASE 3.2
     684      dropColumn(session, "DerivedBioAssays", "parent_id", null);
     685      dropColumn(session, "DerivedBioAssays", "bioassay_id", null);
     686       
    639687      // Update the schema version number
    640688      setSchemaVersionInTransaction(session, schemaVersion);
    641689 
    642       log.info("updateToSchemaVersion109: OK");
    643     }
    644     catch (BaseException ex)
    645     {
    646       log.error("updateToSchemaVersion109: FAILED", ex);
     690      log.info("updateToSchemaVersion110: OK");
     691    }
     692    catch (SQLException ex)
     693    {
     694      log.error("updateToSchemaVersion110: FAILED", ex);
     695      throw new BaseException(ex);
     696    }
     697    catch (RuntimeException ex)
     698    {
     699      log.error("updateToSchemaVersion110: FAILED", ex);
    647700      throw ex;
    648701    }
     
    9571010      }
    9581011     
    959       progress.display(90, "  --alter table " + tableName + " drop column " + columnName);
     1012      if (progress != null)
     1013      {
     1014        progress.display(90, "  --alter table " + tableName + " drop column " + columnName);
     1015      }
    9601016      query = HibernateUtil.getPredefinedSQLQuery(session, "DROP_COLUMN", tableName, columnName);
    9611017      query.executeUpdate();
     
    11181174    {
    11191175      log.debug("Cleaning context for item: " + itemType + "; property=" + propertyName + "; replacement=" + replacementPropertyName);
    1120       tx = HibernateUtil.newTransaction(session);
     1176      tx = session.getTransaction().isActive() ? null : HibernateUtil.newTransaction(session);
     1177     
    11211178      query = HibernateUtil.createQuery(session, "select ctx from ContextData ctx where ctx.itemType = " + itemType.getValue());
    11221179      List<ContextData> contexts = HibernateUtil.loadList(ContextData.class, query, null);
     
    11481205        // Filter
    11491206        Map<String, PropertyFilterData> filters = ctx.getPropertyFilters();
    1150         PropertyFilterData propertyFilter = filters.remove(propertyName);
    1151         if (propertyFilter != null && replacementPropertyName != null && !forceRemoveFilter)
    1152         {
    1153           // Replace with new property
    1154           filters.put(replacementPropertyName, propertyFilter);
    1155         }
    1156        
    1157       }
    1158      
    1159      
    1160       HibernateUtil.commit(tx);
    1161     }
    1162     catch (BaseException ex)
    1163     {
     1207        List<String> filterProperties = new ArrayList<String>(filters.keySet());
     1208        for (String property : filterProperties)
     1209        {
     1210          if (property.startsWith(propertyName))
     1211          {
     1212            PropertyFilterData old = filters.remove(property);
     1213            if (replacementPropertyName != null)
     1214            {
     1215              filters.put(replacementPropertyName + property.substring(propertyName.length()), old);
     1216            }
     1217          }
     1218        }
     1219      }
     1220     
     1221      // Only commit if we started a new transaction
     1222      if (tx != null) HibernateUtil.commit(tx);
     1223    }
     1224    catch (RuntimeException ex)
     1225    {
     1226      // Only rollback if we started the transaction
    11641227      if (tx != null) HibernateUtil.rollback(tx);
    11651228      throw ex;
  • trunk/www/views/derivedbioassays/index.jsp

    r6082 r6084  
    8383  {
    8484    // Register formatters
     85    cc.setObject("export.formatter.&physicalBioAssays(name)", new NameableFormatter());
     86    cc.setObject("export.formatter.&parents(name)", new NameableFormatter());
    8587    cc.setObject("export.formatter.&children(name)", new NameableFormatter());
    8688    cc.setObject("export.formatter.&rawBioAssays(name)", new NameableFormatter());
     
    8890    // Register dataloaders
    8991    String bioassayParameter = "bioassay";
     92    // Physical bioassays
     93    ItemQuery<PhysicalBioAssay> physicalQuery = PhysicalBioAssay.getQuery();
     94    physicalQuery.include(cc.getInclude());
     95    physicalQuery.join(Hql.innerJoin("derivedBioAssays", "dba"));
     96    physicalQuery.restrict(Restrictions.eq(Hql.alias("dba"), Expressions.parameter(bioassayParameter)));
     97    physicalQuery.order(Orders.asc(Hql.property("name")));
     98    cc.setObject("export.dataloader.&physicalBioAssays(name)", new ItemQueryLoader(physicalQuery, bioassayParameter));
     99
     100    // Parent bioassays
     101    ItemQuery<DerivedBioAssay> parentQuery = DerivedBioAssay.getQuery();
     102    parentQuery.include(cc.getInclude());
     103    parentQuery.join(Hql.innerJoin("children", "c"));
     104    parentQuery.restrict(Restrictions.eq(Hql.alias("c"), Expressions.parameter(bioassayParameter)));
     105    parentQuery.order(Orders.asc(Hql.property("name")));
     106    cc.setObject("export.dataloader.&parents(name)", new ItemQueryLoader(parentQuery, bioassayParameter));
     107
    90108    // Child bioassays
    91     ItemQuery<DerivedBioAssay> dbasQuery = DerivedBioAssay.getQuery();
    92     dbasQuery.include(cc.getInclude());
    93     dbasQuery.restrict(Restrictions.eq(Hql.property("parent"), Expressions.parameter(bioassayParameter)));
    94     dbasQuery.order(Orders.asc(Hql.property("name")));
    95     cc.setObject("export.dataloader.&children(name)", new ItemQueryLoader(dbasQuery, bioassayParameter));
     109    ItemQuery<DerivedBioAssay> childQuery = DerivedBioAssay.getQuery();
     110    childQuery.include(cc.getInclude());
     111    childQuery.join(Hql.innerJoin("parents", "p"));
     112    childQuery.restrict(Restrictions.eq(Hql.alias("p"), Expressions.parameter(bioassayParameter)));
     113    childQuery.order(Orders.asc(Hql.property("name")));
     114    cc.setObject("export.dataloader.&children(name)", new ItemQueryLoader(childQuery, bioassayParameter));
    96115   
    97116    // Child raw bioassays
  • trunk/www/views/physicalbioassays/index.jsp

    r6082 r6084  
    9696    ItemQuery<DerivedBioAssay> dbasQuery = DerivedBioAssay.getQuery();
    9797    dbasQuery.include(cc.getInclude());
    98     dbasQuery.restrict(Restrictions.eq(Hql.property("physicalBioAssay"), Expressions.parameter(bioassayParameter)));
    99     dbasQuery.restrict(Restrictions.eq(Hql.property("parent"), null));
     98    dbasQuery.join(Hql.innerJoin("physicalBioAssays", "pba"));
     99    dbasQuery.restrict(Restrictions.eq(Hql.alias("pba"), Expressions.parameter(bioassayParameter)));
     100    dbasQuery.restrict(Restrictions.eq(Hql.property("root"), Expressions.bool(true)));
    100101    dbasQuery.order(Orders.asc(Hql.property("name")));
    101102    cc.setObject("export.dataloader.&rootDerivedBioAssays(name)", new ItemQueryLoader(dbasQuery, bioassayParameter));
Note: See TracChangeset for help on using the changeset viewer.