--- /dev/null
+/**
+ * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.sample.jsp.portlet;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+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;
+import javax.portlet.ResourceRequest;
+import javax.portlet.ResourceResponse;
+import javax.servlet.http.HttpServletResponse;
+
+import com.liferay.portal.kernel.dao.orm.QueryUtil;
+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.ContentTypes;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.util.PortalUtil;
+import com.pentila.entSavoie.casManager.model.AttributSSO;
+import com.pentila.entSavoie.casManager.model.ServiceSSO;
+import com.pentila.entSavoie.casManager.service.AttributSSOLocalServiceUtil;
+import com.pentila.entSavoie.casManager.service.ServiceSSOLocalServiceUtil;
+import com.pentila.entSavoie.gestionServiceURL.NoSuchServiceURLException;
+
+/**
+ * <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);
+ }
+
+ @Override
+ public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
+ throws PortletException, IOException {
+
+ final String cmd = ParamUtil.getString(resourceRequest, "cmd");
+
+ HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(resourceResponse);
+ httpResponse.setContentType(ContentTypes.APPLICATION_JSON);
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", false);
+
+ if (cmd.equals("addService")) {
+ try {
+ result = this.addService(resourceRequest);
+ } catch (SystemException e) {
+ _log.error(e);
+ }
+ }
+ else if (cmd.equals("deleteService")) {
+ try {
+ result = this.deleteService(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ else if (cmd.equals("modifyService")) {
+ try {
+ result = this.modifyService(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ else if (cmd.equals("organizeService")) {
+ try {
+ result = this.organizeService(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ else if (cmd.equals("addAttribut")) {
+ try {
+ result = this.addAttribut(resourceRequest);
+ } catch (final SystemException e) {
+ _log.error(e);
+ }
+ }
+ else if (cmd.equals("deleteAttribut")) {
+ try {
+ result = this.deleteAttribut(resourceRequest);
+ } catch (final Exception e) {
+ _log.error(e);
+ }
+ }
+ else if (cmd.equals("refreshAttribut")) {
+ try {
+ result = this.refreshAttribut(resourceRequest);
+ } catch (final Exception e) {
+ _log.error(e);
+ }
+ }
+ else if (cmd.equals("saveAttribut")) {
+ try {
+ result = this.saveAttribut(resourceRequest);
+ } catch (final Exception e) {
+ _log.error(e);
+ }
+ }
+
+ httpResponse.getWriter().write(result.toString());
+ }
+
+ public void processAction(
+ ActionRequest actionRequest, ActionResponse actionResponse)
+ throws IOException, PortletException {
+
+ }
+
+ private JSONObject addService(ResourceRequest resourceRequest) throws SystemException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ final int order = ParamUtil.getInteger(resourceRequest, "order");
+
+ ServiceSSO sso = ServiceSSOLocalServiceUtil.addServiceSSO("Nouveau service " + order, "", order);
+
+ result.put("success", true);
+ result.put("id", sso.getServiceSSOId());
+ result.put("name", sso.getName());
+ result.put("regEx", sso.getUrlRegExp());
+ result.put("order", sso.getOrders());
+
+ return result;
+ }
+
+ private JSONObject deleteService(ResourceRequest resourceRequest) throws SystemException, PortalException{
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ JSONArray idArray = JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "serviceOrderArray", ""));
+
+ long id;
+ for(int i=0; i < idArray.length(); i++){
+ id = idArray.getInt(i);
+ List<AttributSSO> listAttSSO = AttributSSOLocalServiceUtil.getAttributSSOByServiceId(id);
+ for(AttributSSO att : listAttSSO){
+ AttributSSOLocalServiceUtil.deleteAttributSSO(att);
+ }
+
+ ServiceSSOLocalServiceUtil.deleteServiceSSO(id);
+ }
+
+ result.put("success", true);
+
+ return result;
+ }
+
+ private JSONObject organizeService(ResourceRequest resourceRequest) throws SystemException, JSONException{
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ JSONArray oldOrderArray = JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "serviceOldOrderArray", ""));
+ JSONArray newOrderArray = JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "serviceNewOrderArray", ""));
+
+ int order;
+ List<ServiceSSO> listService = ServiceSSOLocalServiceUtil.getServiceSSOs(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
+ List<ServiceSSO> listNewService = new ArrayList<ServiceSSO>();
+ ServiceSSO temp;
+ for(int i=0; i < oldOrderArray.length(); i++){
+
+ order = oldOrderArray.getInt(i);
+
+ for(ServiceSSO sso : listService){
+ if(order == sso.getOrders()){
+ temp = (ServiceSSO) sso.clone();
+ temp.setOrders(newOrderArray.getInt(i));
+ listNewService.add(temp);
+ break;
+ }
+ }
+ }
+
+ for(ServiceSSO sso : listNewService){
+ ServiceSSOLocalServiceUtil.updateServiceSSO(sso);
+ }
+
+ result.put("success", true);
+
+ return result;
+ }
+
+ private JSONObject modifyService(ResourceRequest resourceRequest) throws SystemException, JSONException, NoSuchServiceURLException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ String name = ParamUtil.getString(resourceRequest, "serviceName", "");
+ String regEx = ParamUtil.getString(resourceRequest, "serviceRegEx", "");
+ int order = ParamUtil.getInteger(resourceRequest, "order");
+
+ saveAttribut(resourceRequest);
+
+ if(name.equals("")){
+ name = "Nouveau Service " + order;
+ }
+
+ List<ServiceSSO> listService = ServiceSSOLocalServiceUtil.getServiceSSOs(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
+ for(ServiceSSO sso : listService){
+ if(order == sso.getOrders()){
+ sso.setName(name);
+ sso.setUrlRegExp(regEx);
+ ServiceSSOLocalServiceUtil.updateServiceSSO(sso);
+ break;
+ }
+ }
+
+ result.put("success", true);
+ result.put("name", name);
+ result.put("regEx", regEx);
+
+ return result;
+ }
+
+ private JSONObject addAttribut(ResourceRequest resourceRequest) throws SystemException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ final Long serviceId = ParamUtil.getLong(resourceRequest, "serviceId");
+ final String samlName = "Nouvel attribut";
+ final String sourceType = ParamUtil.getString(resourceRequest, "sourceType", "");
+ final String sourceAttrName = ParamUtil.getString(resourceRequest, "sourceAttrName", "");
+
+ AttributSSO att = AttributSSOLocalServiceUtil.addAttributSSO(serviceId, samlName, sourceType, sourceAttrName);
+
+ result.put("success", true);
+ result.put("attributSSOId", att.getAttributSSOId());
+ result.put("serviceId", serviceId);
+ result.put("samlName", samlName);
+ result.put("sourceType", sourceType);
+ result.put("sourceAttrName", sourceAttrName);
+
+ return result;
+ }
+
+ private JSONObject deleteAttribut(ResourceRequest resourceRequest) throws SystemException, PortalException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ JSONArray attSSOIdArray =
+ JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "attributSSOIdArray", ""));
+
+ for(int i=0; i < attSSOIdArray.length(); i++) {
+ AttributSSOLocalServiceUtil.deleteAttributSSO(attSSOIdArray.getLong(i));
+ }
+
+ result.put("success", true);
+
+ return result;
+ }
+
+ private JSONObject refreshAttribut(ResourceRequest resourceRequest) throws SystemException, NoSuchServiceURLException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ final Long serviceId = ParamUtil.getLong(resourceRequest, "serviceId");
+
+ List<AttributSSO> listAttributSSO = AttributSSOLocalServiceUtil.getAttributSSOByServiceId(serviceId);
+ JSONArray arrayAttributSSO = JSONFactoryUtil.createJSONArray();
+ for (final AttributSSO att: listAttributSSO) {
+ JSONObject attJSON = JSONFactoryUtil.createJSONObject();
+ attJSON.put("attributSSOId", att.getAttributSSOId());
+ attJSON.put("serviceId", att.getServiceId());
+ attJSON.put("samlName", att.getSamlName());
+ attJSON.put("sourceType", att.getSourceType());
+ attJSON.put("sourceAttrName", att.getSourceAttrName());
+ arrayAttributSSO.put(attJSON);
+ }
+
+ result.put("success", true);
+ result.put("arrayAttributSSO", arrayAttributSSO);
+
+ return result;
+ }
+
+ private JSONObject saveAttribut(ResourceRequest resourceRequest) throws JSONException, NoSuchServiceURLException, SystemException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ final Long serviceId = ParamUtil.getLong(resourceRequest, "serviceId");
+ JSONArray attributAttSSOIdArray =
+ JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "attributAttSSOIdArray", ""));
+ JSONArray attributSamlNameArray =
+ JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "attributSamlNameArray", ""));
+ JSONArray attributSourceTypeArray =
+ JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "attributSourceTypeArray", ""));
+ JSONArray attSourceAttrNameArray =
+ JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "attributSourceAttrNameArray", ""));
+
+ List<AttributSSO> listAttributSSO = AttributSSOLocalServiceUtil.getAttributSSOByServiceId(serviceId);
+
+ for (int i = 0; i < attributAttSSOIdArray.length(); i++) {
+ for (final AttributSSO att : listAttributSSO) {
+ if (attributAttSSOIdArray.getLong(i) == att.getAttributSSOId()) {
+ att.setSamlName(attributSamlNameArray.getString(i));
+ att.setSourceType(attributSourceTypeArray.getString(i));
+ att.setSourceAttrName(attSourceAttrNameArray.getString(i));
+ AttributSSOLocalServiceUtil.updateAttributSSO(att);
+ }
+ }
+ }
+ result.put("success", true);
+
+ return result;
+ }
+
+ 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);
+
+}
\ No newline at end of file