/*
 * 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.service.sample;

import hu.astrid.debgen.patientmanagement.domain.sample.Sample;
import hu.astrid.debgen.patientmanagement.domain.sample.SampleType;
import hu.astrid.shared.server.service.BaseOperationsAbstract;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

/**
 * The Service, which is handling the patients
 * @author inagy
 */
public class SampleManagerService extends BaseOperationsAbstract {

    /**
     * Returns an instance of a PatientManagerService and sets its session.
     * @param session The session os the Service to use the DB connection.
     * @return A new instance.
     */
    public static BaseOperationsAbstract getInstance(Session session) {
        SampleManagerService sampleManagerService = new SampleManagerService();
        sampleManagerService.session = session;
        return sampleManagerService;
    }

    /**
     * Lists the SampleTypes ordered by the order parameter
     * @param order The name of the ordering attribute.
     * @return The list of SampleTypes
     */
    public List<SampleType> listSampleTypes(String order) {
        Criteria criteria = session.createCriteria(SampleType.class).addOrder(Order.asc(order));
        List list = criteria.list();
        ArrayList<SampleType> result = new ArrayList<SampleType>();
        for (Object o : list) {
            result.add((SampleType) o);
        }
        org.apache.log4j.Logger.getLogger(this.getClass()).debug("Fetched " + result.size() + " rows.");
        return result;
    }

    /**
     * Loads a SampleType object with the given id
     * @param id The id of the needed object
     * @return The object with the id
     */
    public SampleType loadSampleType(Long id) {
        return load(SampleType.class, id);
    }

    /**
     * Loads a Sample object with the given id
     * @param id The id of the needed object
     * @return The object with the id
     */
    public Sample loadSample(Long id) {
        return load(Sample.class, id);
    }

    /**
     * Deletes a SampleType objects row from the database
     * @param sampleType The SampleType which should be removed
     */
    public void delete(SampleType sampleType) {
        session.delete(sampleType);
    }

    /**
     * Deletes a Sample objects row from the database
     * @param sample The SampleType which should be removed
     */
    public void deleteSample(Sample sample) {
        session.delete(sample);
    }
}
