001: /*
002: * Copyright (c) 2005 Your Corporation. All Rights Reserved.
003: */
004: package org.concern.library.generic;
005:
006: import org.concern.*;
007: import org.concern.controller.AbstractLoader;
008: import org.concern.controller.Sessions;
009: import org.hibernate.*;
010:
011: import java.io.Serializable;
012: import java.lang.reflect.Constructor;
013: import java.text.DateFormat;
014:
015: public class HibernateLoader extends AbstractLoader implements
016: Persister {
017: protected SessionFactory sessionFactory;
018:
019: private String entityClassName;
020: private String idClassName;
021:
022: public String getEntityClassName() {
023: return entityClassName;
024: }
025:
026: public void setEntityClassName(String entityClassName) {
027: this .entityClassName = entityClassName;
028: }
029:
030: public String getIdClassName() {
031: return idClassName;
032: }
033:
034: public void setIdClassName(String idClassName) {
035: this .idClassName = idClassName;
036: }
037:
038: protected Class getEntityClass() throws LoaderException {
039: try {
040: ClassLoader loader = Thread.currentThread()
041: .getContextClassLoader();
042: if (loader == null)
043: loader = getClass().getClassLoader();
044: return loader.loadClass(entityClassName);
045: } catch (ClassNotFoundException e) {
046: throw new LoaderException(e);
047: }
048: }
049:
050: protected Class getIdClass() throws LoaderException {
051: try {
052: ClassLoader loader = Thread.currentThread()
053: .getContextClassLoader();
054: if (loader == null)
055: loader = getClass().getClassLoader();
056: return loader.loadClass(idClassName);
057: } catch (ClassNotFoundException e) {
058: throw new LoaderException(e);
059: }
060: }
061:
062: public void setSessionFactory(SessionFactory sessionFactory) {
063: this .sessionFactory = sessionFactory;
064: }
065:
066: public String idOf(Object obj) throws SubjectNotFoundException,
067: LoaderException {
068: try {
069: Session session = sessionFactory.getCurrentSession();
070: if (!session.contains(obj)) {
071: session.lock(obj, LockMode.NONE);
072: }
073: return String.valueOf(session.getIdentifier(obj));
074: } catch (ObjectNotFoundException e) {
075: throw new SubjectNotFoundException(
076: "Error while getting id of " + obj, e);
077: } catch (HibernateException e) {
078: throw new LoaderException("Error while getting id of "
079: + obj, e);
080: }
081: }
082:
083: public Object load(String id) throws SubjectNotFoundException,
084: LoaderException {
085: Object obj = null;
086: try {
087: Session session = sessionFactory.getCurrentSession();
088: obj = session.get(getEntityClass(), typedId(id));
089: } catch (ObjectNotFoundException e) {
090: throw new SubjectNotFoundException("Subject with id " + id
091: + " was not found");
092: } catch (Exception ex) {
093: System.err.println("Entity class = " + getEntityClass());
094: System.err.println("Id class = " + getIdClass());
095: throw new LoaderException("Unable to load subject with id "
096: + id, ex);
097: }
098: if (obj == null) {
099: throw new SubjectNotFoundException("Subject with id " + id
100: + " was not found");
101: }
102: return obj;
103: }
104:
105: public void update(Object subject) throws PersisterException {
106: try {
107: Session session = sessionFactory.getCurrentSession();
108: session.update(subject);
109: } catch (Exception ex) {
110: throw new PersisterException("Unable to update subject "
111: + subject, ex);
112: }
113: }
114:
115: public String save(Object subject) throws PersisterException {
116: try {
117: Session session = sessionFactory.getCurrentSession();
118: return "" + session.save(subject);
119: } catch (Exception ex) {
120: throw new PersisterException("Unable to save subject "
121: + subject, ex);
122: }
123: }
124:
125: protected Serializable typedId(String id) throws LoaderException {
126: try {
127: Constructor constructor = getIdClass().getConstructor(
128: new Class[] { String.class });
129: return (Serializable) constructor
130: .newInstance(new Object[] { id });
131: } catch (Exception e) {
132: throw new LoaderException(e);
133: }
134: }
135:
136: /*
137: public Map formatSubjectLines(Object subject) {
138: BeanMeta beanMeta = StaticBeanMetaProvider.INSTANCE.getBeanMeta(subject.getClass());
139: Format format = beanMeta.getFormat();
140: Map map = new HashMap();
141: if (format != null) {
142: map.put("*", format.format(subject));
143: }
144: else {
145: map.put("*", subject.toString());
146: }
147: return map;
148: }
149: */
150: }
|