Friday, 28 August 2015

JSF

1      JSF:

1.1    Lifecycle:

Lifecycle has 6 phases. These phases involved based on the request.
1.       Restore view
2.       Apply Request values
3.       Process validations
4.       Update model values
5.       Invoke application
6.       Render response

1.1.1    Restore View:

This is the first phase and used to construct the view to display in the front end. Every view has its own view id and stores inside the FacesContext session object. JSF view is a collection of components associated with its current status. This is having two state saving mechanisms. Those are
·         Server (Default). The state of each view will be stored inside the server.
·         Client. The state will stored inside the hidden variables of browser.
These values will be configured inside the web.xml file and following is the code to configure this data.
<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

JSF controller uses the view id to look up components for the current view. If it doesn’t exist JSF creates new view. If it exists it will use the same view. View contains all the GUI components.
The phase of lifecycle presents three view instances.
·         New view:
o   In this case JSF builds the view of faces page and wires event handlers and validators to the components
·         Initial view
·         postback
FacesContext contains all state information JSF needed and this will store inside request or current session. FacesContext stores view in its view root property that means view root contains all JSF components for the current view id.

1.1.2    Apply request values:

After component is created/restored each component in component uses decode method to extract new values from request parameter and if the conversion fails jsf send that to render response phase.
This phase is applied only on action submissions. If we did not submit any actions in this phase only first and last phases will be executed.
<h:inputText binding="#{myBean.inputComponent}"
            value="#{myBean.inputValue}"
            valueChangeListener="#{myBean.inputChanged}"
            immediate="true" />
If we apply immediate=”true” as shown in above code JSF will execute validators and converts on this phase only after that JSF skips the validators on process validations phase. If no immediate=”true” system will execute validations on process validations phase.

1.1.3    Process validations:

All validations and converters are applied here. Suppose if we have following tags to html page it will perform validations in this phase.
<h:inputText binding="#{myBean.inputComponent}"
            value="#{myBean.inputValue}"
            valueChangeListener="#{myBean.inputChanged}">
      <f:converter converterId="converter" />
      <f:validator validatorId="validator" />
</h:inputText>
<converter>
    <converter-id>converter</converter-id>
    <converter-class>com.chandra.jsf.MyConverter</converter-class>
</converter>
<validator>
   <validator-id>validator</validator-id>
   <validator-class>com.chandra.jsf.MyValidator</validator-class>
</validator>
Any validations fails on above page system will send response directly to render response phase and displays the error message to the user.
If we apply the immediate =”true” to the input fields system will apply all validations on the apply request value phase only nothing will happen in this pahse.

1.1.4    Update model values:

In this phase all values will be applied to the backing beans and data can be accessed after this phase on backing beans.
public class Bean {
      private Integer id;
      public Integer getId() {
            return id;
      }
      public void setId(Integer id) {
            System.out.println(id);
            this.id = id;
      }
      public String submit() {
            System.out.println("ID value:" + id);
            return "";
      }

}
Bold code well be executed in Phase 4. All previous phases will be applied only one server side.
If we apply immediate=”true”. This phase is also skipped

1.1.5    Invoke application:

Main action will be performed in this phase.

1.1.6    Render response:

Phase construction begins here again.

1.2    Phase Listener class:

This class executes when phase is initialized. We have two methods beforePhase and afterPhase.
public class MyPhaseListener implements PhaseListener {

      private static final long serialVersionUID = -8180590158099109675L;

      public PhaseId getPhaseId() {
            return PhaseId.ANY_PHASE;
      }

      public void beforePhase(PhaseEvent event) {
            System.out.println("START PHASE " + event.getPhaseId());
            System.out.println("Before Phase");
            pahseDetialsPrint(event, '#');
      }

      public void afterPhase(PhaseEvent event) {
            System.out.println("END PHASE " + event.getPhaseId());
            System.out.println("After Phase");
            pahseDetialsPrint(event, '*');
      }

      public void pahseDetialsPrint(PhaseEvent event, char specialCharacter) {
            // findRequestValues(event, specialCharacter);
            // findComponentValues(event);
      }

      public void findComponentValues(PhaseEvent event) {
            List<UIComponent> list = event.getFacesContext().getViewRoot()
                        .getChildren();
            for (UIComponent component : list) {
                  for (UIComponent childComponent : component.getChildren()) {
                        if (childComponent instanceof HtmlInputText) {
                              HtmlInputText text = (HtmlInputText) childComponent;
                              System.out.println(text.getValue());
                        }
                  }
            }
      }

      public void findRequestValues(PhaseEvent event, char specialCharacter) {
            HttpServletRequest request = (HttpServletRequest) event
                        .getFacesContext().getExternalContext().getRequest();
            Map<String, String[]> map = request.getParameterMap();
            for (int i = 0; i < 20; i++) {
                  System.out.print(specialCharacter);
            }
            System.out.println();
            for (String s : map.keySet()) {
                  System.out.println("Key:" + s);
                  for (String val : map.get(s)) {
                        System.out.println("Vlaues:");
                        System.out.println(val);
                  }
            }
            System.out.println();
      }
}

1.3    Login Form:

To design login form

1.3.1    Managed Bean:


1.3.2    Navigation:


1.4    Resource bundle:


1.5    Basic tags:


1.6    Facelets tags:


1.7    Converter class:


1.7.1    Convert Number:


1.7.2    Convert date time:


1.8    Validator class:


1.8.1    Validate Length:


1.8.2    Validate Required:


1.8.3    Validate Regular expression:


1.9    Data table creation:


1.9.1    Add row:


1.9.2    Delete row:


1.9.3    Sorting:


1.9.4    Display row numbers:


1.9.5    Repeat tag:


1.10   Event Handlers:


1.11   Hibernate Integration:


1.12   Spring Integration:




Wednesday, 26 August 2015

Schema

1      Schema Introduction


To generate schema we need to follow the bellow schemata template
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:product=http://www.ibm.com/schemas
      targetNamespace=http://www.ibm.com/schemas
      elementFormDefault="qualified"
      attributeFormDefault="unqualified">
             <!—data here à
</xs:schema>

·         xmlns:xs=http://www.w3.org/2001/XMLSchema indicates that the elements and datatypes used in schema comes from specified URL. Name space name should be preferred as “xs
·         targetNamespace=http://www.ibm.com/schemas indicates that the elements defined by schema comes from this URL
·         elementFormDefault="qualified" indicates that the elements defined in the documents must be qualified by namespace

2      Simple Element:

Simple types contains the following elements.
·         Element
·         Attribute
·         restriction

2.1    Element:

It just contains only one text and it should not contains the other elements or attributes. The text can be of many different types included in the xml schema definition. We can add the restriction to limit the content.

2.1.1    Syntax simple element:

<xs:element name="xxx" type="yyy"/>
xxx is the name of element and yyy is the data type of the element. Schema has lots of built in data types but most common data types are
·         xs:string
·         xs:decimal
·         xs:integer
·         xs:date
·         xs:Boolean
·         xs:time

2.1.2    Examples:

Suppose following are the xml tags and related simple elements
<name>Chandra</name>
<xs:element name=”name” type=”xs:string”/>
<age>21</age>
<xs:element name=”age” type=”xs:integer”/>

2.1.3    Other attributes of simple elements:

·         default
·         fixed
    Elements can contains the default and fixed values. Default value means schema will automatically assign the default values but we change use our own values as well. But fixed means we can’t change the value of that like a constant

2.2    Attribute:

Simple elements can’t have attributes if element contains the attribute that means element is complex element

2.2.1    Syntax of simple element:

<xs:attribute name="xxx" type="yyy"/>
All datatypes are similar to the simple elements

2.2.2    Examples:

<lastName lang=”EN”>Yen</lastName>
<xs:attribute name=”lang” type=”xs:string”/>

2.2.3    Other attributes of attribute element:

·         default
·         fixed
·         use

2.3        restrictions:

 Used to specify the accepted values for the xml element and attributes. Restrictions on xml elements are called as facets.

2.3.1    Restrictions on values:

Following is the age restriction that can’t be lesser than 0 and more than 120
To define this restriction first we need to create xml tag and related simple element:

2.3.1.1   Xml tag:

<age></age>

2.3.1.2   Simple element:

<xs:element name=”age” type=”xs:integer”/>

2.3.1.3   Simple element with condition:

<xs:element name="age">
  
<xs:simpleType>
    
<xs:restriction base="xs:integer">
      
<xs:minInclusive value="0"/>
      
<xs:maxInclusive value="120"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>

2.3.1.4   Restrictions on set of values:

1
<xs:element name="car">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:enumeration value="Audi"/>
      
<xs:enumeration value="Golf"/>
      
<xs:enumeration value="BMW"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
2
<xs:element name="car" type="carType"/>

<xs:simpleType name="carType">
  
<xs:restriction base="xs:string">
    
<xs:enumeration value="Audi"/>
    
<xs:enumeration value="Golf"/>
    
<xs:enumeration value="BMW"/>
  
</xs:restriction>
</xs:simpleType>

2.3.1.5   Restrictions on series of values:

1
One Lower:
<xs:element name="letter">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="[a-z]"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
2
Allow 3 caps:
<xs:element name="initials">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
3
3 lower or upper:
<xs:element name="initials">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
4
Only xyz:
<xs:element name="choice">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="[xyz]"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
5
5 Digits
<xs:element name="prodid">
  
<xs:simpleType>
    
<xs:restriction base="xs:integer">
      
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
6
Zero or more occurrence of a to z:
<xs:element name="letter">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="([a-z])*"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
7
Pair of lower and upper case:
<xs:element name="letter">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="([a-z][A-Z])+"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
8
Acceptable values:
<xs:element name="gender">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="male|female"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
9
Exactly 8 letters from a to z lower or upper and digits:
<xs:element name="password">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:pattern value="[a-zA-Z0-9]{8}"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>

2.3.1.6   Restrictions on white space values:

1
<xs:element name="address">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:whiteSpace value="preserve"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
2
<xs:element name="address">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:whiteSpace value="replace"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>
3
<xs:element name="address">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:whiteSpace value="collapse"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>

2.3.1.7   Restrictions on length:

<xs:element name="password">
  
<xs:simpleType>
    
<xs:restriction base="xs:string">
      
<xs:minLength value="5"/>
      
<xs:maxLength value="8"/>
    
</xs:restriction>
  
</xs:simpleType>
</xs:element>

2.3.1.8   Restrictions for datatypes:

Constraint
Description
enumeration
Defines a list of acceptable values
fractionDigits
Specifies the maximum number of decimal places allowed.
 Must be equal to or greater than zero
Length
Specifies the exact number of characters or list items allowed.
Must be equal to or greater than zero
maxExclusive
Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive
Specifies the upper bounds for numeric values (the value must be less than this value)
maxLength
Specifies the maximum number of characters or list items allowed.
Must be equal to or greater than zero
minExclusive
Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive
Specifies the lower bounds for numeric values
(the value must be greater than or equal to this value)
minLength
Specifies the minimum number of characters or list items allowed.
Must be equal to or greater than zero
pattern
Defines the exact sequence of characters that are acceptable
totalDigits
Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace
Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

3      Complex elements:

Complex element is an element that contains the other elements or attributes. There are 4 types of complex elements
·         Empty element
o   <product pid="1345"/>
·         Element that contains other element
o   <employee>
  
<firstname>John</firstname>
  
<lastname>Smith</lastname>
</employee>
·         Element that contains text
o   <food type="dessert">Ice cream</food>
·         Element that contains other element or text
o   <description>
It happened on 
<date lang="norwegian">03.03.99</date> ....
</description>

3.1    Empty element:

Empty complex element can’t have the contents, it’s just contains the attribute
<product prodid="1345" />
<xs:element name="product">
  
<xs:complexType>
   
<xs:attribute name="prodid" type="xs:positiveInteger"/>
  
</xs:complexType>
</xs:element>
Or we can divide the above code into two parts. 1. Simple type and 2. Related complex type
<xs:element name="product" type="prodtype"/>

<xs:complexType name="prodtype">
  
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>

3.2    Element that contains other elements:

If one element contains the other element then we can call it as a elements only
<person>
  
<firstname>John</firstname>
  
<lastname>Smith</lastname>
</person>
To define above elements in schema we need to define two simple elements and one complex element
<xs:element name="person">
  
<xs:complexType>
    
<xs:sequence>
      
<xs:element name="firstname" type="xs:string"/>
      
<xs:element name="lastname" type="xs:string"/>
    
</xs:sequence>
  
</xs:complexType>
</xs:element>
Same thing we can define as bellow
<xs:element name="person" type="persontype"/>

<xs:complexType name="persontype">
  
<xs:sequence>
    
<xs:element name="firstname" type="xs:string"/>
    
<xs:element name="lastname" type="xs:string"/>
  
</xs:sequence>
</xs:complexType>

3.3    Element that contains text:

This element contains the text and attributes. This type contains the simple content and so we will add simple content tag around it. When we use simple content tag we must use the extinction or restriction tags.
<shoesize country="france">35</shoesize>
To convert this tag we need to use simple content type with extinction
<xs:element name="shoesize">
  
<xs:complexType>
    
<xs:simpleContent>
      
<xs:extension base="xs:integer">
        
<xs:attribute name="country" type="xs:string" />
      
</xs:extension>
    
</xs:simpleContent>
  
</xs:complexType>
</xs:element>

3.4    Element that contains other elements and text:

This can contains the attributes, elements and text. We will call it as a mixed content
To represent the mixed content we will use the mixed tag to true
<letter>
  Dear Mr.
<name>John Smith</name>.
  Your order 
<orderid>1032</orderid>
  will be shipped on 
<shipdate>2001-07-13</shipdate>.
</letter>
To represent the data in xsd we will use bellow format
<xs:element name="letter">
  
<xs:complexType mixed="true">
    
<xs:sequence>
      
<xs:element name="name" type="xs:string"/>
      
<xs:element name="orderid" type="xs:integer"/>
      
<xs:element name="shipdate" type="xs:date"/>
    
</xs:sequence>
  
</xs:complexType>
</xs:element>

4      Indicators

We can control how elements are to be used in documents with indicators. We have 7 indicators and categorised as 3 types.

4.1.1    Order indicators:

Order indicators are used to define the order of elements. We have 3 types of order indicators
·         All
·         Choice
·         Sequence

4.1.1.1   All:

All indicates that the order of child element can occurs in any order
<xs:element name="person">
  
<xs:complexType>
    
<xs:all>
      
<xs:element name="firstname" type="xs:string"/>
      
<xs:element name="lastname" type="xs:string"/>
    
</xs:all>
  
</xs:complexType>
</xs:element>

4.1.1.2   Choice:

Specifies that either one can occur
<xs:element name="person">
  
<xs:complexType>
    
<xs:choice>
      
<xs:element name="employee" type="employee"/>
      
<xs:element name="member" type="member"/>
    
</xs:choice>
  
</xs:complexType>
</xs:element>

4.1.1.3   Sequence:

Specifies that elements can occur on same way that we have specified
<xs:element name="person">
   
<xs:complexType>
    
<xs:sequence>
      
<xs:element name="firstname" type="xs:string"/>
      
<xs:element name="lastname" type="xs:string"/>
    
</xs:sequence>
  
</xs:complexType>
</xs:element>

4.1.2    Occurrence indicators:

It is defines that how often that the element can occur. This two types. All group and order indicators default value of maxOccurs and minOccurs value is 1
·         maxOccurs
·         minOccurs

4.1.2.1   maxOccurs:

Specifies that how many times that the element can repeat
<xs:element name="person">
 <
xs:complexType>
  
<xs:sequence>
   
<xs:element name="fname" type="xs:string"/>
   
<xs:element name=”lname" type="xs:string" maxOccurs="10"/>
  <
/xs:sequence>
 <
/xs:complexType>
</xs:element>

4.1.2.2   minOccurs:

specifies that how many minimum times that the elements can occur
<xs:element name="person">
  
<xs:complexType>
    
<xs:sequence>
      
<xs:element name="full_name" type="xs:string"/>
      
<xs:element name="child_name" type="xs:string"
      maxOccurs=
"10" minOccurs="0"/>
    
</xs:sequence>
  
</xs:complexType>
</xs:element>

4.1.3    Group indicator:

Indicates the related set of elements these are two types.
·         group
·         attributeGroup

4.1.3.1   Group:

Indicates the group of declarations. We must add all or choice or sequence indicator inside the group declaration.
<xs:group name="persongroup">
  
<xs:sequence>
    
<xs:element name="firstname" type="xs:string"/>
    
<xs:element name="lastname" type="xs:string"/>
    
<xs:element name="birthday" type="xs:date"/>
  
</xs:sequence>
</xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">
  
<xs:sequence>
    
<xs:group ref="persongroup"/>
    
<xs:element name="country" type="xs:string"/>
  
</xs:sequence>
</xs:complexType>

4.1.3.2   attributeGroup:

Used to define attribute groups.
<xs:attributeGroup name="personattrgroup">
  
<xs:attribute name="firstname" type="xs:string"/>
  
<xs:attribute name="lastname" type="xs:string"/>
  
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

<xs:element name="person">
  
<xs:complexType>
    
<xs:attributeGroup ref="personattrgroup"/>
  
</xs:complexType>
</xs:element>

5      Element substitution:

Let's say that we have users from two different countries: England and Norway. We would like the ability to let the user choose whether he or she would like to use the Norwegian element names or the English element names in the XML document.
To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element.
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>

<xs:complexType name="custinfo">
  
<xs:sequence>
    
<xs:element ref="name"/>
  
</xs:sequence>
</xs:complexType>

<xs:element name="customer" type="custinfo"/>
<xs:element name="kunde" substitutionGroup="customer"/>
To use this element we will follow bellow code
<customer>
  
<name>John Smith</name>
</customer>
<kunde>
  
<navn>John Smith</navn>
</kunde>

6      Data types:

6.1    String data type:

ENTITIES
ENTITY
ID
IDREF
IDREFS
language
Name
NCName
NMTOKEN
NMTOKENS
normalizedString
QName
string
token


6.2    Date datatype:

date
dateTime
duration
gDay
gMonth
gMonthDay
gYear
gYearMonth
time

6.3    Numeric data types:

byte
decimal
int
integer
long
negativeInteger
nonNegativeInteger
nonPositiveInteger
positiveInteger
short
unsignedLong
unsignedInt
unsignedShort
unsignedByte

6.4    Miscellaneous data type:

anyURI
base64Binary
boolean
double
float
hexBinary
NOTATION
QName