• R/O
  • SSH
  • HTTPS

unitedfront2: Commit


Commit MetaInfo

Revision1186 (tree)
Time2008-12-27 18:11:43
Authorkurokkie

Log Message

community.form-sub-flow のコントローラロジックをリファクタリングしました。

Change Summary

Incremental Difference

--- trunk/src/main/java/org/unitedfront2/domain/communication/AbstractCommunity.java (revision 1185)
+++ trunk/src/main/java/org/unitedfront2/domain/communication/AbstractCommunity.java (revision 1186)
@@ -300,6 +300,29 @@
300300 }
301301
302302 @Override
303+ public <C extends Community> C changeTo(String className)
304+ throws CannotChangeCommunityException {
305+ Class<C> clazz;
306+ try {
307+ clazz = (Class<C>) Class.forName(className);
308+ } catch (ClassNotFoundException e) {
309+ throw new IllegalArgumentException(e);
310+ }
311+ return changeTo(clazz);
312+ }
313+
314+ @Override
315+ public <C extends Community> boolean canChangeTo(String className) {
316+ Class<C> clazz;
317+ try {
318+ clazz = (Class<C>) Class.forName(className);
319+ } catch (ClassNotFoundException e) {
320+ throw new IllegalArgumentException(e);
321+ }
322+ return canChangeTo(clazz);
323+ }
324+
325+ @Override
303326 public Thread getThread() {
304327 List<Thread> threads = communityDao.findThreads(id);
305328 if (threads.isEmpty()) {
--- trunk/src/main/java/org/unitedfront2/domain/communication/Community.java (revision 1185)
+++ trunk/src/main/java/org/unitedfront2/domain/communication/Community.java (revision 1186)
@@ -61,6 +61,18 @@
6161 throws CannotChangeCommunityException;
6262
6363 /**
64+ * コミュニティのタイプを変更したコミュニティを返します。
65+ *
66+ * @param <C> コミュニティ
67+ * @param className コミュニティクラス名
68+ * @return 変更後の新しいコミュニティ
69+ * @throws CannotChangeCommunityException タイプの変更に失敗
70+ * @see #canChangeTo(Class)
71+ */
72+ <C extends Community> C changeTo(String className)
73+ throws CannotChangeCommunityException;
74+
75+ /**
6476 * 指定したコミュニティに変更可能かどうかを判定します。
6577 *
6678 * @param <C> コミュニティ
@@ -70,6 +82,15 @@
7082 <C extends Community> boolean canChangeTo(Class<C> clazz);
7183
7284 /**
85+ * 指定したコミュニティに変更可能かどうかを判定します。
86+ *
87+ * @param <C> コミュニティ
88+ * @param className コミュニティクラス名
89+ * @return 変更可能なら <code>true</code> 、不可能なら <code>false</code>
90+ */
91+ <C extends Community> boolean canChangeTo(String className);
92+
93+ /**
7394 * このコミュニティに参加中であるかどうかを判定します。
7495 *
7596 * @param user ユーザ
--- trunk/src/main/java/org/unitedfront2/web/controller/community/FormValidator.java (revision 1185)
+++ trunk/src/main/java/org/unitedfront2/web/controller/community/FormValidator.java (nonexistent)
@@ -1,35 +0,0 @@
1-package org.unitedfront2.web.controller.community;
2-
3-import org.springframework.beans.factory.annotation.Autowired;
4-import org.springframework.stereotype.Repository;
5-import org.springframework.validation.Errors;
6-import org.unitedfront2.domain.communication.Community;
7-import org.unitedfront2.domain.communication.CommunityValidator;
8-import org.unitedfront2.validation.SpringValidatorSupport;
9-import org.unitedfront2.validation.ValidationException;
10-
11-/**
12- * コミュニティ作成/編集時の入力値検証クラスです。
13- *
14- * @author kurokkie
15- *
16- */
17-@Repository(value = "communityFormValidator")
18-public class FormValidator
19- extends SpringValidatorSupport<CommunityValidator, Community> {
20-
21- @Override
22- protected void doValidate(Community community, Errors errors) {
23- try {
24- getOriginalValidator().validateCode(community);
25- } catch (ValidationException e) {
26- rejectValue("code", e, errors);
27- }
28- }
29-
30- @Override
31- @Autowired
32- public void setOriginalValidator(CommunityValidator originalValidator) {
33- super.setOriginalValidator(originalValidator);
34- }
35-}
--- trunk/src/main/java/org/unitedfront2/web/controller/community/FormAction.java (revision 1185)
+++ trunk/src/main/java/org/unitedfront2/web/controller/community/FormAction.java (nonexistent)
@@ -1,159 +0,0 @@
1-package org.unitedfront2.web.controller.community;
2-
3-import java.util.HashMap;
4-import java.util.Map;
5-
6-import javax.annotation.Resource;
7-
8-import org.springframework.stereotype.Repository;
9-import org.springframework.validation.DataBinder;
10-import org.springframework.validation.Errors;
11-import org.springframework.validation.Validator;
12-import org.springframework.webflow.execution.Event;
13-import org.springframework.webflow.execution.RequestContext;
14-import org.unitedfront2.domain.communication.CannotChangeCommunityException;
15-import org.unitedfront2.domain.communication.Community;
16-import org.unitedfront2.domain.communication.CommunityCodeUsedByOtherException;
17-import org.unitedfront2.domain.communication.MultiThreadCommunity;
18-import org.unitedfront2.domain.communication.SingleThreadCommunity;
19-import org.unitedfront2.web.WebUtils;
20-import org.unitedfront2.web.controller.SubflowUtils;
21-import org.unitedfront2.web.flow.BadRequestException;
22-
23-/**
24- * コミュニティ作成/編集用のアクションクラスです。コード以外は編集できません。
25- *
26- * @author kurokkie
27- *
28- */
29-@Repository(value = "communityFormAction")
30-public class FormAction extends org.unitedfront2.web.controller.FormAction {
31-
32- /** フロースコープに設定するコミュニティオブジェクトの変数名 (community) */
33- public static final String COMMUNITY_PARAM_NAME = "community";
34-
35- /** 画面遷移の最後の画面のボタン名のメッセージコードの変数名 (lastSubmitCode) */
36- public static final String LAST_SUBMIT_CODE_PARAM_NAME = "lastSubmitCode";
37-
38- /** コミュニティドメインパッケージ */
39- private static final String COMMUNITY_PACKAGE
40- = "org.unitedfront2.domain.communication";
41-
42- @Override
43- protected void initAction() {
44- super.initAction();
45- setFormObjectClass(SingleThreadCommunity.class);
46- setFormObjectName(COMMUNITY_PARAM_NAME);
47- }
48-
49- @Override
50- protected void initBinder(RequestContext context, DataBinder binder) {
51- super.initBinder(context, binder);
52- binder.setAllowedFields(new String[] {"code"});
53- }
54-
55- /**
56- * 初期処理を行います。
57- *
58- * @param context {@link RequestContext}
59- * @return 成功イベント
60- */
61- @Override
62- public Event init(RequestContext context) {
63- super.init(context);
64- context.getFlowScope().put("formReadAccessControlDescription",
65- "community.FormAction.formReadAccessControl.description");
66- context.getFlowScope().put("formPostAccessControlDescription",
67- "community.FormAction.formPostAccessControl.description");
68- context.getFlowScope().put("formMessageDescription",
69- "community.FormAction.formMessage.description");
70- context.getFlowScope().put(org.unitedfront2.web.controller.message
71- .FormAction.SUBJECT_REQUIRED_PARAM_NAME, true);
72- context.getFlowScope().put(SubflowUtils.FORM_SUBMIT_CODE_PARAM_NAME,
73- "toNext");
74- return success();
75- }
76-
77- @Override
78- public Event setupForm(RequestContext context) throws Exception {
79- Event event = super.setupForm(context);
80- SubflowUtils.setupMessages(context);
81-
82- Community c = (Community) getFormObject(context);
83- c = domainFactory.prototype(c, Community.class);
84- Map<String, Boolean> canChange = new HashMap<String, Boolean>();
85- canChange.put(SingleThreadCommunity.class.getSimpleName(),
86- c.canChangeTo(SingleThreadCommunity.class));
87- canChange.put(MultiThreadCommunity.class.getSimpleName(),
88- c.canChangeTo(MultiThreadCommunity.class));
89- context.getFlashScope().put("canChange", canChange);
90-
91- return event;
92- }
93-
94- /**
95- * 必要に応じて型を変更します。
96- *
97- * @throws BadRequestException コミュニティのタイプを変更できない
98- */
99- @Override
100- protected void doBind(RequestContext context, DataBinder binder)
101- throws Exception, BadRequestException {
102- Community c = (Community) getFormObject(context);
103- c = domainFactory.prototype(c, Community.class);
104- Class<? extends Community> clazz;
105- try {
106- clazz = (Class<? extends Community>) Class.forName(
107- COMMUNITY_PACKAGE + "."
108- + context.getRequestParameters().get("class"));
109- } catch (IllegalArgumentException e) {
110- logger.error(e.getMessage(), e);
111- throw new BadRequestException(context, this, e);
112- }
113- if (c.getClass() != clazz) {
114- try {
115- c = c.changeTo(clazz);
116- } catch (CannotChangeCommunityException e) {
117- logger.warn(e.getMessage(), e);
118- throw new BadRequestException(context, this, e);
119- }
120- }
121- putFormObject(context, c);
122- super.doBind(context, createBinder(context, c));
123- }
124-
125- @Override
126- protected void doValidate(RequestContext context, Object formObject,
127- Errors errors) throws Exception {
128- super.doValidate(context, getFormObject(context), errors);
129- }
130-
131- /**
132- * コミュニティを保存します。
133- *
134- * @param context {@link RequestContext}
135- * @return 成功イベント
136- * @throws CommunityCodeUsedByOtherException コードが既に使用されている
137- */
138- public Event store(RequestContext context)
139- throws CommunityCodeUsedByOtherException {
140- Community c = WebUtils.getDomain(context.getFlowScope(),
141- COMMUNITY_PARAM_NAME, Community.class, domainFactory);
142- try {
143- c.store();
144- } catch (CommunityCodeUsedByOtherException e) {
145- logger.warn(e.getMessage());
146- Errors errors = getBindingErrors(context, c);
147- errors.rejectValue("code",
148- "community.CommunityCodeUsedByOtherException");
149- throw e;
150- }
151- return success();
152- }
153-
154- @Override
155- @Resource(name = "communityFormValidator")
156- public void setValidator(Validator validator) {
157- super.setValidator(validator);
158- }
159-}
--- trunk/src/main/java/org/unitedfront2/web/controller/community/UpdateAction.java (revision 1185)
+++ trunk/src/main/java/org/unitedfront2/web/controller/community/UpdateAction.java (revision 1186)
@@ -34,9 +34,6 @@
3434 throws AccessDeniedException {
3535 Community c = Utils.findCommunityAndWriteAccess(context, this,
3636 communityTable, "communityCode");
37- context.getFlowScope().put(FormAction.LAST_SUBMIT_CODE_PARAM_NAME,
38- "doUpdate");
39- context.getFlowScope().put(FormAction.COMMUNITY_PARAM_NAME, c);
4037 context.getFlowScope().put(SubflowUtils.PAGE_TITLE_CODE_PARAM_NAME,
4138 "community.UpdateForm");
4239
--- trunk/src/main/java/org/unitedfront2/web/validator/CommunityValidator.java (nonexistent)
+++ trunk/src/main/java/org/unitedfront2/web/validator/CommunityValidator.java (revision 1186)
@@ -0,0 +1,30 @@
1+package org.unitedfront2.web.validator;
2+
3+import org.springframework.binding.message.MessageBuilder;
4+import org.springframework.binding.validation.ValidationContext;
5+import org.unitedfront2.domain.communication.Community;
6+import org.unitedfront2.validation.ValidationException;
7+
8+/**
9+ * {@link Community} 用のバリデータクラスです。
10+ *
11+ * @author kurokkie
12+ *
13+ */
14+public class CommunityValidator extends
15+ org.unitedfront2.domain.communication.CommunityValidator {
16+
17+ /** シリアル番号 */
18+ private static final long serialVersionUID = 2232742528065527039L;
19+
20+ public void validateCodeForm(Community cocmmunity,
21+ ValidationContext context) {
22+ try {
23+ validateCode(cocmmunity);
24+ } catch (ValidationException e) {
25+ context.getMessageContext().addMessage(new MessageBuilder().error()
26+ .source("code").code(e.getCode()).args(e.getArguments())
27+ .build());
28+ }
29+ }
30+}
--- trunk/src/main/resources/applicationContext-domain.xml (revision 1185)
+++ trunk/src/main/resources/applicationContext-domain.xml (revision 1186)
@@ -139,10 +139,6 @@
139139 <property name="communityDao" ref="communityDao"/>
140140 </bean>
141141
142- <bean id="communityValidator" class="org.unitedfront2.domain.communication.CommunityValidator">
143- <property name="communityTable" ref="communityTable"/>
144- </bean>
145-
146142 <bean id="mail" class="org.unitedfront2.domain.communication.Mail" scope="prototype">
147143 <property name="accountTable" ref="accountTable"/>
148144 <property name="simpleUserTable" ref="simpleUserTable"/>
--- trunk/src/main/webapp/WEB-INF/applicationContext-validator.xml (revision 1185)
+++ trunk/src/main/webapp/WEB-INF/applicationContext-validator.xml (revision 1186)
@@ -12,4 +12,8 @@
1212 <property name="blogTable" ref="blogTable"/>
1313 </bean>
1414
15+ <bean id="communityValidator" class="org.unitedfront2.web.validator.CommunityValidator">
16+ <property name="communityTable" ref="communityTable"/>
17+ </bean>
18+
1519 </beans>
--- trunk/src/main/webapp/WEB-INF/view/community/community.form-sub-flow.xml (revision 1185)
+++ trunk/src/main/webapp/WEB-INF/view/community/community.form-sub-flow.xml (revision 1186)
@@ -8,23 +8,24 @@
88 <input name="lastSubmitCode" required="true"/>
99 <input name="community" required="true"/>
1010
11- <on-start>
12- <evaluate expression="communityFormAction.init"/>
13- </on-start>
14-
15- <view-state id="form" view="community.Form">
11+ <view-state id="codeForm" view="community.Form" model="community">
12+ <binder>
13+ <binding property="name"/>
14+ </binder>
1615 <on-render>
17- <evaluate expression="communityFormAction.setupForm"/>
16+ <set name="flashScope.canChangeToSingleThreadCommunity" value="domainFactory.prototype(community).canChangeTo('org.unitedfront2.domain.communication.SingleThreadCommunity')"/>
17+ <set name="flashScope.canChangeToMultiThreadCommunity" value="domainFactory.prototype(community).canChangeTo('org.unitedfront2.domain.communication.MultiThreadCommunity')"/>
18+ <set name="flashScope.validator" value="communityValidator"/>
1819 </on-render>
1920 <transition on="submit" to="readAccessControlForm">
20- <evaluate expression="communityFormAction.bindAndValidate"/>
21+ <set name="flowScope.community" value="domainFactory.prototype(community).changeTo('org.unitedfront2.domain.communication.' + requestParameters.type)"/>
2122 </transition>
2223 </view-state>
2324
2425 <subflow-state id="readAccessControlForm" subflow="accesscontrol.form-sub-flow">
2526 <input name="pageTitleCode" required="true"/>
26- <input name="pageDescriptionCode" value="flowScope.formReadAccessControlDescription" required="true"/>
27- <input name="formSubmitCode" required="true"/>
27+ <input name="pageDescriptionCode" value="'community.FormAction.formReadAccessControl.description'"/>
28+ <input name="formSubmitCode" value="'toNext'"/>
2829 <input name="accessControl" value="flowScope.community.readAccessControl" required="true"/>
2930 <output name="accessControl" value="flowScope.community.readAccessControl" required="true"/>
3031 <transition on="finish" to="postAccessControlForm"/>
@@ -32,8 +33,8 @@
3233
3334 <subflow-state id="postAccessControlForm" subflow="accesscontrol.form-sub-flow">
3435 <input name="pageTitleCode" required="true"/>
35- <input name="pageDescriptionCode" value="flowScope.formPostAccessControlDescription" required="true"/>
36- <input name="formSubmitCode" required="true"/>
36+ <input name="pageDescriptionCode" value="'community.FormAction.formPostAccessControl.description'"/>
37+ <input name="formSubmitCode" value="'toNext'"/>
3738 <input name="accessControl" value="flowScope.community.postAccessControl" required="true"/>
3839 <output name="accessControl" value="flowScope.community.postAccessControl" required="true"/>
3940 <transition on="finish" to="messageForm"/>
@@ -41,17 +42,16 @@
4142
4243 <subflow-state id="messageForm" subflow="message.form-sub-flow">
4344 <input name="pageTitleCode" required="true"/>
44- <input name="pageDescriptionCode" value="flowScope.formMessageDescription" required="true"/>
45+ <input name="pageDescriptionCode" value="'community.FormAction.formMessage.description'"/>
4546 <input name="formSubmitCode" value="flowScope.lastSubmitCode" required="true"/>
46- <input name="subjectRequired" required="true"/>
47- <input name="message" value="flowScope.community.overview"/>
47+ <input name="subjectRequired" value="true"/>
48+ <input name="message" value="flowScope.community.overview" required="true"/>
4849 <output name="message" value="flowScope.community.overview" required="true"/>
4950 <transition on="finish" to="store"/>
5051 </subflow-state>
5152
5253 <action-state id="store">
53- <evaluate expression="communityFormAction.store"/>
54- <transition on-exception="org.unitedfront2.domain.communication.CommunityCodeUsedByOtherException" to="form"/>
54+ <evaluate expression="domainFactory.prototype(community).store()"/>
5555 <transition on="success" to="finish"/>
5656 </action-state>
5757
Show on old repository browser