Changeset 6249


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

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

The core code has now been fixed to keep the parent information synchronized in both places. Still need to create an update fix.

File:
1 edited

Legend:

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

    r6021 r6249  
    693693    }
    694694
    695     if (getData().getBioMaterial() != null)
    696     {
    697       // If the product is a biomaterial we may need to set parent + parent type
    698       MeasuredBioMaterialData product = getData().getBioMaterial();
    699       if (sources.size() == 1)
    700       {
    701         // This is the first parent
    702         product.setParentType(bioMaterial.getType().getValue());
    703         product.setParent(bmData);
    704       }
    705       else
    706       {
    707         // Clear if we get more than one parent
    708         product.setParent(null);
    709       }
    710     }
     695    ensureProductParentCorrectness(bioMaterial.getType());
    711696
    712697    return new BioMaterialEventSource(getDbControl(), src);
     
    734719    BioMaterialEventSourceData src = sources.remove(data);
    735720   
    736     if (sources.size() == 0)
    737     {
    738       if (getData().getBioMaterial() != null)
    739       {
    740         getData().getBioMaterial().setParentType(null);
    741       }
    742     }
    743    
     721    // Early exit if no change
    744722    if (src == null) return;
     723   
     724    ensureProductParentCorrectness(bioMaterial.getType());
    745725   
    746726    if (src.getId() != 0) getDbControl().getHibernateSession().delete(src);
     
    755735  }
    756736
     737  /**
     738    Ensure that the {@link MeasuredBioMaterial#getParent()}
     739    and {@link MeasuredBioMaterial#getParentType()} reflect the current
     740    sources collection.
     741  */
     742  private void ensureProductParentCorrectness(Item itemType)
     743  {
     744    MeasuredBioMaterialData product = getData().getBioMaterial();
     745    if (product != null)
     746    {
     747      // If the product is a biomaterial we may need to set parent + parent type
     748      Map<BioMaterialData, BioMaterialEventSourceData> sources = getData().getSources();
     749      int numSources = sources.size();
     750     
     751      if (numSources == 0)
     752      {
     753        // Clear parent type and parent item
     754        product.setParentType(null);
     755        product.setParent(null);
     756      }
     757      else if (numSources == 1)
     758      {
     759        // This is the only parent
     760        BioMaterialData parent = sources.keySet().iterator().next();
     761        product.setParentType(itemType.getValue());
     762        product.setParent(parent);
     763      }
     764      else
     765      {
     766        // Clear if we get more than one parent
     767        product.setParent(null);
     768        product.setParentType(itemType.getValue());
     769      }
     770    }
     771  }
     772 
    757773  /**
    758774    Check if the biomaterial is allowed as a source.
Note: See TracChangeset for help on using the changeset viewer.