org.openmrs.cwf.api.util.OpenMRSUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.openmrs.cwf.api.util.OpenMRSUtil.java

Source

/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
 * If a copy of the MPL was not distributed with this file, You can obtain one at 
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.openmrs.cwf.api.util;

import org.carewebframework.api.spring.SpringUtil;

import org.hibernate.LockOptions;
import org.hibernate.Session;
import org.hibernate.Session.LockRequest;
import org.hibernate.SessionFactory;

/**
 * Static utility class for the OpenMRS extensions.
 * 
 * 
 */
public class OpenMRSUtil {

    public static void loadIfNecessary(Object domainObject) {
        Session session = null;

        try {
            session = getSessionFactory().getCurrentSession();

            if (!session.contains(domainObject)) {
                LockRequest lr = session.buildLockRequest(LockOptions.NONE);
                lr.lock(domainObject);
            }
        } catch (org.hibernate.NonUniqueObjectException exc) {
            session.merge(domainObject);
        } catch (org.hibernate.LazyInitializationException exc) {
            session.merge(domainObject);
        }
    }

    public static SessionFactory getSessionFactory() {
        return SpringUtil.getBean("sessionFactory", SessionFactory.class);
    }

    /**
     * Enforce static class.
     */
    private OpenMRSUtil() {
    };
}