D2W Stateful Controllers

809 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
809
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

D2W Stateful Controllers

  1. 1. D2W Stateful ControllersFuego Digital Media QSTP-LLCDaniel Roy
  2. 2. • Using WebObjects since 2004• Work with government agencies, private companies and variousfoundations globally• Fuego Content Management System & Fuego Frameworks• Presence in North America and Middle EastFuego History
  3. 3. • 42% of community uses D2W• “D2W is to WebObjects apps as the assembly line is to automobiles...youprovide customization directions (D2W rules) to the assembly line (D2W) tobuild your end products (WOComponent pages).”• Flow is controlled by delegates that manage the actions and direction of theapplication• Use ERDBranchDelegate to define the actions displayed on the page by rulesor via introspectionDirect To Web
  4. 4. The Basicsquery select edit save
  5. 5. Start/StopConfirmEditConfirmConfirmsaveForLaterSaveDeleteeditEdit CommentreadyToSubmit*readyToSubmit*saveAndSubmitSubmitdelete*confirmDeletionapproveDeletionconfirmedit*List ExpenseClaimsselectcancelNotes:- Cancel is present on all pages, but onlyshown for first.- Any branches with * are optionaland are shown based on business logic- Any branches with the same name,are the same page configuration, butwith different branchesNeeds to entercomment?NOEdit CommentYESWHAT?WHAT?????
  6. 6. Back in the day...• Without Project Wonder, rely on nextPageDelegate andnextPage for navigation• nextPageDelegate is checked first, and if found call nextPage()• without nextPageDelegate, nextPage() is called on the D2WPagecomponent directly• Single class between each page
  7. 7. Next Page DelegateD2WPageNPD(select)D2WPageNPD(edit)D2WPageNPD(query)...
  8. 8. Plain WebObjects Controller Sample Codepublic class ManageUserEditNextPageDelegate implements NextPageDelegate { public WOComponent nextPage(WOComponent sender) { NSArray<User> selectedObjects = ((ERDPickPageInterface) sender).selectedObjects(); User currentUser = selectedObjects.get(0); EditPageInterface epi = (EditPageInterface) D2W.factory().pageForConfigurationNamed("ManageUsers_User_Edit_PC", sender.session()); epi.setNextPageDelegate(new ManageUserSaveNextPageDelegate()); epi.setObject(currentUser); return (WOComponent) epi; }}
  9. 9. Enter Project Wonder• ERDBranchDelegate to the rescue!• Now you can show multiple branch choices for a page• Pull choices from D2W context in the methodbranchChoicesForContext(), using the D2W rule key“branchChoices”• Or, use introspection to find all methods that take a singleWOComponent as parameter
  10. 10. ERDBranchDelegateD2WPage ERDBDD2WPage(search)D2WPage(add)D2WPage(cancel)ERDBDD2WPage(save)D2WPage(cancel)D2WPage(add)
  11. 11. ERDBranchDelegate Sample Codepublic class ManageUserControllerSelectBranchDelegate extends ERDBranchDelegate { // Return an edit page for a single selected row in the pick page public WOComponent edit(WOComponent sender) { NSArray<User> selectedObjects = ((ERDPickPageInterface) sender).selectedObjects(); User currentUser = selectedObjects.get(0); EditPageInterface epi = (EditPageInterface) D2W.factory().pageForConfigurationNamed("ManageUsers_User_Edit_PC", sender.session()); epi.setNextPageDelegate(new ManageUserControllerSaveBranchDelegate()); epi.setObject(currentUser); return (WOComponent) epi; } // From the selected items in the pick page, perform a delete public WOComponent delete(WOComponent sender) { NSArray<User> users = ((ERDPickPageInterface) sender).selectedObjects(); for (User user : users) { user.delete(); } if (users.count() > 0) { // Assuming all in same EC. users.get(0).editingContext().saveChanges(); } return sender.pageWithName("PageWrapper"); }}
  12. 12. But wait...• Must still set and get theobjects on each page• Lots of classes to write• Why not reuse the samedelegate?
  13. 13. Benefits of Delegate Reuse• Enter the controller/delegate many times (one controller formany pages)• Reuse the stored state in editing context• Support one or many flows within the single class• Define the visible branches using rules
  14. 14. Re-entrant Branch DelegateD2WPageBDqueryPagesearch()edit()editPageselectPagesave()
  15. 15. Is ERDBranchDelegate Enough?• Yes....but...no.• Branch choices are defined in the rules, but can lead to acomplex ruleset depending on the business logic• Introspection on the ERDBranchDelegate returns all themethods in the class
  16. 16. Hello (Stateful) World!• FDStatefulController is a re-entrant branch delegate extendingERDBranchDelegate• Better separation of MVC• Override branchChoicesForContext(D2WContext context)• Reuse page configurations (define the PC in code)• store state between pages• editing context• EOs, etc in subclasses
  17. 17. branchChoicesForContextpublic class StatefulController extends ERDBranchDelegate { private NSArray<?> branches; private EOEditingContext editingContext; public NSArray<?> getBranches() { return branches; } public void setBranches(NSArray<?> branches) { this.branches = branches; } public EOEditingContext editingContext() { if (this.editingContext == null) { this.editingContext = ERXEC.newEditingContext(); } return this.editingContext; } public NSArray branchChoicesForContext(D2WContext context) { NSArray choices = getBranches(); if (choices == null) { branches = (NSArray) context.valueForKey(BRANCH_CHOICES);} else { NSMutableArray translatedChoices = new NSMutableArray(); for (Iterator iter = choices.iterator(); iter.hasNext();) {... rest of method continues the same as ERDBranchDelegate
  18. 18. StatefulController Code// Return an edit page for a single selected row in the pick pagepublic WOComponent edit(WOComponent sender) { NSArray<User> selectedObjects = ((ERDPickPageInterface) sender).selectedObjects(); User currentUser = selectedObjects.get(0); setBranches(new NSArray("save")); EditPageInterface epi = (EditPageInterface) D2W.factory().pageForConfigurationNamed("ManageUsers_User_Edit_PC", sender.session()); epi.setNextPageDelegate(this); epi.setObject(currentUser); return (WOComponent) epi;}public WOComponent save(WOComponent sender) { editingContext().saveChanges(); return sender.pageWithName("PageWrapper");}// From the selected items in the pick page, perform a deletepublic WOComponent delete(WOComponent sender) { setBranches(null); NSArray<User> users = ((ERDPickPageInterface) sender).selectedObjects(); ERXUtilities.deleteObjects(editingContext(), users); return save(sender);}
  19. 19. Page Utility MethodsInteractioneditPagequeryPagelistPageinspectPagemessagePagepickPageselectPageUtilityeditingContext()nestedEditingContextsave()
  20. 20. Utility Method Examplesprotected EditPageInterface editPage(Session session, String pageConfigurationName, EOEnterpriseObject enterpriseObject) { EditPageInterface epi = (EditPageInterface) pageForConfigurationNamed(pageConfigurationName, session); epi.setNextPageDelegate(this); epi.setObject(enterpriseObject); return epi;}protected WOComponent pageForConfigurationNamed(String pageConfigurationName, Session session) { WOComponent component = D2W.factory().pageForConfigurationNamed(pageConfigurationName, session); if (component instanceof D2WPage) { ((D2WPage) component).setNextPageDelegate(this); } return component;}
  21. 21. Summary• Re-entrant controller provides code reusability• Specify branch choices programmatically• Storing the editing context allows easy access to associatedobjects during flows and encourages a single point of save• Utility and convenience methods allow for simple setup anddevelopment of customized flows
  22. 22. Resources• What is Direct To Web?http://wiki.wocommunity.org/pages/viewpage.action?pageId=1049018• D2W Flow Controlhttp://wiki.wocommunity.org/display/documentation/D2W+Flow+Control• ERDBranchDelegateInterfacehttp://jenkins.wocommunity.org/job/Wonder/lastSuccessfulBuild/javadoc/er/directtoweb/pages/ERD2WPage.html#pageController()• Page controller in ERD2Whttp://osdir.com/ml/web.webobjects.wonder-disc/2006-05/msg00041.html
  23. 23. Q&A

×