The example below illustrates a moving house example.
/*
* Copyright Javelin Software, All rights reserved.
*/
package com.javelin.furniture;
import java.util.*;
import java.io.*;
import com.javelin.beans.*;
/**
* MovingHouseExample - It's time to move house, but this time
* I get to destroy my old house and build 3 nurseries.
* Oh, this is what EJB should have been like had the Sun Engineers
* thought more about usablity and a little less about implementation.
* Fortunately EJB can be generated underneath this lot.
*
* @author Javelin Software
*/
public class MovingHouseExample
{
static FurnitureContext furnitureContext;
public static void main( String[] args )
{
try
{
furnitureContext = new FurnitureContext();
//Create the old house, and then take ownership
House oldHouse = createOldHouse();
Person person = createOwner( oldHouse.getAddress() );
//Create the new house
House newHouse = createNewHouse();
//Move House
moveHouse( person, oldHouse, newHouse );
//Destroy the old House, rooms, contents and all
destroyHouse( oldHouse );
//Build bedrooms for the troops
Room nursery1 = buildNursery( newHouse );
paintNursery( nursery1, "pink" );
Room nursery2 = buildNursery( newHouse );
paintNursery( nursery2, "pink" );
Room nursery3 = buildNursery( newHouse );
paintNursery( nursery3, "blue" );
//Serialize the new house to disk
writeToFile( "C:/tmp/house.ser", newHouse );
//Read the house back from disk
Bean bean = readFromFile( "C:/tmp/house.ser" );
//Print the house out
System.out.println( bean.toString() );
}
catch( BeanException be )
{
e1.printStackTraces();
}
catch( Throwable e )
{
e.printStackTrace();
}
finally
{
System.out.println( "fini" );
while( true ) {}
}
}
public static House createNewHouse() throws Exception
{
House house = furnitureContext.newHouseBean();
house.setHouseType( HouseType.DETATCHED );
Address address = furnitureContext.newAddressBean();
address.setLine1( "New Line1" );
address.setLine2( "New Line2" );
address.setLine3( "New Line3" );
address.setLine4( "New Line4" );
address.setPostCode( "New Code" );
house.setAddress( address );
Room hall = addRoom( house, RoomType.HALL, 12, 6 );
Room dining = addRoom( house, RoomType.DINING, 15, 19 );
Room lounge = addRoom( house, RoomType.LOUNGE, 11, 11 );
Room kitchen = addRoom( house, RoomType.KITCHEN, 9, 15 );
Room bathroom1 = addRoom( house, RoomType.BATHROOM, 214, 213 );
Room bathroom2 = addRoom( house, RoomType.BATHROOM, 14, 13 );
Room bedroom1 = addRoom( house, RoomType.BEDROOM, 20, 9 );
Room bedroom2 = addRoom( house, RoomType.BEDROOM, 10, 8 );
Room utility = addRoom( house, RoomType.UTILITY, 10, 8 );
furnitureContext.getHouseSession().create( house );
return house;
}
public static House createOldHouse() throws Exception
{
House house = furnitureContext.newHouseBean();
house.setHouseType( HouseType.APARTMENT );
Address address = furnitureContext.newAddressBean();
address.setLine1( "Line1" );
address.setLine2( "Line2" );
address.setLine3( "Line3" );
address.setLine4( "Line4" );
address.setPostCode( "PostCode" );
house.setAddress( address );
Room hall = addRoom( house, RoomType.HALL, 20, 4 );
Room dining = addRoom( house, RoomType.DINING, 10, 14 );
Room lounge = addRoom( house, RoomType.LOUNGE, 18, 14 );
Room kitchen = addRoom( house, RoomType.KITCHEN, 18, 14 );
Room bathroom = addRoom( house, RoomType.BATHROOM, 18, 14 );
Room bedroom = addRoom( house, RoomType.BEDROOM, 10, 14 );
furnitureContext.getHouseSession().create( house );
createFurniture( dining, FurnitureType.CHAIR, FurnitureSubType.DINING_CHAIR, 4 );
createFurniture( dining, FurnitureType.CHAIR, FurnitureSubType.DINING_CHAIR, 4 );
createFurniture( dining, FurnitureType.CHAIR, FurnitureSubType.DINING_CHAIR, 4 );
createFurniture( dining, FurnitureType.CHAIR, FurnitureSubType.DINING_CHAIR, 4 );
createFurniture( dining, FurnitureType.TABLE, FurnitureSubType.DINING_TABLE, 4 );
createFurniture( dining, FurnitureType.CABINET, FurnitureSubType.SIDEBOARD );
createFurniture( kitchen, FurnitureType.CHAIR, FurnitureSubType.KITCHEN_CHAIR, 4 );
createFurniture( kitchen, FurnitureType.CHAIR, FurnitureSubType.KITCHEN_CHAIR, 4 );
createFurniture( kitchen, FurnitureType.CHAIR, FurnitureSubType.KITCHEN_CHAIR, 4 );
createFurniture( kitchen, FurnitureType.CHAIR, FurnitureSubType.KITCHEN_CHAIR, 4 );
createFurniture( kitchen, FurnitureType.TABLE, FurnitureSubType.KITCHEN_CHAIR, 4 );
createFurniture( kitchen, FurnitureType.APPLIANCE, FurnitureSubType.DISH_WASHER );
createFurniture( kitchen, FurnitureType.APPLIANCE, FurnitureSubType.OVEN );
createFurniture( kitchen, FurnitureType.APPLIANCE, FurnitureSubType.TUMBLE_DRIER );
createFurniture( kitchen, FurnitureType.APPLIANCE, FurnitureSubType.WASHING_MACHINE );
createFurniture( lounge, FurnitureType.CHAIR, FurnitureSubType.LOUNGE_CHAIR );
createFurniture( lounge, FurnitureType.CHAIR, FurnitureSubType.SOFA_CHAIR );
createFurniture( lounge, FurnitureType.TABLE, FurnitureSubType.COFFEE_TABLE );
createFurniture( bedroom, FurnitureType.BED, FurnitureSubType.DOUBLE_BED );
Furniture furniture = createFurniture( bedroom,
FurnitureType.CABINET, FurnitureSubType.BEDSIDE_CABINET );
furniture.setColour( "Brown" );
furnitureContext.getFurnitureSession().store( furniture );
return house;
}
public static Person createOwner( Address address ) throws Exception
{
Person person = furnitureContext.newPersonBean();
person.setAddress( address );
person.setTitle( "Mr" );
person.setFirstName( "Robin" );
person.setLastName( "Sharp" );
furnitureContext.getPersonSession().create( person );
return person;
}
protected static Room addRoom( House house, RoomType roomType,
double length, double width ) throws Exception
{
Room room = furnitureContext.newRoomBean();
room.setRoomType( roomType );
room.setLength( new Double( length ) );
room.setWidth( new Double( width ) );
house.addRoom( room );
return room;
}
protected static Furniture createFurniture( Room room, FurnitureType furnitureType,
FurnitureSubType furnitureSubType ) throws Exception
{
return createFurniture( room, furnitureType, furnitureSubType, 0 );
}
protected static Furniture createFurniture( Room room, FurnitureType furnitureType,
FurnitureSubType furnitureSubType, int legs ) throws Exception
{
//Do my Lord Lindley bit
Furniture furniture = furnitureContext.newFurnitureBean();
furniture.setFurnitureType(furnitureType);
furniture.setFurnitureSubType(furnitureSubType);
furniture.setRoom( room );
for( int index = 0 ; index < legs; index++ )
{
Leg leg = furnitureContext.newLegBean();
leg.setFurniture( furniture );
}
return furnitureContext.getFurnitureSession().create( furniture );
}
public static void moveHouse( Person person, House oldHouse, House newHouse ) throws Exception
{
//Move the furniture for each room type
moveFurniture( oldHouse, newHouse, RoomType.DINING );
moveFurniture( oldHouse, newHouse, RoomType.KITCHEN );
moveFurniture( oldHouse, newHouse, RoomType.LOUNGE );
moveFurniture( oldHouse, newHouse, RoomType.BEDROOM );
//Change address
person.setAddress( newHouse.getAddress() );
//Store the changes
furnitureContext.getPersonSession().store( person );
}
public static void moveFurniture( House oldHouse, House newHouse, RoomType roomType ) throws Exception
{
//Go and get the rooms (once of each type) ...
Enumeration oldRooms = furnitureContext.getRoomSession().findByHouseAndRoomType(
oldHouse.getPrimaryKey(), roomType );
Enumeration newRooms = furnitureContext.getRoomSession().findByHouseAndRoomType(
newHouse.getPrimaryKey(), roomType );
Room oldRoom = (Room)oldRooms.nextElement();
Room newRoom = (Room)newRooms.nextElement();
//Move the furniture
for( Enumeration enum = furnitureContext.getFurnitureSession().findByRoom(
oldRoom.getPrimaryKey() ); enum.hasMoreElements(); )
{
Furniture furniture = (Furniture)enum.nextElement();
oldRoom.removeFurniture( furniture );
newRoom.addFurniture( furniture );
}
//Store the changes
furnitureContext.getRoomSession().store( oldRoom );
furnitureContext.getRoomSession().store( newRoom );
}
public static void destroyHouse( House house ) throws Exception
{
//Does what it says on the cover
furnitureContext.getHouseSession().remove( house );
}
public static Room buildNursery( House house ) throws Exception
{
Room nursery = addRoom( house, RoomType.BEDROOM, 14, 14 );
furnitureContext.getRoomSession().create( nursery );
createFurniture( nursery, FurnitureType.BED, FurnitureSubType.COT_BED );
return nursery;
}
public static void paintNursery( Room nursery, String wallcovering ) throws Exception
{
nursery.setWallcovering( wallcovering );
furnitureContext.getRoomSession().store( nursery );
}
public static void writeToFile( String file, Bean bean ) throws IOException
{
FileOutputStream fos = new FileOutputStream( file );
ObjectOutputStream oos = new ObjectOutputStream( fos );
oos.writeObject( bean );
oos.close();
}
public static Bean readFromFile( String file ) throws IOException, ClassNotFoundException
{
FileInputStream fis = new FileInputStream( file );
ObjectInputStream ois = new ObjectInputStream( fis );
Object object = ois.readObject();
ois.close();
return (Bean)object;
}
}
|