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.List;
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.database.DataTable;
25  import org.seasar.tuigwaa.database.function.UpdateExeFunction;
26  import org.seasar.tuigwaa.model.DAOService;
27  import org.seasar.tuigwaa.model.core.TgwEntity;
28  import org.seasar.tuigwaa.plugin.PluginException;
29  import org.seasar.tuigwaa.plugin.PluginRequest;
30  import org.seasar.tuigwaa.plugin.basic.VelocityPlugin;
31  import org.seasar.tuigwaa.plugin.database.component.ActionLinkFactory;
32  import org.seasar.tuigwaa.plugin.database.component.EditTableDto;
33  import org.seasar.tuigwaa.plugin.database.component.ResultDto;
34  import org.seasar.tuigwaa.view.HtmlService;
35  import org.seasar.tuigwaa.view.TableFormComponent;
36  
37  /***
38   * @author nishioka
39   */
40  public class EditTablePlugin extends VelocityPlugin {
41  
42  	private HtmlService htmlService = (HtmlService) getService(HtmlService.class);
43  
44  	private DAOService daoProvider = (DAOService) getService(DAOService.class);
45  
46  	private static final String PATH_TEMPLATE = "template/parts/edittable.vm";
47  
48  	public String doHTMLView(CmsRequest req, CmsResponse res,
49  			PluginRequest prequest) throws PluginException {
50  
51  		String[] args = prequest.getArgs();
52  		String domain = req.getSiteName();
53  		TgwEntity entity = getEntity(domain, args[0]);
54  		String filter = (existArg(args, 1)) ? args[1] : null;
55  
56  		ResultDto resultDto = getResultDto(req, entity, filter);
57  		if (resultDto == null) {
58  			return null;
59  		}
60  
61  		// Get Output Dto
62  		DataTable data = resultDto.getResultDataTable();
63  		WikiContext ctx = getConfiguration().getWikiContext();
64  		EditTableDto dto = toDto(req, entity, data, args);
65  		if (dto == null) {
66  			return null;
67  		}
68  
69  		// Set Action Links
70  
71  		if ("edittable".equals(prequest.getName())) {
72  			List links = new ActionLinkFactory(ctx, entity, req, 3, args)
73  					.create();
74  			dto.setActionLinks(links);
75  			dto.setForwardPage(req.getPage().getResource().getPath());
76  		} else {
77  			int size = 10;
78  			if (existArg(args, 3)) {
79  				dto.setForwardPage(args[3]);
80  			} else {
81  				dto.setForwardPage(req.getPage().getResource().getPath());
82  			}
83  			if (existArg(args, 4)) {
84  				size = Integer.parseInt(args[4]);
85  			}
86  			dto.setBlankRowSize(size);
87  		}
88  		return doHTMLViewBindingObject(req, res, prequest, dto, PATH_TEMPLATE);
89  	}
90  
91  	private EditTableDto toDto(CmsRequest req, TgwEntity entity,
92  			DataTable table, String[] args) throws PluginException {
93  		if (args.length < 3) {
94  			return null;
95  		}
96  		TableFormComponent component = null;
97  		if (args.length > 2) {
98  			Map map = daoProvider.getCustomFormFunctionMap(entity);
99  			UpdateExeFunction updateExe = (UpdateExeFunction) map.get(args[2]);
100 			component = htmlService.createTableFormComponent(entity, updateExe);
101 		} else {
102 			component = htmlService.createTableFormComponent(entity, null);
103 		}
104 		
105 		Map bindingFkObjMap = searchBindingFkData(req, entity);
106 		component.setBindingFkObjMap(bindingFkObjMap);
107 		
108 		return new EditTableDto(table, component);
109 	}
110 }