Usage of JSF framework and EJB technology in the creation of corporate applications

– In the following article we describe the architecture of an online store project, which is an application utilising Java Enterprise Edition. Our project is based on a customer expectations model. The choice of technology has been due to its easy expandability by additional modules as well as its functionality which does not require reorganising the existing code.


Introduction
Over the last years the Internet has become one of the most important media. It has become a means for both static websites based on ordinary HTML and for the powerful applications. Creation of the latter may be a complex and time-consuming process. The implementation of IT based systems across the society has triggered a demand for a variety of tools for convenient creation of complex corporate applications.
There is a number of ready-made solutions available on the IT market. For instance, Enterprise Resource Planning applications support the management of a number of tasks performed by a company or groups of co-operating companies through gathering data or through making it possible to perform operations on the gathered data. Currently, the highest demand for Internet applications is particularly notable in companies (ranging from sole traders to powerful corporations).
There are groups of companies, such as online stores, with certain features in common. They have a similar physical structure of a company which can be easily described by means of appropriate tools. One of such tools is the Java Enterprise Edition platform and its component technologies.

Why Enterprise Java Bean?
Enterprise Java Beans is a technology of creating components which perform certain tasks for the benefit of customers. It is a server-based technology, which makes it possible to transfer a significant share of business logic from client applications to server components. Thanks to this it is easier to make changes in the application and maintain it at the same time. EJB is a very complex technology comprising many modules responsible for, among others, data security or storage, providing tools for creating large, solid and portable business applications such as online stores.
Apart from the typical customer service, the technology streamlines financial management and staff management of a company. Moreover, it reminds the user about payment deadlines (Java Messenger Service, e-mail, a very convenient Timer interface) and makes it possible to use Web Services. Web Services can accelerate the speed and reduce the cost of integration with various internal applications and systems, and therefore they are particularly desirable in such applications [1].

Why Java Server Faces?
Java Server Faces is a framework which considerably simplifies and accelerates creation of large web applications. It has been created on the basis of Java Servlet Specification, which has entirely changed the attitude towards web application programming. In its structure JSF resembles window applications such as Swing. Its key features include: view creation based on the tree of ready-made user interface components, event-driven programming and data validation as well as conversion services. The programmer can focus on the implementation of an application's business logic thanks to ready-made mechanisms generating responses, checking data integrity as well as intercepting events.

Facelets or how to simplify the usage of JSF?
JSF can be associated with different technologies for creating view. The basic one is Java Server Pages which was created for different purposes before JSF. Consequently, JavaServer Faces cannot be fully used. An alternative solution is offered by a new Facelets framework created specifically for JSF. Its main advantage is the ability to create view based on templates, which cannot be achieved with JSP

Session Bean as JSF Servlet event listeners
Session beans may serve as event listeners in a JSF-based application. Event listener methods perform certain tasks, such as adding goods to the shopping cart by using

53
EntityManager from Enterprise JavaBeans 3.0 in order to gain access to the database and return a text value which suggests which view should be generated in response.

Application of Java Enterprise Edition platform to create utility programs
The Internet is widely used as a medium for data presentation by online stores, among others. Files are dynamically generated after receiving a query from the server, which is linked with the necessity of choosing the right technology. The choices developers make are often based on their own preferences and past experiences. Nevertheless, literature on the subject comprises comparative studies which may be helpful in making such choices. Yet, the choices are not always favourable against the results of such studies. For example, the results of study [2] on "the cost" of using a given web application within various technologies have shown that Java Servlets are one of the most expensive (compared to CGI/C++ among others). The authors of the study have named several reasons for this and one of the most significant is the servlet interpretation by Java Virtual Machine. On the other hand, the authors point to advantages of using servlets, especially with applications which are not highly complex. Moreover, if this is accompanied by the use of JSF and Facelets technologies, thanks to which the work of a team creating an application may be efficiently organised, the choice seems to be justified. It is also possible to look for convenient but faster solutions such as Portlets [3,4] or even to use code generating frameworks such as SEAM or Ruby on Rails. Some [5] use the UML tool to accelerate the process of creating applications. However, the ability to use metadata as well as the CRUD mechanism of entity beans EJB 3.0 has eliminated such necessity.

Online store application
Designing a user friendly online store is the most important issue. As [6] indicates "online shopping Web Sites contain a lot of irrelevant information related to new types of products or reduced items". Customers get confused by details and become impatient being unable to intuitively find relevant information. Another common problem is posed by bad navigation due to which it is easy to get lost in cyberspace [7].
Whereas in creating educational platforms [8] the main focus is on tailoring them to the customer's cognitive requirements, in the implementation of service application the main focus is on the customer's expectations, which has been proved by empirical study [9].
(2) In the case of a large number of categories, requiring more attention than just a glance, for instance in a drop down menu -grouping and sub-categorisation. Pobrane z czasopisma Annales AI-Informatica http://ai.annales.umcs.pl Data: 04/03/2020 00:27:18 U M C S (6) Good timing for loading another navigated webpage (according to research [10] if it takes longer than a minute for a webpage to load, the application user will not browse it). (7) The website has to provide detailed product information "on request".
We have tried to utilise the above-mentioned customer expectations model in the application described below. To deal with the issue of speed we have used the rendering of single or multiple UI components, which has reduced the number of loaded pages and complied with point 5 above.

Application's functionality
The function of the project is an online photographic equipment store. The application is divided into customer and administrator modules. Each of them comprises a separate web interface created on the basis of JSF Framework + Facelets. The customer module's functionality allows customers to browse the catalogue, add products to the shopping cart, order selected items, express opinions on them as well as browse their shopping history. The administrator's interface makes it possible to create product categories, add or edit items.
The application has been designed in a way which makes it possible to change the store's profile. Each catalogued product comprises items belonging to a given category. Each category has a set of attributes which are typical of the items belonging to it. The administrator can define them, thanks to which the application is universal and may be used for cataloguing products from a variety of fields.

Design and Application
The application's architecture is represented by the picture below. Its core comprises the EJB module which serves as a link between web clients and the database. It can be divided into two basic groups: session beans and entity beans. Web clients only gain access to session beans which make use of the entity beans and these, in turn, serve as a database abstraction.

Entity beans
Entity beans are used to translate the database tables into Java objects. The application comprises 12 entity classes.
AdminUser -store administrator. Customer -store customer. Category -item category (Item) or product category (Product). Item -single item (e.g. a camera or lens); items comprising a catalogued product.
Attribute -an item's attribute (e.g. pixel count for the camera sensor); each category has a given set of attributes.
AttributeValue -attribute value of a particular item; value is of the the kind java.lang.String, and its interpretation is expressed by AttributeValueType.
AttributeValueType -logical type of attribute value; four types have been defined: INTEGER, FLOAT, BOOLEAN, STRING. Producer -producer of an item. Product -catalogued product, it has its price as well as quantity in stock, comprises one main item (of the same category as the product) and optional additional items, e.g. a camera (main item) + lens (optional additional item).
LineItem -item in an order; it comprises the item, its price at the moment of purchase as well as quantity ordered.
Purchase -order; it comprises items (LineItem), information on the customer, payment method, price and shipping address.
Review -the customer's opinion on a product.

Session beans
Session beans are responsible for the application's business logic and serve as an interface between the client application and the database. Certain operations multiplied in entity classes have been abstracted to a stateless session bean EntityManagerBean.
This bean comprises a set of typical methods used in applications utilising a database. Each of the methods performs a calculation whose result is independent of other methods' results, which justifies the usage of the stateless session bean.
Pobrane z czasopisma Annales AI-Informatica http://ai.annales.umcs.pl Data: 04/03/2020 00:27:18 The other session beans comprise methods dedicated to particular entity beans. The methods include, among others: searching according to specific attributes of a given entity, saving preceeded by checking whether or not the uniqueness of a certain attribute will be impaired (e.g. producer's name should be unique) etc. The method ItemManagerBean.search() may serve as a good example. Its task is to search all items whose attributes' values have been defined in a template. ) match = f a l s e ; } e l s e i f ( o i n s t a n c e o f S t r i n g R a n g e ) { S t r i n g R a n g e av = ( S t r i n g R a n g e ) o ; i f ( a v i . g e t A t t r i b u t e ( ) . g e t V a l u e T y p e ( ) . g e t T y p e ( ) . e q u a l s ( " INTEGER" ) ) { I n t e g e r avfrom = n u l l , avto=n u l l , a v i v a l = n u l l ; i f ( ! av . getFrom ( ) . isEmpty ( ) ) avfrom = new I n t e g e r ( av . getFrom ( ) ) ; i f ( ! av . getTo ( ) . isEmpty ( ) ) a v t o = new I n t e g e r ( av . getTo ( ) ) ; a v i v a l = new I n t e g e r ( a v i . g e t V a l u e ( ) ) ; The reason for using it is the fact that for digital attributes it is possible to set a range of values circumscribing the values of such attributes.
Initially all items belonging to a given category are searched. If the map of template values is empty the method returns the items found and becomes inactive. Otherwise, each of the items is analysed by comparing its attributes with template attributes. For attributes of type BOOLEAN and STRING the analysis involves searching for identical values. For digital types the analysis checks whether their value is higher or equal to the value of from field of an object of type StringRange and whether it is lower or equal to the value of to field. If an item does not have value set for an attribute from the template map or its value does not match the value from the template, the analysis of a given item stops and proceeds to another one. If the analysis of an item is successful, it is copied to a list which will be returned as a search result.
CustomerManagerBean.add()is a method of persisting a new entity bean (in this case Customer) in the database with a prior examination whether the persistent entity will not interfere with uniqueness limit (limits are imposed on email field in Customer entity, among others).
merge()operation of EntityManager persistence service only checks the uniqueness of the primary key. When an attempt is made to persist an entity interfering with the limitations Pobrane z czasopisma Annales AI-Informatica http://ai.annales.umcs.pl Data: 04/03/2020 00:27:18 U M C S imposed on a different column, the database will return an error. Java Persistence 1.0 specifications do not define a separate exception for such a situation, therefore in case of failure persistence service will not indicate its cause. The solution offered by add()method is to perform a query checking whether the record with the value of the indicated column and uniqueness limit exists in the database. If the search result is positive the method generates the exception UniqueException, otherwise the entity becomes persistent.

Customer applications
Customer modules are in the form of web applications. We have created two separate ones: one for customers and the other for store administrators. They have been built on the basis of JavaServer Faces technology combined with Facelets framework and may be divided into management and presentation layers.
The management layer comprises managed beans, also referred to as controllers (figure 1). Initializing particular controllers is performed declaratively by means of appropriate XML implementation descriptor elements faces-config.xml.
Controllers implement all operations which the application offers its users. Access to those operations is given by means of links and forms on the viewed website. The controller object uses session beans injected by @EJB annotations, thus gaining access to data gathered in the database.
Each controller is an extension of the abstract class AbstractController. This class comprises references to the session component's interface EntityManagerLocal used in all the controllers. Another feature of the class is view which is a String type object storing information on view. AbstractController has one getEntityFromRequest() method to get an entity object according to the HTTP query parameter (parameter value should constitute the primary key of searched entity). T obj = n u l l ; S t r i n g p = F a c e s C o n t e x t . g e t C u r r e n t I n s t a n c e ( ) . g e t E x t e r n a l C o n t e x t ( ) . g e t R e q u e s t P a r a m e t e r M a p ( ) . g e t ( param ) ; ProducerController from the store administrator application may serve as an example of a controller. This class comprises a number of methods utilised in the InvokeApplication phase of the JSF query processing cycle. Their function is to initiate parameter states so that their values may be displayed or set by means of forms on the viewed webpages. For instance, the createSetup() method has to perform the following tasks: (1) it creates a new object of type Producer whose attributes will be set by means of a form on the viewed webpage, (2) it sets the value of view parameter to CREATE, which means displaying the webpage with the form in order to add information concerning the producer, (3) it returns the value of type String, it is navigational information whose task is to define the page returned as a search result.
After filling in the form, sending it to the server, passing all the validation phases and being converted in the InvokeApplication phase, the method save() is performed. The method does the following: (1) by invoking the method save()of an entity bean, it tries to save a new record, reflecting the producer entity, in the database, (2) in case of failure it generates a message to the user and returns an empty writing informing JSF about displaying the same view, (3) if the operation of persisting succeeds, the parameter view will assume the value LIST -i.e. displaying the list of all the producers -and the navigated value will be returned.
Facelets framework has been used to create the presentation layer because it allows for creating view in a convenient way based on templates. XHTML has served as a standard for creating each component file adding up to the ultimate appearance of a webpage. Each page makes use of a template defined in the layout.xhtml file which is presented in the code snippet below (certain elements responsible for the appearance of the webpage have been removed for the sake of clarity):  The document describes general appearance and common elements for each page (menu, footnote) as well as separates a variable section with the help of <ui:insert name="mainbody"/> whose content varies from page to page. The documents describing separate pages define the appearance and content of this section only. Thanks to the usage of templating mechanism it is much easier to maintain the code and to make changes. If we wanted to, for instance, change the position of certain elements on the webpage, we would only have to edit the template file and the changes would be automatically introduced in all the subpages utilising it. We would be deprived of such a possibility if we had used the standard way of creating view in JSF, namely JSP.
The content itself varies on the majority of pages and it depends on the user's action. In such cases the main document of a webpage -depending on the view parameter of the suitable controller -imports the document defining the content of the webpage. An extract from the file producers.xhtml from the administrator application may serve as an example: The element <ui:composition> points to the template used by the webpage. <ui:define> points to the variable section of the template. Its content is the same as the content of the section. In this case the content is not directly set in <ui:define> element but rather divided into two separate files. If the requested view is the product list (LIST), the file producer_list.xhtml is imported, if the administrator wants to create a new product (CREATE) or make changes in an existing one (EDIT), the file producers_edit.xhtml is imported.

Summary
The application has been designed and implemented in a way which enables it to be used as a template for convenient creation of online stores. We have achieved this, among others, thanks to utilising general entity classes which we have transposed into the tables of a relation database. Such indispensable elements of successful online retailing as a shopping trolley service, credit card validation service and many others are also present. However, the most important element is strict adherence to "desirable expectations" according to the store customer model. Data protection is also a notable aspect. We have not used typical technologies such as Java Authentication and Authorization Service [11], but focused on the methods offered by the PhaseListener interface: beforePhase() and afterPhase(), which, combined with the roles assigned to particular groups of application users, supervise redirecting unlogged and unregistered users to the webpages they are permitted to view. The architecture of the application allows for easy extension by an accounting module or online store staff management module as well as to create such modules as a service module, etc.

Future work
Further work on the application will concern utilising the rule system for giving discounts to customers, dealing with their interests and including external data if customers are interested in equipment which is out of stock.
We are also considering adding customer support while shopping. For instance, it could include suggestions to increase the practical value of a product purchased.
Due to our concern about the effectiveness of online retailing, we are of the opinion that i