AusLUG2011
Meet.Share.Learn
29th & 30th August, Sydney, Australia
Russell Maher | President | RGM Consultingrgmconsulting.comXPageTips.com
Showtime! An XPage Dynamic Table Managed Bean
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
2
Who is Russell Maher?
• Independent consultant located outside of Chicago• Developing, administering, teaching, speaking on
Notes/Domino since 1993• Co-presenter of The VIEW Advanced XPages for
Domino Developers• XPageTips.com• Managing partner of QDiligence
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
3
Agenda
• Application Requirements• Architectural Overview• The Code• Deployment• Using the Custom Control
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
4
Agenda
• Application Requirements• Architectural Overview• The Code• Deployment• Using the Custom Control
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
5
What is this about?
Domino developers have always needed ability to provide dynamic tables.
This presentation describes a dynamic table solution using XPages and managed beans that is easy to administer and deploy.
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
6
Application Requirements
• Users…– Must be able to add any
number of rows to a table within the same document
– Must be able to save document as a Draft or Final
– If document is not saved, table must be restored next time document is opened
• Administrators…– Must be able to configure
table without programming
• Column Headings• Number of columns• Field types• Field choices• Field validation
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
7
Demo!
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
8
Agenda
• Application Requirements• Architectural Overview• The Code• Deployment• Using the Custom Control
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
9
Architectural Overview
• Table data is stored on a single document– Repeated fields
Answer1_1,Answer1_2,Answer1_3Answer2_1,Answer2_2,Answer2_3Answer3_1,Answer3_2,Answer3_3
– Why?• Less documents• Local data (right on the document)• Easier to code manage
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
10
Required Configuration Information
• You need a keyed table config document that describes:– # of Columns– Header text for each table column– Field details for each column
• Answer is: Radio, Text, Checkbox, Combobox• Field choices (Yes/No, True/False, etc.)• Is field required?
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
11
Table Configuration Document
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
12
Required Dynamic Table Information
• For each dynamic table you must…1. Repeat the # of columns• Found in the table configuration document
2. Repeat the number of rows• Found in the current user document• The document used for data entry
3. Repeat the number of questions in a row• Found in the table configuration document
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
13
Dynamic Table Visualization
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
14
Adding A Row
• Initially appears simple:– Increment the rowCount for the table by 1– Add a new row to the table
• What happens if document is…– Not saved?
• You don’t keep the new rows
– Saved as Draft?• You keep the new rows but don’t validate
– Saved as Final?• You validate and keep the rows
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
15
Deleting a Row
• Again appears simple:– Decrement rowCount by 1– Remove the row from the table
• What happens if document is…– Not saved?
• You can’t lose them – user expects them next time
– Saved as Draft or as Final?• You remove the rows from the table permanently
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
16
Gaps. You don’t want ‘em.
• You always want:Answer1_1,Answer1_2,Answer1_3Answer2_1,Answer2_2,Answer2_3Answer3_1,Answer3_2,Answer3_3
• You never want “gaps”:Answer5_1,Answer5_2,Answer5_3Answer13_1,Answer13_2,Answer13_3Answer14_1,Answer14_2,Answer14_3
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
17
Every Save Requires Normalization
• The user document table fields must…– Always accurately reflect the number of rows– Never include missing rows
• On document open you must list rowCount number of rows– Gaps will cause problems
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
18
Table Field Normalization
• When a dynamic table is saved…– You clean the table fields on the document by…
• Walking the existing table and removing deleted rows– They are only “hidden” when a user deletes them in case they
don’t save
• Rebuilding the table using remaining rows
• Rearranging the document fields to match new table data– You don’t want any “ghost” table fields lingering about on your
document
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
19
Field Normalization Visualized
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
20
Agenda
• Application Requirements• Architectural Overview• The Code• Deployment• Using the Custom Control
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
21
Dynamic Binding Act 1
• Expression Language (EL) binds XPage controls to data
#{dataSource.fieldname}
• EL bindings can be dynamic#{dataSource.SOMEVARIABLE}
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
22
Dynamic Binding Act 2
• Custom Controls can have defined properties– Property values can be set at runtime– Property values are provided by the containing
XPage/control– Accessed from within the custom control as:
• compositeData.propertyname
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
23
Dynamic Binding Finale
• Dynamic EL binding + Custom Control =#{dataSource[compositeData.propertyname]}
• This allows you to have any field on a custom control bind to any field on the document that you wish
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
24
Dynamic Binding Illustration
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
25
Dynamic Binding Illustration 2
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
26
Table_Configuration.xsp
• Contains…– Lookup Key Field– Number of Columns Field– Repeat control…
• Repeats “Number of Columns” times• Repeats a configTableColumnControl for each column
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
27
Table Configuration
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
28
configTableColumnControl
• Contains…– configColumnLabelControl
• Contains column header label for a column
– configFieldTypeControl• Defines type of field: Radio, Checkbox, etc.
– configFieldChoicesControl• List choices for appropriate fields
– configFieldRequiredControl• Is this field required?
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
29
configTableColumnControl
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
30
UserDocument.xsp
• Contains…– A tableBeanDynamicTableControl custom control
• Gets single property:
the key of the configuration document
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
31
tableBeanDynamicTableControl
• Contains…– An XPage table control
• Repeat control for the column headers• Repeat for the table rows which contains…
– Repeat for each row of questions
– Buttons• Add New Row• Save as Draft• Save as Final
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
32
tableBeanDynamicTableControl
DEMO
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
33
EL = Bean Connection
• Expression Language actually binds controls to a JavaBean• #{dataSource.fieldname}
really means
“Connect to the dataSource bean and access its fieldname property.”
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
34
Managed Beans
• Managed beans …– Are Java classes not represented by a UI control
• vs. the “backing beans” used by the XPage controls
– Are a different way to code your XPage application business logic
– Are like agents or script libraries that are running all the time• You just call their functionality whenever you need them to
do something
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
35
Managed Bean Development Process
1. Write a Java class
2. Deploy the managed bean into your NSF
3. Use EL to bind your XPage controls the your managed bean
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
36
Write a Managed Bean Class
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
37
TableBean
• One managed bean that provides all dynamic table functionality
• Represents a single table• Called from the dynamicTableControl• Creates a session scoped representation of the
entire table for current document
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
38
TableBean Construction Methods
• buildBean() – Creates an entire table object
• getTableColumnHeadings()– Returns a Vector of column headings
• getTableRows()– Returns a Vector of table rows
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
39
TableBean Management Methods
• addTableRow() • removeTableRow()• cleanTableFields()• saveAsDraft()• saveAsFinal()
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
40
Agenda
• Application Requirements• Architectural Overview• The Code• Deployment• Using the Custom Control
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
41
Managed Bean Deployment
• Your Java source files need to be accessible to your NSF– Use the Package Explorer– Create a new source folder
• Right-click the project• Use Build Path…New Source Folder…
– Place your packaged Java source in the new source folder
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
42
Making a Source Folder
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
43
Managed Bean Deployment
• Tell the NSF the managed bean exists– Package Explorer– Edit faces-config.xml– Give the bean a name
• This is the name you use in your code to call the bean
– Identify the Java source of the bean– Identify the scope of the managed bean
• application, session, view, request, none
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
44
Editing faces-config.xml
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
45
“Then A Miracle Happens”
• Once you have configured your managed bean, the first call to any of its methods instantiates the bean!
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
46
Code & Demo Time!
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
47
Agenda
• Application Requirements• Architectural Overview• The Code• Deployment• Using the Custom Control
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
48
Using the Dynamic Table Control
• If you want it, you can have it.
• http://www.rgmconsulting.com/tablebean
• Need to clean the Java code and add more comments then I will send people a link and post publicly.
Meet.Share.Learn 29th & 30th August, Sydney, Australia
AusLUG2011
49
Questions??
• Thank you!• Visit XPagetips.com• Contact Me If You Have Questions
Russell [email protected]