1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.plugin.database;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.seasar.tuigwaa.cms.core.CmsRequest;
22 import org.seasar.tuigwaa.cms.core.CmsResponse;
23 import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
24 import org.seasar.tuigwaa.controller.config.ValidatorUtils;
25 import org.seasar.tuigwaa.model.common.EntityUtils;
26 import org.seasar.tuigwaa.model.core.TgwAttribute;
27 import org.seasar.tuigwaa.model.core.TgwEntity;
28 import org.seasar.tuigwaa.model.core.impl.LinkAttribute;
29 import org.seasar.tuigwaa.model.core.impl.NestedAttribute;
30 import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
31 import org.seasar.tuigwaa.plugin.PluginException;
32 import org.seasar.tuigwaa.plugin.PluginRequest;
33 import org.seasar.tuigwaa.system.TgwSecurityException;
34 import org.seasar.tuigwaa.view.DataViewUtils;
35
36 import com.isenshi.util.HtmlBuffer;
37
38 /***
39 * @author nishioka
40 */
41 public class DataPlugin extends AbstractTgwPlugin {
42
43 public String doHTMLView(CmsRequest req, CmsResponse res,
44 PluginRequest prequest) throws PluginException {
45
46 String[] array = prequest.getArgs();
47
48 String domainName = req.getSiteName();
49 String entityName = array[0];
50 TgwEntity entity = getEntity(domainName, entityName);
51
52 Object bean = null;
53
54 if ("userdata".equals(prequest.getName())) {
55 bean = getBeanByUserName(req, entity);
56 } else {
57 bean = getBean(req, entity);
58 }
59
60 boolean buttonFlag = prequest.getName().endsWith("button");
61
62 if ("data".equals(prequest.getName())
63 || "userdata".equals(prequest.getName())) {
64 return doViewData(req, entity, bean, array);
65 } else if ("fklink".equals(prequest.getName())) {
66 return doFkLink(req, entity, bean, array, buttonFlag);
67 } else {
68 return doViewLink(req, entity, bean, array, buttonFlag);
69 }
70 }
71
72 private String doViewData(CmsRequest req, TgwEntity entity, Object bean,
73 String[] array) throws PluginException {
74 if (bean == null) {
75 throw new PluginException("data bean not found");
76 }
77 String fieldName = array[1];
78
79
80
81 if (fieldName.equals(entity.getPrimaryKeyDisplayName())
82 || fieldName.equals(entity.getPrimaryKeyColumnName())) {
83 return EntityUtils.getId(bean).toString();
84 }
85
86
87 TgwAttribute attr = entity.getAttributeByDisplayName(fieldName);
88 if (attr == null) {
89
90 attr = entity.getField(fieldName);
91 }
92 if (attr == null) {
93 throw new PluginException(entity.getDisplayName()
94 + " has not an attribute " + fieldName);
95 }
96 return DataViewUtils.getFilteredData(bean, attr);
97 }
98
99 private String doFkLink(CmsRequest req, TgwEntity entity, Object bean,
100 String[] array, boolean buttonFlag) {
101 String fkcolumn = array[1];
102 String forwardPageName = array[2];
103 TgwAttribute attr = entity.getAttributeByDisplayName(fkcolumn);
104
105 LinkAttribute fk = null;
106 if (attr instanceof LinkAttribute) {
107 fk = (LinkAttribute) attr;
108 } else {
109 fk = (LinkAttribute) ((NestedAttribute) attr).getRealField();
110 }
111
112
113 String label = (existArg(array, 3)) ? array[3] : DataViewUtils
114 .getFilteredData(bean, attr);
115
116
117 TgwEntity refEntity = fk.getRefEntity();
118 Object fkObj = EntityUtils.getProperty(bean, attr);
119 return doViewLink(req, refEntity, fkObj, forwardPageName, label,
120 buttonFlag);
121 }
122
123 private String doViewLink(CmsRequest req, TgwEntity entity, Object bean,
124 String[] array, boolean buttonFlag) {
125 String forwardPageName = array[1];
126 String label = null;
127 if (array.length > 2) {
128 label = array[2];
129 }
130 return doViewLink(req, entity, bean, forwardPageName, label, buttonFlag);
131 }
132
133 private String doViewLink(CmsRequest req, TgwEntity entity, Object bean,
134 String forwardPageName, String label, boolean buttonFlag) {
135
136 WikiContext context = getConfiguration().getWikiContext();
137
138
139 Long id = EntityUtils.getId(bean);
140 Map params = new HashMap();
141 bindEntityId(params, entity.getName(), "" + id);
142
143 String link = null;
144 try {
145 link = context.getURLByName(forwardPageName, req, params)
146 .toString();
147 } catch (TgwSecurityException tse) {
148 return "";
149 }
150
151 if (label == null) {
152 label = link;
153 }
154
155 if (buttonFlag) {
156 return ValidatorUtils.getLinkButton(label, link);
157 } else {
158
159 HtmlBuffer buf = new HtmlBuffer();
160 buf.appendAnchor(link, label);
161 return buf.toString();
162 }
163 }
164 }