45
MWLUG 2014 AD107: Don’t Put the Cart Before the Source: Tips for Building Your First XPages Java Application Graham Acres, President, Brytek Systems Inc. Mike McGarel, Collaborative Solutions Developer, Czarnowski Display Services, Inc.

MWLUG2014 AD107 First Java App Tips

Embed Size (px)

DESCRIPTION

Presentation on building a shopping cart using Java with XPages by Graham Acres and myself at the 2014 Midwest Lotus User Group Conference (MWLUG). We include the thinking behind the code to help with the Java journey.

Citation preview

Page 1: MWLUG2014 AD107 First Java App Tips

MWLUG 2014

AD107: Don’t Put the Cart Before the Source: Tips for Building Your First XPages Java Application

Graham Acres, President, Brytek Systems Inc.Mike McGarel, Collaborative Solutions Developer,

Czarnowski Display Services, Inc.

Page 2: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Graham Acres IBM Lotus Notes Developer/Designer

since v2.1 Brytek is an IBM Business Partner based

in Vancouver, Canada Currently focus on application

development (Social Business, XPages, Mobile)

OpenNTF Contributor Away from work

Cyclist, Ride to Conquer Cancer

Page 3: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Mike McGarel Collaborative Solutions Developer at

Czarnowski Display Services Working with Notes/Domino since version 4.6 Working on the Web for over 14 years OpenNTF Contributor Maintain MWLUG site

Page 4: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Disclaimer We are

Professionals, but NOT Experts (in this case anyway)

This advice is likely not Best Practices, but it is our experience in learning Java

Page 5: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Midwest Biking Inc.

Page 6: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Agenda Where to Begin Planning Your Application Shopping Cart Demo Let’s Look at the Code Lessons Learned and Cool Tips OpenNTF Domino API Resources Questions

Page 7: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

You Are HereWhere to Begin?

Page 8: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Why Java? XPages is built on Java SSJS is converted to Java Futureproofs YOUR skill set Syntax is not that different from LotusScript

‘LotusScriptDim strName As StringstrName = “Schwinn”

//JavaString strName = “Schwinn”;

Page 9: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

“I Don’t Know What I Don’t Know”

Page 10: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

“Checking the Roadmap” Websites galore! Blog posts Notes in 9 videos Books Colleagues Java APIs Conference sessions

Page 11: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Java “Rules of the Road” Case sensitive

Mandatory semicolon;

XPages version is Java 6

Data types: Primitive vs. Class (double <> Double)Primitive (lower case), e.g., double, int, charClass (proper case), e.g., Double, Integer, String

Page 12: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Don’t “Reinvent the Bicycle Wheel” Example: String class has 72 methods!

Page 13: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Collections You will almost always use collections whenever you

interact with a database LotusScript has them, e.g., NotesDocumentCollection,

ViewEntryCollection Java has many more, e.g., ArrayList, Vector, HashMap,

TreeMap, LinkedHashSet, TreeSet List – stores index position Map – has a key-value pairing Set – no duplicates

Page 14: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

“Planning your route”

Page 15: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Application Design Front end UI Functionality Back end structure Client-side code Server-side code

Page 16: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Java Class Design Java classes needed

Order Shopping cart Cart item

Optional Utility Catalog

Order

ShoppingCart

CartItemCartItemCartItemCartItemCartItem

Page 17: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Class Elements Properties

Often tied to form or document fields, e.g., unit price for a cart item if the cart contains items

Methods Standard getters / setters Custom, e.g., calculate cost (quantity x price)

Page 18: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Java Beans (a canned explanation)

A Java object defined by specific standards Public Java class Serializable Private properties (optional) Public constructor with no arguments Public methods

Page 19: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Sample Bean (outside)package com.mbi.shopping;import java.io.Serializable;/* other possible libraries */

public class Sample implements Serializable {private static final long serialVersionUID = 1L;

private String myText;

public Sample() {}public String getMyText() {

return myText;}public void setMyText (String txt) {

this.myText = txt;}

}

Page 20: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Sample Bean (inside)package com.mbi.shopping;import java.io.Serializable;/* other possible libraries */

public class Sample implements Serializable {

private static final long serialVersionUID = 1L;

private String myText;

public Sample() {}public String getMyText() {

return myText;}public void setMyText (String txt) {

this.myText = txt;}

}

Page 21: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Shopping Cart Demo

Page 22: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

ShoppingCart Class Properties:

private LinkedHashMap<String,CartItem> cart;

private BigDecimal totalCost;

Why a LinkedHashMap ? Keeps the insertion order Uses a key,value structure The key can be used on other parts of the page,

e.g., "Add to Cart" button rendering

Page 23: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

LinkedHashMap Class Methods .containsKey() .get() .isEmpty() .put(K key, V value) .size() .values()

Page 24: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

XPage Code for “Add to Cart” Button<xp:button value="Add to Cart" id="btnAddToCart"><xp:this.rendered><![CDATA[#{javascript:var productId = productEntry.getColumnValues()[0]; !Order.containsKey(productId)}]]></xp:this.rendered>

<xp:eventHandler event="onclick" submit="true” refreshMode="partial" refreshId="content"><xp:this.action><![CDATA[#{javascript:var id = productEntry.getColumnValues()[0];var name = productEntry.getColumnValues()[1];var price = new java.math.BigDecimal(productEntry.getColumnValues()[4].toString());var qty = new java.lang.Double(1);Order.addCartItem(name,id,price,qty)}]]></xp:this.action></xp:eventHandler></xp:button>

Page 25: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

ShoppingCart Class Code for addCartItempublic ShoppingCart() {

this.cart = new LinkedHashMap<String,CartItem>();totalCost = new BigDecimal(0);

}

public void addCartItem (String name, String id, BigDecimal price, Double quantity) {

try {CartItem cartItem = new CartItem();cartItem.load(name,id,price,quantity);cart.put(id,cartItem);calcTotalCost();

} catch (Exception ex) {ex.printStackTrace();

}}

Page 26: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

CartItem Class Properties

Contains only what’s needed for processing, e.g.,Name, ID, price, quantity, cost:

private String itemName;private String itemId;private BigDecimal itemPrice;private Double itemQuantity;private BigDecimal itemCost;

Page 27: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

CartItem Class Methods

Mostly getters / setters, e.g.,

public Double getItemQuantity() {return itemQuantity;

}

public void setItemQuantity (Double qty) {this.itemQuantity = qty;

}

Page 28: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

CartItem Loading Method

In ShoppingCart Class: CartItem cartItem = new CartItem(); cartItem.load(name,id,price,quantity); cart.put(id,cartItem);

In CartItem Class:public void load (String name, String id, BigDecimal price, Double quantity) {

setItemName(name);setItemId(id);setItemPrice(price);setItemQuantity(quantity);setItemCost(calcItemCost(quantity));

}

Page 29: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Order Class Managed bean Instantly available Loaded in faces-config.xml file

Tip: available in 9.0.1 through Domino Designer without Package Explorer. Preferences > Domino Designer > Application Navigator > Application Configuration > Faces-config

Page 30: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Faces-config.xml Sample<?xml version="1.0" encoding="UTF-8"?><faces-config> <managed-bean> <managed-bean-name>Order</managed-bean-name> <managed-bean-class>com.mbi.shopping.Order

</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <!--AUTOGEN-START-BUILDER: Automatically generated

by IBM Domino Designer. Do not modify.--> <!--AUTOGEN-END-BUILDER: End of automatically generated section--></faces-config>

Page 31: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Order Class Properties and Methods Contains ShoppingCart property Wrapped methods for easier access, e.g.,

private ShoppingCart cart;

public Order() {this.cart = new ShoppingCart();

}

public void addCartItem (String name, String id, BigDecimal price, Double quantity) {

this.cart.addCartItem(name,id,price,quantity);}

public void removeCartItem (String key) {this.cart.removeCartItem(key);

}

Page 32: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Lessons Learned and Cool Tips

Page 33: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Lesson Learned #1 – Number Field Number value becomes JavaScript number

(thanks to JSF number converter) Equivalent to Java Double (the class not the primitive) Originally used Integer class for the item quantity

Integer or int reference results in type mismatch errorError not showing up using System.out.println Error display control worked, because problem was

on the XPage itself

Page 34: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Lesson Learned #2 – Date Fields Java Calendar object

Page 35: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Lesson Learned #2 – Date Fields

Page 36: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Lesson Learned #2 – Date Fields

Page 37: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Lesson Learned #3 – Try / Catch Blocks Error management

Different from LotusScript Not just errors

Page 38: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Cool Tip #1 – To Do Reminder Add to your Java code for a handy reminder Written as: TODO in any comment line

Page 39: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

OpenNTF Domino API What is it? Should you use it? Pros / Cons

http://www.openntf.org/main.nsf/project.xsp?r=project/OpenNTF%20Domino%20API https://github.com/OpenNTF/org.openntf.domino/commits/M4.5

Page 40: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Resources: Tutorials and Books Tutorial on Collections

http://docs.oracle.com/javase/tutorial/collections/interfaces/index.html

Head First Java

Page 41: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Resources: Websites Java 6 API

http://docs.oracle.com/javase/6/docs/api/ Notes in 9

http://www.notesin9.com OpenNTF

http://www.opentf.org Collaboration Today

http://collaborationtoday.info StackOverflow

http://stackoverflow.com How to Program With Java

http://www.howtoprogramwithjava.com

Page 43: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

“You’re On Your Way”

Page 44: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Page 45: MWLUG2014 AD107 First Java App Tips

AD107: Don’t Put the Cart . . .

Thank You!Graham AcresBlog - http://grahamacres.wordpress.com/Twitter - @gacres99Email - [email protected]

Mike McGarelBlog - http://www.bleedyellow.com/blogs/McGarelGramming/Twitter - @mmcgarelEmail - [email protected]