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.List;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.action.ActionForward;
27  import org.apache.struts.action.ActionMapping;
28  import org.apache.struts.action.ActionMessage;
29  import org.apache.struts.action.ActionMessages;
30  import org.apache.struts.actions.MappingDispatchAction;
31  import org.seasar.tuigwaa.cms.ContentsStoreService;
32  import org.seasar.tuigwaa.system.Constants;
33  import org.seasar.tuigwaa.system.DesignService;
34  import org.seasar.tuigwaa.system.SiteConfig;
35  import org.seasar.tuigwaa.system.SiteService;
36  import org.seasar.tuigwaa.util.TgwContext;
37  
38  /***
39   * @author someda
40   */
41  public class DesignAction extends MappingDispatchAction {
42  
43  	private Log log = LogFactory.getLog(getClass());
44  	
45  	private SiteService siteService;
46  	
47  	private ContentsStoreService contentsStoreService;
48  	
49  	private DesignService designService;
50  		
51  	public DesignAction(SiteService siteService,
52  			ContentsStoreService contentsStoreService,
53  			DesignService designService){
54  		this.siteService = siteService;
55  		this.contentsStoreService = contentsStoreService;
56  		this.designService = designService;		
57  	}
58  	
59  	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
60  		
61  		String siteName = TgwContext.getSiteName();
62  		SiteConfig siteConfig = siteService.getSiteConfig(siteName);
63  		
64  		bindPageList(request);
65  		bindSkinList(siteName, request);
66  						
67  		DesignForm designForm = new DesignForm();
68  		designForm.setMenuPagename(siteConfig.getMenuPagename());
69  		designForm.setRightPagename(siteConfig.getRightPagename());		
70  		request.setAttribute("designForm",designForm);
71  		
72  		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
73  	}
74  	
75  	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {	
76  		
77  		String siteName = TgwContext.getSiteName();
78  		SiteConfig siteConfig = siteService.getSiteConfig(siteName);
79  		
80  		bindSkinList(siteName,request);
81  		bindPageList(request);
82  		
83  		DesignForm designForm = (DesignForm) form;		
84  		
85  		String menuPagename = designForm.getMenuPagename();
86  		String rightPagename = designForm.getRightPagename();		
87  		siteConfig.setMenuPagename(menuPagename);
88  		
89  		if(rightPagename != null && !"".equals(rightPagename))
90  			siteConfig.setRightPagename(rightPagename);
91  		
92  		siteService.editSite(siteConfig);
93  				
94  		ActionMessages msgs = new ActionMessages();
95  		msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("msg.design.save"));
96  		saveMessages(request, msgs);		
97  										
98  		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
99  	}
100 	
101 	public ActionForward change(ActionMapping mapping, ActionForm form,
102 			HttpServletRequest request, HttpServletResponse response) throws Exception{
103 		
104 		String siteName = TgwContext.getSiteName();
105 		SiteConfig siteConfig = siteService.getSiteConfig(siteName);
106 		
107 		bindSkinList(siteName,request);
108 		bindPageList(request);
109 		
110 		String skinName = request.getParameter(Constants.PARAM_SKIN_NAME);
111 		siteConfig.setSkin(skinName);		
112 		siteService.editSite(siteConfig);
113 		siteService.updatePath(siteConfig, true);
114 		
115 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
116 	}
117 	
118 	public ActionForward sync(ActionMapping mapping, ActionForm form, 
119 			HttpServletRequest request, HttpServletResponse response) throws Exception {
120 		
121 		String siteName = TgwContext.getSiteName();
122 		SiteConfig siteConfig = TgwContext.getSiteConfig();
123 
124 		bindSkinList(siteName, request);
125 		bindPageList(request);
126 		
127 		designService.sync(siteConfig,true);
128 		siteService.updatePath(siteConfig, true);
129 		
130 		return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
131 	}
132 		
133 	// ------ [private methods ] -----
134 	
135 	private void bindSkinList(String siteName, HttpServletRequest request){
136 		String[] skins = designService.getAvailableSkinNames(siteName);
137 		request.setAttribute(Constants.RATTR_SKINS, skins);		
138 	}	
139 		
140 	private void bindPageList(HttpServletRequest request){
141 		String siteName = TgwContext.getSiteName();
142 		try{
143 			List list = contentsStoreService.getRecursivePageList(siteName,"");		
144 			request.setAttribute(Constants.RATTR_PAGELIST,list);
145 		}catch(Exception e){
146 			log.error("failed to retrieve page list : " + e.getMessage());			
147 		}
148 	}
149 		
150 }