01: package org.osbl;
02:
03: import org.osbl.persistence.*;
04: import org.osbl.persistence.model.Entity;
05: import org.conform.hibernate.HibernateEnvironment;
06: import org.hibernate.Hibernate;
07:
08: import java.util.*;
09:
10: public abstract class PersistenceObjectLocator<O extends Entity>
11: implements ObjectLocator<O> {
12: private Persistence persistence;
13:
14: public Persistence getCommandFactory() {
15: if (persistence == null)
16: persistence = (Persistence) ServiceProvider.getInstance()
17: .getService(getPersistenceServiceName());
18: return persistence;
19: }
20:
21: protected abstract String getPersistenceServiceName();
22:
23: protected abstract Class getObjectClass();
24:
25: public O objectByQuery(Map<String, String> query) {
26: String key = query.get("key");
27: if (key == null)
28: return null;
29:
30: try {
31: HibernateEnvironment.getInstance().beginTransaction();
32:
33: SimpleQueryCommand command = (SimpleQueryCommand) getCommandFactory()
34: .createCommand("list");
35: command.setType(getObjectClass());
36: command.addFilter("key", Operator.EQUAL, key);
37: List<O> list = command.execute();
38: O object = list.size() == 1 ? list.get(0) : null;
39: if (object != null)
40: initialize(object);
41: return object;
42: } finally {
43: HibernateEnvironment.getInstance().endTransaction();
44: }
45: }
46:
47: protected void initialize(O object) {
48: Hibernate.initialize(object);
49: }
50:
51: public Map<String, String> queryByObject(O entity) {
52: Map<String, String> map = new HashMap<String, String>();
53: map.put("key", entity.getKey());
54: return map;
55: }
56: }
|