01: package com.technoetic.xplanner.wiki;
02:
03: import com.technoetic.xplanner.db.hibernate.ThreadSession;
04: import com.technoetic.xplanner.domain.Iteration;
05: import com.technoetic.xplanner.domain.Person;
06: import com.technoetic.xplanner.domain.Project;
07: import com.technoetic.xplanner.domain.Task;
08: import com.technoetic.xplanner.domain.UserStory;
09: import com.technoetic.xplanner.filters.ThreadServletRequest;
10: import org.apache.commons.beanutils.BeanUtils;
11: import org.apache.log4j.Logger;
12:
13: import javax.servlet.http.HttpServletRequest;
14: import java.util.HashMap;
15: import java.util.Properties;
16: import java.util.regex.Matcher;
17: import java.util.regex.Pattern;
18:
19: public class DomainObjectSchemeHandler implements SchemeHandler {
20: private Logger log = Logger.getLogger(getClass());
21: private String action;
22: private static HashMap schemeClasses = new HashMap();
23:
24: static {
25: schemeClasses.put("project", Project.class);
26: schemeClasses.put("iteration", Iteration.class);
27: schemeClasses.put("story", UserStory.class);
28: schemeClasses.put("task", Task.class);
29: schemeClasses.put("person", Person.class);
30: }
31:
32: public DomainObjectSchemeHandler(String action) {
33: this .action = action;
34: }
35:
36: public String translate(Properties properties, String scheme,
37: String location, String linkText) {
38: Class domainClass = (Class) schemeClasses.get(scheme);
39: Pattern pattern = Pattern.compile("^(\\d+)(.*)$");
40: Matcher matcher = pattern.matcher(location);
41: try {
42: matcher.find();
43: location = matcher.group(1);
44: Object object = ThreadSession.get().load(domainClass,
45: new Integer(location));
46: if (linkText == null) {
47: try {
48: linkText = scheme + ": "
49: + BeanUtils.getProperty(object, "name");
50: } catch (Exception e) {
51: // ignored
52: linkText = scheme + ":" + location;
53: }
54: }
55: //FIXME: Why using a second group? What is it for?
56: linkText += matcher.group(2);
57: HttpServletRequest request = ThreadServletRequest.get();
58: return "<a href='" + request.getContextPath() + "/do/view/"
59: + (action != null ? action : scheme) + "?oid="
60: + location + "'>" + linkText + "</a>";
61: } catch (Exception e) {
62: return "[" + scheme + ": " + e.getMessage() + "]";
63: }
64: }
65: }
|