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.ArrayList;
19  import java.util.List;
20  
21  import org.seasar.tuigwaa.cms.core.CmsRequest;
22  import org.seasar.tuigwaa.cms.core.CmsResponse;
23  import org.seasar.tuigwaa.cms.core.Page;
24  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
25  import org.seasar.tuigwaa.database.DataTable;
26  import org.seasar.tuigwaa.model.core.TgwEntity;
27  import org.seasar.tuigwaa.plugin.PluginException;
28  import org.seasar.tuigwaa.plugin.PluginRequest;
29  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
30  import org.seasar.tuigwaa.plugin.basic.VelocityPlugin;
31  import org.seasar.tuigwaa.plugin.database.component.ResultDto;
32  import org.seasar.tuigwaa.system.Constants;
33  import org.seasar.tuigwaa.system.TgwSecurityException;
34  
35  public class DataListPlugin extends VelocityPlugin {
36  
37  	private static final String FIRST_RESULT = getParameterName("datalist.firstResult");
38  
39  	public String doHTMLView(CmsRequest request, CmsResponse response,
40  			PluginRequest prequest) throws PluginException {
41  
42  		String[] args = prequest.getArgs();
43  
44  		String entityDisplayName = args[0];
45  		TgwEntity entity = getEntity(request.getSiteName(), entityDisplayName);
46  
47  		String dataFilter = (existArg(args, 1)) ? args[1] : null;
48  		String pagepath = (existArg(args, 2)) ? args[2] : TgwPluginUtils.getDetailPageName(
49  				request, entity);
50  
51  		boolean existPage = existPage(request, pagepath);
52  
53  		int firstResult = getFirstResult(request);
54  		int maxResult = existPage ? getMaxResult(args) : 1;
55  		int testMaxResult = existPage ? maxResult + 1 : 1;
56  		if (maxResult == 0) {
57  			testMaxResult = 0;
58  		}
59  
60  		// assertIncludePage(request, pagepath);
61  
62  		ResultDto dto = getResultDto(request, entity, dataFilter, firstResult,
63  				testMaxResult);
64  		if (dto == null) {
65  			return "";
66  		}
67  
68  		DataTable resultTable = dto.getResultDataTable();
69  		if (!existPage) {
70  			String id = null;
71  			if (resultTable.hasNext()) {
72  				resultTable.next();
73  				Object idObj = resultTable.getData(Constants.ENTITY_BUILTIN_ID);
74  				id = String.valueOf(idObj);
75  			}
76  			return getNoPageMessage(request, pagepath, entity.getName(), id);
77  		} else {
78  			dto.setPages(getPageResults(resultTable, entity, pagepath, request,
79  					firstResult, maxResult));
80  			if (dto.getSearchDto() == null) {
81  				dto.setMax(maxResult);
82  				try {
83  					String thisPageName = request.getPage().getResource()
84  							.getPath();
85  					WikiContext context = getConfiguration().getWikiContext();
86  
87  					String path = context.getURLByName(thisPageName, request)
88  							.toString();
89  					dto.setPath(path);
90  				} catch (TgwSecurityException e) {
91  					// no link
92  				}
93  			}
94  			return doHTMLViewBindingObject(request, response, prequest, dto,
95  					"template/parts/datalist.vm");
96  		}
97  	}
98  
99  	private int getFirstResult(CmsRequest request) {
100 		String firstResultStr = request.getParameter(FIRST_RESULT);
101 		if (firstResultStr != null) {
102 			return Integer.parseInt(firstResultStr);
103 		} else {
104 			return 0;
105 		}
106 	}
107 
108 	private int getMaxResult(String[] args) {
109 		if (existArg(args, 3)) {
110 			return Integer.parseInt(args[3]);
111 		} else {
112 			return 0;
113 		}
114 	}
115 
116 	private List getPageResults(DataTable resultTable, TgwEntity entity,
117 			String pagepath, CmsRequest wRequest, int firstResult, int maxResult) {
118 
119 		if (resultTable == null) {
120 			return null;
121 		}
122 
123 		List list = new ArrayList();
124 
125 		for (int i = 0; (maxResult <= 0 || i < maxResult)
126 				&& resultTable.hasNextRow(); i++) {
127 			resultTable.next();
128 
129 			wRequest.pushScope();
130 			TgwPluginUtils.bindEntityObject(wRequest, entity, resultTable
131 					.getRowObject());
132 			Page page = getPage(wRequest, pagepath);
133 			wRequest.popScope();
134 
135 			list.add(page.getContent().toString());
136 		}
137 		return list;
138 	}
139 }