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.controller;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.beanutils.DynaBean;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  import org.apache.struts.action.ActionMessage;
33  import org.apache.struts.action.ActionMessages;
34  import org.apache.struts.actions.MappingDispatchAction;
35  import org.apache.struts.validator.DynaValidatorForm;
36  import org.seasar.tuigwaa.cms.ContentsService;
37  import org.seasar.tuigwaa.cms.ContentsStoreService;
38  import org.seasar.tuigwaa.cms.TgwResourceAlreadyExistsException;
39  import org.seasar.tuigwaa.security.Action;
40  import org.seasar.tuigwaa.security.SecurityService;
41  import org.seasar.tuigwaa.system.Constants;
42  import org.seasar.tuigwaa.system.SiteConfig;
43  import org.seasar.tuigwaa.util.TgwContext;
44  import org.seasar.tuigwaa.util.TgwUtils;
45  
46  import com.isenshi.util.CharUtil;
47  import com.isenshi.util.PathUtils;
48  import com.isenshi.util.extlib.StrutsUtil;
49  
50  /***
51   * @author someda
52   */
53  public class FolderAction extends MappingDispatchAction {
54  
55  	private ContentsStoreService storeSerice;
56  
57  	private SecurityService securityService;
58  
59  	private ContentsService contentsService;
60  
61  	private Log log_ = LogFactory.getLog(getClass());
62  
63  	public FolderAction(ContentsStoreService slideService,
64  			SecurityService securityService, ContentsService contentsService) {
65  		this.storeSerice = slideService;
66  		this.securityService = securityService;
67  		this.contentsService = contentsService;
68  	}
69  
70  	public ActionForward view(ActionMapping mapping, ActionForm form,
71  			HttpServletRequest request, HttpServletResponse response)
72  			throws Exception {
73  		String folderPath = TgwContext.getPageName();
74  		return doView(folderPath, mapping, form, request, response);
75  	}
76  
77  	public ActionForward createFolder(ActionMapping mapping, ActionForm form,
78  			HttpServletRequest request, HttpServletResponse response)
79  			throws Exception {
80  
81  		DynaValidatorForm folderForm = (DynaValidatorForm) form;
82  		String folder = (String) folderForm.get("foldername");
83  		String orgFolderPath = (String) folderForm.get("folderpath");
84  		String folderPath = null;
85  		if (orgFolderPath == null || orgFolderPath.equals("")) {
86  			folderPath = folder;
87  		} else if (orgFolderPath.endsWith("/")) {
88  			folderPath = orgFolderPath + folder;
89  		} else {
90  			folderPath = orgFolderPath + "/" + folder;
91  		}
92  
93  		try {
94  			contentsService
95  					.createFolder(TgwContext.getSiteConfig(), folderPath);
96  			request.removeAttribute("folderForm");
97  		}catch(TgwResourceAlreadyExistsException e){
98  			ActionMessages msgs = new ActionMessages();
99  			msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
100 					"msg.folderForm.duplicate"));
101 			saveMessages(request, msgs);
102 		}
103 		return doView(orgFolderPath, mapping, form, request, response);
104 	}
105 
106 	public ActionForward deleteFolder(ActionMapping mapping, ActionForm form,
107 			HttpServletRequest request, HttpServletResponse response)
108 			throws Exception {
109 
110 		String siteName = TgwContext.getSiteName();
111 		DynaValidatorForm folderForm = (DynaValidatorForm) form;
112 		String[] uris = (String[]) folderForm.get("uris");
113 
114 		for (int i = 0; i < uris.length; i++) {
115 			String folderPath = uris[i];
116 			log_.info("delete folder :" + folderPath);
117 			storeSerice.delete(siteName, folderPath);
118 		}
119 
120 		String folderPath = folderForm.getString("folderpath");
121 		return doView(folderPath, mapping, form, request, response);
122 	}
123 
124 	public ActionForward createFolderSetting(ActionMapping mapping,
125 			ActionForm form, HttpServletRequest request,
126 			HttpServletResponse response) throws Exception {
127 
128 		SiteConfig siteConfig = TgwContext.getSiteConfig();
129 		String pageName = TgwContext.getPageName();
130 		FolderSettingForm settingForm = new FolderSettingForm(siteConfig
131 				.getName(), pageName, siteConfig, securityService);
132 
133 		request.getSession().setAttribute("folderSettingForm", settingForm);
134 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
135 	}
136 
137 	public ActionForward saveFolderSetting(ActionMapping mapping,
138 			ActionForm form, HttpServletRequest request,
139 			HttpServletResponse response) throws Exception {
140 
141 		SiteConfig siteConfig = TgwContext.getSiteConfig();
142 		String siteName = siteConfig.getName();
143 
144 		FolderSettingForm settingForm = (FolderSettingForm) form;
145 		String path = settingForm.getPath();
146 		Action action = Action.PAGE_VIEW.createParameteredAction(path);
147 
148 		String[] roleNames = siteConfig.getRoleNames();
149 
150 		String[] grantRoleNames = settingForm.getCheckedRoleNames();
151 		String[] denyRoleNames = CharUtil.removeArrays(roleNames,
152 				grantRoleNames);
153 
154 		securityService.setPermissions(siteName, action, grantRoleNames,
155 				denyRoleNames);
156 
157 		StrutsUtil.clearSessionForm(request, mapping);
158 
159 		String parent = PathUtils.getParentPath(path);
160 		Map map = null;
161 		if (parent != null) {
162 			map = new HashMap();
163 			map.put(Constants.PARAM_PAGENAME, CharUtil.urlDecode(parent));
164 		}
165 		return TgwUtils.dialogCloseForward(mapping, request, siteConfig
166 				.getName(), Constants.ACTION_URI_VIEWFOLDER, map);
167 	}
168 
169 	public ActionForward createPageRename(ActionMapping mapping,
170 			ActionForm form, HttpServletRequest request,
171 			HttpServletResponse response) throws Exception {
172 		String pageName = request.getParameter("_pageName_");
173 		String resourceName = PathUtils.getResourceName(pageName);
174 		request.setAttribute("resourceName", resourceName);
175 		return mapping.findForward("success");
176 	}
177 
178 	public ActionForward createPageMove(ActionMapping mapping, ActionForm form,
179 			HttpServletRequest request, HttpServletResponse response)
180 			throws Exception {
181 
182 		String siteName = TgwContext.getSiteName();
183 		List list = storeSerice.getRecursiveFolderList(siteName, "");
184 		request.setAttribute(Constants.RATTR_FOLDERLIST, list);
185 		return mapping.findForward("success");
186 	}
187 
188 	public ActionForward createPageList(ActionMapping mapping, ActionForm form,
189 			HttpServletRequest request, HttpServletResponse response)
190 			throws Exception {
191 		String siteName = TgwContext.getSiteName();
192 		List list = null;
193 		if ("true".equals(request.getParameter(Constants.PARAM_MODE_FOLDER))) {
194 			list = storeSerice.getRecursiveFolderList(siteName, "");
195 		} else {
196 			list = storeSerice.getRecursivePageList(siteName, "");
197 		}
198 		request.setAttribute(Constants.RATTR_PAGELIST, list);
199 		return mapping.findForward("success");
200 	}
201 
202 	public ActionForward savePageMove(ActionMapping mapping, ActionForm form,
203 			HttpServletRequest request, HttpServletResponse response)
204 			throws Exception {
205 		String siteName = TgwContext.getSiteName();
206 		DynaBean bean = (DynaBean) form;
207 
208 		String pagePath = (String) bean.get("pagePath");
209 		String newParentPath = (String) bean.get("newParentPath");
210 
211 		try {
212 			storeSerice.move(siteName, pagePath, newParentPath);
213 		} catch (TgwResourceAlreadyExistsException e) {
214 			ActionMessages msgs = new ActionMessages();
215 			msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
216 					"msg.folderForm.duplicate"));
217 			saveMessages(request, msgs);
218 		}
219 
220 		// String parent = CharUtil.getParentPath(pagePath);
221 		String parent = newParentPath;
222 		Map map = null;
223 		if (parent != null) {
224 			map = new HashMap();
225 			map.put(Constants.PARAM_PAGENAME, CharUtil.urlDecode(parent));
226 		}
227 		return TgwUtils.dialogCloseForward(mapping, request, siteName,
228 				Constants.ACTION_URI_VIEWFOLDER, map);
229 	}
230 
231 	public ActionForward savePageRename(ActionMapping mapping, ActionForm form,
232 			HttpServletRequest request, HttpServletResponse response)
233 			throws Exception {
234 		String siteName = TgwContext.getSiteName();
235 		DynaBean bean = (DynaBean) form;
236 
237 		String pagePath = (String) bean.get("pagePath");
238 		String newPageName = (String) bean.get("newPageName");
239 
240 		try {
241 			storeSerice.rename(siteName, pagePath, newPageName);
242 		} catch (TgwResourceAlreadyExistsException e) {
243 			ActionMessages msgs = new ActionMessages();
244 			msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
245 					"msg.folderForm.duplicate"));
246 			saveMessages(request, msgs);
247 		}
248 
249 		String parent = PathUtils.getParentPath(pagePath);
250 		Map map = null;
251 		if (parent != null) {
252 			map = new HashMap();
253 			map.put(Constants.PARAM_PAGENAME, CharUtil.urlDecode(parent));
254 		}
255 		return TgwUtils.dialogCloseForward(mapping, request, siteName,
256 				Constants.ACTION_URI_VIEWFOLDER, map);
257 	}
258 
259 	private ActionForward doView(String folderPath, ActionMapping mapping,
260 			ActionForm form, HttpServletRequest request,
261 			HttpServletResponse response) throws Exception {
262 		if (folderPath == null) {
263 			folderPath = "";
264 		}
265 		bindParentPath(request, folderPath);
266 		TgwContext.bindPageName(folderPath);
267 
268 		String siteName = TgwContext.getSiteName();
269 
270 		folderPath = CharUtil.chartrim(folderPath, '/');
271 
272 		List list = storeSerice.getPageOrFolderList(siteName, folderPath);
273 		request.setAttribute(Constants.RATTR_FOLDERCONTENTS, list);
274 
275 		Collections.sort(list, TgwUtils.COMPARATOR_FILEFOLDER);
276 
277 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
278 	}
279 
280 	private void bindParentPath(HttpServletRequest request, String path) {
281 		String parent = PathUtils.getParentPath(path);
282 		if (parent != null) {
283 			request.setAttribute(Constants.RATTR_PARENTFOLDER, parent);
284 		} else if (path.length() > 0) {
285 			request.setAttribute(Constants.RATTR_PARENTFOLDER, "");
286 		}
287 	}
288 }