1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.controller;
17
18 import java.util.Enumeration;
19 import java.util.List;
20 import java.util.Properties;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32 import org.apache.struts.actions.MappingDispatchAction;
33 import org.seasar.tuigwaa.cms.ContentsStoreService;
34 import org.seasar.tuigwaa.cms.core.CmsConstants;
35 import org.seasar.tuigwaa.logic.LogicService;
36 import org.seasar.tuigwaa.logic.TgwEvent;
37 import org.seasar.tuigwaa.logic.TgwFunction;
38 import org.seasar.tuigwaa.logic.TgwLogic;
39 import org.seasar.tuigwaa.logic.functor.PageJumpProcedure;
40 import org.seasar.tuigwaa.system.Constants;
41 import org.seasar.tuigwaa.system.DesignConfig;
42 import org.seasar.tuigwaa.system.DesignService;
43 import org.seasar.tuigwaa.system.SiteConfig;
44 import org.seasar.tuigwaa.util.TgwContext;
45 import org.seasar.tuigwaa.util.TgwResource;
46 import org.seasar.tuigwaa.util.functor.TruePredicate;
47
48 import com.isenshi.util.CharUtil;
49
50
51 /***
52 * @author someda
53 */
54 public class PageSettingAction extends MappingDispatchAction {
55
56 private Log log = LogFactory.getLog(getClass());
57
58 private ContentsStoreService contentsStoreService;
59
60 private DesignService designService;
61
62 private LogicService logicService;
63
64 public PageSettingAction(ContentsStoreService contentsStoreService, DesignService designService, LogicService logicService){
65 this.contentsStoreService = contentsStoreService;
66 this.designService = designService;
67 this.logicService = logicService;
68 }
69
70 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
71
72 TgwContext.initPageName();
73 String pageName = TgwContext.getPageName();
74 String siteName = TgwContext.getSiteName();
75 TgwContext.bindPageName(pageName);
76
77 bindPageList(request);
78 bindSkinList(request);
79
80 Properties props = contentsStoreService.getProperty(siteName,pageName);
81 PageSettingForm pageSettingForm = new PageSettingForm();
82 pageSettingForm.setSettings(props);
83
84 String lockUser = props.getProperty(CmsConstants.PROPERTY_LOCKUSER);
85 if(lockUser != null){
86 pageSettingForm.setLock(true);
87 if(!isUserAbleToOverwrite(request,lockUser)){
88 request.setAttribute(Constants.RATTR_PAGELOCK,"true");
89 }
90 }
91
92 TgwLogic logic = logicService.getLogic(siteName,getAliasLogicName(pageName));
93 if(logic != null){
94 TgwEvent event = logic.getEvent();
95 if(TgwEvent.PAGE_VIEW.equals(event.getType())){
96 pageSettingForm.setAlias(event.getOption());
97 }
98 }
99 request.setAttribute(Constants.RATTR_PAGESETTINGFORM,pageSettingForm);
100 return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
101 }
102
103 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
104 PageSettingForm pageSettingForm = (PageSettingForm)form;
105 TgwContext.initPageName();
106 String pageName = pageSettingForm.getPageName();
107 String siteName = TgwContext.getSiteName();
108 TgwContext.bindPageName(pageName);
109
110 bindPageList(request);
111 bindSkinList(request);
112
113 boolean isInitialize = (request.getParameter("initialize") != null);
114
115 ActionMessages msgs = new ActionMessages();
116 if(isInitialize){
117 doInitialize(msgs,siteName,pageName);
118 }else{
119 doSave(request,msgs,pageSettingForm,siteName,pageName);
120 }
121
122 if(logicService.getLogicList(siteName) != null){
123 logicService.saveLogics(siteName);
124 }
125
126 saveMessages(request, msgs);
127 return mapping.findForward(Constants.LOCAL_FORWARD_SUCCESS);
128 }
129
130 private void doInitialize(ActionMessages msgs, String siteName, String pageName) throws Exception{
131 Properties props = contentsStoreService.getProperty(siteName,pageName);
132
133 if(props != null){
134 Enumeration penum = props.keys();
135 while(penum.hasMoreElements()){
136 String key = (String)penum.nextElement();
137 contentsStoreService.removeProperty(siteName,pageName,key);
138 }
139 }
140 logicService.deleteLogic(siteName,getAliasLogicName(pageName));
141 msgs.add(ActionMessages.GLOBAL_MESSAGE,
142 new ActionMessage("msg.pageSetting.initialize"));
143 }
144
145 private void doSave(HttpServletRequest request,ActionMessages msgs, PageSettingForm pageSettingForm,String siteName, String pageName)
146 throws Exception{
147
148 handleLock(request,pageSettingForm,msgs);
149 contentsStoreService.setProperty(siteName,pageName,pageSettingForm.getSettings());
150
151 String alias = pageSettingForm.getAlias();
152 if(alias != null && !"".equals(alias)){
153 TgwEvent event = TgwEvent.createPageViewEvent(alias);
154 TgwFunction function = new TgwFunction();
155 function.setPredicate(TruePredicate.INSTANCE);
156 function.setProcedure(new PageJumpProcedure(CharUtil.charpop(pageName,'/')));
157 TgwLogic logic = new TgwLogic(getAliasLogicName(pageName),event,function);
158 logicService.editLogic(siteName,logic);
159 }else{
160 logicService.deleteLogic(siteName,getAliasLogicName(pageName));
161 }
162
163 msgs.add(ActionMessages.GLOBAL_MESSAGE,
164 new ActionMessage("msg.pageSetting.save"));
165 }
166
167 private void handleLock(HttpServletRequest request,PageSettingForm pageSettingForm,ActionMessages msgs){
168
169 String pageName = TgwContext.getPageName();
170 String siteName = TgwContext.getSiteName();
171 boolean isLock = pageSettingForm.isLock();
172
173 try{
174 Properties oldProps = contentsStoreService.getProperty(siteName,pageName);
175 String oldLockUser = oldProps.getProperty(CmsConstants.PROPERTY_LOCKUSER);
176 if(isLock){
177 bindLock(request,oldLockUser,pageSettingForm,msgs);
178 }else{
179 unbindLock(request,oldLockUser);
180 }
181 }catch(Exception e){
182 log.error("failed to handle lock/unlock request");
183 e.printStackTrace();
184 }
185 }
186
187 private void bindLock(HttpServletRequest request,String oldLockUser,PageSettingForm pageSettingForm,ActionMessages msgs){
188
189 String remoteUser = request.getRemoteUser();
190 String pageName = TgwContext.getPageName();
191
192 Properties props = pageSettingForm.getSettings();
193
194 if(remoteUser == null){
195
196 pageSettingForm.setLock(false);
197 msgs.add(ActionMessages.GLOBAL_MESSAGE,
198 new ActionMessage("msg.pageSetting.failtolock"));
199 }else{
200 if(oldLockUser == null || isUserAbleToOverwrite(request,oldLockUser)){
201 props.setProperty(CmsConstants.PROPERTY_LOCKUSER,remoteUser);
202 log.info("lock page " + pageName + " with user " + remoteUser);
203 }else{
204 log.warn("current user cannot have enough priviledge to override lock settings.");
205 }
206 }
207 }
208
209 private void unbindLock(HttpServletRequest request,String oldLockUser) throws Exception{
210
211 String remoteUser = request.getRemoteUser();
212 String pageName = TgwContext.getPageName();
213 String siteName = TgwContext.getSiteName();
214
215 if(remoteUser == null){
216 if(oldLockUser != null){
217 log.warn("unauthorized user cannot unlock page, this should not be happen.");
218 }
219 }else{
220 if(oldLockUser != null){
221 if(isUserAbleToOverwrite(request,oldLockUser)){
222 contentsStoreService.removeProperty(siteName,pageName,CmsConstants.PROPERTY_LOCKUSER);
223 log.info("unlock page " + pageName + " by user " + remoteUser);
224 }else{
225 log.warn("current user cannot have enough priviledge to override lock settings.");
226 }
227 }
228 }
229 }
230
231 private boolean isUserAbleToOverwrite(HttpServletRequest request, String targetUser){
232 boolean flag = false;
233
234 if(request.isUserInRole(TgwResource.getProperty("ldap.admin.rolename"))){
235 flag = true;
236 }else{
237 String remoteUser = request.getRemoteUser();
238 if(targetUser.equals(remoteUser)){
239 flag = true;
240 }
241 }
242 return flag;
243 }
244
245 private void bindPageList(HttpServletRequest request){
246 String siteName = TgwContext.getSiteName();
247 try{
248 List list = contentsStoreService.getRecursivePageList(siteName,"");
249 request.setAttribute(Constants.RATTR_PAGELIST,list);
250 }catch(Exception e){
251 log.error("failed to retrieve page list : " + e.getMessage());
252 }
253 }
254
255 private void bindSkinList(HttpServletRequest request){
256 SiteConfig siteConfig = TgwContext.getSiteConfig();
257 DesignConfig config = designService.getDesignConfig(siteConfig.getSkin());
258 request.setAttribute(Constants.RATTR_SKIN_PAGELIST,config.getAvailablePages());
259 }
260
261 private String getAliasLogicName(String pageName){
262 pageName = CharUtil.charpop(pageName,'/');
263 return pageName + Constants.LOGIC_PAGEALIAS_SUFFIX;
264 }
265
266 }