tabular-data-sdmx-export-wi.../src/main/java/org/gcube/portlets/user/sdmxexportwizardtd/server/SessionUtil.java

167 lines
5.6 KiB
Java

/**
*
*/
package org.gcube.portlets.user.sdmxexportwizardtd.server;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.analysis.tabulardata.service.operation.Task;
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Agencies;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Codelist;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Constants;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Dataset;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.SDMXExportSession;
import org.gcube.portlets.user.td.gxtservice.shared.TRId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author "Giancarlo Panichi" <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class SessionUtil {
protected static final String SDMX_CLIENT_ATTRIBUTE = "SDMX_CLIENT";
protected static final String SDMX_EXPORT_SESSION = "SDMX_EXPORT";
protected static final String SDMX_TR_TASK = "SDMX_TR_TASK";
protected static final String SDMX_TR_ID = "SDMX_TR_ID";
protected static Logger logger = LoggerFactory.getLogger(SessionUtil.class);
public static void setSDMXExportSession(HttpSession httpSession, SDMXExportSession s){
SDMXExportSession session=(SDMXExportSession)httpSession.getAttribute(SDMX_EXPORT_SESSION);
if(session!=null) httpSession.removeAttribute(SDMX_EXPORT_SESSION);
httpSession.setAttribute(SDMX_EXPORT_SESSION, s);
ASLSession aslSession = getAslSession(httpSession);
ScopeProvider.instance.set(aslSession.getScope().toString());
}
public static SDMXExportSession getSDMXExportSession(HttpSession httpSession){
SDMXExportSession sdmxExportSession = (SDMXExportSession) httpSession.getAttribute(SDMX_EXPORT_SESSION);
if(sdmxExportSession==null){
logger.error("SDMXImportSession was not acquired");
}
return sdmxExportSession;
}
public static List<Codelist> retrieveCodelists(HttpSession httpSession)
throws Exception {
logger.info("SessionUtil retriveCodelists");
SDMXClient client = getSdmxClient(httpSession);
ASLSession aslSession = getAslSession(httpSession);
ScopeProvider.instance.set(aslSession.getScope().toString());
return client.getAllCodelists();
}
public static List<Dataset> retrieveDatasets(HttpSession httpSession)
throws Exception {
logger.info("SessionUtil retriveDatasets");
SDMXClient client = getSdmxClient(httpSession);
ASLSession aslSession = getAslSession(httpSession);
ScopeProvider.instance.set(aslSession.getScope().toString());
return client.getAllDatasets();
}
public static List<Agencies> retrieveAgencies(HttpSession httpSession)
throws Exception {
logger.info("SessionUtil retriveAgencies");
SDMXClient client = getSdmxClient(httpSession);
if(client==null){
logger.error("SessionUtil retriveAgencies client null");
throw new Exception("SessionUtil retriveAgencies client null");
} else {
ASLSession aslSession = getAslSession(httpSession);
ScopeProvider.instance.set(aslSession.getScope().toString());
return client.getAllAgencies();
}
}
public static SDMXClient getSdmxClient(HttpSession httpSession){
SDMXClient sdmxClient = (SDMXClient) httpSession.getAttribute(SDMX_CLIENT_ATTRIBUTE);
SDMXExportSession sdmxExportSession=(SDMXExportSession) httpSession.getAttribute(SDMX_EXPORT_SESSION);
if(sdmxExportSession==null){
logger.error("SDMXExportSession was not acquired");
} else {
if (sdmxClient == null) {
sdmxClient = new SDMXClient();
} else {
if(sdmxClient.type.compareTo(SDMXClient.TYPE.ANOTHER)==0){
sdmxClient = new SDMXClient();
}
}
}
return sdmxClient;
}
protected static ASLSession getAslSession(HttpSession httpSession) {
String username = (String) httpSession
.getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
if (username == null) {
logger.warn("no user found in session, using test one");
username = Constants.DEFAULT_USER;
String scope = Constants.DEFAULT_SCOPE;
httpSession.setAttribute(ScopeHelper.USERNAME_ATTRIBUTE, username);
ASLSession session = SessionManager.getInstance().getASLSession(
httpSession.getId(), username);
session.setScope(scope);
return session;
} else {
return SessionManager.getInstance().getASLSession(
httpSession.getId(), username);
}
}
public static Task getTRTask(HttpSession httpSession){
Task task = (Task) httpSession.getAttribute(SDMX_TR_TASK);
if(task==null){
logger.error("SDMX_TR_TASK was not acquired");
}
return task;
}
public static void setTRTask(HttpSession httpSession, Task task){
Task t = (Task) httpSession.getAttribute(SDMX_TR_TASK);
if(t!=null) httpSession.removeAttribute(SDMX_TR_TASK);
httpSession.setAttribute(SDMX_TR_TASK, task);
ASLSession aslSession = getAslSession(httpSession);
ScopeProvider.instance.set(aslSession.getScope().toString());
}
public static TRId getTRId(HttpSession httpSession){
TRId id = (TRId) httpSession.getAttribute(SDMX_TR_ID);
if(id==null){
logger.error("SDMX_TR_ID was not acquired");
}
return id;
}
public static void setTRId(HttpSession httpSession, TRId trId){
TRId id = (TRId) httpSession.getAttribute(SDMX_TR_ID);
if(id!=null) httpSession.removeAttribute(SDMX_TR_ID);
httpSession.setAttribute(SDMX_TR_ID, trId);
ASLSession aslSession = getAslSession(httpSession);
ScopeProvider.instance.set(aslSession.getScope().toString());
}
}