Source code: er/directtoweb/ERDObjectSaveDelegate.java
1 /*
2 * Copyright (C) NetStruxr, Inc. All rights reserved.
3 *
4 * This software is published under the terms of the NetStruxr
5 * Public Software License version 0.5, a copy of which has been
6 * included with this distribution in the LICENSE.NPL file. */
7 package er.directtoweb;
8
9 import com.webobjects.directtoweb.*;
10 import com.webobjects.foundation.*;
11 import com.webobjects.eocontrol.*;
12 import com.webobjects.eoaccess.*;
13 import com.webobjects.appserver.*;
14
15 /**
16 * Simple {@link com.webobjects.directtoweb.NextPageDelegate}
17 * implementation that saves the editing context of an enterprise
18 * object before returning the next page. This can be particularly
19 * handy for example if you want a user to confirm an action before
20 * the editing context is committed, for example:<br/>
21 * Assume that you have a User object that has been editinged in
22 * a peer context and now you want the user to confirm that they
23 * really should save the changes to the editing context, you
24 * could have this method:<br/><code>
25 * public WOComponent confirmSave() {
26 * ConfirmPageInterface cpi = (ConfirmPageInterface)D2W.factory().pageForConfigurationNamed("ConfirmSaveUserChanges", session());
27 * cpi.setConfirmDelegate(new ERXObjectSaveDelegate(user, context().page()));
28 * cpi.setCancelDelegate(someCancelDelegate);
29 * return (WOComponent)cpi;
30 * }</code>
31 * This way if the user selects the confirm button the editing context
32 * will be saved and they will be brought back to the current page.
33 */
34 public class ERDObjectSaveDelegate implements NextPageDelegate {
35 /** holds the object */
36 private EOEnterpriseObject _object;
37 /**
38 * holds a reference to the objects ec so that it won't be
39 * collected by the garbage collector
40 */
41 private EOEditingContext _context;
42 /** holds the next page */
43 private WOComponent _nextPage;
44
45 /**
46 * Public constructor
47 * @param object to be saved
48 * @param nextPage to be returned
49 */
50 public ERDObjectSaveDelegate(EOEnterpriseObject object, WOComponent nextPage) {
51 _object = object;
52 if (_object != null)
53 _context = _object.editingContext();
54 _nextPage = nextPage;
55 }
56
57 /**
58 * returns the object. Useful for subclasses
59 * @return the object
60 */
61 protected EOEnterpriseObject object() { return _object; }
62 /**
63 * returns the editing context of the object.
64 * Useful for subclasses
65 * @return the editing context
66 */
67 protected EOEditingContext editingContext() { return _context; }
68
69 /**
70 * Implementation of the NextPageDelegate interface
71 * First saves the changes in the object's editing
72 * context and then returns the nextPage.
73 * @param sender component calling the delegate
74 * @return the nextPage component passed in via the
75 * constructor.
76 */
77 public WOComponent nextPage(WOComponent sender) {
78 if (_context != null && _context.hasChanges())
79 _context.saveChanges();
80 return _nextPage;
81 }
82 }