View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin.database;
17  
18  import java.util.Map;
19  
20  import org.seasar.tuigwaa.cms.core.CmsRequest;
21  import org.seasar.tuigwaa.cms.core.CmsResponse;
22  import org.seasar.tuigwaa.model.common.EntityUtils;
23  import org.seasar.tuigwaa.model.core.TgwEntity;
24  import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
25  import org.seasar.tuigwaa.plugin.PluginException;
26  import org.seasar.tuigwaa.plugin.PluginRequest;
27  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
28  import org.seasar.tuigwaa.system.ServiceHelper;
29  import org.seasar.tuigwaa.util.TgwContext;
30  import org.seasar.tuigwaa.view.EntityFormComponent;
31  import org.seasar.tuigwaa.view.HtmlService;
32  
33  import com.isenshi.util.LabelValue;
34  
35  /***
36   * @author nishioka
37   */
38  public class FormPlugin extends AbstractTgwPlugin {
39  
40  	public static final String ACTION_FORMCLEAR = "formClear";
41  
42  	private ServiceHelper helper = (ServiceHelper) getService(ServiceHelper.class);
43  
44  	private HtmlService htmlService = (HtmlService) getService(HtmlService.class);
45  
46  	public FormPlugin() {
47  	}
48  
49  	public static void clear(TgwEntity entity) {
50  		TgwContext.setCmsParameter(ACTION_FORMCLEAR, EntityUtils
51  				.getKeys(entity));
52  	}
53  
54  	public String doHTMLView(CmsRequest req, CmsResponse res,
55  			PluginRequest prequest) throws PluginException {
56  
57  		if (!req.getPage().getResource().isPersistent()) {
58  			return ""; // Ajax Form Insertion of IE has bugs ??
59  		}
60  
61  		String domainName = req.getSiteName();
62  		FormInfo formInfo = new FormInfo(req, prequest);
63  
64  		TgwEntity entity = getEntity(domainName, formInfo.getEntityName());
65  
66  		Object bean = null;
67  
68  		String entityKey = EntityUtils.getKeys(entity);
69  		String param = req.getParameter(ACTION_FORMCLEAR);
70  		if (param == null || !param.equals(entityKey)) {
71  			bean = getBean(req, entity);
72  		} else {
73  			TgwPluginUtils
74  					.removeEntityObject(req, domainName, entity.getName());
75  		}
76  		Map bindingFkObjMap = null;
77  		//if (bean == null ) {
78  		bindingFkObjMap = searchBindingFkData(req, entity);
79  		//}
80  
81  		EntityFormComponent form = getFormComponent(formInfo, entity, bean,
82  				bindingFkObjMap);
83  		form.setDisplayName(formInfo.getCustomFormLabel());
84  		return createForm(formInfo, form, bindingFkObjMap);
85  	}
86  
87  	private String createForm(FormInfo formInfo, EntityFormComponent form,
88  			Map bindingFkObjMap) {
89  		return htmlService.toHtml(form);
90  	}
91  
92  	private EntityFormComponent getFormComponent(FormInfo formInfo,
93  			TgwEntity entity, Object bean, Map bindingFkObjMap) {
94  
95  		EntityFormComponent form = null;
96  		if (formInfo.isCustomFlag()) {
97  			String cformName = formInfo.getCustomFormName();
98  			form = helper.getCustomForm(entity, cformName, bean);
99  		} else {
100 			form = htmlService.createFormComponent(entity, bean);
101 		}
102 		form.setForwardPageName(formInfo.getForwardPageName());
103 		form.setThisPageName(formInfo.getThisPageName());
104 		form.setBindingFkObjMap(bindingFkObjMap);
105 		return form;
106 	}
107 
108 	class FormInfo {
109 
110 		private String forwardPageName;
111 
112 		private String thisPageName;
113 
114 		private boolean customFlag;
115 
116 		private String customFormName;
117 
118 		private String customFormLabel;
119 
120 		private String entityName;
121 
122 		public FormInfo(CmsRequest wrequest, PluginRequest prequest) {
123 			String[] array = prequest.getArgs();
124 			
125 			int forwardIndex = 1;
126 
127 			if ("form".equals(prequest.getName())) {
128 				LabelValue lv = new LabelValue(array[0]);
129 				this.entityName = lv.getLabel();
130 				this.customFormLabel = lv.getValue();
131 				if(this.customFormLabel == null){
132 					this.customFormLabel = this.entityName;
133 				}
134 				customFlag = false;
135 			} else {
136 				this.entityName = array[0];
137 				forwardIndex = 2;
138 				customFlag = true;
139 				LabelValue lv = new LabelValue(array[1]);
140 				this.customFormName = lv.getLabel();
141 				this.customFormLabel = lv.getValue();
142 				if (this.customFormLabel == null) {
143 					this.customFormLabel = this.customFormName;
144 				}
145 			}
146 
147 			if (array.length > forwardIndex && !array[forwardIndex].equals("")) {
148 				this.forwardPageName = array[forwardIndex];
149 			} else {
150 				this.forwardPageName = wrequest.getPage().getResource()
151 						.getPath();
152 			}
153 			this.thisPageName = wrequest.getPage().getResource().getPath();
154 		}
155 
156 		public String getCustomFormLabel() {
157 			return customFormLabel;
158 		}
159 
160 		public boolean isCustomFlag() {
161 			return customFlag;
162 		}
163 
164 		public String getForwardPageName() {
165 			return forwardPageName;
166 		}
167 
168 		public String getThisPageName() {
169 			return thisPageName;
170 		}
171 
172 		public String getEntityName() {
173 			return entityName;
174 		}
175 
176 		public String getCustomFormName() {
177 			return customFormName;
178 		}
179 	}
180 }