Changeset 3004


Ignore:
Timestamp:
Dec 6, 2006, 4:06:47 PM (16 years ago)
Author:
Martin Svensson
Message:

Fixes #374 Restoring a Bioassayset or Transformation restores
all child bioassay sets and transformations but not the parents

Restoring directories and files had a strange behavior too. It is also fixed in this commit.

Location:
trunk
Files:
2 edited

Legend:

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

    r2866 r3004  
    8282    @param dc The database control to use when accessing the database
    8383    @param itemType The itemType to delete
    84     @param items A set of the selected items in the list
     84    @param items A set of ids from the items that shall be deleted.
    8585    @param removed True if the items should be deleted. False if the items should be restored.
    86     @return An int[2] where [0] is the number of removed items, including the parent.
    87       and [1] is the total number of subitems that were included in the selection
     86    @return An int[2] where [0] is the number of removed/restored items, including the parent
     87      and [1] is the total number of items that were included in the selection.
    8888    @throws BaseException If there is an error
    8989  */
     
    9191    throws BaseException
    9292  {
    93     int[] numRemoved = {0,0};
     93    int[] numTotalCount = {0,0};
    9494    for (Integer id : items)
    9595    {     
     96      int[] numHandled = {0,0};
    9697      if (id != null)
    97       {
    98         int[] subRemovedCount = {0, 0};
     98      {       
    9999        Removable item = (Removable)itemType.getById(dc, id);
    100         subRemovedCount = removeSubItems(dc, item, removed);
    101         numRemoved[0] += subRemovedCount[0];
    102         numRemoved[1] += subRemovedCount[1];
    103        
    104         numRemoved[0] += setRemovedFlag(item, removed) ? 1 : 0;       
    105       }
    106     }
    107     return numRemoved;
     100        numHandled = removeDependingItems(dc, item, removed);
     101        if ((item instanceof Directory || item instanceof File) && !removed)
     102        {
     103          int[] numRestored = {0,0};
     104          Directory dir = null;
     105         
     106          if (item instanceof Directory)
     107          {
     108            dir = ((Directory)item).getParent();
     109          }
     110          else
     111          {
     112            dir = ((File)item).getDirectory();
     113          }         
     114          numRestored = restorePath(dir);
     115          numHandled[0] += numRestored[0];
     116          numHandled[1] += numRestored[1];
     117        }
     118        numHandled[0] += setRemovedFlag(item, removed) ? 1 : 0;       
     119      }
     120      numTotalCount[0] += numHandled[0];
     121      numTotalCount[1] += numHandled[1];
     122    }
     123    return numTotalCount;
    108124  }
    109125 
    110 
    111126  public static int delete(DbControl dc, Item itemType, Set<Integer> items)
    112127    throws BaseException
     
    128143    }
    129144    return numDeleted;
     145  }
     146 
     147  /**
     148    Restores the path to a directory
     149      @param parentDir The Directory that is being restored
     150      @return An int[2] were [0] is the number of restored directories and
     151        [1] is the total number of directories.
     152  */
     153  private static int[] restorePath(Directory parentDir)
     154  {
     155    int[] countRestored = {0,0};
     156    if (!parentDir.isRootDirectory())
     157    {
     158      countRestored = restorePath(parentDir.getParent());
     159    }
     160    countRestored[1]++;
     161    countRestored[0] += setRemovedFlag(parentDir, false) ? 1 : 0;
     162   
     163    return countRestored;
    130164  }
    131165 
     
    140174    @throws BaseException If there is an error
    141175  */
    142   private static int[] removeSubItems(DbControl dc, Removable parent, boolean removed)
     176  private static int[] removeDependingItems(DbControl dc, Removable item, boolean removed)
    143177    throws BaseException
    144178  {
     
    147181   
    148182    /**
    149       parent is a <code>Directory</code>
     183      item is a <code>Directory</code>
    150184    */
    151     if (parent instanceof Directory)
    152     {
    153       ItemQuery<Directory> subDirQuery = ((Directory)parent).getSubDirectories();
    154       ItemQuery<File> fileQuery = ((Directory)parent).getFiles();
     185    if (item instanceof Directory)
     186    {
     187      ItemQuery<Directory> subDirQuery = ((Directory)item).getSubDirectories();
     188      ItemQuery<File> fileQuery = ((Directory)item).getFiles();
    155189     
    156190      ItemResultList<Directory> subDirectories = null;
     
    160194      fileQuery.include(Include.REMOVED, Include.MINE, Include.OTHERS, Include.SHARED);
    161195                 
     196      int totalFiles = 0;
     197      int totalDirectories = 0;
     198     
    162199      try
    163200      {
     
    170207      if (files != null)
    171208      {
     209        totalFiles = files.size();
    172210        for (File file : files)
    173211        {
    174212          numItemsRemoved += setRemovedFlag(file, removed) ? 1 : 0;
    175         }
    176         numSubItemsRemoved[1] += files.size();
     213        }       
    177214      }
    178215     
    179216      if (subDirectories != null)
    180217      {
     218        totalDirectories = subDirectories.size();
    181219        for (Directory dir : subDirectories)
    182220        {
    183           numSubItemsRemoved = removeSubItems(dc, dir, removed);
     221          numSubItemsRemoved = removeDependingItems(dc, dir, removed);         
    184222          numItemsRemoved += setRemovedFlag(dir, removed) ? 1 : 0;
    185         }
    186         numSubItemsRemoved[1] += subDirectories.size();
    187       }
    188     }
    189    
     223        }       
     224        numSubItemsRemoved[1] += totalFiles + totalDirectories;
     225      }
     226    }
     227     
    190228    /**
    191       parent is a <code>Transformation</code>
     229      item is a <code>Transformation</code>
    192230    */
    193     else if (parent instanceof Transformation)
    194     {
    195       ItemQuery<BioAssaySet> bioAssayQuery = ((Transformation)parent).getProducts();     
    196       ItemResultList<BioAssaySet> bioAssaySets = null;
    197      
    198       bioAssayQuery.include(Include.REMOVED,Include.MINE, Include.OTHERS, Include.SHARED);
     231    else if (item instanceof Transformation)
     232    {
     233      if (removed)
     234      {
     235        ItemQuery<BioAssaySet> bioAssayQuery = ((Transformation)item).getProducts();     
     236        ItemResultList<BioAssaySet> bioAssaySets = null;
     237       
     238        bioAssayQuery.include(Include.REMOVED,Include.MINE, Include.OTHERS, Include.SHARED);
     239       
     240        try
     241        {
     242          bioAssaySets = bioAssayQuery.list(dc);
     243        }
     244        catch (BaseException ex)
     245        {}
     246       
     247        if (bioAssaySets != null)
     248        {
     249          for (BioAssaySet bsa : bioAssaySets)
     250          {
     251            numSubItemsRemoved = removeDependingItems(dc, bsa, removed);
     252            numItemsRemoved  += setRemovedFlag(bsa, removed) ? 1 : 0;
     253          }
     254          numSubItemsRemoved[1] += bioAssaySets.size();
     255        }     
     256      }
     257      else
     258      {
     259        Transformation tf = (Transformation)item;
     260        BioAssaySet bas = tf.getSource();
     261        // The transformation is not a root transformation
     262        if (bas != null)
     263        {
     264          numSubItemsRemoved = removeDependingItems(dc, bas, removed);
     265          numItemsRemoved += setRemovedFlag(bas, removed) ? 1 : 0;
     266          numSubItemsRemoved[1]++;
     267        }
     268      }
     269    }
     270   
     271   
     272    /**
     273      item is a <code>BioAssaySet</code>
     274    */
     275    else if (item instanceof BioAssaySet)
     276    {
     277      if (removed)
     278      {
     279        ItemQuery<Transformation> transformationQuery = ((BioAssaySet)item).getTransformations();
     280        ItemResultList<Transformation> transformations = null;
     281       
     282        transformationQuery.include(Include.REMOVED,Include.MINE, Include.OTHERS, Include.SHARED);
     283       
     284        try
     285        {
     286          transformations = transformationQuery.list(dc);
     287        }
     288        catch (BaseException ex)
     289        {}
     290       
     291        if (transformations != null)
     292        {
     293          for (Transformation tf : transformations)
     294          {
     295            numSubItemsRemoved = removeDependingItems(dc, tf, removed);
     296            numItemsRemoved += setRemovedFlag(tf, removed) ? 1 : 0;
     297          }
     298          numSubItemsRemoved[1] += transformations.size();
     299        }           
     300      }
     301      else
     302      {
     303        BioAssaySet bas = (BioAssaySet)item;
     304        Transformation tf = bas.getTransformation();
     305        if (tf != null)
     306        {
     307          numSubItemsRemoved = removeDependingItems(dc, tf, removed);
     308          numItemsRemoved += setRemovedFlag(tf, removed) ? 1 : 0;
     309          numSubItemsRemoved[1]++;
     310        }
     311      }
     312    }
     313   
     314    /**
     315      item is an <code>Experiment</code>
     316    */
     317    else if (item instanceof Experiment)
     318    {
     319      ItemQuery<Transformation> transformationQuery = ((Experiment)item).getRootTransformations();
     320      ItemResultList<Transformation> transformations = null;
     321     
     322      transformationQuery.include(Include.REMOVED,Include.MINE, Include.OTHERS, Include.SHARED);
    199323     
    200324      try
    201325      {
    202         bioAssaySets = bioAssayQuery.list(dc);
     326        transformations = transformationQuery.list(dc);
    203327      }
    204328      catch (BaseException ex)
    205329      {}
    206330     
    207       if (bioAssaySets != null)
    208       {
    209         for (BioAssaySet bsa : bioAssaySets)
    210         {
    211           numSubItemsRemoved = removeSubItems(dc, bsa, removed);
    212           numItemsRemoved  += setRemovedFlag(bsa, removed) ? 1 : 0;
    213         }
    214         numSubItemsRemoved[1] += bioAssaySets.size();
    215       }     
    216     }
    217    
    218     /**
    219       parent is a <code>BioAssaySet</code>
    220     */
    221     else if (parent instanceof BioAssaySet)
    222     {
    223       ItemQuery<Transformation> transformationQuery = ((BioAssaySet)parent).getTransformations();
    224       ItemResultList<Transformation> transformations = null;
    225      
    226       transformationQuery.include(Include.REMOVED,Include.MINE, Include.OTHERS, Include.SHARED);
    227      
    228       try
    229       {
    230         transformations = transformationQuery.list(dc);
    231       }
    232       catch (BaseException ex)
    233       {}
    234      
    235331      if (transformations != null)
    236332      {
    237333        for (Transformation tf : transformations)
    238334        {
    239           numSubItemsRemoved = removeSubItems(dc, tf, removed);
    240           numItemsRemoved += setRemovedFlag(tf, removed) ? 1 : 0;
    241         }
    242         numSubItemsRemoved[1] += transformations.size();
    243       }           
    244     }
    245    
    246     /**
    247       parent is an <code>Experiment</code>
    248     */
    249     else if (parent instanceof Experiment)
    250     {
    251       ItemQuery<Transformation> transformationQuery = ((Experiment)parent).getRootTransformations();
    252       ItemResultList<Transformation> transformations = null;
    253      
    254       transformationQuery.include(Include.REMOVED,Include.MINE, Include.OTHERS, Include.SHARED);
    255      
    256       try
    257       {
    258         transformations = transformationQuery.list(dc);
    259       }
    260       catch (BaseException ex)
    261       {}
    262      
    263       if (transformations != null)
    264       {
    265         for (Transformation tf : transformations)
    266         {
    267           numSubItemsRemoved = removeSubItems(dc, tf, removed);
     335          numSubItemsRemoved = removeDependingItems(dc, tf, removed);
    268336          numItemsRemoved += setRemovedFlag(tf, removed) ? 1 : 0;
    269337        }
  • trunk/www/filemanager/files/index.jsp

    r2978 r3004  
    208208    dc = sc.newDbControl();
    209209    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
    210     RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
     210    RemovableUtil.removeRecursively(dc, itemType, Collections.singleton(cc.getId()), false);
    211211    dc.commit();
    212212    redirect = viewPage;
     
    218218    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
    219219    int numTotal = cc.getSelected().size()+dirContext.getSelected().size();
    220     int[] numRemoved = RemovableUtil.removeRecursively(dc, Item.DIRECTORY, dirContext.getSelected(), false);
    221     numRemoved[0] += RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
    222     numTotal += numRemoved[1];
    223     dc.commit();
    224     if (numTotal != numRemoved[0])
    225     {
    226       message = (numRemoved[0] == 0 ? "No" : "Only "+numRemoved[0]+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
     220    int numRemoved = 0;
     221    int[] numDirRemoved = RemovableUtil.removeRecursively(dc, Item.DIRECTORY, dirContext.getSelected(), false);
     222    int[] numFileRemoved = RemovableUtil.removeRecursively(dc, itemType, cc.getSelected(), false);
     223    numTotal += numDirRemoved[1] + numFileRemoved[1];
     224    numRemoved = numDirRemoved[0] + numFileRemoved[0];
     225   
     226    dc.commit();
     227    if (numTotal != numRemoved)
     228    {
     229      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
    227230    }
    228231    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
Note: See TracChangeset for help on using the changeset viewer.