Monday, October 13, 2014

HIS Hibernate


  • Ward.java
    public class Ward {
    private String wardNo;
    private String category;
    private String wardGender;
   
    private Set<Bed> bedsSet = new HashSet<Bed>();
    private Set<InternalTransfer> internalTransferSet = new HashSet<InternalTransfer>();
   
    public Ward() {
       
       
    }
   
   
    public Set<InternalTransfer> getInternalTransferSet() {
        return internalTransferSet;
    }

    public void setInternalTransferSet(Set<InternalTransfer> internalTransferSet) {
        this.internalTransferSet = internalTransferSet;
    }

    /**
     * @return the bedsSet
     */
    public Set<Bed> getBedsSet() {
        return bedsSet;
    }

    /**
     * @param bedsSet the bedsSet to set
     */
    public void setBedsSet(Set<Bed> bedsSet) {
        this.bedsSet = bedsSet;
    }

    /**
     * @return the wardNo
     */
    public String getWardNo() {
        return wardNo;
    }
    /**
     * @param wardNo the wardNo to set
     */
    public void setWardNo(String wardNo) {
        this.wardNo = wardNo;
    }
    /**
     * @return the category
     */
    public String getCategory() {
        return category;
    }
    /**
     * @param category the category to set
     */
    public void setCategory(String category) {
        this.category = category;
    }
    /**
     * @return the wardGender
     */
    public String getWardGender() {
        return wardGender;
    }
    /**
     * @param wardGender the wardGender to set
     */
    public void setWardGender(String wardGender) {
        this.wardGender = wardGender;
    } 
}

  • Bed.java
package core.classes.inward.admin;

import core.classes.inward.WardAdmission.Inpatient;



public class Bed {
   
    private int bedID;
    private int bedNo;
    private String bedType;
    private Ward wardNo;
    private String availability;
    private Inpatient patientID;
   
    public Bed(){}

   
   
    /**
     * @return the bedID
     */
    public int getBedID() {
        return bedID;
    }



    /**
     * @param bedID the bedID to set
     */
    public void setBedID(int bedID) {
        this.bedID = bedID;
    }



    /**
     * @return the bedNo
     */
    public int getBedNo() {
        return bedNo;
    }

    /**
     * @param bedNo the bedNo to set
     */
    public void setBedNo(int bedNo) {
        this.bedNo = bedNo;
    }

    /**
     * @return the bedType
     */
    public String getBedType() {
        return bedType;
    }

    /**
     * @param bedType the bedType to set
     */
    public void setBedType(String bedType) {
        this.bedType = bedType;
    }



    /**
     * @return the wardNo
     */
    public Ward getWardNo() {
        return wardNo;
    }



    /**
     * @param wardNo the wardNo to set
     */
    public void setWardNo(Ward wardNo) {
        this.wardNo = wardNo;
    }





    public String getAvailability() {
        return availability;
    }



    public void setAvailability(String availability) {
        this.availability = availability;
    }



    /**
     * @return the patientID
     */
    public Inpatient getPatientID() {
        return patientID;
    }



    /**
     * @param patientID the patientID to set
     */
    public void setPatientID(Inpatient patientID) {
        this.patientID = patientID;
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<hibernate-mapping>
    <class name="core.classes.inward.admin.Ward" table="ward_wards" catalog="his"  lazy="false" >
        <id name="wardNo" type="string">
            <column name="ward_no" />
            <generator class="assigned" />
        </id>            
        <property name="category" type="string">
            <column name="category" length="100" not-null="false" />
        </property>
        <property name="wardGender" type="string">
            <column name="ward_gender"   not-null="false" />
        </property>

        <set name="bedsSet" table="ward_beds" inverse="true" lazy="true" fetch="select" >
            <key>
                <column name="ward_no" not-null="true"/>
            </key>
            <one-to-many class="core.classes.inward.admin.Bed" />
        </set>
       
        <set name="internalTransferSet" table="ward_internaltransfer" inverse="true" lazy="true" fetch="select" >
            <key>
                <column name="transfer_ward" not-null="true"/>
            </key>
            <one-to-many class="core.classes.inward.transfer.InternalTransfer" />
        </set>                       
    </class>
</hibernate-mapping>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<hibernate-mapping>
    <class name="core.classes.inward.admin.Bed" table="ward_beds"  catalog="his"  lazy="false" >
        <id name="bedID" type="java.lang.Integer">
            <column name="bed_id" />
            <generator class="native" />
        </id>
        <property name="bedNo" type="java.lang.Integer">
            <column name="bed_no" />
        </property>
        <property name="bedType" type="string">
            <column name="bed_type" />
        </property>
       
         <many-to-one name="wardNo" class="core.classes.inward.admin.Ward" fetch="join">
            <column name="ward_no" />
        </many-to-one>
       
        <property name="availability" type="string">
            <column name="availability" />
        </property>
       
        <many-to-one name="patientID" class="core.classes.inward.WardAdmission.Inpatient" fetch="join">
            <column name="patient_id" />
        </many-to-one>
    </class>
</hibernate-mapping>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  • Ward DB drive
public class WardDBDriver {
    Session session = SessionFactoryUtil.getSessionFactory().openSession();
   
    public boolean insertWard(Ward ward) {

        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(ward);
            tx.commit();
            return true;
        } catch (RuntimeException ex) {
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException he) {
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return false;
        }

    }
      
    public List<Ward> getWardList() {
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("select w from Ward as w");
            @SuppressWarnings("unchecked")
            List<Ward> wardlist = query.list();
            tx.commit();
            return wardlist;
        } catch (RuntimeException ex) {
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException he) {
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return null;
        }
    }
   
   
    public boolean deleteWard(Ward ward)
    {
        Transaction tx=null;
      
    try {
          
            tx=session.beginTransaction();
            session.delete(ward);
            tx.commit();
            return true;  
        }
   
    catch (RuntimeException ex) {
        if(tx != null && tx.isActive()){
            try{
                tx.rollback();
            }catch(HibernateException he){
                System.err.println("Error rolling back transaction");
            }
            throw ex;
        }
        return false;
    }
      
    }
   
   
    public List<Ward> getWardDetailsByWardNo(String wardNo){
        Transaction tx = null;
      
        try{
            tx = session.beginTransaction();
            Query query =  session.createQuery("select u from Ward as u where u.wardNo=:wardNo");
            query.setParameter("wardNo", wardNo);
            List<Ward> wardList =CastList.castList(Ward.class, query.list());
            tx.commit();
            return wardList;
          
        }
        catch(RuntimeException ex){
            if(tx != null && tx.isActive())
            {
                try{
                    tx.rollback();
                }catch(HibernateException he){
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return null;
        }
    }
   
    public boolean updateUserDetails(Ward wardObj){
            Transaction tx=null;
          
            try{
                tx=session.beginTransaction();
          
                session.update(wardObj);
                tx.commit();
                return true;
            }
          
            catch (RuntimeException ex) {
                if(tx != null && tx.isActive()){
                    try{
                        tx.rollback();
                    }catch(HibernateException he){
                        System.err.println("Error rolling back transaction");
                    }
                    throw ex;
                }
                return false;
            }
          
        }
   

}
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


  • Bed DB drive
package lib.driver.inward.driver_class.admin;

import java.util.List;

import lib.SessionFactoryUtil;
import lib.classes.CasttingMethods.CastList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;



import core.classes.inward.WardAdmission.Inpatient;
import core.classes.inward.admin.Bed;
import core.classes.inward.admin.Ward;





public class BedDBDriver {
   
    Session session = SessionFactoryUtil.getSessionFactory().openSession();
   
    public boolean insertBed(Bed bed,String wardno) {

        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Ward ward = (Ward) session.get(Ward.class, wardno);
            bed.setWardNo(ward);
            session.save(bed);
            tx.commit();
            return true;
        } catch (RuntimeException ex) {
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException he) {
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return false;
        }

    }
   
    public List<Bed> getAllBedByWardNo(String wardNo){
        Transaction tx = null;
       
        try{
            tx = session.beginTransaction();
            Query query =  session.createQuery("select u from Bed as u where u.wardNo.wardNo=:wardNo");
            query.setParameter("wardNo", wardNo);
            List<Bed> bedList =CastList.castList(Bed.class, query.list());
            tx.commit();
            return bedList;
           
        }
        catch(RuntimeException ex){
            if(tx != null && tx.isActive())
            {
                try{
                    tx.rollback();
                }catch(HibernateException he){
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return null;
        }
    }
   
   
    public boolean deleteBed(Bed bed)
    {
        Transaction tx=null;
       
    try {
           
            tx=session.beginTransaction();
            session.delete(bed);
            tx.commit();
            return true;   
        }
   
    catch (RuntimeException ex) {
        if(tx != null && tx.isActive()){
            try{
                tx.rollback();
            }catch(HibernateException he){
                System.err.println("Error rolling back transaction");
            }
            throw ex;
        }
        return false;
    }
       
    }
   
   
    public boolean updateBed(Bed bedObj,String wardNo,int pid){
        Transaction tx=null;
       
        try{
            tx=session.beginTransaction();
            Ward ward = (Ward) session.get(Ward.class, wardNo);
            bedObj.setWardNo(ward);
           
            if(pid==0){
               
                bedObj.setPatientID(null);
            }else{
                Inpatient p = (Inpatient) session.get(Inpatient.class, pid);
                bedObj.setPatientID(p);
            }
           
           
            session.update(bedObj);
           
           
       
            tx.commit();
            return true;
        }
       
        catch (RuntimeException ex) {
            if(tx != null && tx.isActive()){
                try{
                    tx.rollback();
                }catch(HibernateException he){
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return false;
        }
       
    }
   
   
    public List<Bed> geBedByWardNoAndBedNo(String wardNo,int bedNo){
        Transaction tx = null;
       
        try{
            tx = session.beginTransaction();
            Query query =  session.createQuery("select u from Bed as u where u.wardNo.wardNo=:wardNo and u.bedNo=:bedNo");
            query.setParameter("wardNo", wardNo);
            query.setParameter("bedNo", bedNo);
            List<Bed> bedList =CastList.castList(Bed.class, query.list());
            tx.commit();
            return bedList;
           
        }
        catch(RuntimeException ex){
            if(tx != null && tx.isActive())
            {
                try{
                    tx.rollback();
                }catch(HibernateException he){
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return null;
        }
    }
   
    public List<Bed> getFreeBedByWardNo(String wardNo){
        Transaction tx = null;
       
        try{
            tx = session.beginTransaction();
            Query query =  session.createQuery("select u from Bed as u where u.wardNo.wardNo=:wardNo and u.availability=:free");
            query.setParameter("wardNo", wardNo);
            query.setParameter("free", "free");
            List<Bed> bedList =CastList.castList(Bed.class, query.list());
            tx.commit();
            return bedList;
           
        }
        catch(RuntimeException ex){
            if(tx != null && tx.isActive())
            {
                try{
                    tx.rollback();
                }catch(HibernateException he){
                    System.err.println("Error rolling back transaction");
                }
                throw ex;
            }
            return null;
        }
    }
   
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////Ward Resourece

package core.resources.inward.admin;


import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import core.classes.inward.admin.Ward;
import flexjson.JSONSerializer;
import lib.driver.inward.driver_class.admin.WardDBDriver;

@Path("Ward")
public class WardResource {
   
    WardDBDriver warddbdriver = new WardDBDriver();
   
    @POST
    @Path("/addWard")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String addWard(JSONObject wJson)
    {
       
        try {
            Ward ward  =  new Ward();
            ward.setWardNo(wJson.getString("wardNo").toString());
            ward.setCategory(wJson.getString("category").toString());   
            ward.setWardGender(wJson.getString("wardGender"));
            //ward.setNoOfBed(wJson.getInt("noOfBed"));
                       
            warddbdriver.insertWard(ward);
                        
            return "true";
        } catch (Exception e) {
             System.out.println(e.getMessage());
           
            return e.getMessage().toString();
        }

    }
   
   
    @GET
    @Path("/getWard")
    @Produces(MediaType.APPLICATION_JSON)
    public String getWard()
    {
        List<Ward> wardList =warddbdriver.getWardList();
        JSONSerializer serializer = new JSONSerializer();
        return  serializer.serialize(wardList);

    }
   

   
    @DELETE
    @Path("/deleteWard")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON)
    public  String deleteWard(JSONObject jsnObj){
        String result="false";
        boolean r=false;
        Ward ward=new Ward();
       
       
        try{
           
           
            ward.setWardNo(jsnObj.getString("wardNo"));           
                       
            r=warddbdriver.deleteWard(ward);
            result=String.valueOf(r);
           
            return result;
           
           
        }
       
        catch( JSONException ex){
            ex.printStackTrace();   
            return result;
        }
       
       
    }
   
   
    @GET
    @Path("/getWardByWardNo/{wardNo}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getWardByWardNo(@PathParam("wardNo")  String wardNo){
        String result="";
         List<Ward> wardlist =warddbdriver.getWardDetailsByWardNo(wardNo);
         JSONSerializer serializor=new JSONSerializer();
         result= serializor.serialize(wardlist);
         return result;
   
       
    }
   
   
    @PUT
    @Path("/updateWard")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON)
    public  String updateWardDetails(JSONObject wJson){
       
        String result="false";
        boolean r=false;
         Ward ward=new Ward();
         System.out.println(ward.toString());
        try{
           
            ward.setWardNo(wJson.getString("wardNo").toString());
            ward.setCategory(wJson.getString("category").toString());   
            ward.setWardGender(wJson.getString("wardGender"));
            //ward.setNoOfBed(wJson.getInt("noOfBed"));
            r=warddbdriver.updateUserDetails(ward);
            result=String.valueOf(r);
            return result;
           
        }
        catch( JSONException ex){
            ex.printStackTrace();   
            return result;
        }
       
        catch( Exception ex){
            ex.printStackTrace();
            return ex.getMessage();
        }

    }
   
   
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////bed resourece


package core.resources.inward.admin;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import core.classes.inward.admin.Bed;
import flexjson.JSONSerializer;
import lib.driver.inward.driver_class.admin.BedDBDriver;

@Path("Bed")
public class BedResource {
   
    BedDBDriver beddriver=new BedDBDriver();
   
    @POST
    @Path("/addBed")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String addBed(JSONObject wJson)
    {
       
        try {
            Bed bed  =  new Bed();
            bed.setBedNo(wJson.getInt("bedNo"));
            bed.setBedType(wJson.getString("bedType"));
            //bed.setWardNo(wJson.getString("wardNo"));
            String wardno=wJson.getString("wardNo").toString();
            bed.setAvailability(wJson.getString("availability"));
            bed.setPatientID(null);
                       
            beddriver.insertBed(bed,wardno);
                        
            return "true";
        } catch (Exception e) {
             System.out.println(e.getMessage());
           
            return e.getMessage().toString();
        }

    }
   
   
    @GET
    @Path("/getAllBedByWardNo/{wardNo}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllBedByWardNo(@PathParam("wardNo")  String wardNo){
        //String result="";
        try{
         List<Bed> bedlist =beddriver.getAllBedByWardNo(wardNo);
         JSONSerializer serializor=new JSONSerializer();
         //result= serializor.serialize(bedlist);
         return serializor.include("Ward.wardNo").exclude("*.class","Ward.*").serialize(bedlist);
        }catch(Exception e)
        {
            return e.getMessage().toString();
        }
   
       
    }
   
    @DELETE
    @Path("/deleteBed")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON)
    public  String deleteBed(JSONObject jsnObj){
        String result="false";
        boolean r=false;
        Bed bed=new Bed();
       
       
        try{
           
           
            bed.setBedID(jsnObj.getInt("bedID"));           
                       
            r=beddriver.deleteBed(bed);
            result=String.valueOf(r);
           
            return result;
           
           
        }
       
        catch( JSONException ex){
            ex.printStackTrace();   
            return result;
        }
       
       
    }
   
   

    @PUT
    @Path("/updateBed")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON)
    public  String updateBed(JSONObject wJson){
       
        String result="false";
        boolean r=false;
         Bed bed=new Bed();
         System.out.println(bed.toString());
        try{
            bed.setBedID(wJson.getInt("bedID"));
            bed.setBedNo(wJson.getInt("bedNo"));
            //bed.setWardNo(wJson.getString("wardNo").toString());
            String wardno=wJson.getString("wardNo").toString();
            bed.setBedType(wJson.getString("bedType").toString());   
            bed.setAvailability(wJson.getString("availability"));
           
            int pid=wJson.getInt("patientID");
           
           
            r=beddriver.updateBed(bed,wardno,pid);
            result=String.valueOf(r);
            return result;
           
        }
        catch( JSONException ex){
            ex.printStackTrace();   
            //return result;
            return ex.getMessage();
        }
       
        catch( Exception ex){
            ex.printStackTrace();
            return ex.getMessage();
        }

    }
   
   
    @GET
    @Path("/geBedByWardNoAndBedNo/{wardNo}/{bedNo}")
    @Produces(MediaType.APPLICATION_JSON)
    public String geBedByWardNoAndBedNo(@PathParam("wardNo")  String wardNo,@PathParam("bedNo")  int bedNo){
        //String result="";
               
        try{
               
           
         List<Bed> bedlist =beddriver.geBedByWardNoAndBedNo(wardNo,bedNo);
         JSONSerializer serializor=new JSONSerializer();
        // result= serializor.serialize(bedlist);
         return serializor.include("Ward.wardNo").exclude("*.class","Ward.*").serialize(bedlist);
        }catch(Exception e)
        {
            return e.getMessage().toString();
        }
   
       
    }
   
   
    @GET
    @Path("/getFreeBedByWardNo/{wardNo}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getFreeBedByWardNo(@PathParam("wardNo")  String wardNo){
        //String result="";
               
        try{
               
           
         List<Bed> bedlist =beddriver.getFreeBedByWardNo(wardNo);
         JSONSerializer serializor=new JSONSerializer();
         return serializor.include("Ward.wardNo").exclude("*.class","Ward.*").serialize(bedlist);
        }catch(Exception e)
        {
            return e.getMessage().toString();
        }
   
       
    }

}

No comments:

Post a Comment