HibernateLoader.java :  » Workflow-Engines » osbl-1_0 » org » concern » library » generic » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » concern » library » generic » HibernateLoader.java
/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package org.concern.library.generic;


import org.concern.*;
import org.concern.controller.AbstractLoader;
import org.concern.controller.Sessions;
import org.hibernate.*;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.text.DateFormat;


public class HibernateLoader
    extends AbstractLoader
    implements Persister
{
    protected SessionFactory sessionFactory;

    private String entityClassName;
    private String idClassName;

    public String getEntityClassName() {
        return entityClassName;
    }

    public void setEntityClassName(String entityClassName) {
        this.entityClassName = entityClassName;
    }

    public String getIdClassName() {
        return idClassName;
    }

    public void setIdClassName(String idClassName) {
        this.idClassName = idClassName;
    }

    protected Class getEntityClass() throws LoaderException {
        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            if (loader == null)
                loader = getClass().getClassLoader();
            return loader.loadClass(entityClassName);
        }
        catch (ClassNotFoundException e) {
            throw new LoaderException(e);
        }
    }

    protected Class getIdClass() throws LoaderException {
        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            if (loader == null)
                loader = getClass().getClassLoader();
            return loader.loadClass(idClassName);
        }
        catch (ClassNotFoundException e) {
            throw new LoaderException(e);
        }
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public String idOf(Object obj) throws SubjectNotFoundException, LoaderException {
        try {
            Session session = sessionFactory.getCurrentSession();
            if (!session.contains(obj)) {
                session.lock(obj, LockMode.NONE);
            }
            return String.valueOf(session.getIdentifier(obj));
        }
        catch (ObjectNotFoundException e) {
            throw new SubjectNotFoundException("Error while getting id of " + obj, e);
        }
        catch (HibernateException e) {
            throw new LoaderException("Error while getting id of " + obj, e);
        }
    }

    public Object load(String id) throws SubjectNotFoundException, LoaderException {
        Object obj = null;
        try {
            Session session = sessionFactory.getCurrentSession();
            obj = session.get(getEntityClass(), typedId(id));
        }
        catch (ObjectNotFoundException e) {
            throw new SubjectNotFoundException("Subject with id " + id + " was not found");
        }
        catch (Exception ex) {
            System.err.println("Entity class = " + getEntityClass());
            System.err.println("Id class = " + getIdClass());
            throw new LoaderException("Unable to load subject with id " + id, ex);
        }
        if (obj == null) {
            throw new SubjectNotFoundException("Subject with id " + id + " was not found");
        }
        return obj;
    }

    public void update(Object subject) throws PersisterException {
        try {
            Session session = sessionFactory.getCurrentSession();
            session.update(subject);
        }
        catch (Exception ex) {
            throw new PersisterException("Unable to update subject " + subject, ex);
        }
    }

    public String save(Object subject) throws PersisterException {
        try {
            Session session = sessionFactory.getCurrentSession();
            return "" + session.save(subject);
        }
        catch (Exception ex) {
            throw new PersisterException("Unable to save subject " + subject, ex);
        }
    }

    protected Serializable typedId(String id) throws LoaderException {
        try {
            Constructor constructor = getIdClass().getConstructor(new Class[] { String.class });
            return (Serializable)constructor.newInstance(new Object[] { id });
        }
        catch (Exception e) {
            throw new LoaderException(e);
        }
    }

    /*
    public Map formatSubjectLines(Object subject) {
        BeanMeta beanMeta = StaticBeanMetaProvider.INSTANCE.getBeanMeta(subject.getClass());
        Format format = beanMeta.getFormat();
        Map map = new HashMap();
        if (format != null) {
            map.put("*", format.format(subject));
        }
        else {
            map.put("*", subject.toString());
        }
        return map;
    }
    */
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.