Changeset 3958


Ignore:
Timestamp:
Nov 5, 2010, 12:09:35 PM (13 years ago)
Author:
olle
Message:

Refs #704. Refs #585. Class/file plugins/ProjectToTrash.java in plugin/
updated:

  1. Public method

void run(Request request, Response response, ProgressReporter progress)
updated:

  1. The Project item was re-created with the new DbControl from

the id value of the former, as this eliminates the need to call
reattachItem(...).

  1. The lists of directories and files in the project is now obtained

by calling new private methods
List<Directory> directoryTree(DbControl dc, List<Directory> dirList, Directory startDir)
and List<File> filesInDirectories(DbControl dc, List<Directory> dirList),
respectively.

  1. New private method

List<Directory> directoryTree(DbControl dc, List<Directory> dirList, Directory startDir)
added. It obtains a list of all directories that have the start directory
as ancestor.

  1. New private method

List<File> filesInDirectories(DbControl dc, List<Directory> dirList)
added. It obtains a list of all files in a list of directories.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugin/src/org/proteios/plugins/ProjectToTrash.java

    r3445 r3958  
    5353import org.proteios.core.query.Restrictions;
    5454
     55import java.util.ArrayList;
    5556import java.util.List;
    5657
     
    6364    extends AbstractPlugin
    6465{
     66  /**
     67   * Logger used. Used to log specific events.
     68   */
     69  protected static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
     70    .getLogger("org.proteios.io");
     71 
    6572  public MainType getMainType()
    6673  {
     
    94101      int jobCount = 0, hitCount = 0, featureCount = 0, ssCount = 0, fileCount = 0, dirCount = 0;
    95102      Project project = (Project) job.getValue("project");
    96       // Only needed if the project wasn't active when the job was
    97       // created:
     103      // Only needed if the project wasn't active when the job was created:
    98104      sc.setActiveProject(project);
    99105      DbControl dc = sc.newDbControl();
    100       dc.reattachItem(project);
     106      //dc.reattachItem(project);
     107      project = Project.getById(dc, project.getId());
    101108      int project_id = sc.getActiveProjectId();
    102109      /* Check that the feature file hasn't been imported already */
     
    110117      for (Hit hit : hitList)
    111118      {
    112         dc.reattachItem(hit);
    113119        dc.deleteItem(hit);
    114120        hitCount++;
     
    120126      for (Feature feat : featureList)
    121127      {
    122         dc.reattachItem(feat);
    123128        dc.deleteItem(feat);
    124129        featureCount++;
     
    130135      for (Job job : jobList)
    131136      {
    132         dc.reattachItem(job);
    133137        job.setRemoved(true);
    134138        jobCount++;
     
    140144      for (SpectrumSearch ss : spectrumSearchList)
    141145      {
    142         dc.reattachItem(ss);
    143146        ss.setRemoved(true);
    144147        ssCount++;
     
    147150      fileQuery.exclude(Include.MINE, Include.OTHERS);
    148151      fileQuery.include(Include.IN_PROJECT);
    149       List<File> fileList = fileQuery.list(dc);
     152      List<File> fileItemResultList = fileQuery.list(dc);
     153      log.debug("ProjectToTrash: fileItemResultList.size() = " + fileItemResultList.size() + " fileList = " + fileItemResultList);
     154      //
     155      Directory projDir = project.getProjectDirectory();
     156      List<Directory> dirList = new ArrayList<Directory>();
     157      dirList = directoryTree(dc, dirList, projDir);
     158      log.debug("ProjectToTrash: dirList.size() = " + dirList.size() + " dirList = " + dirList);
     159      //
     160      List<File> fileList = new ArrayList<File>();
     161      fileList = filesInDirectories(dc, dirList);
     162      log.debug("ProjectToTrash: fileList.size() = " + fileList.size() + " fileList = " + fileList);
    150163      for (File f : fileList)
    151164      {
    152         dc.reattachItem(f);
     165        //dc.reattachItem(f);
    153166        f.setRemoved(true);
    154167        fileCount++;
     
    157170      dirQuery.exclude(Include.MINE, Include.OTHERS);
    158171      dirQuery.include(Include.IN_PROJECT);
    159       List<Directory> dirList = dirQuery.list(dc);
    160       Directory projDir = project.getProjectDirectory();
     172      List<Directory> dirItemResultList = dirQuery.list(dc);
     173      log.debug("ProjectToTrash: dirItemResultList.size() = " + dirItemResultList.size());
     174      // Set no active project
     175      log.debug("ProjectToTrash: active project set to null");
     176      sc.setActiveProject(null);
    161177      if (!dirList.contains(projDir))
    162178      {
    163         // Doesn't work to reattach for some reason
     179        log.debug("ProjectToTrash: projDir.getName() = \"" + projDir.getName() + "\" set to removed");
    164180        projDir.setRemoved(true);
    165181        dirCount++;
     
    167183      for (Directory d : dirList)
    168184      {
    169         dc.reattachItem(d);
     185        log.debug("ProjectToTrash: d.getName() = \"" + d.getName() + "\" set to removed");
    170186        d.setRemoved(true);
    171187        dirCount++;
    172188      }
    173       project.setClosed(true);
    174       /**
    175        * Don't delete, just close.
    176        */
    177       // project.setRemoved(true);
     189      project.setRemoved(true);
    178190      dc.commit();
    179191      response
     
    181193    }
    182194  }
     195
     196
     197  /**
     198   * Obtains a list of all directories that have the start directory as ancestor.
     199   *
     200   * @param dc DbControl The DbControl to use.
     201   * @param dirList List<Directory> The directory list that will be appended with found directories.
     202   * @param startDir Directory The start directory for the search.
     203   * @return List<Directory> The directory list updated with found directories.
     204   */
     205  private List<Directory> directoryTree(DbControl dc, List<Directory> dirList, Directory startDir)
     206  {
     207    if (startDir != null)
     208    {
     209      // Find sub-directories to start directory
     210      ItemQuery<Directory> dirQuery = startDir.getSubDirectories();
     211      /*
     212      if (sc.getActiveProjectId() > 0)
     213      {
     214        dirQuery.include(Include.IN_PROJECT);
     215      }
     216      */
     217      List<Directory> tmpList = dirQuery.list(dc);
     218      // Add sub-directories to directory list
     219      for (Directory dir: tmpList)
     220      {
     221        // Add directory to dirList, if not already in list
     222        if (!dirList.contains(dir))
     223        {
     224          dirList.add(dir);
     225        }
     226        // Add directories in sub-directory to directory list
     227        dirList = directoryTree(dc, dirList, dir);
     228      }
     229    }
     230    return dirList;
     231  }
     232
     233
     234  /**
     235   * Obtains a list of all files in a list of directories.
     236   *
     237   * @param dc DbControl The DbControl to use.
     238   * @param dirList List<Directory> The directory list.
     239   * @return List<File> The file list updated with found files.
     240   */
     241  private List<File> filesInDirectories(DbControl dc, List<Directory> dirList)
     242  {
     243    List<File> fileList = new ArrayList<File>();
     244    for (Directory dir: dirList)
     245    {
     246      ItemQuery<File> fileQuery = dir.getFileQuery();
     247      List<File> tmpList = fileQuery.list(dc);
     248      for (File file: tmpList)
     249      {
     250        // Add file to fileList, if not already in list
     251        if (!fileList.contains(file))
     252        {
     253          fileList.add(file);
     254        }
     255      }
     256    }
     257    return fileList;
     258  }
    183259}
Note: See TracChangeset for help on using the changeset viewer.