Enterprise Java

Service-Oriented UI with JSF

In large software development projects, service-oriented architecture is very common because it provides a functional interface that can be used by different teams or departments. The same principles should be applied when creating user interfaces.
In the case of a large company that has, among others, a billing department and a customer management department, an organizational chart might look like this:

If the billing department wants to develop a new dialog for creating invoices, it might look like this:

As you can see, the screen above references a customer in the upper part. Clicking the “..” button right behind the short name text field will open the below dialog that allows the user to select the customer:

After pressing “Select” the customer data is shown in the invoice form.

It’s also possible to select a customer by simply entering a customer number or typing a short name into the text fields on the invoice screen. If a unique short name is entered, no selection dialog appears at all. Instead, the customer data is displayed directly. Only an ambiguous short name results in opening the customer selection screen.

The customer functionality will be provided by developers who belong to the customer management team. A typical approach involves the customer management development team providing some services while the billing department developers create the user interface and call these services.

However, this approach involves a stronger coupling between these two distinct departments than is actually necessary. The invoice only needs a unique ID for referencing the customer data. Developers creating the invoice dialog don’t really want to know how the customer data is queried or what services are used in the background to obtain that information.

The customer management developers should provide the complete part of the UI that displays the customer ID and handles the selection of the customer:

Using JSF 2, this is easy to achieve with composite components. The logical interface between the customer management department and the billing department consists of three parts:

  • Composite component (XHTML)
  • Backing bean for the composite component
  • Listener interface for handling the selection results


Provider (customer management departement)

Composite component:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
 
<ui:composition>
 
 <composite:interface name="customerSelectionPanel" displayName="Customer Selection Panel"
                      shortDescription="Select a customer using it's number or short name">
  <composite:attribute name="model" type="org.fuin.examples.soui.view.CustomerSelectionBean" required="true" /> 
 </composite:interface>
 
 <composite:implementation>
  
  <ui:param name="model" value="#{cc.attrs.model}"/>
  
  <ice:form id="customerSelectionForm">
   <icecore:singleSubmit submitOnBlur="true" />
   <h:panelGroup id="table" layout="block">
 
    <table>
 
     <tr>
      <td><h:outputLabel for="customerNumber"
        value="#{messages.customerNumber}" /></td>
      <td><h:inputText id="customerNumber"
        value="#{model.id}" required="false" /></td>
      <td>&nbsp;</td>
      <td><h:outputLabel for="customerShortName"
        value="#{messages.customerShortName}" /></td>
      <td><h:inputText id="customerShortName"
        value="#{model.shortName}" required="false" /></td>
      <td><h:commandButton action="#{model.select}"
        value="#{messages.select}" /></td>
     </tr>
 
     <tr>
      <td><h:outputLabel for="customerName"
        value="#{messages.customerName}" /></td>
      <td colspan="5"><h:inputText id="customerName"
        value="#{model.name}" readonly="true" /></td>
     </tr>
 
    </table>
 
   </h:panelGroup>
  </ice:form>
 
 </composite:implementation>
 
</ui:composition>
 
</html>

Backing bean for the composite component:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.fuin.examples.soui.view;
 
import java.io.Serializable;
 
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
 
import org.apache.commons.lang.ObjectUtils;
import org.fuin.examples.soui.model.Customer;
import org.fuin.examples.soui.services.CustomerService;
import org.fuin.examples.soui.services.CustomerShortNameNotUniqueException;
import org.fuin.examples.soui.services.UnknownCustomerException;
 
@Named
@Dependent
public class CustomerSelectionBean implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 private Long id;
 
 private String shortName;
 
 private String name;
 
 private CustomerSelectionListener listener;
 
 @Inject
 private CustomerService service;
 
 public CustomerSelectionBean() {
  super();
  listener = new DefaultCustomerSelectionListener();
 }
 
 public Long getId() {
  return id;
 }
 
 public void setId(final Long id) {
  if (ObjectUtils.equals(this.id, id)) {
   return;
  }
  if (id == null) {
   clear();
  } else {
   clear();
   this.id = id;
   try {
    final Customer customer = service.findById(this.id);
    changed(customer);
   } catch (final UnknownCustomerException ex) {
    FacesUtils.addErrorMessage(ex.getMessage());
   }
  }
 }
 
 public String getShortName() {
  return shortName;
 }
 
 public void setShortName(final String shortNameX) {
  final String shortName = (shortNameX == "") ? null : shortNameX;
  if (ObjectUtils.equals(this.shortName, shortName)) {
   return;
  }
  if (shortName == null) {
   clear();
  } else {
   if (this.id != null) {
    clear();
   }
   this.shortName = shortName;
   try {
    final Customer customer = service
      .findByShortName(this.shortName);
    changed(customer);
   } catch (final CustomerShortNameNotUniqueException ex) {
    select();
   } catch (final UnknownCustomerException ex) {
    FacesUtils.addErrorMessage(ex.getMessage());
   }
  }
 }
 
 public String getName() {
  return name;
 }
 
 public CustomerSelectionListener getConnector() {
  return listener;
 }
 
 public void select() {
  // TODO Implement...
 }
 
 public void clear() {
  changed(null);
 }
 
 private void changed(final Customer customer) {
  if (customer == null) {
   this.id = null;
   this.shortName = null;
   this.name = null;
   listener.customerChanged(null, null);
  } else {
   this.id = customer.getId();
   this.shortName = customer.getShortName();
   this.name = customer.getName();
   listener.customerChanged(this.id, this.name);
  }
 }
 
 public void setListener(final CustomerSelectionListener listener) {
  if (listener == null) {
   this.listener = new DefaultCustomerSelectionListener();
  } else {
   this.listener = listener;
  }
 }
 
 public void setCustomerId(final Long id) throws UnknownCustomerException {
  clear();
  if (id != null) {
   clear();
   this.id = id;
   changed(service.findById(this.id));
  }
 }
 
 private static final class DefaultCustomerSelectionListener implements
   CustomerSelectionListener {
 
  @Override
  public final void customerChanged(final Long id, final String name) {
   // Do nothing...
  }
 
 }
 
}

Listener interface for handling results:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
package org.fuin.examples.soui.view;
 
/**
 * Gets informed if customer selection changed.
 */
public interface CustomerSelectionListener {
 
 /**
  * Customer selection changed.
  *
  * @param id New unique customer identifier - May be NULL.
  * @param name New customer name - May be NULL.
  */
 public void customerChanged(Long id, String name);
 
}

User (billing departement)

The invoice bean simply uses the customer selection bean by injecting it, and connects to it using the listener interface:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.fuin.examples.soui.view;
 
import java.io.Serializable;
 
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.New;
import javax.inject.Inject;
import javax.inject.Named;
 
@Named("invoiceBean")
@SessionScoped
public class InvoiceBean implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 @Inject @New
 private CustomerSelectionBean customerSelectionBean;
 
 private Long customerId;
 
 private String customerName;
 
 @PostConstruct
 public void init() {
  customerSelectionBean.setListener(new CustomerSelectionListener() {
   @Override
   public final void customerChanged(final Long id, final String name) {
    customerId = id;
    customerName = name;
   }
  });
 }
 
 public CustomerSelectionBean getCustomerSelectionBean() {
  return customerSelectionBean;
 }
 
 public String getCustomerName() {
  return customerName;
 }
 
}

Finally, in the invoice XHTML, the composite component is used and linked to the injected backing bean:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:customer="http://java.sun.com/jsf/composite/customer">
 
    <ui:composition template="/WEB-INF/templates/template.xhtml">
         
        <ui:param name="title" value="#{messages.invoiceTitle}" />
     
        <ui:define name="header"></ui:define>
     
        <ui:define name="content">
         <customer:selection-panel model="#{invoiceBean.customerSelectionBean}" />
        </ui:define>
 
        <ui:define name="footer"></ui:define>
 
    </ui:composition>
     
</html>

Summary
In conclusion, parts of the user interface that reference data from other departments should be the responsibility of the department that delivers the data. Any changes in the providing code can then be easily made without any changes to the using code. Another important benefit of this method is the harmonization of the application’s user interface. Controls and panels that display the same data always look the same. Every department can also create a repository of its provided user interface components, making the process of designing a new dialog as easy as putting the right components together.

Reference: Service-Oriented UI from our JCG partner Michael Schnell at the A Java Developer’s Life blog.

Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ali akbar Azizkhani
ali akbar Azizkhani
12 years ago

where is it service oriented ?
bad article

Back to top button