Friday, July 30, 2010

Fixed length String Reader



Domain :
package com.test.file.domain;

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

import org.beanio.annotation.Field;
import org.beanio.annotation.Record;

@Record
public class Team {

@Field(ordinal = 1, length = 10)
   private String teamName;
   private List employees = new ArrayList<>();
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public List getEmployees() {
return employees;
}
public void setEmployees(List employees) {
this.employees = employees;
}
 
 
 
}

package com.test.file.domain;

import java.util.Date;

import org.beanio.annotation.Field;
import org.beanio.annotation.Record;

@Record
public class Employee {

@Field(ordinal = 1, length = 30)
private String firstName;
public Employee(String firstName, String lastName, String title, String salary, Date hireDate) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.title = title;
this.salary = salary;
this.hireDate = hireDate;
}
@Field(ordinal = 2, length = 30)
private String lastName;
@Field(ordinal = 3, length = 30)
private String title;
@Field(ordinal = 4, length = 8)
private String salary;
@Field(ordinal = 5, format="MMddyyyy", length = 8)
private Date hireDate;
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
}

package com.test.file.generator;

import java.io.File;
import java.util.Date;

import org.beanio.BeanWriter;
import org.beanio.StreamFactory;
import org.beanio.builder.FixedLengthParserBuilder;
import org.beanio.builder.StreamBuilder;

import com.test.file.domain.Employee;
import com.test.file.domain.Team;

public class Test {

public static void main(String[] args) {
StreamFactory factory = StreamFactory.newInstance();
    StreamBuilder builderfixedLength = new StreamBuilder("Tm")
            .format("fixedlength")
            .parser(new FixedLengthParserBuilder())
            .addRecord(com.test.file.domain.Team.class)
            .addRecord(com.test.file.domain.Employee.class);
    factory.define(builderfixedLength);

    BeanWriter out = factory.createWriter("Tm", new File("C:\\TEMP\\tm.txt"));

    Employee e1 = new Employee("EmpF1", "EmpL1", "Developer", "1", new Date());
    Employee e2 = new Employee("EmpF2", "EmpL2", "Developer", "2", new Date());
    Team team = new Team();
    team.setTeamName("Great Team");
    team.getEmployees().add(e1);
    team.getEmployees().add(e2);

    out.write(team);
    for (Employee e : team.getEmployees()) {
        out.write(e);
    }

    out.flush();
    out.close();


}

}
















package com.rajeshpollepalli.javautils.examples;

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

public class Test {

public static void main(String[] args) {
String r = "1234567890123456789012345678902345212121212";
parseGetRecord(r);

}
public enum GetTransactionFields
{
  ADDR_ID                 (10, 0),
  ADDRESS                 (20, 5),
  TYPE_CODE               (1,  0),
  ACCOUNT_INFO_IND        (1,  0),
  RECEIVE_OFFERS_IND      (1,  0),
  THIRD_PARTY_IND         (1,  0),
  CUSTOMER_EMAIL_STATUS   (1,  0),
  LAST_UPDATE             (8,  0),
  ;

  private int fieldSize;
  private Object type;
  private int subFieldSize;

  /** Private constructor */
  private GetTransactionFields(int fieldSize, int subFieldSize)
  {
      this.fieldSize = fieldSize;
      this.subFieldSize=subFieldSize;
  }

 /**
  *
  * @return
  */
  public Object getType()
  {
return type;
  }




public int getSubFieldSize() {
return subFieldSize;
}



/**
   * @return Returns the fieldSize.
   */
  public int getFieldSize()
  {
      return fieldSize;
  }
}
private static void parseGetRecord(String record)
  {
      int posn = 0;
      int endIdx = 0;
      for (GetTransactionFields f: GetTransactionFields.values())
      {
          endIdx = posn + f.getFieldSize();
          String x =record.substring(posn, endIdx);
          System.out.println("Parsing field " + f.toString() +", value = " + x );
       
          if(f.getSubFieldSize() >0){
          List list =  splitEqually(x, f.getSubFieldSize());
          System.out.println("list>>>>>>>>>>>>" +list);
          }else{
          System.out.println("x>>>>>>>>>>>>" +x);
          }
       
          posn = endIdx;
      }
  }

public static List splitEqually(final String text, final int size) { 
List ret = new ArrayList((text.length() + size - 1) / size);
   for (int start = 0; start < text.length(); start += size) {

       if (start + size > 0) {

           String temp = text.substring(start, Math.min(text.length(), start + size));
           int length = temp.length();

           for (int i = 0; i < (size - length); i++) {
               temp = temp + " ";
           }

           ret.add(temp);

       } else {
           ret.add(text.substring(start, Math.min(text.length(), start + size)));
       }


   }
   return ret;
}

}

------------------------------------
-- Westside Auto Sales Tables & View
------------------------------------

create table Cars
(
    CarID   int identity primary key,
    Make    varchar(50) not null,
    Model   varchar(50) not null,
    Description varchar(800) not null
)
go

create table Inventory
(
    SKU     int identity primary key,
    CarID   int not null,
    Price   int not null,
    Status  varchar(50) 
)
go

create view CarInventory
as
 select c.CarID, Make, Model, Description, SKU, Price, Status
 from Cars c, Inventory i where c.CarID = i.CarID
go

----------------------------------
-- Sample Data
----------------------------------

insert Cars select "Volkswagen", "New Beetle", 
"The 2003 New Beetle is a 2-door, 4-passenger family coupe, or sports coupe, 
available in 8 trims, ranging from the GL 2.0L to the Turbo S 1.8L."

insert Cars select "Mini", "Cooper", 
"The new Mini Cooper is more fun than a carnival ride. Both models handle 
like sports cars and the Mini Cooper S can accelerate from 0 to 60 mph in 
less than seven seconds."

insert Cars select "Mercedes-Benz", "SL-Class", 
"The 2003 SL-Class is a 2-door, 2-passenger luxury convertible, or convertible 
sports car, available in two trims, the SL500 Roadster and the SL55 
AMG Roadster."

insert Cars select "Land Rover", "Range Rover", 
"The 2003 Range Rover is a 4-door, 5-passenger luxury sport-utility, 
available in one trim only, the HSE."

insert Cars select "Honda", "Civic Coupe", 
"The 2003 Civic Coupe is a 2-door, 5-passenger family coupe, available 
in 16 trims, ranging from the DX 5-spd MT to the EX 4-spd AT w/ 
Front Side Airbags."

insert Inventory select 1, 18999, "Available"

insert Inventory select 2, 17597, "Available"

insert Inventory select 3, 92399, "Available"

insert Inventory select 4, 71200, "Available"

insert Inventory select 5, 12110, "Available"

------------------------------------------
-- Manufacturer Web Service Tables 
------------------------------------------

create table ManufacturerOrders
(
    OrderNumber int identity primary key,
    SKU     int not null,
    Make    varchar(50) not null,
    Model   varchar(50) not null,
    Price   int not null,
    Status varchar(20) not null
)
go

Create table BasePrices
(
    Make    varchar(50) not null,
    Model   varchar(50) not null,
    Price   int not null,
    primary key (Make, Model)
)
go

------------------------------------------
-- Manufacturer Web Service Tables 
------------------------------------------

create procedure AddOrder (@SKU int, @Make varchar(50), @Model varchar(50), 
@Price int, @Status varchar(20))
as
 set nocount on;
     
      insert ManufacturerOrders
 select @SKU, @Make, @Model, @Price, @Status

 select @@identity as "ID"
go

create procedure UpdateOrder (@OrderNum int)
as
 set nocount on

 update ManufacturerOrders set Status = "Shipped" where 
 OrderNumber = @OrderNum;

  SELECT SKU, Price FROM ManufacturerOrders
 WHERE OrderNumber = @OrderNum
go

------------------------------------------
-- Manufacturer Web Sample Data
------------------------------------------

insert BasePrices select "Volkswagen", "New Beetle", 18000
insert BasePrices select "Mini", "Cooper", 1700
insert BasePrices select "Mercedes-Benz", "SL-Class", 85000
insert BasePrices select "Honda", "Civic Coupe", 12000
insert BasePrices select "Land Rover", "Range Rover",

     

No comments:

Post a Comment