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.component;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.seasar.tuigwaa.cms.core.CmsRequest;
24  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
25  import org.seasar.tuigwaa.model.core.TgwEntity;
26  import org.seasar.tuigwaa.plugin.PluginUtils;
27  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
28  import org.seasar.tuigwaa.plugin.database.TablePlugin;
29  import org.seasar.tuigwaa.system.Constants;
30  import org.seasar.tuigwaa.system.TgwSecurityException;
31  import org.seasar.tuigwaa.util.TgwContext;
32  import org.seasar.tuigwaa.util.TgwResource;
33  
34  import com.isenshi.util.CharUtil;
35  
36  public class ActionLinkFactory {
37  
38  	private TgwEntity entity;
39  
40  	private CmsRequest req;
41  
42  	private String detailPage;
43  
44  	private String editPage;
45  
46  	private boolean viewFlag = false;
47  
48  	private boolean editFlag = false;
49  
50  	private boolean deleteFlag = false;
51  
52  	private String idQuery = "";
53  
54  	private WikiContext ctx;
55  
56  	private boolean buttonFlag = "true".equals(TgwResource
57  			.getProperty("option.tgwlink.button"));
58  
59  	public ActionLinkFactory(WikiContext ctx, TgwEntity entity, CmsRequest req,
60  			int startIndex, String[] args) {
61  		if (args.length <= startIndex) {
62  			return;
63  		}
64  		this.ctx = ctx;
65  		this.entity = entity;
66  		this.req = req;
67  		this.viewFlag = (args[startIndex].indexOf("v") >= 0);
68  		this.editFlag = (args[startIndex].indexOf("e") >= 0);
69  		this.deleteFlag = (args[startIndex].indexOf("d") >= 0);
70  
71  		if (args.length > startIndex + 1 && !"".equals(args[startIndex + 1])) {
72  			detailPage = args[startIndex + 1];
73  		} else {
74  			detailPage = TgwPluginUtils.getDetailPageName(req, entity);
75  		}
76  		if (args.length > startIndex + 2 && !"".equals(args[startIndex + 2])) {
77  			editPage = args[startIndex + 2];
78  		} else {
79  			editPage = req.getPage().getResource().getPath();
80  		}
81  		idQuery = TgwPluginUtils.createEntityBindingName(entity.getName())
82  				+ "=${id}";
83  	}
84  
85  	public List create() {
86  		List list = new ArrayList();
87  		bindActionLink(list, viewFlag, detailPage, "button.label.detail");
88  		bindActionLink(list, editFlag, editPage, "button.label.edit");
89  		bindDeleteActionLink(list);
90  		return list;
91  	}
92  
93  	private void bindActionLink(List list, boolean flag, String pageName,
94  			String messageKey) {
95  		if (flag) {
96  			try {
97  				String label = PluginUtils.getMessage(messageKey);
98  				if (pageName.indexOf(":") > 0) {
99  					String[] data = pageName.split(":");
100 					label = data[0];
101 					pageName = data[1];
102 				}
103 				String path = ctx.getURLByName(pageName, req).toString();
104 				path += "?" + idQuery;
105 
106 				ResultDto dto = TablePlugin.getResult(entity);
107 				if (dto != null && !"".equals(dto)) {
108 					path += "&" + Constants.PARAM_SEARCH_REDIREDT_URL + "=" +  CharUtil.urlEncode(dto.getPath());
109 				}
110 				String key = TgwPluginUtils.createEntityBindingName(entity.getName());
111 				path += req.getURLEncodedQuery(key);
112 				list.add(new ActionLink(label, path, buttonFlag, false));
113 			} catch (TgwSecurityException e) {
114 				// no link
115 			}
116 		}
117 	}
118 
119 	private void bindDeleteActionLink(List list) {
120 		if (deleteFlag) {
121 			Map deleteParams = new HashMap();
122 			deleteParams.put(Constants.PARAM_PAGENAME, req.getPage()
123 					.getResource().getPath());
124 //			deleteParams.put(Constants.PARAM_ENTITY_NAME, entity.getName());
125 
126 			ResultDto dto = TablePlugin.getResult(entity);
127 			if (dto != null && !"".equals(dto)) {
128 				deleteParams.put(Constants.PARAM_SEARCH_REDIREDT_URL, dto.getPath());
129 			}
130 			
131 			String page = TgwContext.getContextPath() + "/"
132 					+ TgwContext.getSiteName() + "/deleteRecord.do?"
133 					+ CharUtil.createQuery(deleteParams)  + 
134 					"&" + Constants.PARAM_ENTITY_NAME + "=" + CharUtil.urlEncode(entity.getName()) +
135 					"&id=${id}";
136 			
137 			String key = TgwPluginUtils.createEntityBindingName(entity.getName());
138 			page += req.getURLEncodedQuery(key);
139 
140 			// boolean needEncode = !buttonFlag;
141 			// String path = ValidatorUtils.getDeleteConfirmationFunc(page,
142 			// needEncode);
143 			
144 			String label = PluginUtils.getMessage("button.label.delete");						
145 			list.add(new ActionLink(label, page, buttonFlag, true));				
146 
147 		}
148 	}
149 }