/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.patientmanagement.facade.sample;

import hu.astrid.debgen.patientmanagement.domain.sample.Sample;
import hu.astrid.debgen.patientmanagement.domain.sample.SampleType;
import hu.astrid.debgen.patientmanagement.service.sample.SampleManagerService;
import hu.astrid.toucan.control.persistence.DatabaseServiceClass;
import java.util.List;
import org.hibernate.Session;

/**
 * The facade, which is handling the patients
 * @author inagy
 */
public class SampleManagerFacade extends DatabaseServiceClass {

    /**
     * Gets a new SampleManagerService using a Session
     * @param session The session
     * @return the new service
     */
    private SampleManagerService getService(Session session) {
        SampleManagerService service = (SampleManagerService) SampleManagerService.getInstance(session);
        return service;
    }

    // <editor-fold defaultstate="collapsed" desc="SampleType">
    /**
     * Starts a session, and creates or modifies the given sampleTypes DB record
     * @param sampleType The new sampleType
     * @throws Exception When DB fails
     */
    public void createOrModifySampleType(SampleType sampleType) throws Exception {
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            SampleManagerService service = getService(session);
            service.create(sampleType);
            session.getTransaction().commit();
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }

    /**
     * Loads the sampleType by the given id
     * @param id The id of the sampleType
     * @return The sampleType object with the specified id (if there is such thing)
     * @throws Exception When DB fails
     */
    public SampleType loadSampleType(Long id) throws Exception {
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            SampleManagerService service = getService(session);
            SampleType sampleType = service.loadSampleType(id);
            session.getTransaction().commit();
            return sampleType;
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }

    /**
     * Deletes the sampleType by the given id
     * @param id The id of the sampleType
     * @throws Exception When DB fails
     */
    public void deleteSampleType(Long id) throws Exception {
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            SampleManagerService service = getService(session);
            SampleType sampleType = service.loadSampleType(id);
            service.delete(sampleType);
            session.getTransaction().commit();
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }

    /**
     * Returns a list of sampleType
     * @param order The ordering attribute to order the results ascending
     * @return The list of sampleType records.
     * @throws Exception When DB fails.
     */
    public List<SampleType> listSampleTypes(String order) throws Exception {
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            SampleManagerService service = getService(session);
            List<SampleType> found = service.listSampleTypes(order);
            session.getTransaction().commit();
            return found;
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }// </editor-fold>

    /**
     * Loads the sample by the given id
     * @param id The id of the sample
     * @return The sample object with the specified id (if there is such thing)
     * @throws Exception When DB fails
     */
    public Sample loadSample(Long id) throws Exception {
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            SampleManagerService service = getService(session);
            Sample sample = service.loadSample(id);
            session.getTransaction().commit();
            return sample;
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }
}
