1   /*
2    *  Copyright (c) 1998-2001, 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.2 2001/10/29 17:11:26 valyt Exp $
12   *
13   */
14  package gate.util.persistence;
15  
16  import gate.*;
17  import gate.creole.*;
18  import gate.util.*;
19  import gate.persist.PersistenceException;
20  
21  import java.util.*;
22  
23  public class ControllerPersistence extends ResourcePersistence {
24    /**
25     * Populates this Persistence with the data that needs to be stored from the
26     * original source object.
27     */
28    public void extractDataFromSource(Object source)throws PersistenceException{
29      if(! (source instanceof Controller)){
30        throw new UnsupportedOperationException(
31                  getClass().getName() + " can only be used for " +
32                  Controller.class.getName() +
33                  " objects!\n" + source.getClass().getName() +
34                  " is not a " + Controller.class.getName());
35      }
36      Controller controller = (Controller)source;
37  
38      super.extractDataFromSource(source);
39      prList = new ArrayList(controller.getPRs().size());
40      Iterator prIter = controller.getPRs().iterator();
41  
42      while(prIter.hasNext()){
43        ((List)prList).add(prIter.next());
44      }
45      prList = PersistenceManager.getPersistentRepresentation(prList);
46    }
47  
48    /**
49     * Creates a new object from the data contained. This new object is supposed
50     * to be a copy for the original object used as source for data extraction.
51     */
52    public Object createObject()throws PersistenceException,
53                                       ResourceInstantiationException{
54  
55      Controller controller = (Controller)super.createObject();
56  
57      if(controller.getPRs().isEmpty()){
58        prList = PersistenceManager.getTransientRepresentation(prList);
59        controller.setPRs((Collection)prList);
60      }
61  
62      return controller;
63    }
64  
65    protected Object prList;
66    static final long serialVersionUID = 727852357092819439L;
67  }