Package ch.tatool.app.service.impl

Source Code of ch.tatool.app.service.impl.TrialDAO

/*******************************************************************************
* Copyright (c) 2011 Michael Ruflin, Andr� Locher, Claudia von Bastian.
*
* This file is part of Tatool.
*
* Tatool is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tatool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tatool. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package ch.tatool.app.service.impl;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.tatool.app.data.ModuleImpl;
import ch.tatool.app.data.ModuleSessionImpl;
import ch.tatool.data.Module;
import ch.tatool.data.ModuleSession;
import ch.tatool.data.Trial;
import ch.tatool.element.Node;

/**
* Property access object for module objects.
*
* @author Michael Ruflin
*/
public class TrialDAO {

  private SessionFactory sessionFactory;

  Logger logger = LoggerFactory.getLogger(TrialDAO.class);

  /** Get a list of all sessions. */
  @SuppressWarnings("unchecked")
  public List<Trial> loadAllTrials(final ModuleImpl module) {
    String queryString = "select trial from TrialImpl trial where trial.session.module = :module";
    Query query = sessionFactory.getCurrentSession().createQuery(
        queryString);
    query.setParameter("module", module);
    return (List<Trial>) query.list();
  }

  /** Get all trials for a given session. */
  @SuppressWarnings("unchecked")
  public List<Trial> getTrials(final ModuleSession moduleSession) {
    sessionFactory.getCurrentSession().update(moduleSession);
    Query query = sessionFactory.getCurrentSession().createQuery("select trial from TrialImpl trial where trial.session = :session order by trial.id");
    query.setParameter("session", moduleSession);
    List<Trial> trials = (List<Trial>) query.list();
    // make sure we set the moduleSession correctly
    for (Trial t : trials) {
      t.setSession(moduleSession);
    }
    return trials;
  }

  /** Get all trials for a given session. */
  @SuppressWarnings("unchecked")
  public List<Trial> getTrials(final ModuleImpl module, final ModuleSession moduleSession, final String elementNameLike, final String propertyNameLike, final int offset, final int maxResults) {
    if (elementNameLike == null && propertyNameLike == null) {
      throw new RuntimeException(
          "Either elementName or propertyName needs to be non-null");
    }

    sessionFactory.getCurrentSession().update(module);
    if (moduleSession != null) {
      sessionFactory.getCurrentSession().update(moduleSession);
    }

    StringBuilder q = new StringBuilder();
    q.append("select distinct(trial) from TrialImpl trial ");
    q.append(" join trial.entriesImpl as entry where ");
    if (moduleSession != null) {
      q.append(" trial.session = :session ");
    } else {
      q.append(" trial.session.module = :module ");
    }
    if (elementNameLike != null) {
      q.append(" and entry.nodeId like :elementName");
    }
    if (propertyNameLike != null) {
      q.append(" and entry.name like :propName");
    }

    // sort reverse
    q.append(" order by trial.id DESC");

    // create the query and set the various parameters
    Query query = sessionFactory.getCurrentSession().createQuery(
        q.toString());
    if (moduleSession != null) {
      query.setParameter("session", moduleSession);
    } else {
      query.setParameter("module", module);
    }
    if (elementNameLike != null) {
      query.setParameter("elementName", elementNameLike);
    }
    if (propertyNameLike != null) {
      query.setParameter("propName", propertyNameLike);
    }
    // limit results if requested
    if (offset > 0) {
      query.setFirstResult(offset);
    }
    if (maxResults > -1) {
      query.setMaxResults(maxResults);
    }
   
    List<Trial> trials = (List<Trial>) query.list();

    // make sure we set the moduleSession correctly
    if (moduleSession != null) {
      for (Trial t : trials) {
        t.setSession(moduleSession);
      }
    }

    return trials;
  }

  /** Get the last trial index for a given session. */
  @SuppressWarnings("unchecked")
  public int getLastTrialIndex(final ModuleSession moduleSession) {
    Query query = sessionFactory
        .getCurrentSession()
        .createQuery(
            "select max(trial.index) from TrialImpl trial where trial.session = :session");
    query.setParameter("session", moduleSession);
    List<Integer> index = (List<Integer>) query.list();

    if (index != null && index.isEmpty()) {
      return 0;
    } else {
      return index.get(0);
    }
  }

  /** Persist a trial. */
  public void saveTrial(Module module, ModuleSession session, Trial trial) {
    ModuleSessionImpl sessionImpl = (ModuleSessionImpl) session;
    if (trial.getId() == null) {
      sessionFactory.getCurrentSession().update(session);
      trial.setSession(session);
      sessionImpl.getTrials().add(trial);
      trial.setIndex(sessionImpl.getTrials().indexOf(trial));
      sessionFactory.getCurrentSession().save(trial);
    } else {
      sessionFactory.getCurrentSession().update(trial);
    }
  }

  /** Delete a trial object. */
  public void deleteTrial(Module module, ModuleSession session, Trial trial) {
    Iterator<Trial> it = ((ModuleSessionImpl) session).getTrials()
        .iterator();
    while (it.hasNext()) {
      Trial t = it.next();
      if (t.getId().equals(trial.getId())) {
        it.remove();
      }
    }
    sessionFactory.getCurrentSession().delete(trial);
  }

  @SuppressWarnings("unchecked")
  public List<Trial> getTrials(ModuleSession session, Node node, int maxResults) {
    List<Trial> trials = null;

    DetachedCriteria criteria = DetachedCriteria.forClass(Trial.class);
    // limit the session
    if (session != null) {
      criteria.add(Property.forName("session").eq(session));
    }
    // limit the element
    if (node != null) {
      criteria.add(Property.forName("nodeId").eq(node.getId()));
    }
    // get the last trial as first
    criteria.addOrder(Order.desc("id"));

    // find the trials
    trials = (List<Trial>) criteria.getExecutableCriteria(sessionFactory.getCurrentSession()).setMaxResults(maxResults).list();

    return trials;
  }

  /**
   * Get all distinct trial property names for a module.
   *
   * @return An array with [0] containing the item name, [1] the property name
   */
  @SuppressWarnings("unchecked")
  public List<Object[]> findDistinctTrialPropertyNames(final ModuleImpl module) {
    StringBuilder queryString = new StringBuilder();
    queryString
        .append("Select distinct(entry.nodeId), entry.name")
        .append(" from TrialImpl trial left join trial.entriesImpl entry")
        .append(" where trial.session.module = :module")
        .append(" and entry.nodeId is not NULL")
        .append(" order by entry.nodeId, entry.name");

    // put together the complete query
    Query query = sessionFactory.getCurrentSession().createQuery(
        queryString.toString());
    query.setParameter("module", module);

    // and get the results
    return (List<Object[]>) query.list();
  }

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }
}
TOP

Related Classes of ch.tatool.app.service.impl.TrialDAO

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.