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.PluginException;
25  import org.seasar.tuigwaa.plugin.PluginRequest;
26  import org.seasar.tuigwaa.plugin.basic.VelocityPlugin;
27  import org.seasar.tuigwaa.plugin.database.component.ResultDto;
28  import org.seasar.tuigwaa.system.Constants;
29  import org.seasar.tuigwaa.system.ServiceHelper;
30  import org.seasar.tuigwaa.util.TgwContext;
31  import org.seasar.tuigwaa.util.TgwNameUtils;
32  import org.seasar.tuigwaa.util.TgwPathResolver;
33  import org.seasar.tuigwaa.view.EntityFormComponent;
34  import org.seasar.tuigwaa.view.HtmlService;
35  
36  public class TemplatePlugin extends VelocityPlugin {
37  
38  	private ServiceHelper helper = (ServiceHelper) getService(ServiceHelper.class);
39  
40  	private HtmlService htmlService = (HtmlService) getService(HtmlService.class);
41  
42  	public String doHTMLView(CmsRequest request, CmsResponse response,
43  			PluginRequest prequest) throws PluginException {
44  
45  		String[] args = prequest.getArgs();
46  
47  		String siteName = request.getSiteName();
48  		String entityDisplayName = args[2];
49  		TgwEntity entity = getEntity(siteName, entityDisplayName);
50  
51  		String type = args[0];
52  		String templateName = args[1];
53  		String option = existArg(args, 3) ? args[3] : null;
54  		String option2 = existArg(args, 4) ? args[4] : null;
55  
56  		String path = TgwPathResolver.getTemplatePath(siteName, entity
57  				.getName(), type, templateName, null);
58  		Object outputDto = getOutputDto(request, type, entity, option, option2);
59  
60  		return doHTMLViewBindingObject(request, response, prequest, outputDto,
61  				path);
62  	}
63  
64  	private Object getOutputDto(CmsRequest req, String type, TgwEntity entity,
65  			String option, String option2) {
66  		if ("data".equals(type)) {
67  			return getBean(req, entity);
68  		} else if ("table".equals(type)) {
69  			return createTableDto(req, entity, option, option2);
70  		} else if ("form".equals(type)) {
71  			return createFormDto(req, entity, option, option2);
72  		}
73  		return null;
74  	}
75  
76  	private Object createTableDto(CmsRequest req, TgwEntity entity,
77  			String option, String option2) {
78  		String customFormName = option2;
79  
80  		int first = 0;
81  		String firstStr = TgwContext.getRequest().getParameter(
82  				Constants.PARAM_ENTITY_FIRST);
83  		String entityParam = TgwContext.getRequest().getParameter(
84  				Constants.PARAM_PREFIX_ENTITY_ESCAPE
85  						+ Constants.PARAM_ENTITY_NAME);
86  		if (firstStr != null && firstStr.length() > 0 && entityParam != null
87  				&& entityParam.equals(entity.getName())) {
88  			first = Integer.parseInt(firstStr);
89  		}
90  
91  		ResultDto dto = getResultDto(req, entity, option, first);
92  		if (customFormName != null) {
93  			String cActionName = helper.getCustomActionName(entity,
94  					customFormName);
95  			String tableAction = TgwNameUtils
96  					.toCustomTableSaveActionName(cActionName);
97  			dto.setEditActionPath(tableAction);
98  		}
99  		return dto;
100 	}
101 
102 	private Object createFormDto(CmsRequest req, TgwEntity entity,
103 			String option, String option2) {
104 		String customFormName = option;
105 		String pageName = option2;
106 
107 		EntityFormComponent form = null;
108 
109 		if (customFormName == null || customFormName.length() == 0) {
110 			form = htmlService.createFormComponent(entity, option);
111 		} else {
112 			form = helper.getCustomForm(entity, customFormName, null);
113 		}
114 		Object obj = getBean(req, entity);
115 		form.setValueObject(obj);
116 		// if (obj == null) {
117 		Map bindingFkObjMap = searchBindingFkData(req, entity);
118 		form.setBindingFkObjMap(bindingFkObjMap);
119 		// }
120 		String param = req.getParameter(FormPlugin.ACTION_FORMCLEAR);
121 		String entityKey = EntityUtils.getKeys(entity);
122 		if (param != null && param.equals(entityKey)) {
123 			form.setFormclear(true);
124 		}
125 		if (pageName == null) {
126 			form.setForwardPageName(req.getMainPagePath());
127 		} else {
128 			form.setForwardPageName(pageName);
129 		}
130 
131 		return form;
132 	}
133 }