Package ch.tatool.app.service.impl

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

/*******************************************************************************
* 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.Date;
import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

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

/**
* DataService implementation.
*
* @author Michael Ruflin
*/
public class DataServiceImpl implements DataService {

  private Messages messages;

  /**
   * Save the module instance. This will save all changed module properties
   */
  public void saveModule(Module module) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    // load the module data
    moduleImpl.getTransactionTemplate().execute(
        new TransactionCallbackWithoutResult() {
          public void doInTransactionWithoutResult(TransactionStatus status) {
            ModuleDAO moduleDAO = moduleImpl.getModuleDAO();
            moduleDAO.saveModule(moduleImpl);
          }
        });
  }

  // Session data

  /**
   * Creates a new module session. The module start date is set immediately
   */
  @SuppressWarnings("unchecked")
  public ModuleSession createSession(Module module) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    ModuleSession moduleSession = (ModuleSession) moduleImpl
        .getTransactionTemplate().execute(new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            ModuleSession moduleSession = new ModuleSessionImpl();
            moduleSession.setStartTime(new Date());
            moduleSession.setCompleted(0);
            moduleImpl.getSessionDAO().saveSession(moduleImpl, moduleSession);
            return moduleSession;
          }
        });
    return moduleSession;
  }

  /**
   * Save a session without finishing it.
   */
  public void saveSession(final ModuleSession moduleSession) {
    final ModuleImpl moduleImpl = (ModuleImpl) moduleSession.getModule();
    moduleImpl.getTransactionTemplate().execute(
        new TransactionCallbackWithoutResult() {
          public void doInTransactionWithoutResult(TransactionStatus status) {
            moduleImpl.getSessionDAO().saveSession(moduleImpl, moduleSession);
          }
        });
  }

  /** Finishes a module session. */
  public void finishSession(final ModuleSession moduleSession) {
    final ModuleImpl moduleImpl = (ModuleImpl) moduleSession.getModule();
    moduleImpl.getTransactionTemplate().execute(
        new TransactionCallbackWithoutResult() {
          public void doInTransactionWithoutResult(TransactionStatus status) {
            moduleSession.setEndTime(new Date());
            moduleImpl.getSessionDAO().saveSession(moduleImpl, moduleSession);
          }
        });
  }

  /**
   * Load all session in a given module.
   */
  @SuppressWarnings("unchecked")
  public List<ModuleSession> getSessions(Module module) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (List<ModuleSession>) moduleImpl.getTransactionTemplate()
        .execute(new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getSessionDAO().getSessions(moduleImpl);
          }
        });
  }

  // Trial data

  /** Inserts a trial object into the session. */
  public void insertTrial(final ModuleSession session, final Trial trial) {
    final ModuleImpl moduleImpl = (ModuleImpl) session.getModule();
    moduleImpl.getTransactionTemplate().execute(
        new TransactionCallbackWithoutResult() {
          public void doInTransactionWithoutResult(TransactionStatus status) {
            moduleImpl.getTrialDAO().saveTrial(moduleImpl, session, trial);
          }
        });
  }

  @SuppressWarnings("unchecked")
  public List<Trial> getTrials(final ModuleSession session) {
    final ModuleImpl moduleImpl = (ModuleImpl) session.getModule();
    return (List<Trial>) moduleImpl.getTransactionTemplate()
        .execute(new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getTrialDAO().getTrials(session);
          }
        });
  }

  /** Load all trials of a given module and session. */
  public List<Trial> loadAllTrials(Module module) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (List<Trial>) moduleImpl.getTransactionTemplate()
        .execute(new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getTrialDAO().loadAllTrials(moduleImpl);
          }
        });
  }

  /**
   * Returns a list of trial instances for a given element or session.
   *
   * @return list of trials
   */
  public List<Trial> getTrials(Module module, final ModuleSession session, final Node node, final int maxResults) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (List<Trial>) moduleImpl.getTransactionTemplate()
        .execute(new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getTrialDAO().getTrials(session, node, maxResults);
          }
        });
  }

  /**
   * Get the last session of a module.
   *
   * @param module
   * @return the last created session
   */
  public ModuleSession getLastSession(Module module) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (ModuleSession) moduleImpl.getTransactionTemplate()
        .execute(new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getSessionDAO().findLastSession(moduleImpl);
          }
        });
  }

  /**
   * Get the number of sessions in a module
   *
   * @param module
   *            the module to check
   * @param includeUnfinished
   *            whether to include unfinished module (modules without end
   *            time)
   * @return
   */
  @SuppressWarnings("unchecked")
  public long getSessionCount(Module module, final boolean includeUnfinished) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (Long) moduleImpl.getTransactionTemplate().execute(
        new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getSessionDAO().getSessionCount(moduleImpl, includeUnfinished);
          }
        });
  }

  /**
   * Find the last x trials with given property of a given element
   *
   */
  @SuppressWarnings("unchecked")
  public List<Trial> getTrials(final Module module, final ModuleSession session, final String elementNameLike, final String propertyNameLike, final int offset, final int maxResults) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (List<Trial>) moduleImpl.getTransactionTemplate().execute(
        new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getTrialDAO().getTrials(moduleImpl, session, elementNameLike, propertyNameLike, offset, maxResults);
          }
        });
  }

  /**
   * Get all distinct trial property names contained in a module
   *
   * @return a List of object arrays containing [0] the item name and [1] the
   *         property name
   */
  @SuppressWarnings("unchecked")
  public List<Object[]> findDistinctTrialPropertyNames(final Module module) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (List<Object[]>) moduleImpl.getTransactionTemplate().execute(
        new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getTrialDAO().findDistinctTrialPropertyNames(moduleImpl);
          }
        });
  }

  /**
   * Get all distinct session property names contained in a module
   *
   * @return a List of object arrays containing [0] the item name and [1] the
   *         property name
   */
  @SuppressWarnings("unchecked")
  public List<Object[]> findDistinctSessionPropertyNames(final Module module) {
    final ModuleImpl moduleImpl = (ModuleImpl) module;
    return (List<Object[]>) moduleImpl.getTransactionTemplate().execute(
        new TransactionCallback() {
          public Object doInTransaction(TransactionStatus status) {
            return moduleImpl.getSessionDAO().findDistinctSessionPropertyNames(moduleImpl);
          }
        });
  }

  public void setMessages(Messages messages) {
    this.messages = messages;
  }

  public Messages getMessages() {
    return messages;
  }
}
TOP

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

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.