Package com.vst.dao.hibernate

Source Code of com.vst.dao.hibernate.XmlGatewayDaoHibernate

package com.vst.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.vst.model.ConstructionType;
import com.vst.model.ext.Building;
import com.vst.model.ext.GeoClassificator;
import com.vst.model.ext.Organization;
import com.vst.model.ext.OrganizationType;

/**
* This class contains db actions for xml gateway;
*/
@SuppressWarnings("unchecked")
public class XmlGatewayDaoHibernate extends HibernateDaoSupport {
  protected final Log log = LogFactory.getLog(XmlGatewayDaoHibernate.class);

  /**
   * @return
   */
  public List<?> getSimpleList(Class<?> clazz) {
    if (clazz == null) {
      throw new IllegalArgumentException("Can't support null argument.");
    }
    return (List<?>) getHibernateTemplate().loadAll(clazz);
  }

  public List<?> getConstructionTypesList() {
    List<?> res = (List<?>) getHibernateTemplate().execute(
        new HibernateCallback() {

          public List<?> doInHibernate(Session arg0)
              throws HibernateException, SQLException {
            Query q = getSession().createQuery(
                "select c  from "
                    + ConstructionType.class.getName()
                    + " as c "
                    + " left join fetch c.material ");
            List<?> qlist = q.list();
            return qlist;
          }
        });
    return res;
  }

  public List<?> getCustomers() {
    List<?> res = (List<?>) getHibernateTemplate().execute(
        new HibernateCallback() {

          public List<?> doInHibernate(Session arg0)
              throws HibernateException, SQLException {
            // getSession().createQuery("select addr from Address as addr").list();
            Query q = getSession().createQuery(
                "select distinct  r  from "
                    + Organization.class.getName()
                    + " as r "
                    + " left join fetch r.address "
                    + " left join fetch r.contacts "
                    + " where r.orgType = :p1")
                .setParameter("p1", OrganizationType.CUSTOMER);
            List<?> qlist = q.list();

            return qlist;
          }
        });
    return res;
  }

  public Organization getCustomer(Integer customerId) {
    final Integer localId = customerId;
    List<?> qres = (List<?>) getHibernateTemplate().execute(
        new HibernateCallback() {

          public List<?> doInHibernate(Session arg0)
              throws HibernateException, SQLException {
            // getSession().createQuery("select addr from Address as addr").list();
            Query q = getSession()
                .createQuery(
                    "select distinct  r  from "
                        + Organization.class.getName()
                        + " as r "
                        + " left join fetch r.address "
                        + " left join fetch r.contacts "
                        + " where r.orgType = :p1 "
                        + " and r.id = :p2")
                .setParameter("p1", OrganizationType.CUSTOMER)
                .setParameter("p2", localId);
            List<?> qlist = q.list();

            return qlist;
          }
        });
    Organization res = null;
    if (!qres.isEmpty()) {
      res = (Organization) qres.get(0);
    }

    return res;
  }

  private String keysToStr(Integer[] keys) {
    String res = "";
    int idx = 0;
    for (Integer key : keys) {
      if (idx > 0) {
        res = res + ", ";
      }
      res = res + key;
      idx++;
    }
    return res;
  }

  public List<?> getSimpleList(Class<?> clazz, Integer[] keys) {
    // use only for entity extended from BaseAnnotatedOblect
    if (clazz == null) {
      throw new IllegalArgumentException("Can't support null argument.");
    }
    final Integer[] lkeys;
    final Class<?> lclazz = clazz;
    if (keys == null || keys.length == 0) {
      lkeys = new Integer[] { -1 };
    } else {
      lkeys = keys;
    }
    List<?> res = (List<?>) getHibernateTemplate().execute(
        new HibernateCallback() {

          public List<?> doInHibernate(Session arg0)
              throws HibernateException, SQLException {
            Query q = getSession().createQuery(
                "select p  from " + lclazz.getName() + " as p "
                    + " where p.id in (" + keysToStr(lkeys)
                    + ")");
            List<?> qlist = q.list();

            return qlist;
          }
        });
    return res;
  }

  public <T> T getEntity(Class<T> clazz, Integer key) {
    // use only for entity extended from BaseAnnotatedOblect
    if (clazz == null) {
      throw new IllegalArgumentException("Can't support null argument.");
    }
    final Integer lkey;
    final Class<?> lclazz = clazz;
    if (key == null) {
      lkey = -1;
    } else {
      lkey = key;
    }
    T res = (T) getHibernateTemplate().execute(new HibernateCallback() {

      public T doInHibernate(Session arg0) throws HibernateException,
          SQLException {
        T o = (T) getSession().get(lclazz, lkey);

        return o;
      }
    });
    return res;
  }

  public List<GeoClassificator> getGeoListForAncestor(
      Class<? extends GeoClassificator> clazz, GeoClassificator parent) {
    final Class<?> lclazz = clazz;
    final GeoClassificator lparent = parent;
    List<GeoClassificator> res = (List<GeoClassificator>) getHibernateTemplate()
        .execute(new HibernateCallback() {

          public List<GeoClassificator> doInHibernate(Session arg0)
              throws HibernateException, SQLException {
            Query q = getSession().createQuery(
                "select p  from " + lclazz.getName() + " as p "
                    + " where (select n from  "
                    + GeoClassificator.class.getName()
                    + " as n where n.id=" + lparent.getId()
                    + ") in elements (p.ancestors)");
            List<GeoClassificator> qlist = q.list();

            return qlist;
          }
        });
    return res;
  }

  public Building getBuilding(Integer buildingKey) {
    final Integer localId = buildingKey;
    List<?> qres = (List<?>) getHibernateTemplate().execute(
        new HibernateCallback() {

          public Object doInHibernate(Session arg0)
              throws HibernateException, SQLException {
            // getSession().createQuery("select addr from Address as addr").list();
            Query q = getSession().createQuery(
                "select distinct  r  from "
                    + Building.class.getName() + " as r "
                    + " left join fetch r.address "
                    + " where r.id = :p2").setParameter(
                "p2", localId);
            List<?> qlist = q.list();

            return qlist;
          }
        });
    Building res = null;
    if (!qres.isEmpty()) {
      res = (Building) qres.get(0);
    }

    return res;
  }

}
TOP

Related Classes of com.vst.dao.hibernate.XmlGatewayDaoHibernate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.