View Javadoc
1   /*
2    * Copyright (c) 2015, Fraunhofer FOKUS
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * 
8    * * Redistributions of source code must retain the above copyright notice, this
9    *   list of conditions and the following disclaimer.
10   * 
11   * * Redistributions in binary form must reproduce the above copyright notice,
12   *   this list of conditions and the following disclaimer in the documentation
13   *   and/or other materials provided with the distribution.
14   * 
15   * * Neither the name of particity nor the names of its
16   *   contributors may be used to endorse or promote products derived from
17   *   this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   * 
30   * 
31   * 
32   * @author sma
33   */
34  package de.fraunhofer.fokus.oefit.particity.portlet;
35  
36  import java.util.List;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.PortletRequest;
41  import javax.portlet.RenderRequest;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import com.liferay.portal.kernel.exception.SystemException;
46  import com.liferay.portal.kernel.servlet.ServletResponseUtil;
47  import com.liferay.portal.kernel.util.WebKeys;
48  import com.liferay.portal.model.Role;
49  import com.liferay.portal.model.User;
50  import com.liferay.portal.theme.ThemeDisplay;
51  import com.liferay.portal.util.PortalUtil;
52  
53  import de.fraunhofer.fokus.oefit.adhoc.custom.Constants;
54  import de.fraunhofer.fokus.oefit.adhoc.custom.CustomPortalServiceHandler;
55  import de.fraunhofer.fokus.oefit.adhoc.custom.E_ConfigKey;
56  import de.fraunhofer.fokus.oefit.adhoc.custom.E_Role;
57  
58  /**
59   * An abstract controller class to bundle common utility methods used by all controllers
60   */
61  public class BaseController {
62  
63  	/**
64  	 * Copy render parameter.
65  	 *
66  	 * @param name the name
67  	 * @param request the request
68  	 * @param response the response
69  	 */
70  	protected void copyRenderParameter(final String name,
71  	        final ActionRequest request, final ActionResponse response) {
72  		final String paramValue = request.getParameter(name);
73  		if (paramValue != null) {
74  			response.setRenderParameter(name, paramValue);
75  		}
76  	}
77  
78  	/**
79  	 * Gets the company id.
80  	 *
81  	 * @param request the request
82  	 * @return the company id
83  	 */
84  	protected Long getCompanyId(final PortletRequest request) {
85  		final ThemeDisplay themeDisplay = (ThemeDisplay) request
86  		        .getAttribute(WebKeys.THEME_DISPLAY);
87  		return themeDisplay.getCompanyId();
88  	}
89  
90  	private String getGlobalParam(final PortletRequest request,
91  	        final String name) {
92  		return PortalUtil.getOriginalServletRequest(
93  		        PortalUtil.getHttpServletRequest(request)).getParameter(name);
94  	}
95  
96  	/**
97  	 * Gets the group id.
98  	 *
99  	 * @param request the request
100 	 * @return the group id
101 	 */
102 	protected Long getGroupId(final PortletRequest request) {
103 		final ThemeDisplay themeDisplay = (ThemeDisplay) request
104 		        .getAttribute(WebKeys.THEME_DISPLAY);
105 		return themeDisplay.getScopeGroupId();
106 	}
107 
108 	private Long getId(final PortletRequest request, final String name) {
109 		final String offerId = request.getParameter(name);
110 		Long l_offerId = null;
111 		if (offerId != null) {
112 			l_offerId = this.toLong(offerId);
113 		}
114 		return l_offerId;
115 	}
116 
117 	private Integer getIntId(final ActionRequest request, final String name) {
118 		final String offerId = request.getParameter(name);
119 		Integer l_offerId = null;
120 		if (offerId != null) {
121 			l_offerId = this.toInt(offerId);
122 		}
123 		return l_offerId;
124 	}
125 
126 	/**
127 	 * Gets the offer id.
128 	 *
129 	 * @param request the request
130 	 * @return the offer id
131 	 */
132 	protected Long getOfferId(final ActionRequest request) {
133 		return this.getId(request, "offerId");
134 	}
135 
136 	/**
137 	 * Gets the org id.
138 	 *
139 	 * @param request the request
140 	 * @return the org id
141 	 */
142 	protected Long getOrgId(final PortletRequest request) {
143 		return this.getId(request, "orgId");
144 	}
145 
146 	/*
147 	 * protected Long getCompanyId(PortletRequest request) { ThemeDisplay
148 	 * themeDisplay = (ThemeDisplay)
149 	 * request.getAttribute(WebKeys.THEME_DISPLAY); return
150 	 * themeDisplay.getCompanyId(); }
151 	 */
152 
153 	/**
154 	 * Gets the result skip.
155 	 *
156 	 * @param request the request
157 	 * @return the result skip
158 	 */
159 	protected Integer getResultSkip(final ActionRequest request) {
160 		return this.getIntId(request, "resultSkip");
161 	}
162 
163 	/**
164 	 * Gets the role.
165 	 *
166 	 * @param display the display
167 	 * @return the role
168 	 */
169 	protected E_Role getRole(final ThemeDisplay display) {
170 		E_Role result = null;
171 
172 		final User user = display.getUser();
173 		if (user != null && user.getEmailAddress() != null) {
174 			List<Role> roles = null;
175 			try {
176 				roles = user.getRoles();
177 			} catch (final SystemException e) {
178 			}
179 			if (roles != null) {
180 				for (final Role role : roles) {
181 					result = CustomPortalServiceHandler.matchesRole(role);
182 					if (result != null) {
183 						break;
184 					}
185 				}
186 			}
187 		}
188 
189 		return result;
190 
191 	}
192 
193 	/**
194 	 * Gets the search cat id.
195 	 *
196 	 * @param request the request
197 	 * @return the search cat id
198 	 */
199 	protected Long[] getSearchCatId(final RenderRequest request) {
200 		Long[] result = null;
201 		final String param = this.getGlobalParam(request, "catId");
202 		if (param != null) {
203 			final String[] ids = param.split(",");
204 			if (ids != null && ids.length > 0) {
205 				result = new Long[ids.length];
206 				for (int i = 0; i < ids.length; i++) {
207 					result[i] = this.toLong(ids[i]);
208 				}
209 			}
210 		}
211 		return result;
212 	}
213 
214 	/**
215 	 * Gets the search cat id str.
216 	 *
217 	 * @param request the request
218 	 * @return the search cat id str
219 	 */
220 	protected String[] getSearchCatIdStr(final RenderRequest request) {
221 		String[] result = null;
222 		final String param = this.getGlobalParam(request, "catId");
223 		if (param != null) {
224 			result = param.split(",");
225 
226 		}
227 		return result;
228 	}
229 
230 	/**
231 	 * Gets the sub id.
232 	 *
233 	 * @param request the request
234 	 * @return the sub id
235 	 */
236 	protected Long getSubId(final ActionRequest request) {
237 		return this.getId(request, "subId");
238 	}
239 
240 	/**
241 	 * Gets the theme display.
242 	 *
243 	 * @param request the request
244 	 * @return the theme display
245 	 */
246 	protected ThemeDisplay getThemeDisplay(final PortletRequest request) {
247 		return (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
248 	}
249 
250 	/**
251 	 * Gets the user id.
252 	 *
253 	 * @param request the request
254 	 * @return the user id
255 	 */
256 	protected Long getUserId(final PortletRequest request) {
257 		final ThemeDisplay themeDisplay = (ThemeDisplay) request
258 		        .getAttribute(WebKeys.THEME_DISPLAY);
259 		return themeDisplay.getUserId();
260 	}
261 
262 	/**
263 	 * Checks if is logged in mgmt.
264 	 *
265 	 * @param request the request
266 	 * @return true, if is logged in mgmt
267 	 */
268 	protected boolean isLoggedInMgmt(final PortletRequest request) {
269 		boolean result = false;
270 		final ThemeDisplay themeDisplay = (ThemeDisplay) request
271 		        .getAttribute(WebKeys.THEME_DISPLAY);
272 		if (themeDisplay.isSignedIn()) {
273 			final User user = themeDisplay.getUser();
274 			if (user != null) {
275 				try {
276 					final List<Role> roles = user.getRoles();
277 					String mgmtRoleName = CustomPortalServiceHandler.getConfigValue(E_ConfigKey.ROLE_NAME_MGMT);
278 					for (final Role role : roles) {
279 						if (role.getName().equals(mgmtRoleName)) {
280 							result = true;
281 							break;
282 						}
283 					}
284 				} catch (final Throwable t) {
285 				}
286 			}
287 		}
288 		return result;
289 	}
290 
291 	/**
292 	 * Checks if is logged in org.
293 	 *
294 	 * @param request the request
295 	 * @return true, if is logged in org
296 	 */
297 	protected boolean isLoggedInOrg(final PortletRequest request) {
298 		boolean result = false;
299 		final ThemeDisplay themeDisplay = (ThemeDisplay) request
300 		        .getAttribute(WebKeys.THEME_DISPLAY);
301 		if (themeDisplay.isSignedIn()) {
302 			final User user = themeDisplay.getUser();
303 			if (user != null) {
304 				try {
305 					final List<Role> roles = user.getRoles();
306 					final String orgRoleName = CustomPortalServiceHandler.getConfigValue(E_ConfigKey.ROLE_NAME_ORG);
307 					for (final Role role : roles) {
308 						if (role.getName().equals(orgRoleName)) {
309 							result = true;
310 							break;
311 						}
312 					}
313 				} catch (final Throwable t) {
314 				}
315 			}
316 		}
317 		return result;
318 	}
319 
320 	/**
321 	 * Send file.
322 	 *
323 	 * @param name the name
324 	 * @param mimeType the mime type
325 	 * @param contents the contents
326 	 * @param request the request
327 	 * @param response the response
328 	 */
329 	protected void sendFile(final String name, final String mimeType,
330 	        final byte[] contents, final ActionRequest request,
331 	        final ActionResponse response) {
332 		try {
333 			if (name != null && mimeType != null && contents != null) {
334 				final HttpServletRequest httpServletRequest = PortalUtil
335 				        .getHttpServletRequest(request);
336 				final HttpServletResponse httpServletResponse = PortalUtil
337 				        .getHttpServletResponse(response);
338 
339 				httpServletResponse.setContentType(mimeType);
340 				httpServletResponse.setHeader("Expires", "0");
341 				httpServletResponse.setHeader("Cache-Control",
342 				        "must-revalidate, post-check=0, pre-check=0");
343 				httpServletResponse.setHeader("Pragma", "public");
344 				httpServletResponse.setContentLength(contents.length);
345 				ServletResponseUtil.sendFile(httpServletRequest,
346 				        httpServletResponse, name, contents);
347 			}
348 		} catch (final Throwable t) {
349 			t.printStackTrace();
350 		}
351 	}
352 
353 	/**
354 	 * To int.
355 	 *
356 	 * @param str the str
357 	 * @return the integer
358 	 */
359 	protected Integer toInt(final String str) {
360 		Integer result = null;
361 
362 		try {
363 			result = Integer.parseInt(str);
364 		} catch (final Throwable t) {
365 		}
366 
367 		return result;
368 	}
369 
370 	/**
371 	 * To long.
372 	 *
373 	 * @param str the str
374 	 * @return the long
375 	 */
376 	protected Long toLong(final String str) {
377 		Long result = null;
378 
379 		try {
380 			result = Long.parseLong(str);
381 		} catch (final Throwable t) {
382 		}
383 
384 		return result;
385 	}
386 
387 }