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: