001: /*
002: * Copyright (c) Mateusz Prokopowicz. All Rights Reserved.
003: */
004:
005: package com.technoetic.xplanner;
006:
007: import java.util.List;
008: import java.util.Properties;
009: import java.util.Enumeration;
010: import java.util.Set;
011: import java.util.HashSet;
012: import java.util.Collection;
013: import java.util.Iterator;
014: import java.util.Collections;
015: import java.util.ArrayList;
016:
017: import net.sf.hibernate.Hibernate;
018: import net.sf.hibernate.HibernateException;
019: import net.sf.hibernate.Session;
020: import net.sf.hibernate.SessionFactory;
021: import net.sf.hibernate.type.Type;
022: import org.springframework.orm.hibernate.HibernateCallback;
023: import org.springframework.orm.hibernate.HibernateTemplate;
024:
025: import com.technoetic.xplanner.domain.Attribute;
026: import com.technoetic.xplanner.domain.Iteration;
027: import com.technoetic.xplanner.domain.Project;
028: import com.technoetic.xplanner.domain.Task;
029: import com.technoetic.xplanner.domain.UserStory;
030:
031: /**
032: * User: mprokopowicz
033: * Date: Feb 6, 2006
034: * Time: 12:59:52 PM
035: */
036: public class DomainSpecificProperties extends Properties {
037: private SessionFactory sessionFactory;
038: private Object object;
039:
040: protected DomainSpecificProperties(Properties defaultProperties,
041: SessionFactory sessionFactory, Object domainObject) {
042: super (defaultProperties);
043: this .sessionFactory = sessionFactory;
044: this .object = domainObject;
045: }
046:
047: public String getProperty(String key) {
048: HibernateTemplate hibernateTemplate = new HibernateTemplate(
049: this .sessionFactory);
050: final Integer attributeTargetId = getAttributeTargetId(object,
051: hibernateTemplate);
052: if (attributeTargetId != null) {
053: HibernateCallback action = new GetAttributeHibernateCallback(
054: attributeTargetId, key);
055: Attribute attribute = (Attribute) hibernateTemplate
056: .execute(action);
057: if (attribute != null) {
058: return attribute.getValue();
059: }
060: }
061: return super .getProperty(key);
062: }
063:
064: private Integer getAttributeTargetId(Object object,
065: HibernateTemplate hibernateTemplate) {
066: if (object instanceof Project) {
067: Project project = (Project) object;
068: return new Integer(project.getId());
069: }
070: if (object instanceof Iteration) {
071: Iteration iteration = (Iteration) object;
072: return new Integer(iteration.getProjectId());
073: }
074: Integer iterationId = null;
075: if (object instanceof UserStory) {
076: UserStory story = (UserStory) object;
077: iterationId = new Integer(story.getIterationId());
078: }
079: if (object instanceof Task) {
080: Task task = (Task) object;
081: iterationId = new Integer(task.getStory().getIterationId());
082: }
083: if (iterationId != null) {
084: final Integer id = iterationId;
085: Iteration iteration = (Iteration) hibernateTemplate
086: .execute(new HibernateCallback() {
087: public Object doInHibernate(Session session)
088: throws HibernateException {
089: return session.load(Iteration.class, id);
090: }
091: });
092: return new Integer(iteration.getProjectId());
093: }
094: return null;
095: }
096:
097: public synchronized Enumeration keys() {
098: Set keys = keySet();
099: return Collections.enumeration(keys);
100: }
101:
102: public Set keySet() {
103: Set keys = new HashSet(defaults.keySet());
104: keys.addAll(super .keySet());
105: return keys;
106: }
107:
108: public Collection values() {
109: List values = new ArrayList(defaults.values());
110: values.addAll(super .values());
111: return values;
112: }
113:
114: private static class GetAttributeHibernateCallback implements
115: HibernateCallback {
116: private final Integer attributeTargetId;
117: private final String key;
118:
119: public GetAttributeHibernateCallback(Integer attributeTargetId,
120: String key) {
121: this .attributeTargetId = attributeTargetId;
122: this .key = key;
123: }
124:
125: public Object doInHibernate(Session session)
126: throws HibernateException {
127: Attribute attribute = null;
128:
129: List attributeList = session
130: .find(
131: "select attribute from Attribute attribute where attribute.targetId = ? and attribute.name= ? ",
132: new Object[] { attributeTargetId, key },
133: new Type[] { Hibernate.INTEGER,
134: Hibernate.STRING });
135: if (attributeList.size() > 0) {
136: attribute = (Attribute) attributeList.get(0);
137: }
138: return attribute;
139: }
140: }
141: }
|