accept-invite-portlet/src/main/java/org/gcube/portlets/user/acceptinvite/PortletViewController.java

173 lines
6.3 KiB
Java

/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 GNU Lesser General Public License for more
* details.
*/
package org.gcube.portlets.user.acceptinvite;
import java.io.IOException;
import javax.portlet.PortalContext;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.binary.Base64;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.Invite;
import org.gcube.portal.databook.shared.InviteStatus;
import org.gcube.portal.databook.shared.ex.InviteIDNotFoundException;
import org.gcube.portal.databook.shared.ex.InviteStatusNotFoundException;
import org.gcube.portal.invites.InvitesManager;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;
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.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.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
@Controller(value = "PortletViewController")
@RequestMapping("VIEW")
public class PortletViewController {
private static Log _log = LogFactoryUtil.getLog(PortletViewController.class);
private static String PAGE_NOT_AUTHORIZED = "not-authorized";
private static String PAGE_INVITE_NOTFOUND = "invite-notfound";
private static String PAGE_VRE_NOTFOUND = "vre-notfound";
private static String PAGE_INVITE_EXPIRED = "invite-expired";
private static String PAGE_INVITE_PROCESS = "view";
public static String INVITE_INSTANCE = "inviteInstance";
private static DatabookStore store;
/**
*
* @return the unique instance of the store
*/
public static synchronized DatabookStore getStore() {
if (store == null) {
store = new DBCassandraAstyanaxImpl();
}
return store;
}
// .append(PortalContext.getConfiguration().getSiteLandingPagePath(request))
@RenderMapping
public String handleRenderRequest(RenderRequest request,RenderResponse response, Model model) {
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
final String INVITE_ID_ENCODED = new String(Base64.encodeBase64(InvitesManager.INVITEID_ATTR.getBytes()));
final String SITE_ID_ENCODED = new String(Base64.encodeBase64(InvitesManager.SITEID_ATTR.getBytes()));
if (httpReq.getParameter(INVITE_ID_ENCODED) == null || SITE_ID_ENCODED == null)
return PAGE_NOT_AUTHORIZED;
String inviteIdEncoded = (String) httpReq.getParameter(INVITE_ID_ENCODED);
String siteIdEncoded = (String) httpReq.getParameter(SITE_ID_ENCODED);
String inviteId= new String(Base64.decodeBase64(inviteIdEncoded));
String groupId = new String(Base64.decodeBase64(siteIdEncoded));
_log.info("GOT inviteId=" + inviteId);
_log.info("siteId=" + groupId);
Group site = null;
try {
site = GroupLocalServiceUtil.getGroup(Long.parseLong(groupId));
} catch (Exception e1) {
e1.printStackTrace();
return PAGE_VRE_NOTFOUND;
}
Invite invite = null;
try {
invite = getStore().readInvite(inviteId);
} catch (InviteIDNotFoundException | InviteStatusNotFoundException e) {
e.printStackTrace();
return PAGE_INVITE_NOTFOUND;
}
if (invite.getStatus() == InviteStatus.ACCEPTED)
return PAGE_INVITE_EXPIRED;
GCubeUser invitedUser = null;
try {
invitedUser = new LiferayUserManager().getUserByEmail(invite.getInvitedEmail());
model.addAttribute("invitedUser", invitedUser);
} catch (UserManagementSystemException | UserRetrievalFault e) {
_log.info("No user account exist with this email: " + invite.getInvitedEmail());
}
//we set the invite instance retrieved in the model
model.addAttribute(INVITE_INSTANCE, invite);
model.addAttribute("vreName", site.getName());
model.addAttribute("vreFriendlyURL", site.getFriendlyURL());
model.addAttribute("groupId", site.getGroupId());
return PAGE_INVITE_PROCESS;
}
@ResourceMapping(value="findState")
public void findStateForCountry(ResourceRequest request, ResourceResponse response) throws IOException {
String countryName = ParamUtil.getString(request, "countryName");
JSONArray stateArray = JSONFactoryUtil.createJSONArray();
JSONObject stateObject,stateObject2;
if(countryName.equalsIgnoreCase("india"))
{
stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("stateId", "1");
stateObject.put("name", "Delhi");
stateObject2 = JSONFactoryUtil.createJSONObject();
stateObject2.put("stateId", "2");
stateObject2.put("name", "Gujrat");
}
else{
stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("stateId", "21");
stateObject.put("name", "LA");
stateObject2 = JSONFactoryUtil.createJSONObject();
stateObject2.put("stateId", "22");
stateObject2.put("name", "California");
}
stateArray.put(stateObject);
stateArray.put(stateObject2);
response.getWriter().println(stateArray);
}
}