View Javadoc

1   package com.sri.emo.wizard.completion;
2   
3   import com.sri.common.dbobj.ObjectNotFoundException;
4   import com.sri.emo.wizard.Wizard;
5   import com.sri.emo.wizard.WizardException;
6   import com.sri.emo.wizard.WizardMementoConverter;
7   import com.sri.emo.wizard.completion.model.CompletionBean;
8   
9   import java.io.Serializable;
10  
11  /***
12   * Writes a CompletionWizard to and from a compact form. (A memento).  This
13   * memento is then saved in state.  See the WizardMementoConverter interface
14   * for more information.
15   *
16   * @author Michael Rimov
17   * @version 1.0
18   */
19  public class CompletionMementoConverter extends CompletionBuilder implements WizardMementoConverter {
20      public CompletionMementoConverter(CompletionRepository completionRepository) {
21          super(completionRepository);
22      }
23  
24      /***
25       * Re-fleshes out the wizard into usable forms.
26       *
27       * @param previouslyExternalized Serializable
28       * @return Wizard
29       * @throws WizardException
30       */
31      public Wizard fromMemento(final Serializable previouslyExternalized) throws WizardException {
32          EmoCompletionWizard cwiz = (EmoCompletionWizard) previouslyExternalized;
33  
34          CompletionBean bean = null;
35          try {
36              bean = repository.findById(cwiz.getId());
37          } catch (ObjectNotFoundException e) {
38              cwiz.setCompletionBean(bean);
39              throw new WizardException("Could not find underlying wizard of id: "
40                      + cwiz.getId().toString(), e);
41          }
42  
43  
44          cwiz.setCompletionBean(bean);
45  
46          return cwiz;
47      }
48  
49      /***
50       * Breaks the wizard down into minimal component state parts that can be
51       * stored in the session or client.
52       *
53       * @param target Wizard the wizard being 'dehydrated'
54       * @return Serializable -- an opaque object that should be serialized, stashed
55       *         or something, but nothing else.
56       * @throws WizardException
57       */
58      public Serializable toMemento(final Wizard target) throws WizardException {
59          EmoCompletionWizard cwiz = (EmoCompletionWizard) target;
60  
61          //Set turing constitution and 'dehydration' as well.
62          cwiz.setCompletionBean(null);
63  
64          return cwiz;
65      }
66  
67  }