Package at.fhj.itm.business

Source Code of at.fhj.itm.business.DefaultServiceAssembler

package at.fhj.itm.business;

import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import at.fhj.itm.dao.MySqlDAOFactory;
import at.fhj.itm.dao.PointDAO;
import at.fhj.itm.dao.TripDAO;
import at.fhj.itm.dao.UserDAO;
import at.fhj.itm.dao.WaypointDAO;
import at.fhj.itm.util.EmailUtil;
import at.fhj.itm.util.EmailUtilImpl;
import at.fhj.itm.util.GoogleUtil;
import at.fhj.itm.util.GoogleUtilImpl;
import at.fhj.itm.util.JsfUtil;
import at.fhj.itm.util.JsfUtilImpl;
import at.fhj.itm.util.RandomUtil;
import at.fhj.itm.util.RandomUtilImpl;

/**
* The DefaultServiceAssembler is used in case the ServiceAssemler did not
* receive an other ServiceAssembler.
*
* The injected DAOs will be created by the {@link MySqlDAOFactory} and the
* Datasource will be retrieved from the Context (
* {@link #getDefaultDataSource()}).
*
* @author Christian Tassler, Gerhard Seuchter
*
*/
public class DefaultServiceAssembler extends ServiceAssembler {

  private GoogleUtil googleUtil = new GoogleUtilImpl();
  private RandomUtil randUtil = null;
  private EmailUtil mailUtil = null;

  @Override
  public ServiceTrip createServiceTrip() {
    try {
      DataSource ds = getDefaultDataSource();
      TripDAO tripDAO = MySqlDAOFactory.getInstance().getTripDAO();
      PointDAO pointDAO = MySqlDAOFactory.getInstance().getPointDAO();
      WaypointDAO waypointDAO = MySqlDAOFactory.getInstance()
          .getWaypointDAO();
      GoogleUtil googleUtil = createGoogleUtil();
      JsfUtil jsfUtil = createJsfUtil();

      ServiceTrip serviceTrip = new ServiceTripImpl(tripDAO, waypointDAO,
          pointDAO, ds, jsfUtil, googleUtil);

      return serviceTrip;
    } catch (NamingException ex) {
      throw new ServiceException(
          "Error while looking up data source in context.", ex);
    }

  }

  @Override
  public ServiceUser createServiceUser() {

    try {
      DataSource ds = getDefaultDataSource();
      UserDAO userDAO = MySqlDAOFactory.getInstance().getUserDAO();
      return new ServiceUserImpl(userDAO, ds, createJsfUtil(),
          createRandomUtil(), createEmailUtil());
    } catch (NamingException e) {
      throw new ServiceException(
          "Error while looking up data source in context.", e);
    }

  }

  @Override
  public JsfUtil createJsfUtil() {
    JsfUtil jsfUtil = (JsfUtil) FacesContext.getCurrentInstance()
        .getExternalContext().getSessionMap().get("jsfUtil");
    if (jsfUtil == null) {
      jsfUtil = new JsfUtilImpl();
      FacesContext.getCurrentInstance().getExternalContext()
          .getSessionMap().put("jsfUtil", jsfUtil);
    }
    return jsfUtil;
  }

  /**
   * A lookup will be used to retrieve the Datasource "jdbc/drivetogether_db"
   * from the Context. The context.xml is located in WebContent/META-INF/.
   *
   * @return DataSource retrieved from the Context.
   * @throws NamingException
   *             if the lookup fails.
   */
  private DataSource getDefaultDataSource() throws NamingException {
    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:/comp/env");
    return (DataSource) envContext.lookup("jdbc/drivetogether_db");
  }

  @Override
  public GoogleUtil createGoogleUtil() {
    return googleUtil;
  }

  @Override
  public RandomUtil createRandomUtil() {
    if (this.randUtil == null)
      this.randUtil = new RandomUtilImpl();

    return this.randUtil;
  }

  @Override
  public EmailUtil createEmailUtil() {
    if (this.mailUtil == null)
      this.mailUtil = new EmailUtilImpl();

    return this.mailUtil;
  }
}
TOP

Related Classes of at.fhj.itm.business.DefaultServiceAssembler

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.