Changeset 4617


Ignore:
Timestamp:
Oct 30, 2008, 8:19:35 AM (13 years ago)
Author:
Martin Svensson
Message:

Fixes #1149 Mark related items for removal when "parent" item is marked for removal.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/RemovableUtil.java

    r4612 r4617  
    3030import net.sf.basedb.core.Experiment;
    3131import net.sf.basedb.core.File;
     32import net.sf.basedb.core.FileSetMember;
     33import net.sf.basedb.core.FileStoreEnabled;
    3234import net.sf.basedb.core.Include;
    3335import net.sf.basedb.core.Item;
     
    135137      {
    136138        BasicItem item = itemType.getById(dc, id);
     139        if (item instanceof FileStoreEnabled)
     140        {
     141          try
     142          {
     143            removeFileSetMembers(dc, (FileStoreEnabled)item, true);
     144          }
     145          catch(BaseException ex)
     146          {}
     147        }
    137148        try
    138149        {
     
    221232        for (Directory dir : subDirectories)
    222233        {
    223           numSubItemsRemoved = removeDependingItems(dc, dir, removed);         
     234          int[]numTemp = removeDependingItems(dc, dir, removed);
     235          numSubItemsRemoved[0] += numTemp[0];
     236          numSubItemsRemoved[1] += numTemp[1];
    224237          numItemsRemoved += setRemovedFlag(dir, removed) ? 1 : 0;
    225238        }       
     
    233246    else if (item instanceof Transformation)
    234247    {
     248      // Remove AnyToAny items this item links to.
     249      ItemQuery<AnyToAny> linkedItemQuery = AnyToAny.getLinksFrom((Transformation)item);     
     250      numSubItemsRemoved = sumArrays(numSubItemsRemoved, removeAnyToAnyLinkedTo(dc, linkedItemQuery, removed));
    235251      if (removed)
    236252      {
     
    251267          for (BioAssaySet bsa : bioAssaySets)
    252268          {
    253             numSubItemsRemoved = removeDependingItems(dc, bsa, removed);
     269            numSubItemsRemoved = sumArrays(numSubItemsRemoved, removeDependingItems(dc, bsa, removed));           
    254270            numItemsRemoved  += setRemovedFlag(bsa, removed) ? 1 : 0;
    255271          }
     
    264280        if (bas != null)
    265281        {
    266           numSubItemsRemoved = removeDependingItems(dc, bas, removed);
     282          int[]numTemp = removeDependingItems(dc, bas, removed);
     283          numSubItemsRemoved[0] += numTemp[0];
     284          numSubItemsRemoved[1] += numTemp[1];
     285         
    267286          numItemsRemoved += setRemovedFlag(bas, removed) ? 1 : 0;
    268287          numSubItemsRemoved[1]++;
    269288        }
    270289      }
    271       // Remove AnyToAny items this item links to.
    272       ItemQuery<AnyToAny> linkedItemQuery = AnyToAny.getLinksFrom((Transformation)item);
    273       int[] numAnyToAnyRemoved = removeAnyToAnyLinkedTo(linkedItemQuery.list(dc), removed);
    274       numSubItemsRemoved[0] += numAnyToAnyRemoved[0];
    275       numSubItemsRemoved[1] += numAnyToAnyRemoved[1];
    276290    }
    277291   
     
    282296    else if (item instanceof BioAssaySet)
    283297    {
     298      numSubItemsRemoved = sumArrays(numSubItemsRemoved, removeFileSetMembers(dc, (BioAssaySet)item, removed));     
    284299      if (removed)
    285300      {
     
    300315          for (Transformation tf : transformations)
    301316          {
    302             numSubItemsRemoved = removeDependingItems(dc, tf, removed);
     317            numSubItemsRemoved = sumArrays(numSubItemsRemoved, removeDependingItems(dc, tf, removed));
    303318            numItemsRemoved += setRemovedFlag(tf, removed) ? 1 : 0;
    304319          }
     
    312327        if (tf != null)
    313328        {
    314           numSubItemsRemoved = removeDependingItems(dc, tf, removed);
     329          int[]numTemp = removeDependingItems(dc, tf, removed);
     330          numSubItemsRemoved[0] += numTemp[0];
     331          numSubItemsRemoved[1] += numTemp[1];
    315332          numItemsRemoved += setRemovedFlag(tf, removed) ? 1 : 0;
    316333          numSubItemsRemoved[1]++;
     
    340357        for (Transformation tf : transformations)
    341358        {
    342           numSubItemsRemoved = removeDependingItems(dc, tf, removed);
     359          int[]numTemp = removeDependingItems(dc, tf, removed);
     360          numSubItemsRemoved[0] += numTemp[0];
     361          numSubItemsRemoved[1] += numTemp[1];
    343362          numItemsRemoved += setRemovedFlag(tf, removed) ? 1 : 0;
    344363        }
     
    350369    return numSubItemsRemoved;
    351370  }
     371 
     372  private static boolean setRemovedFlag(Removable item, boolean removed)
     373  {
     374    boolean isRemoved = false;
     375    if (item.isRemoved() != removed)
     376    {
     377      try
     378      {
     379        item.setRemoved(removed);
     380        isRemoved = true;
     381      }
     382      catch (PermissionDeniedException ex)
     383      {}
     384    }
     385    else
     386    {
     387      isRemoved = true;
     388    }
     389    return isRemoved;
     390  }
     391 
     392  // Adds numbers with same index and returns the summarized array.
     393  private static int[] sumArrays(int[] baseArray, int[] newResult)
     394  {
     395    for (int i=0; i<baseArray.length; i++)
     396    {
     397      baseArray[i] += newResult[i];
     398    }
     399    return baseArray;
     400  }
     401 
     402
    352403 
    353404  /*
    354405    Removes all removable items that are linked to items in a list
    355406   */
    356   private static int[] removeAnyToAnyLinkedTo(List<AnyToAny> anyToAnyItems, boolean removed)
    357   {
    358     int numRemoved = 0;
    359     int numTotal  = 0;   
     407  private static int[] removeAnyToAnyLinkedTo(DbControl dc, ItemQuery<AnyToAny> any2AnyQuery, boolean removed)
     408  {
     409    int[] numA2AItemsRemoved = {0,0};
    360410    try
    361411    {
    362       for (AnyToAny a2a : anyToAnyItems)
     412      for (AnyToAny a2a : any2AnyQuery.list(dc))
    363413      {
    364414        if (a2a.getTo() instanceof Removable)
    365415        {
    366416          Removable removableItem = (Removable)a2a.getTo();
    367           numRemoved += setRemovedFlag(removableItem, removed) ? 1 : 0;
    368           numTotal++;
     417          if (removableItem instanceof Directory)
     418          {
     419            numA2AItemsRemoved = removeDependingItems(dc, removableItem, removed);
     420          }
     421          numA2AItemsRemoved[0] += setRemovedFlag(removableItem, removed) ? 1 : 0;
     422          numA2AItemsRemoved[1]++;
    369423        }
    370424      }
    371425    }
    372426    catch (BaseException ex)
     427    {}
     428    return numA2AItemsRemoved;
     429  }
     430 
     431  /*
     432    Sets all fileset members to be removed
     433   */
     434  private static int[] removeFileSetMembers(DbControl dc, FileStoreEnabled fileStore, boolean removed)
     435  {
     436    int[] counter = {0,0};
     437    try
     438    {
     439      ItemQuery<FileSetMember> memberQuery = fileStore.getFileSet().getMembers();
     440      for (FileSetMember member : memberQuery.list(dc))
     441      {
     442        counter[0] += setRemovedFlag(member.getFile(), removed) ? 1 : 0;
     443        counter[1]++;
     444      }
     445    }
     446    catch(BaseException ex)
    373447    {}
    374     int[] result = new int[]{numRemoved, numTotal};
    375     return result;
    376   }
    377 
    378   private static boolean setRemovedFlag(Removable item, boolean removed)
    379   {
    380     boolean isRemoved = false;
    381     if (item.isRemoved() != removed)
    382     {
    383       try
    384       {
    385         item.setRemoved(removed);
    386         isRemoved = true;
    387       }
    388       catch (PermissionDeniedException ex)
    389       {}
    390     }
    391     else
    392     {
    393       isRemoved = true;
    394     }
    395     return isRemoved;
    396   }
    397  
     448    return counter;
     449  }
     450
    398451}
Note: See TracChangeset for help on using the changeset viewer.