1   /*
2    *  Copyright (c) 1998-2005, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 26/10/2001
10   *
11   *  $Id: ControllerPersistence.java,v 1.5 2005/01/11 13:51:37 ian Exp $
12   *
13   */
14  package gate.util.persistence;
15  
16  import java.util.*;
17  
18  import gate.Controller;
19  import gate.creole.ResourceInstantiationException;
20  import gate.persist.PersistenceException;
21  
22  public class ControllerPersistence extends ResourcePersistence {
23    /**
24     * Populates this Persistence with the data that needs to be stored from the
25     * original source object.
26     */
27    public void extractDataFromSource(Object source)throws PersistenceException{
28      if(! (source instanceof Controller)){
29        throw new UnsupportedOperationException(
30                  getClass().getName() + " can only be used for " +
31                  Controller.class.getName() +
32                  " objects!\n" + source.getClass().getName() +
33                  " is not a " + Controller.class.getName());
34      }
35      Controller controller = (Controller)source;
36  
37      super.extractDataFromSource(source);
38      prList = new ArrayList(controller.getPRs().size());
39      Iterator prIter = controller.getPRs().iterator();
40  
41      while(prIter.hasNext()){
42        ((List)prList).add(prIter.next());
43      }
44      prList = PersistenceManager.getPersistentRepresentation(prList);
45    }
46  
47    /**
48     * Creates a new object from the data contained. This new object is supposed
49     * to be a copy for the original object used as source for data extraction.
50     */
51    public Object createObject()throws PersistenceException,
52                                       ResourceInstantiationException{
53  
54      Controller controller = (Controller)super.createObject();
55  
56      if(controller.getPRs().isEmpty()){
57        prList = PersistenceManager.getTransientRepresentation(prList);
58        controller.setPRs((Collection)prList);
59      }
60  
61      return controller;
62    }
63  
64    protected Object prList;
65    static final long serialVersionUID = 727852357092819439L;
66  }