src/core/net/sf/basedb/util/annotations/RunnableInheritAnnotationsManager.java

Code
Comments
Other
Rev Date Author Line
6694 26 Jan 15 nicklas 1 /**
6694 26 Jan 15 nicklas 2   $Id $
6694 26 Jan 15 nicklas 3
6694 26 Jan 15 nicklas 4   Copyright (C) 2015 Nicklas Nordborg
6694 26 Jan 15 nicklas 5
6694 26 Jan 15 nicklas 6   This file is part of BASE - BioArray Software Environment.
6694 26 Jan 15 nicklas 7   Available at http://base.thep.lu.se/
6694 26 Jan 15 nicklas 8
6694 26 Jan 15 nicklas 9   BASE is free software; you can redistribute it and/or
6694 26 Jan 15 nicklas 10   modify it under the terms of the GNU General Public License
6694 26 Jan 15 nicklas 11   as published by the Free Software Foundation; either version 3
6694 26 Jan 15 nicklas 12   of the License, or (at your option) any later version.
6694 26 Jan 15 nicklas 13
6694 26 Jan 15 nicklas 14   BASE is distributed in the hope that it will be useful,
6694 26 Jan 15 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
6694 26 Jan 15 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6694 26 Jan 15 nicklas 17   GNU General Public License for more details.
6694 26 Jan 15 nicklas 18
6694 26 Jan 15 nicklas 19   You should have received a copy of the GNU General Public License
6694 26 Jan 15 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
6694 26 Jan 15 nicklas 21 */
6694 26 Jan 15 nicklas 22 package net.sf.basedb.util.annotations;
6694 26 Jan 15 nicklas 23
6694 26 Jan 15 nicklas 24 import java.util.ArrayList;
6694 26 Jan 15 nicklas 25 import java.util.Collection;
6694 26 Jan 15 nicklas 26 import java.util.List;
6694 26 Jan 15 nicklas 27
6694 26 Jan 15 nicklas 28 import net.sf.basedb.core.Annotatable;
6694 26 Jan 15 nicklas 29 import net.sf.basedb.core.DbControl;
6694 26 Jan 15 nicklas 30 import net.sf.basedb.core.Nameable;
6694 26 Jan 15 nicklas 31 import net.sf.basedb.core.ProgressReporter;
6694 26 Jan 15 nicklas 32 import net.sf.basedb.core.SessionControl;
6694 26 Jan 15 nicklas 33 import net.sf.basedb.core.SimpleProgressReporter;
6694 26 Jan 15 nicklas 34 import net.sf.basedb.core.signal.ThreadSignalHandler;
6694 26 Jan 15 nicklas 35 import net.sf.basedb.core.snapshot.SnapshotManager;
6694 26 Jan 15 nicklas 36
6694 26 Jan 15 nicklas 37 /**
6694 26 Jan 15 nicklas 38   Utility class for processing multiple items with a 
6694 26 Jan 15 nicklas 39   {@link InheritAnnotationsManager} in a separated thread 
6694 26 Jan 15 nicklas 40   with progress reporting.
6694 26 Jan 15 nicklas 41   <p>
6694 26 Jan 15 nicklas 42   Initialize this class with a session control, a list
6694 26 Jan 15 nicklas 43   of {@link Annotatable} items and a list of 
6694 26 Jan 15 nicklas 44   {@link InheritSpecification}:s, then start a new 
6694 26 Jan 15 nicklas 45   thread: new Thread(manager).start();
6694 26 Jan 15 nicklas 46   
6694 26 Jan 15 nicklas 47   @author nicklas
6694 26 Jan 15 nicklas 48   @since 3.5
6694 26 Jan 15 nicklas 49 */
6694 26 Jan 15 nicklas 50 public class RunnableInheritAnnotationsManager 
6694 26 Jan 15 nicklas 51   implements Runnable
6694 26 Jan 15 nicklas 52 {
6694 26 Jan 15 nicklas 53
6694 26 Jan 15 nicklas 54   private final SessionControl sc;
6694 26 Jan 15 nicklas 55   private ProgressReporter progress;
6694 26 Jan 15 nicklas 56   private final List<InheritSpecification> specifications;
6694 26 Jan 15 nicklas 57   private final List<Annotatable> items;
6694 26 Jan 15 nicklas 58   
6694 26 Jan 15 nicklas 59   /**
6694 26 Jan 15 nicklas 60     Create a new instance that should use the given session
6694 26 Jan 15 nicklas 61     control.
6694 26 Jan 15 nicklas 62   */
6694 26 Jan 15 nicklas 63   public RunnableInheritAnnotationsManager(SessionControl sc) 
6694 26 Jan 15 nicklas 64   {
6694 26 Jan 15 nicklas 65     this.sc = sc;
6694 26 Jan 15 nicklas 66     this.specifications = new ArrayList<InheritSpecification>();
6694 26 Jan 15 nicklas 67     this.items = new ArrayList<Annotatable>();
6694 26 Jan 15 nicklas 68   }
6694 26 Jan 15 nicklas 69   
6694 26 Jan 15 nicklas 70   /**
6694 26 Jan 15 nicklas 71     Set the progress reporter to use for reporting progress.
6694 26 Jan 15 nicklas 72   */
6694 26 Jan 15 nicklas 73   public void setProgressReporter(ProgressReporter progress)
6694 26 Jan 15 nicklas 74   {
6694 26 Jan 15 nicklas 75     this.progress = progress;
6694 26 Jan 15 nicklas 76   }
6694 26 Jan 15 nicklas 77   
6694 26 Jan 15 nicklas 78   /**
6694 26 Jan 15 nicklas 79     Add a specification with rules for inheriting annotations.
6694 26 Jan 15 nicklas 80   */
6694 26 Jan 15 nicklas 81   public void addSpecification(InheritSpecification specification)
6694 26 Jan 15 nicklas 82   {
6694 26 Jan 15 nicklas 83     specifications.add(specification);
6694 26 Jan 15 nicklas 84   }
6694 26 Jan 15 nicklas 85   
6694 26 Jan 15 nicklas 86   /**
6694 26 Jan 15 nicklas 87     Add an item that should inherit annotations. The item will
6694 26 Jan 15 nicklas 88     be processed with {@link InheritAnnotationsManager#process(Annotatable)}
6694 26 Jan 15 nicklas 89     once the thread is stared.
6694 26 Jan 15 nicklas 90   */
6694 26 Jan 15 nicklas 91   public void addItem(Annotatable item)
6694 26 Jan 15 nicklas 92   {
6694 26 Jan 15 nicklas 93     items.add(item);
6694 26 Jan 15 nicklas 94   }
6694 26 Jan 15 nicklas 95   
6694 26 Jan 15 nicklas 96   /**
6694 26 Jan 15 nicklas 97     Add all items for processing.
6694 26 Jan 15 nicklas 98   */
6694 26 Jan 15 nicklas 99   public void addAllItems(Collection<? extends Annotatable> items)
6694 26 Jan 15 nicklas 100   {
6694 26 Jan 15 nicklas 101     this.items.addAll(items);
6694 26 Jan 15 nicklas 102   }
6694 26 Jan 15 nicklas 103   
6694 26 Jan 15 nicklas 104   @Override
6694 26 Jan 15 nicklas 105   public void run() 
6694 26 Jan 15 nicklas 106   {
6694 26 Jan 15 nicklas 107     DbControl dc = null;
6694 26 Jan 15 nicklas 108     String itemName = null;
6694 26 Jan 15 nicklas 109     try
6694 26 Jan 15 nicklas 110     {
7962 04 Jun 21 nicklas 111       dc = sc.newDbControl(":Batch inherit annotations");
6694 26 Jan 15 nicklas 112       InheritAnnotationsManager manager = new InheritAnnotationsManager(dc, new SnapshotManager(), specifications);
6694 26 Jan 15 nicklas 113       
6694 26 Jan 15 nicklas 114       int i = 0;
6694 26 Jan 15 nicklas 115       int numItems = items.size();
6694 26 Jan 15 nicklas 116       for (Annotatable item : items)
6694 26 Jan 15 nicklas 117       {
6694 26 Jan 15 nicklas 118         ThreadSignalHandler.checkInterrupted();
6694 26 Jan 15 nicklas 119         // Reload item in current transaction
7610 27 Feb 19 nicklas 120         item = item.getType().getById(dc, item.getId());
6909 18 May 15 nicklas 121
6909 18 May 15 nicklas 122         itemName = item instanceof Nameable ? ((Nameable)item).getName() : item.toString();
6694 26 Jan 15 nicklas 123         
6694 26 Jan 15 nicklas 124         int percent = (100 * i) / numItems;
6694 26 Jan 15 nicklas 125         i++;
6694 26 Jan 15 nicklas 126         if (progress != null)
6694 26 Jan 15 nicklas 127         {
6694 26 Jan 15 nicklas 128           progress.display(percent, "Processing (" + i + " of " + numItems + "): " + itemName);
6694 26 Jan 15 nicklas 129         }
6694 26 Jan 15 nicklas 130         manager.process(item);
6694 26 Jan 15 nicklas 131       }
6694 26 Jan 15 nicklas 132       if (progress != null) progress.display(99, "Committing transaction. Please wait...");
6694 26 Jan 15 nicklas 133       dc.commit();
6694 26 Jan 15 nicklas 134       
6694 26 Jan 15 nicklas 135       if (progress != null) progress.display(100, "Successfully processed " + numItems + " items");
6694 26 Jan 15 nicklas 136     }
6694 26 Jan 15 nicklas 137     catch (RuntimeException ex)
6694 26 Jan 15 nicklas 138     {
6694 26 Jan 15 nicklas 139       if (progress != null)
6694 26 Jan 15 nicklas 140       {
6694 26 Jan 15 nicklas 141         progress.display(100, "Failed while processing " + itemName + ": " + ex.getMessage());
6694 26 Jan 15 nicklas 142         if (progress instanceof SimpleProgressReporter)
6694 26 Jan 15 nicklas 143         {
6694 26 Jan 15 nicklas 144           ((SimpleProgressReporter)progress).setError(ex);
6694 26 Jan 15 nicklas 145         }
6694 26 Jan 15 nicklas 146       }
6694 26 Jan 15 nicklas 147       ex.printStackTrace(System.out);
6694 26 Jan 15 nicklas 148     }
6694 26 Jan 15 nicklas 149     finally
6694 26 Jan 15 nicklas 150     {
6694 26 Jan 15 nicklas 151       if (dc != null) dc.close();
6694 26 Jan 15 nicklas 152     }
6694 26 Jan 15 nicklas 153     
6694 26 Jan 15 nicklas 154   }
6694 26 Jan 15 nicklas 155
6694 26 Jan 15 nicklas 156 }