1 package com.sri.emo.wizard.creation;
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.creation.model.CreationBeans;
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 CreationMementoConverter extends CreationBuilder implements WizardMementoConverter {
20 public CreationMementoConverter(CreationRepository 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 EmoCreationWizard cwiz = (EmoCreationWizard) previouslyExternalized;
33
34 CreationBeans beans = null;
35 try {
36 beans = repository.findById(cwiz.getId());
37 } catch (ObjectNotFoundException e) {
38 cwiz.setCompletionBeans(beans);
39 throw new WizardException("Could not find underlying wizard of id: "
40 + cwiz.getId().toString(), e);
41 }
42
43
44 cwiz.setCompletionBeans(beans);
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 EmoCreationWizard cwiz = (EmoCreationWizard) target;
60
61
62 cwiz.setCompletionBeans(null);
63
64 return cwiz;
65 }
66
67 }