com.model.manegers.ExchangeWS_maneger.java Source code

Java tutorial

Introduction

Here is the source code for com.model.manegers.ExchangeWS_maneger.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model.manegers;

import com.util.Agents;
import com.util.HibernateUtil;
import static com.util.HibernateUtil.getSessionFactory;
import com.ws.ExchangeWS;
import com.ws.ExchangeWS_Service;
import com.ws.Orders;
import com.ws.Products;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Khalil Sharkawi
 */
public class ExchangeWS_maneger {

    public static List<Orders> getOrders() {
        Session session = getSessionFactory().openSession();
        List<Orders> l = session.createQuery("FROM Orders").list();
        //        session.close();
        return l;
    }

    public static void AddOrder(Orders wsorder) {

        com.util.Orders myorder = new com.util.Orders();
        myorder.setProductname("masalco");
        myorder.setLoadamount(wsorder.getLoadamount());
        myorder.setTotalcost(wsorder.getTotalcost());

        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            session.save(myorder);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }

    }

    public static void DeleteOrder(int id) {

        Orders myObject;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            myObject = (Orders) session.load(Orders.class, new Integer((int) id));
            session.delete(myObject);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    public static void updateOrder(Orders pro) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.saveOrUpdate(pro);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public static Orders getByid(int id) {
        Orders myObject = null;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            //            trs = session.beginTransaction();
            myObject = (Orders) session.load(Orders.class, new Integer((int) id));
            //            session.delete(myObject);
            //            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                //                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
            return myObject;
        }
    }

    //web service call
    public static void initPurchaseOrder(Products selected, int amount) {
        ExchangeWS_Service service = new ExchangeWS_Service();
        Orders order;
        try {
            ExchangeWS port = service.getExchangeWSPort();
            order = port.purchaceOrder(selected, amount, "Daboul");
            AddOrder(order);
        } catch (Exception e) {
            //role back

        }

    }

    //web service call
    public static List<Products> getProducts() {
        ExchangeWS_Service service = new ExchangeWS_Service();
        ExchangeWS port = service.getExchangeWSPort();

        return port.getProducts("Daboul");
    }

}