View Javadoc

1   package pl.caltha.forms.internal.model;
2   
3   import org.dom4j.Document;
4   
5   import pl.caltha.forms.ConstructionException;
6   import pl.caltha.forms.FormsException;
7   import pl.caltha.forms.internal.FormImpl;
8   
9   /***
10   * Contains default DOM4J instance document, produces Instance documents.
11   *
12   * @author <a href="mailto:zwierzem@ngo.pl">Damian Gajda</a>
13   * @version $Id: DefaultInstance.java,v 1.1 2005/01/19 06:55:35 pablo Exp $
14   */
15  public class DefaultInstance extends InstanceImpl
16  {
17      public DefaultInstance(FormImpl form, String schemaURI, Document instanceDocument)
18      throws ConstructionException
19      {
20          super(form, schemaURI, instanceDocument);
21      }
22  
23      /*** Creates a new instance object. */
24      public InstanceImpl createInstance(String instanceId)
25      {
26          InstanceImpl instance = new InstanceImpl(form, schemaURI, (Document)(instanceDocument).clone());
27  
28          instance.id = instanceId;
29  
30          return instance;
31      }
32  
33      /*** Creates an instance object from it's serialized state.
34       *  It also connects a form to deserialized instance object.
35       * @throws Exception thrown in following cases:
36       * <ul>
37       *    <li>on deserialization problems</li>
38       *    <li>on <code>instance.formId == null</code> - it means that this
39       *      field was not serialized (sic)</li>
40       *    <li>and on <code>instance.formId</code> different than one
41       *    from <code>DefaultInstance</code> object - it means that this instance should not be
42       *    connected to this <code>Form</code> because it was created for a different
43       *    form definition.</li>
44       * </ul>
45       */
46  
47      public InstanceImpl createInstance(String instanceId, byte[] savedState)
48      throws Exception
49      {
50          java.io.ByteArrayInputStream in = new java.io.ByteArrayInputStream(savedState);
51          java.io.ObjectInputStream oin = new java.io.ObjectInputStream(in);
52          InstanceImpl instance = (InstanceImpl)(oin.readObject());
53  
54          instance.id = instanceId;
55  
56          instance.form = this.form;
57          if(instance.formId == null)
58          {
59              throw new FormsException("An instance is not connected to a Form definition.");
60          }
61          else if(!instance.formId.equals(this.form.getId()))
62          {
63              throw new FormsException("Cannot connect Instance to a different Form definition.");
64          }
65  
66          return instance;
67      }
68  }