1 | package edu.iu.uis.sit.portal.portlet.publishing.web; |
2 | |
3 | import java.sql.Timestamp; |
4 | import java.text.NumberFormat; |
5 | import java.util.ArrayList; |
6 | import java.util.Iterator; |
7 | import java.util.List; |
8 | |
9 | import javax.portlet.ActionRequest; |
10 | import javax.portlet.ActionResponse; |
11 | import javax.portlet.PortletRequest; |
12 | import javax.portlet.RenderRequest; |
13 | import javax.portlet.RenderResponse; |
14 | |
15 | import org.springframework.beans.propertyeditors.CustomNumberEditor; |
16 | import org.springframework.beans.propertyeditors.StringTrimmerEditor; |
17 | import org.springframework.validation.BindException; |
18 | import org.springframework.validation.Errors; |
19 | import org.springframework.web.portlet.ModelAndView; |
20 | import org.springframework.web.portlet.bind.PortletRequestDataBinder; |
21 | import org.springframework.web.portlet.mvc.AbstractWizardFormController; |
22 | |
23 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Acl; |
24 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Tab; |
25 | import edu.iu.uis.sit.portal.portlet.publishing.services.PublishingService; |
26 | import edu.iu.uis.sit.portal.portlet.utility.Constants; |
27 | |
28 | public class TabWizardController extends AbstractWizardFormController { |
29 | |
30 | private PublishingService publishingService; |
31 | |
32 | public void afterPropertiesSet() throws Exception { |
33 | if (getPublishingService() == null) { |
34 | throw new IllegalArgumentException("A PublishingService is required"); |
35 | } |
36 | } |
37 | |
38 | protected void handleActionRequestInternal(ActionRequest request, ActionResponse response) throws Exception { |
39 | WebTab webTab = (WebTab) getCommand(request); |
40 | webTab.setAddPublishGroup(request.getParameter("addPublishGroup") != null); |
41 | webTab.setAddViewGroup(request.getParameter("addViewGroup") != null); |
42 | webTab.setAddViewRole(request.getParameter("addViewRole") != null); |
43 | |
44 | if (request.getParameter("removeAclValue") != null && !"".equals(request.getParameter("removeAclValue"))) { |
45 | for (Iterator iter = webTab.getTab().getAcls().iterator(); iter.hasNext();) { |
46 | Acl acl = (Acl) iter.next(); |
47 | if (acl.getKey().equals(request.getParameter("removeAclKey")) && acl.getType().equals(request.getParameter("removeAclType")) && acl.getValue().equals(request.getParameter("removeAclValue"))) { |
48 | iter.remove(); |
49 | break; |
50 | } |
51 | } |
52 | } |
53 | String attributeName = getFormSessionAttributeName(); |
54 | request.getPortletSession().setAttribute(attributeName, (Object) webTab); |
55 | super.handleActionRequestInternal(request, response); |
56 | } |
57 | |
58 | protected ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception { |
59 | ModelAndView modelAndView = super.handleRenderRequestInternal(request, response); |
60 | try { |
61 | WebTab webTab = (WebTab) getRenderCommand(request); |
62 | List groupViews = new ArrayList(); |
63 | List roleViews = new ArrayList(); |
64 | List groupPublishs = new ArrayList(); |
65 | for (Iterator iter = webTab.getTab().getAcls().iterator(); iter.hasNext();) { |
66 | Acl acl = (Acl) iter.next(); |
67 | if (Constants.GROUP_PUBLISH.equals(acl.getType())) { |
68 | groupPublishs.add(acl); |
69 | } else if (Constants.GROUP_VIEW.equals(acl.getType())) { |
70 | groupViews.add(acl); |
71 | } else if (Constants.ROLE_VIEW.equals(acl.getType())) { |
72 | roleViews.add(acl); |
73 | } |
74 | } |
75 | modelAndView.addObject("roleViews", roleViews); |
76 | modelAndView.addObject("groupPublishs", groupPublishs); |
77 | modelAndView.addObject("groupViews", groupViews); |
78 | } catch (Exception e) { |
79 | |
80 | } |
81 | if (request.getParameter("updateTab") != null) { |
82 | modelAndView.addObject("page", 4); |
83 | modelAndView.setViewName("tabPage4"); |
84 | } |
85 | return modelAndView; |
86 | } |
87 | |
88 | protected void processFinish(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception { |
89 | Timestamp now = new Timestamp(System.currentTimeMillis()); |
90 | WebTab webTab = (WebTab) command; |
91 | Tab tab = webTab.getTab(); |
92 | tab.setLastUpdateDate(now); |
93 | if (tab.getTabId() == null) { |
94 | tab.setCreateDate(now); |
95 | } |
96 | |
97 | if (tab.getTabId() == null) { |
98 | response.setRenderParameter("new", "new"); |
99 | } |
100 | String xml = parseExpressionHtml(webTab); |
101 | getPublishingService().saveTab(tab); |
102 | } |
103 | |
104 | private String parseExpressionHtml(WebTab webTab) { |
105 | if (webTab.getWebAclExpression() == null) { |
106 | return ""; |
107 | } |
108 | String xml = webTab.getWebAclExpression(); |
109 | System.out.println(xml); |
110 | xml = xml.replaceAll("<script.*script>", ""); |
111 | xml = xml.replaceAll("<input.*type=\"radio\">", ""); |
112 | System.out.println(xml); |
113 | xml = xml.replaceAll("<a href.*>remove", ""); |
114 | xml = xml.replaceAll("</a>", ""); |
115 | System.out.println(xml); |
116 | return xml; |
117 | } |
118 | |
119 | protected void processCancel(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception { |
120 | response.setRenderParameter("action", "addTab"); |
121 | } |
122 | |
123 | protected void validatePage(Object command, Errors errors, int page, boolean finish) { |
124 | if (finish) { |
125 | this.getValidator().validate(command, errors); |
126 | return; |
127 | } |
128 | WebTab webTab = (WebTab) command; |
129 | TabValidator tabValidator = (TabValidator) getValidator(); |
130 | switch (page) { |
131 | case 0: |
132 | tabValidator.validatePage1(webTab, errors); |
133 | break; |
134 | case 1: |
135 | tabValidator.validatePage2(webTab, errors); |
136 | break; |
137 | case 2: |
138 | tabValidator.validatePage3(webTab, errors); |
139 | break; |
140 | } |
141 | } |
142 | |
143 | protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) throws Exception { |
144 | NumberFormat nf = NumberFormat.getInstance(); |
145 | binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, nf, true)); |
146 | binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true)); |
147 | binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); |
148 | } |
149 | |
150 | protected ModelAndView renderCancel(RenderRequest request, RenderResponse response, Object command, BindException errors) throws Exception { |
151 | ModelAndView modelAndView = new ModelAndView("start"); |
152 | return modelAndView; |
153 | } |
154 | |
155 | protected ModelAndView renderFinish(RenderRequest request, RenderResponse response, Object command, BindException errors) throws Exception { |
156 | if (request.getParameter("new") != null) { |
157 | ModelAndView modelAndView = new ModelAndView("message"); |
158 | modelAndView.addObject("message", "Saved successfully"); |
159 | return modelAndView; |
160 | } |
161 | ModelAndView modelAndView = new ModelAndView("tabPage4"); |
162 | modelAndView.addObject("webTab", ((WebTab) command)); |
163 | modelAndView.addObject("page", 4); |
164 | String attributeName = getFormSessionAttributeName(); |
165 | |
166 | request.getPortletSession().setAttribute(attributeName, command); |
167 | return modelAndView; |
168 | } |
169 | |
170 | protected int getInitialPage(PortletRequest request, Object command) { |
171 | WebTab webTab = (WebTab) command; |
172 | |
173 | if (request.getParameter("tabId") != null) { |
174 | Tab found = getPublishingService().findTabById(new Long(request.getParameter("tabId"))); |
175 | if (found != null) { |
176 | webTab.setTab(found); |
177 | } |
178 | } else { |
179 | webTab.getTab().getAclExpression().setAclExpressionType(Constants.OR); |
180 | } |
181 | return super.getInitialPage(request, webTab); |
182 | } |
183 | |
184 | protected ModelAndView renderInvalidSubmit(RenderRequest request, RenderResponse response) throws Exception { |
185 | return null; |
186 | } |
187 | |
188 | protected void handleInvalidSubmit(ActionRequest request, ActionResponse response) throws Exception { |
189 | response.setRenderParameter("action", "addTab"); |
190 | } |
191 | |
192 | public PublishingService getPublishingService() { |
193 | return publishingService; |
194 | } |
195 | |
196 | public void setPublishingService(PublishingService publishingService) { |
197 | this.publishingService = publishingService; |
198 | } |
199 | |
200 | } |