--- /dev/null
+/*******************************************************************************
+ * Copyright Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+
+
+package com.pentila.entSavoie.gestionGepi;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONException;
+import com.liferay.portal.kernel.json.JSONFactoryUtil;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.pentila.entSavoie.gestionServiceURL.model.ServiceURL;
+import com.pentila.entSavoie.gestionServiceURL.service.ServiceURLLocalServiceUtil;
+
+
+import java.io.IOException;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.GenericPortlet;
+import javax.portlet.PortletException;
+import javax.portlet.PortletRequestDispatcher;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+
+/**
+ * <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
+ *
+ * @author Brian Wing Shun Chan
+ *
+ */
+public class JSPPortlet extends GenericPortlet {
+
+ public void init() throws PortletException {
+ editJSP = getInitParameter("edit-jsp");
+ helpJSP = getInitParameter("help-jsp");
+ viewJSP = getInitParameter("view-jsp");
+ }
+
+ public void doDispatch(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ String jspPage = renderRequest.getParameter("jspPage");
+
+ if (jspPage != null) {
+ include(jspPage, renderRequest, renderResponse);
+ }
+ else {
+ super.doDispatch(renderRequest, renderResponse);
+ }
+ }
+
+ public void doEdit(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ if (renderRequest.getPreferences() == null) {
+ super.doEdit(renderRequest, renderResponse);
+ }
+ else {
+ include(editJSP, renderRequest, renderResponse);
+ }
+ }
+
+ public void doHelp(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ include(helpJSP, renderRequest, renderResponse);
+ }
+
+ public void doView(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ include(viewJSP, renderRequest, renderResponse);
+ }
+
+ public void processAction(
+ ActionRequest actionRequest, ActionResponse actionResponse)
+ throws IOException, PortletException {
+
+ final String cmd = ParamUtil.getString(actionRequest, "cmd");
+ // save configuration
+ if(cmd.equals("saveGepiUrls")){
+ try{
+ this.saveGepiUrls(actionRequest, actionResponse);
+ } catch(Exception e){
+ _log.error(e);
+ }
+ }
+
+ //Delete gepi url
+ if(cmd.equals("deleteGepiURL")){
+ try{
+ this.deleteGepiURL(actionRequest, actionResponse);
+ } catch(Exception e){
+ _log.error(e);
+ }
+ }
+ }
+
+ private void saveGepiUrls(ActionRequest actionRequest,
+ ActionResponse actionResponse) throws JSONException, SystemException {
+ JSONArray gepiUrls = JSONFactoryUtil.createJSONArray(ParamUtil.getString(actionRequest, "gepiUrls"));
+ actionResponse.setRenderParameter("jspPage", "/ajax/defaultJSONResponse.jsp");
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ result.put("success", true);
+
+ for (int i=0; i<gepiUrls.length(); i++) {
+ JSONObject pUrl = gepiUrls.getJSONObject(i);
+ if(pUrl.getLong("gepiURLId") != 0){
+ try {
+ ServiceURL gepiUrl = ServiceURLLocalServiceUtil.getServiceURL(pUrl.getLong("gepiURLId"));
+ gepiUrl.setEtabId(pUrl.getLong("etabId"));
+ gepiUrl.setServiceURL(pUrl.getString("gepiURL"));
+ ServiceURLLocalServiceUtil.updateServiceURL(gepiUrl, true);
+ } catch (PortalException e) {
+ _log.error(e);
+ }
+ } else {
+ ServiceURLLocalServiceUtil.addServiceURL(pUrl.getLong("etabId"), "gepi", pUrl.getString("gepiURL"));
+ }
+ }
+
+ actionResponse.setRenderParameter("result", result.toString());
+
+ }
+
+ private void deleteGepiURL(ActionRequest actionRequest,
+ ActionResponse actionResponse) throws PortalException, SystemException {
+
+ final long pUrlId = ParamUtil.getLong(actionRequest, "gepiURLId");
+ actionResponse.setRenderParameter("jspPage", "/ajax/defaultJSONResponse.jsp");
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ ServiceURLLocalServiceUtil.deleteServiceURL(pUrlId);
+
+ result.put("success", true);
+ result.put("pUrlId", pUrlId);
+
+ actionResponse.setRenderParameter("result", result.toString());
+ }
+
+ protected void include(
+ String path, RenderRequest renderRequest,
+ RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ PortletRequestDispatcher portletRequestDispatcher =
+ getPortletContext().getRequestDispatcher(path);
+
+ if (portletRequestDispatcher == null) {
+ _log.error(path + " is not a valid include");
+ }
+ else {
+ portletRequestDispatcher.include(renderRequest, renderResponse);
+ }
+ }
+
+ protected String editJSP;
+ protected String helpJSP;
+ protected String viewJSP;
+
+ private static Log _log = LogFactoryUtil.getLog(JSPPortlet.class);
+
+}