01: package org.osbl.orga;
02:
03: import org.osbl.orga.model.Membership;
04: import org.osbl.*;
05: import org.osbl.persistence.*;
06: import org.conform.hibernate.HibernateEnvironment;
07: import org.hibernate.Hibernate;
08:
09: import java.util.*;
10:
11: public class MembershipLocator implements ObjectLocator<Membership> {
12: private Persistence persistence;
13:
14: public Persistence getCommandFactory() {
15: if (persistence == null)
16: persistence = (Persistence) ServiceProvider.getInstance()
17: .getService("OrgaPersistence");
18: return persistence;
19: }
20:
21: public Membership objectByQuery(Map<String, String> query) {
22: String member = query.get("member");
23: String group = query.get("group");
24: String role = query.get("role");
25: if (member == null || group == null || role == null)
26: return null;
27:
28: try {
29: HibernateEnvironment.getInstance().beginTransaction();
30:
31: JoinedQueryCommand command = (JoinedQueryCommand) getCommandFactory()
32: .createCommand("list");
33: command.setType(Membership.class);
34: command.addFilter("member.key", Operator.EQUAL, member);
35: command.addFilter("group.key", Operator.EQUAL, group);
36: command.addFilter("role", Operator.EQUAL, role);
37: List<Membership> list = command.execute();
38: Membership 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(Membership object) {
48: Hibernate.initialize(object);
49: }
50:
51: public Map<String, String> queryByObject(Membership membership) {
52: Map<String, String> map = new HashMap<String, String>();
53: map.put("member", membership.getMember().getKey());
54: map.put("group", membership.getGroup().getKey());
55: map.put("role", membership.getRole());
56: return map;
57: }
58: }
|