Wednesday, October 11, 2017

Hibernate Annotations Approach



hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection properties - Driver, URL, user, password -->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@172.20.1.47:1521:XE</property>
        <property name="hibernate.connection.username">DLAUDIT</property>
        <property name="hibernate.connection.password">DLAUDIT</property>
        <!-- Connection Pool Size -->
        <property name="hibernate.connection.pool_size">10</property>
       
        <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>
       
        <!-- Outputs the SQL queries, should be disabled in Production -->
        <property name="hibernate.show_sql">true</property>
        <property name="format_sql">true</property>
       
        <!-- Dialect is required to let Hibernate know the Database Type, MySQL, Oracle etc
            Hibernate 4 automatically figure out Dialect from Database Connection Metadata -->
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- mapping file, we can use Bean annotations too -->
        <mapping class="com.hiberante.api.annotations.Account" />
    </session-factory>

</hibernate-configuration>


Account.java


package com.hiberante.api.annotations;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="ACCOUNT")
public class Account {

@Id
@Column(name="ID")
private Integer id;

@Column(name = "GUI")
private String gui;

@Column(name="FIRST_NAME")
private String firstName;

@Column(name="LAST_NAME")
private String lastName;

@Column(name="AGE")
private Integer age;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGui() {
return gui;
}
public void setGui(String gui) {
this.gui = gui;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

}

Hibernate Declarative Approach

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection properties - Driver, URL, user, password -->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@172.20.1.47:1521:XE</property>
        <property name="hibernate.connection.username">DLAUDIT</property>
        <property name="hibernate.connection.password">DLAUDIT</property>
        <!-- Connection Pool Size -->
        <property name="hibernate.connection.pool_size">10</property>
       
        <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>
       
        <!-- Outputs the SQL queries, should be disabled in Production -->
        <property name="hibernate.show_sql">true</property>
        <property name="format_sql">true</property>
       
        <!-- Dialect is required to let Hibernate know the Database Type, MySQL, Oracle etc
            Hibernate 4 automatically figure out Dialect from Database Connection Metadata -->
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- mapping file, we can use Bean annotations too -->
        <mapping resource="resorces/Account.hbm.xml" />
    </session-factory>
</hibernate-configuration>


Account.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
 <hibernate-mapping>
  <class name="com.hiberante.api.declartive.Account" table = "ACCOUNT">
  <id name="id" column="ID" type="java.lang.Integer"></id>
  <property name="gui" column="GUI" type="java.lang.String"/>
  <property name="firstName" column="FIRST_NAME" type="java.lang.String"/>
  <property name="lastName" column="LAST_NAME" type="java.lang.String"/>
  <property name="age" column="AGE" type="java.lang.Integer"/>
  </class>

 </hibernate-mapping>


Hibernate Programmatic Approch

package resorces;

import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.hiberante.api.programetic.Account;

public class HiberanateConfig {

//Property based configuration
    private static SessionFactory sessionJavaConfigFactory;

private static SessionFactory buildSessionJavaConfigFactory() {
       try {
       Configuration configuration = new Configuration();
       
       //Create Properties, can be read from property files too
       Properties props = new Properties();
       props.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
       props.put("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:XE");
       props.put("hibernate.connection.username", "DLAUDIT");
       props.put("hibernate.connection.password", "DLAUDIT");
       props.put("hibernate.current_session_context_class", "thread");
       props.put("hibernate.show_sql", true);
       props.put("format_sql", true);
       props.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
     
       configuration.setProperties(props);
       
       //we can set mapping file or class with annotation
       //addClass(Employee1.class) will look for resource
       // com/journaldev/hibernate/model/Employee1.hbm.xml (not good)
       configuration.addAnnotatedClass(Account.class);
     
       //configuration.addClass(Account.class);
       //configuration.addResource("resorces/Account.hbm.xml");
     
      // ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
       System.out.println("Hibernate Java Config serviceRegistry created");
       
       SessionFactory sessionFactory = configuration.buildSessionFactory();
       
       return sessionFactory;
       }
       catch (Throwable ex) {
           System.err.println("Initial SessionFactory creation failed." + ex);
           throw new ExceptionInInitializerError(ex);
       }
   }
   
   public static SessionFactory getSessionJavaConfigFactory() {
       if(sessionJavaConfigFactory == null) sessionJavaConfigFactory = buildSessionJavaConfigFactory();
       return sessionJavaConfigFactory;
   }

}

Hibernate and Spring With Domain Scan Package

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <!-- It register the beans in context and scan the annotations inside beans and activate them -->
    <context:component-scan base-package="com.hibernate.spring" />
    <context:component-scan base-package="com.hibernate.spring.controller" />
   
    <!-- This allow for dispatching requests to Controllers -->
    <mvc:annotation-driven />
 
     <!-- This helps in mapping the logical view names to directly view files under a certain pre-configured directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
   
    <!-- This produces a container-managed EntityManagerFactory;
         rather than application-managed EntityManagerFactory as in case of LocalEntityManagerFactoryBean-->
    <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <!-- This makes /META-INF/persistence.xml is no longer necessary -->
      <property name="packagesToScan" value="com.hibernate.spring.domain" />
      <!-- JpaVendorAdapter implementation for Hibernate EntityManager.
           Exposes Hibernate's persistence provider and EntityManager extension interface -->
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
      </property>
      <property name="jpaProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
         </props>
      </property>
   </bean>

   <!-- Simple implementation of the standard JDBC DataSource interface,
        configuring the plain old JDBC DriverManager via bean properties -->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
      <property name="url" value="jdbc:oracle:thin:@172.20.1.47:1521:XE" />
      <property name="username" value="DLAUDIT" />
      <property name="password" value="DLAUDIT" />
   </bean>
   
    <!-- This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access.
        JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
   </bean>
 
   <!-- responsible for registering the necessary Spring components that power annotation-driven transaction management;
        such as when @Transactional methods are invoked -->
   <tx:annotation-driven />
 
</beans>

Sunday, June 25, 2017

Find Type Of Object By Data or Itself

package com.test;

public class FindType {

public static void main(String[] args) {
System.out.println(findTypeOfObject("10"));
System.out.println(findTypeOfObject(10));
System.out.println(findTypeOfObject(10.00));
}

private static String findTypeOfObject(Object object){
String type = null;
if (object != null ) {
type = object.getClass().getName();
}
return type;
}

}

Transpose Matrix

package com.test;

import java.util.ArrayList;
import java.util.List;

public class Transpose {
public static void main(String[] args) {
List<List<String>> data = getData();
System.out.println("\n--------------------------Before Transpoese------------------------");
for (int row = 0; row < data.size(); row++) {
List<String> colLst = data.get(row);
System.out.println("  ");
for (int i = 0; i < colLst.size(); i++) {
String cell = colLst.get(i);
System.out.print("  "+cell);
}
}
System.out.println("\n--------------------------After Transpoese------------------------");
List<List<String>> transposedData = transposeData(data);
for (int row = 0; row < transposedData.size(); row++) {
List<String> colLst = transposedData.get(row);
System.out.println("  ");
for (int i = 0; i < colLst.size(); i++) {
String cell = colLst.get(i);
System.out.print("  "+cell);
}
}
}

private static List<List<String>> transposeData(List<List<String>> data){

List<String> rowList = new ArrayList<String>();
List<List<String>> colList = new ArrayList<List<String>>();

for (int row = 0; row < data.get(0).size(); row++) {
rowList = new ArrayList<String>();
for (int col = 0; col < data.size(); col++) {
String d = data.get(col).get(row);
rowList.add(d);
}
colList.add(rowList);
}

return colList;
}


private static List<List<String>> getData(){
List<List<String>> data = new ArrayList<List<String>>();

List<String> dataRow = new ArrayList<String>();
dataRow.add("1");
dataRow.add("A");
dataRow.add("0");
dataRow.add("-");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("2");
dataRow.add("B");
dataRow.add("1");
dataRow.add("-");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("3");
dataRow.add("C");
dataRow.add("1");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("4");
dataRow.add("D");
dataRow.add("2");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("5");
dataRow.add("E");
dataRow.add("2");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("6");
dataRow.add("F");
dataRow.add("3");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("7");
dataRow.add("G");
dataRow.add("3");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("8");
dataRow.add("H");
dataRow.add("7");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("9");
dataRow.add("I");
dataRow.add("7");
dataRow.add("");
data.add(dataRow);

return data;
}
}

Thursday, June 22, 2017

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestService {

static Map<String, String> mapN = new HashMap<String, String>();
static int max_depth = 0;
public static void main(String[] args) {
List<List<String>> data = getData();
System.out.println(data);
Map<String, List<String>> mapP = new HashMap<String, List<String>>();

for (int i = 0; i < data.size(); i++) {
List<String> rows = data.get(i);
if(mapP.get(rows.get(2)) != null){
List<String> listC = mapP.get(rows.get(2));
listC.add(rows.get(0));
mapN.put(rows.get(0), rows.get(1));
} else {
List<String> listC = new ArrayList<String>();
listC.add(rows.get(0));
mapN.put(rows.get(0), rows.get(1));
mapP.put(rows.get(2), listC);
}
}
List<List<String>> level_list = new ArrayList<List<String>>();
List<List<String>> final_list = new ArrayList<List<String>>();
List<String> first_level = mapP.get("0");
List<String> this_level_list = new ArrayList<>();
loopingLowerLevel(first_level, mapP, 1, level_list, this_level_list);
for(int i = 0; i < level_list.size(); i++){
List<String> list = level_list.get(i);
for(int j = 0; j < max_depth; j++){
if(j >= list.size()){
if(i == 0){
List<String> list_d = new ArrayList<>();
list_d.add("");
final_list.add(list_d);
} else {
List<String> list_dd = final_list.get(j);
list_dd.add("");
final_list.set(j, list_dd);
}
} else {
if(i == 0){
List<String> list_d = new ArrayList<>();
list_d.add(list.get(j));
final_list.add(list_d);
} else {
List<String> list_dd = final_list.get(j);
list_dd.add(list.get(j));
final_list.set(j, list_dd);
}
}
}
}
for(int finall = 0; finall < final_list.size(); finall++){
System.out.println(final_list.get(finall));
}
}

private static void loopingLowerLevel(List<String> low_level_list, Map<String, List<String>> mapP, int level, List<List<String>> level_list, List<String> tree){
for(int i = 0; i < low_level_list.size(); i++){
List<String> another_level_list = mapP.get(low_level_list.get(i));
List<String> this_level_list = new ArrayList<>(tree);
this_level_list.add(mapN.get(low_level_list.get(i)));
if(another_level_list != null){
loopingLowerLevel(another_level_list, mapP, (level+1), level_list, this_level_list);
} else {
level_list.add(this_level_list);
if(level > max_depth)
max_depth = level;
}
}
}

/*private static int loopingLowerLevel(List<String> low_level_list, Map<String, List<String>> mapP, String parent, int level, List<List<String>> level_list){
for(int i = 0; i < low_level_list.size(); i++){
List<String> another_level_list = mapP.get(low_level_list.get(i));
if(level_list.size() > level) {
List<String> list_p = level_list.get(level);
list_p.add(mapN.get(low_level_list.get(i)));
level_list.add(level, list_p);
} else {
List<String> list_p = new ArrayList<String>();
list_p.add(mapN.get(low_level_list.get(i)));
level_list.add(level, list_p);
}
if(another_level_list != null){
int rtnLevel = loopingLowerLevel(another_level_list, mapP, low_level_list.get(i), level++, level_list);

} else {

}
}

}*/


private static List<List<String>> getData(){
List<List<String>> data = new ArrayList<List<String>>();

List<String> dataRow = new ArrayList<String>();
dataRow.add("1");
dataRow.add("A");
dataRow.add("0");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("2");
dataRow.add("B");
dataRow.add("1");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("3");
dataRow.add("C");
dataRow.add("1");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("4");
dataRow.add("D");
dataRow.add("2");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("5");
dataRow.add("E");
dataRow.add("2");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("6");
dataRow.add("F");
dataRow.add("3");
dataRow.add("");
data.add(dataRow);

dataRow = new ArrayList<String>();
dataRow.add("7");
dataRow.add("G");
dataRow.add("3");
dataRow.add("");
data.add(dataRow);

return data;
}

}

Thursday, June 8, 2017

Super Model

<H3>SuperModel</H3>

package com.test;

import java.util.List;

public class SuperModel {

private String data;
private String dataType;
private String name;
private String id;
private String cssClass;
private int rowSpan = 1;
private int colSpan = 1;

List<SuperModel> childSuperModel;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public String getDataType() {
return dataType;
}

public void setDataType(String dataType) {
this.dataType = dataType;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getCssClass() {
return cssClass;
}

public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}

public int getRowSpan() {
return rowSpan;
}

public void setRowSpan(int rowSpan) {
this.rowSpan = rowSpan;
}

public int getColSpan() {
return colSpan;
}

public void setColSpan(int colSpan) {
this.colSpan = colSpan;
}

public List<SuperModel> getChildSuperModel() {
return childSuperModel;
}

public void setChildSuperModel(List<SuperModel> childSuperModel) {
this.childSuperModel = childSuperModel;
}

@Override
public String toString() {
return "SuperModel [data=" + data + ", dataType=" + dataType + ", name=" + name + ", id=" + id + ", cssClass="
+ cssClass + ", rowSpan=" + rowSpan + ", colSpan=" + colSpan + ", childSuperModel=" + childSuperModel
+ "]";
}

}

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SuperModelTestController {
@Autowired
SuperModelService superModelService;
@RequestMapping("getSuperModelView.htm")
public ModelAndView getSuperModelView(){
ModelAndView view = new ModelAndView("superModelView");
SuperModel superModelTable = superModelService.getSuperModelData();
view.addObject("superModelTable", superModelTable);
return view;
}

}


package com.test;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class SuperModelServiceImpl implements SuperModelService {

private List<List<Object>> getData(){
List<List<Object>> data = new ArrayList<List<Object>>();
for (int i = 0; i < 10; i++) {
List<Object> row = new ArrayList<Object>();
for (int j = 0; j < 5; j++) {
Object obj = "Cell-"+i+"-"+j;
row.add(obj);
}
data.add(row);
}
System.out.println(data);
return data;
}

@Override
public SuperModel getSuperModelData() {
List<List<Object>> data = getData();
SuperModel superModelTable = new SuperModel();
List<SuperModel> superModelTableList = null;
if(data != null && !data.isEmpty()){
superModelTableList = new ArrayList<SuperModel>();
SuperModel superModelRow = new SuperModel();
for (List<Object> listObj : data) {
List<SuperModel> superModelRowList = new ArrayList<SuperModel>();
for (Object object : listObj) {
SuperModel superModelCell = new SuperModel();
superModelCell.setData(object+"");
superModelRowList.add(superModelCell);
superModelRow.setChildSuperModel(superModelRowList);
}
superModelTableList.add(superModelRow);
}
superModelTable.setChildSuperModel(superModelTableList);
}

System.out.println(superModelTable);
return superModelTable;
}
}


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<c:forEach items="${superModelTable.childSuperModel}" var="superModelTableList">
<tr>
<c:forEach items="${superModelTableList.childSuperModel}" var="superModelRow">
<td>
<c:out value="${superModelRow.data}"></c:out>
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>

Wednesday, October 10, 2012

Complex Element


Complex Element

Complex Element : which element contains simple elements or other complex elements is call complex element.

We can create complex elements in two ways in xsd

1.creating complex type elements and using them
2.annoums complex element


1.creating complex type elements and using them

we can create a complex element in xsd by using "complexType" key word
as follows

we can resue the structure of the complex type (it is creating class in java)

compareing java class and complex type in xsd.










Class in java

Complex Type in xsd

Class name_of_class
{

         //statements


}


<complexType name=”name_of_complex _type”>
            <sequence/all>
  
                   //simple elements

            </sequence/all>
</complexType>

class ItemType
{
          String itemcode;
          int quantity;
}





<complexType name="itemtype">
                <sequance>
                                <element name="itemcode" type="string"/>
                                <element name="quantity" type="int"/>
                </sequance>
</complexType>


ItemType  item=new ItemType();



<element name=”item” type=”itemtype”>



Structure:

<complexType name="complex_type_name">
                <sequance/all>
                                //simple elements or complex elements
                </sequance/all>
</complexType>


Example:

complexType which contains simple elements

<complexType name="itemtype">
                <sequance>
                                <element name="itemcode" type="string"/>
                                <element name="quantity" type="int"/>
                </sequance>
</complexType>

complexType which contains complex elements

<complexType name="orderitemstype">
                <sequance>
                                <element name="quantity" type="itemtype"/>
                </sequance>
</complexType>


XML Well Form


XML Well Form
For well formed XML we follow the following rules.
Well form talks about the structure of the xml document
Rules:
·         XML file should start with prolog (optional)
·         Only one root tag is allowed
·         All the elements (tags) must be under Root Tag only
·         Every stating tag must have ending tag (closing tag)
·         The level of opening tag must be closed the same level
·         XML tags are case sensitive  i.e <Tag> and <tag> both are treated as different tags
Demo:
File Name: Demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<PURCHAGEORDER> <!- root tag opening at level 0 -->
                <ORDERITEMS> <!- opening tag at level 1 -->
                                <ITEM>  <!- opening tag at level 2 -->
                                                <ITEMCODE>NOKIA345</ITEMCODE>
                                                <QUANTITY>6</QUANTITY>
                                </ITEM><!- closing tag at level 2 -->
                </ORDERITEMS> <!- closing tag at level 1 -->
                <SHIPPINGADDRESS><!- opening tag at level 1 -->
                                <ADDR>SAN</ADDR><!- opening and closing tag at level 2 -->
                                <DORNO>77-98</DORNO><!- opening and closing tag at level 2 -->                                                                                                     <STREAT>GOPALNAGAR</STREAT><!- opening and closing tag at level 2 -->
                                <CITY>HYD</CITY><!- opening and closing tag at level 2 -->
                </SHIPPINGADDRESS><!- closing tag at level 1 -->
</PURCHAGEORDER><!- closing tag at level 0 -->

Note: above demo.xml file satisfy’s all the rules so its call well formed xml
1.     For checking is well formed or not open in the browser, if its shows the above content as it we wrote
Otherwise it shows the error where and what
2.     use the xml spy tool check whether the xml is well formed or not

SIMPLE WEB SERVICE PROVIDER SIDE APPLICATION


SIMPLE WEB SERVICE PROVIDER SIDE APPLICATION
Requirement:
                Develop a web service provider side application which, provide complete information about a book by giving a book name as input.
Web service development environment:
1.       Using Jax-rpc api si (sun implementation) 
2.       Contract as  last
3.       Synchronous reply/response message exchange pattern
4.       rpc-encoded
5.       servlet end-point
Software requirements:
1.       java 1.5.0_22and java 1.6.0 in between
2.       jax-rpc specific jar files
3.        apache- tomcat 6.x serve
4.       Jwsdp 2.0 tool
Development procedure:
Step 1:
                Create a dynamic project in eclipse as follows.
File->New->Others->Dynamic web project
Give the project name and set server as tomcat
project Name: bookproject
Create a package under project : bookproject as shown
Package name: com.bookweb.service
Step 2:
                Copy the all jax-rpc specific jar files into lib folder.
  Step 3:
                Create a SEI interface as follows
                Interface name: BookInterface
package com.bookweb.service;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface BookInterface extends Remote {
      
       float getBookPrice(String isbn) throws RemoteException;

}

Step 4:
                Implemented class name: BookImpl.java
                Provide implementation for SEI interface, which is full fill our business requirements
package com.bookweb.service;

import java.rmi.RemoteException;

public class BookImpl implements BookInterface {

       public float getBookPrice(String isbn) throws RemoteException {
              float bookprice=0.0f;
              if(isbn.equals("java"))
                     bookprice=234.56f;
              return bookprice;
       }

}


Step 5:

      
 Write a configuration file under WEB-INF folder as following tags
Configuration file name: config.xml
<?xml version="1.0" encoding="UTF-8"?>

<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
       <service name="BookInfoService"
              targetNamespace="http://bookinfoservice.org/wsdl"
              typeNamespace="http://bookinfoservice.org/types"
              packageName="com.bookweb.service.binding">
              <interface name="com.bookweb.service.BookInterface"
                     servantName="com.bookweb.service.BookImpl" />
       </service>
</configuration>

Step 5:
                Install jwsdp2.0 tool
Set path for bin as C:\Sun\jwsdp-2.0\jaxrpc\bin
Run wscompile.bat with following options and pass config.xml as input
C:\Users\sant\workspace\BookService>wscompile -keep -verbose -gen:server -d src
-cp build\classes -model model-rpc-enc.xml.gz WebContent\WEB-INF\config.xml
It will generates wsdl file and model files and saxparsers and java classes
Move the wsdl and model file into WEB-INF folder
Step 5:
                Write one more configuration file jax-rpc.xml (file name with same name only) under WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<webServices
    xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd"
    version="1.0"
    targetNamespaceBase="http://bookinfoservice.org/wsdl"
    typeNamespaceBase="http://bookinfoservice.org/types"
    urlPatternBase="/getBookPrice">

    <endpoint
        name="booksriceservice"
        displayName="Book Service"
        description="A simple web service"
        wsdl="/WEB-INF/BookInfoService.wsdll"
        interface="com.bookweb.service.BookInterface" 
        implementation="com.bookweb.service.BookImpl"
            model="/WEB-INF/model-rpc-enc.xml.gz">
           
        </endpoint>

    <endpointMapping
        endpointName="booksriceservice"
        urlPattern="/getBookPrice"/>

</webServices>

Step 6:
                Export project as .war file
Give the war as input to the wsdeploy.bat
C:\wsdeploy –o taget.war bookproject.war
It generates the jax-rpc-runtime.xml and web.xml
Copy the same file into the project WEB-INF folder
Step 7:
Deploy the project in server and run the server and provide the url;
output as follows:
Web Services
Port Name
Status
Information
booksriceservice
ACTIVE
Address:
http://localhost:8080/bookproject/getBookPrice
WSDL:
Port QName:
{http://bookinfoservice.org/wsdl}BookInterfacePort
Remote interface:
com.bookweb.service.BookInterface
Implementation class:
com.bookweb.service.BookImpl
Model: