01: package com.technoetic.xplanner.domain.repository;
02:
03: import com.technoetic.xplanner.domain.Attribute;
04: import com.technoetic.xplanner.db.hibernate.ThreadSession;
05: import net.sf.hibernate.HibernateException;
06: import net.sf.hibernate.Hibernate;
07: import net.sf.hibernate.Session;
08: import net.sf.hibernate.type.Type;
09:
10: import java.util.List;
11: import java.util.Map;
12: import java.util.HashMap;
13:
14: import org.apache.commons.lang.StringUtils;
15:
16: public class AttributeRepositoryImpl implements AttributeRepository {
17: public void setAttribute(int targetId, String name, String value)
18: throws RepositoryException {
19: try {
20: final Session session = ThreadSession.get();
21: final Attribute attribute = new Attribute(targetId, name,
22: value);
23: List existingAttributes = session.find("from a in "
24: + Attribute.class
25: + " where targetId = ? and name= ?", new Object[] {
26: new Integer(targetId), name }, new Type[] {
27: Hibernate.INTEGER, Hibernate.STRING });
28: if (existingAttributes.size() == 0) {
29: session.save(attribute);
30: } else {
31: session.update(attribute);
32: }
33: } catch (RuntimeException e) {
34: throw e;
35: } catch (HibernateException e) {
36: throw new RepositoryException(e);
37: }
38: }
39:
40: public String getAttribute(int targetId, String name)
41: throws RepositoryException {
42: try {
43: final Session session = ThreadSession.get();
44: final Attribute id = new Attribute(targetId, name, null);
45: Attribute attribute = (Attribute) session.load(
46: Attribute.class, id);
47: return attribute.getValue();
48: } catch (net.sf.hibernate.ObjectNotFoundException e) {
49: return null;
50: } catch (RuntimeException e) {
51: throw e;
52: } catch (HibernateException e) {
53: throw new RepositoryException(e);
54: }
55: }
56:
57: public Map getAttributes(int targetId, String prefix)
58: throws RepositoryException {
59: HashMap attributes = new HashMap();
60: try {
61: final Session session = ThreadSession.get();
62: String pattern = (prefix != null ? prefix : "") + "%";
63: List attributeObjects = session.find("from a in "
64: + Attribute.class
65: + " where targetId = ? and name like ?",
66: new Object[] { new Integer(targetId), pattern },
67: new Type[] { Hibernate.INTEGER, Hibernate.STRING });
68: for (int i = 0; i < attributeObjects.size(); i++) {
69: Attribute attribute = (Attribute) attributeObjects
70: .get(i);
71: String name = attribute.getName();
72: if (StringUtils.isNotEmpty(prefix)) {
73: name = name.replaceAll("^" + prefix, "");
74: }
75: attributes.put(name, attribute.getValue());
76: }
77: return attributes;
78: } catch (RuntimeException e) {
79: throw e;
80: } catch (HibernateException e) {
81: throw new RepositoryException(e);
82: }
83: }
84:
85: public void delete(int targetId, String name)
86: throws RepositoryException {
87: try {
88: ThreadSession.get().delete(
89: "from a in " + Attribute.class
90: + " where a.targetId = ? and a.name = ?",
91: new Object[] { new Integer(targetId), name },
92: new Type[] { Hibernate.INTEGER, Hibernate.STRING });
93: } catch (RuntimeException e) {
94: throw e;
95: } catch (HibernateException e) {
96: throw new RepositoryException(e);
97: }
98: }
99: }
|