Package com.din.din.model.dao

Source Code of com.din.din.model.dao.QuantityTypeDAO

package com.din.din.model.dao;

import java.text.MessageFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.din.din.model.entities.Project;
import com.din.din.model.entities.QuantityType;
import com.din.din.model.entities.QuantityType.MeasurementType;
import com.din.din.model.util.EntityCachingManager;
import com.din.din.webapp.beans.util.BeanUtil;
import com.din.din.webapp.listeners.EMFListener;

public class QuantityTypeDAO extends GenericDAO {
 
  public static QuantityType getQuantityTypeByName(Project project, String name) {
    List<QuantityType> quantityTypes = getQuantityType(null, project, name);
    return quantityTypes != null && quantityTypes.size() > 0 ? quantityTypes.get(0) : null;
  }
 
  public static List<QuantityType> getQuantityTypeByProject(Project project) {
    return getQuantityType(null, project, null);   
  }
 
  public static List<QuantityType> getQuantityTypeByProject(EntityManager entityManager, Project project) {
    return getQuantityType(entityManager, project, null)
  }
 
  public static Map<String, QuantityType> getQuantityTypeMappedByName(Project project) {
    return getQuantityTypeMappedByName(null, project);
  }
 
  protected static Map<String, QuantityType> getQuantityTypeMappedByName(EntityManager entityManager, Project project) {
    List<QuantityType> quantityTypes = getQuantityType(null, project, null)
    Map<String, QuantityType> mappedQuantityTypes = new LinkedHashMap<String, QuantityType>();
    for(QuantityType type : quantityTypes) {
      mappedQuantityTypes.put(type.getName(), type);
    }
    return mappedQuantityTypes;
  }
 
  @SuppressWarnings("unchecked")
  protected static List<QuantityType> getQuantityType(EntityManager entityManager, Project project, String name) {
    boolean contained = false;
    if(entityManager == null) {
      entityManager = EMFListener.get();
      contained = true;
    }
   
    List<QuantityType> quantityTypes = null;
    EntityTransaction transaction = entityManager.getTransaction();
    try {
      if(!transaction.isActive()) {
        contained = true;
        transaction.begin();
      }
      String queryString = "select qt from {0} qt where project = :project";
      if(name != null) {
        queryString += " and name=:name";
      }
      Query q = entityManager.createQuery(MessageFormat.format(queryString, QuantityType.class.getName()));
      q.setParameter("project", project);
      if(name != null) {
        q.setParameter("name", name);
      }
     
      quantityTypes = (List<QuantityType>)q.getResultList();
     
      if(contained) {
        transaction.commit();   
      }
    } finally {
      if(contained) {
        // If transaction is active and we started it, it means we weren't able to commit it
        if(transaction.isActive()) {
          transaction.rollback();
        }
       
        entityManager.close();
      }
    }
    return quantityTypes;
  }
 
  public enum MeasurementSystem {
    Imperial,
    Metric
  }
 
  public static void createDefaultQuantityTypes(Project project, MeasurementSystem system, EntityCachingManager cache) {
    createDefaultQuantityTypes(null, project, system, cache);
  }
 
  protected static void createDefaultQuantityTypes(EntityManager entityManager, Project project, MeasurementSystem system, EntityCachingManager cache) {
    QuantityType type;
   
    boolean contained = false;
    if(entityManager == null) {
      contained = true;
      entityManager = EMFListener.get();
    }
    EntityTransaction transaction = entityManager.getTransaction();
    try {
      if(!transaction.isActive()) {
        contained = true;
        transaction.begin();
      }
      List<QuantityType> quantityTypes = getQuantityType(entityManager, project, null);
     
      if(quantityTypes == null || quantityTypes.size() == 0) {
        if(system == MeasurementSystem.Metric) {
          type = new QuantityType();
          type.setName("kg");
          type.setDescription(BeanUtil.getMessage("quantityType.kilograms"));
          type.setType(MeasurementType.Weight);
          type.setProject(project);
          save(entityManager, type, cache);
         
          type = new QuantityType();
          type.setName("hg");
          type.setDescription(BeanUtil.getMessage("quantityType.hectograms"));
          type.setType(MeasurementType.Weight);
          type.setProject(project);
          save(entityManager, type, cache);   
         
          type = new QuantityType();
          type.setName("g");
          type.setDescription(BeanUtil.getMessage("quantityType.grams"));
          type.setType(MeasurementType.Weight);
          type.setProject(project);
          save(entityManager, type, cache);
         
          type = new QuantityType();
          type.setName("L");
          type.setDescription(BeanUtil.getMessage("quantityType.liters"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache);
         
          type = new QuantityType();
          type.setName("mL");
          type.setDescription(BeanUtil.getMessage("quantityType.milliliters"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache)
        } else {
          type = new QuantityType();
          type.setName("oz.");
          type.setDescription(BeanUtil.getMessage("quantityType.ounces"));
          type.setType(MeasurementType.Weight);
          type.setProject(project);
          save(entityManager, type, cache)
         
          type = new QuantityType();
          type.setName("lbs.");
          type.setDescription(BeanUtil.getMessage("quantityType.pounds"));
          type.setType(MeasurementType.Weight);
          type.setProject(project);
          save(entityManager, type, cache)
         
          type = new QuantityType();
          type.setName("gal.");
          type.setDescription(BeanUtil.getMessage("quantityType.gallons"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache);
         
          type = new QuantityType();
          type.setName("qt.");
          type.setDescription(BeanUtil.getMessage("quantityType.quarts"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache);
         
          type = new QuantityType();
          type.setName("fl. oz.");
          type.setDescription(BeanUtil.getMessage("quantityType.fluidOunces"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache);   
         
          type = new QuantityType();
          type.setName("c");
          type.setDescription(BeanUtil.getMessage("quantityType.cups"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache)
         
          type = new QuantityType();
          type.setName("tsp.");
          type.setDescription(BeanUtil.getMessage("quantityType.teaspoons"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache)
         
          type = new QuantityType();
          type.setName("tbsp.");
          type.setDescription(BeanUtil.getMessage("quantityType.tablespoons"));
          type.setType(MeasurementType.Volume);
          type.setProject(project);
          save(entityManager, type, cache);   
        }
        type = new QuantityType();
        type.setName(BeanUtil.getMessage("quantityType.pieces"));
        type.setDescription(BeanUtil.getMessage("quantityType.pieces"));
        type.setType(MeasurementType.Quantity);
        type.setProject(project);
        type.setRemoveable(false);
        save(entityManager, type, cache);         
      }
      if(contained) {
        transaction.commit();
      }
    } finally {
      if(contained) {
        if(transaction.isActive()) {
          transaction.rollback();
        }
        entityManager.close();
      }
    }
  }
}
TOP

Related Classes of com.din.din.model.dao.QuantityTypeDAO

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.