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

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * Abstract class of base operations
 * @author inagy
 */
public abstract class BaseOperationsAbstract {

    /**
     * The session
     */
    protected Session session = null;

    /**
     * Should return a new service instance, but THIS RETURNS NULL
     * @param session The session
     * @return NULL
     */
    public static BaseOperationsAbstract getInstance(Session session) {
        return null;
    }

    public <T> T load(Class<T> clazz, Long id) {
        return (T) this.session.createCriteria(clazz).add(Restrictions.eq("id", id)).uniqueResult();
    }

    public List list(Class clazz, String order) {
        return this.session.createCriteria(clazz).addOrder(Order.asc(order)).list();
    }

    public void create(Object entry) {
        session.saveOrUpdate(entry);
    }

    public void refresh(Object object) {
        session.refresh(object);
    }

}
