org.openmrs.module.complexdatadb.api.db.hibernate.HibernateComplexDataToDBDAO.java Source code

Java tutorial

Introduction

Here is the source code for org.openmrs.module.complexdatadb.api.db.hibernate.HibernateComplexDataToDBDAO.java

Source

/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.complexdatadb.api.db.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.openmrs.module.complexdatadb.ComplexDataToDB;
import org.openmrs.module.complexdatadb.api.db.ComplexDataToDBDAO;

/**
 * It is a default implementation of  {@link ComplexDataToDBDAO}.
 */
public class HibernateComplexDataToDBDAO implements ComplexDataToDBDAO {
    protected final Log log = LogFactory.getLog(this.getClass());

    private SessionFactory sessionFactory;

    /**
      * @param sessionFactory the sessionFactory to set
      */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /**
      * @return the sessionFactory
      */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @see ComplexDataToDBDAO#saveComplexDataToDB()
     */
    @Override
    public void saveComplexDataToDB(ComplexDataToDB complexDataToDB) {
        sessionFactory.getCurrentSession().saveOrUpdate(complexDataToDB);

    }

    /**
      * @see ComplexDataToDBDAO#getComplexDataToDB()
      */
    @Override
    public ComplexDataToDB getComplexDataToDB(String uuid) {
        Session session = sessionFactory.getCurrentSession();
        SQLQuery query = session.createSQLQuery("select * from obs_complex_data where uuid = :uuid")
                .addEntity(ComplexDataToDB.class);
        ComplexDataToDB data = (ComplexDataToDB) query.setString("uuid", uuid).list().get(0);

        return data;
    }

    /**
     * @see ComplexDataToDBDAO#deleteComplexDataToDB(String)
     */
    @Override
    public void deleteComplexDataToDB(String uuid) {
        Session session = sessionFactory.getCurrentSession();

        Query q = session.createQuery("delete from obs_complex_data where uuid = :uuid");
        q.setString("uuid", uuid);
        q.executeUpdate();

    }
}