showtime! an xpage dynamic table managed bean

advertisement
AusLUG2011
Showtime! An XPage Dynamic Table
Managed Bean
Meet.Share.Learn
Russell Maher | President | RGM Consulting
rgmconsulting.com
XPageTips.com
29th & 30th August, Sydney, Australia
AusLUG2011
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
2
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Agenda
• Application Requirements
• Architectural Overview
• The Code
• Deployment
• Using the Custom Control
3
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Agenda
• Application Requirements
• Architectural Overview
• The Code
• Deployment
• Using the Custom Control
4
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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.
5
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Application Requirements
• Users…
– Must be able to add any
number of rows to a table
within the same
document
• Administrators…
– Must be able to configure
table without
programming
– Must be able to save
document as a Draft or
Final
• Column Headings
– If document is not saved,
table must be restored
next time document is
opened
• Field choices
• Number of columns
• Field types
• Field validation
6
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Demo!
7
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Agenda
• Application Requirements
• Architectural Overview
• The Code
• Deployment
• Using the Custom Control
8
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Architectural Overview
• Table data is stored on a single document
– Repeated fields
Answer1_1,Answer1_2,Answer1_3
Answer2_1,Answer2_2,Answer2_3
Answer3_1,Answer3_2,Answer3_3
– Why?
• Less documents
• Local data (right on the document)
• Easier to code manage
9
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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?
10
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Table Configuration Document
11
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
12
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Dynamic Table Visualization
13
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
14
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
15
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Gaps. You don’t want ‘em.
• You always want:
Answer1_1,Answer1_2,Answer1_3
Answer2_1,Answer2_2,Answer2_3
Answer3_1,Answer3_2,Answer3_3
• You never want “gaps”:
Answer5_1,Answer5_2,Answer5_3
Answer13_1,Answer13_2,Answer13_3
Answer14_1,Answer14_2,Answer14_3
16
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
17
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
18
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Field Normalization Visualized
19
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Agenda
• Application Requirements
• Architectural Overview
• The Code
• Deployment
• Using the Custom Control
20
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Dynamic Binding Act 1
• Expression Language (EL) binds XPage controls to
data
#{dataSource.fieldname}
• EL bindings can be dynamic
#{dataSource.SOMEVARIABLE}
21
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
22
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
23
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Dynamic Binding Illustration
24
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Dynamic Binding Illustration 2
25
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Table_Configuration.xsp
• Contains…
– Lookup Key Field
– Number of Columns Field
– Repeat control…
• Repeats “Number of Columns” times
• Repeats a configTableColumnControl for each column
26
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Table Configuration
27
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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?
28
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
configTableColumnControl
29
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
UserDocument.xsp
• Contains…
– A tableBeanDynamicTableControl custom control
• Gets single property:
the key of the configuration document
30
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
31
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
tableBeanDynamicTableControl
DEMO
32
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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.”
33
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
34
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
35
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Write a Managed Bean Class
36
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
37
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
TableBean Construction Methods
• buildBean()
– Creates an entire table object
• getTableColumnHeadings()
– Returns a Vector of column headings
• getTableRows()
– Returns a Vector of table rows
38
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
TableBean Management Methods
• addTableRow()
• removeTableRow()
• cleanTableFields()
• saveAsDraft()
• saveAsFinal()
39
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Agenda
• Application Requirements
• Architectural Overview
• The Code
• Deployment
• Using the Custom Control
40
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
41
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Making a Source Folder
42
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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
43
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Editing faces-config.xml
44
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
“Then A Miracle Happens”
• Once you have configured your managed bean, the
first call to any of its methods instantiates the bean!
45
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Code & Demo Time!
46
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Agenda
• Application Requirements
• Architectural Overview
• The Code
• Deployment
• Using the Custom Control
47
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
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.
48
Meet.Share.Learn
29th & 30th August, Sydney, Australia
AusLUG2011
Questions??
• Thank you!
• Visit XPagetips.com
• Contact Me If You Have Questions
Russell Maher
Russ.Maher@rgmconsulting.com
49
Meet.Share.Learn
29th & 30th August, Sydney, Australia
Download