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

280 lines
9.3 KiB
Java

/**
*
*/
package org.gcube.portlets.user.sdmxexportwizardtd.server;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.gcube.data.analysis.tabulardata.operation.worker.ActivityStatus;
import org.gcube.data.analysis.tabulardata.operation.worker.EligibleOperation;
import org.gcube.data.analysis.tabulardata.operation.worker.JobResult;
import org.gcube.data.analysis.tabulardata.operation.worker.OperationInvocation;
import org.gcube.data.analysis.tabulardata.service.TabularDataService;
import org.gcube.data.analysis.tabulardata.service.TabularDataServiceMock;
import org.gcube.data.analysis.tabulardata.service.operation.OperationInterface;
import org.gcube.data.analysis.tabulardata.service.operation.Task;
import org.gcube.data.analysis.tabulardata.service.tabular.TabularResource;
import org.gcube.data.analysis.tabulardata.service.tabular.TabularResourceId;
import org.gcube.data.analysis.tabulardata.service.tabular.TabularResourceInterface;
import org.gcube.data.analysis.tabulardata.service.tabular.metadata.AgencyMetadata;
import org.gcube.data.analysis.tabulardata.service.tabular.metadata.CreationDateMetadata;
import org.gcube.data.analysis.tabulardata.service.tabular.metadata.DescriptionMetadata;
import org.gcube.data.analysis.tabulardata.service.tabular.metadata.NameMetadata;
import org.gcube.data.analysis.tabulardata.service.tabular.metadata.TabularResourceMetadata;
import org.gcube.portlets.user.sdmxexportwizardtd.client.rpc.SDMXExportWizardService;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Agencies;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Codelist;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.Dataset;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.ExportMonitor;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.SDMXExportSession;
import org.gcube.portlets.user.sdmxexportwizardtd.shared.SDMXExportWizardException;
import org.gcube.portlets.user.td.gxtservice.shared.TRId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
//import org.gcube.portlets.user.sdmximportwizardtd.server.cl.ReadOnlyGroupMock;
/**
*
* @author "Giancarlo Panichi" <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class SDMXExportWizardServiceImpl extends RemoteServiceServlet implements
SDMXExportWizardService {
private static final long serialVersionUID = -5707400086333186368L;
protected static Logger logger = LoggerFactory.getLogger(SDMXExportWizardServiceImpl.class);
protected TabularDataService service;
protected org.gcube.data.analysis.tabulardata.model.table.TableId serviceTableId;
protected org.gcube.data.analysis.tabulardata.model.table.Table serviceTable;
public void setSDMXSession(SDMXExportSession s)
throws SDMXExportWizardException {
try {
HttpSession session = this.getThreadLocalRequest().getSession();
SessionUtil.setSDMXExportSession(session, s);
return;
} catch (Exception e) {
e.printStackTrace();
throw new SDMXExportWizardException(
"Error setting SDMXSession parameter: "
+ e.getLocalizedMessage());
}
}
/**
* jar {@inheritDoc}
*/
@Override
public List<Codelist> getCodelists() throws SDMXExportWizardException {
try {
logger.info("getCodelists()");
HttpSession session = this.getThreadLocalRequest().getSession();
return SessionUtil.retrieveCodelists(session);
} catch (Exception e) {
e.printStackTrace();
throw new SDMXExportWizardException("Error retrieving codelists: "
+ e.getLocalizedMessage());
}
}
/**
* {@inheritDoc}
*/
@Override
public List<Dataset> getDatasets() throws SDMXExportWizardException {
try {
logger.info("getDatasets()");
HttpSession session = this.getThreadLocalRequest().getSession();
return SessionUtil.retrieveDatasets(session);
} catch (Exception e) {
e.printStackTrace();
throw new SDMXExportWizardException("Error retrieving datasets: "
+ e.getLocalizedMessage());
}
}
/**
* {@inheritDoc}
*/
@Override
public List<Agencies> getAgencies() throws SDMXExportWizardException {
try {
logger.info("getAgencies()");
HttpSession session = this.getThreadLocalRequest().getSession();
return SessionUtil.retrieveAgencies(session);
} catch (Exception e) {
throw new SDMXExportWizardException("Error retrieving agencies: "
+ e.getMessage());
}
}
protected EligibleOperation getEligibleOperationWithId(String id,
List<EligibleOperation> capabilities) {
for (EligibleOperation eligibleOperation : capabilities) {
if (eligibleOperation.getOperationDescriptor().getOperationId()
.getValue() == Long.parseLong(id)) {
return eligibleOperation;
}
}
return null;
}
protected Map<String, Object> setAgenciesParameters(
SDMXExportSession sdmxExportSession) {
Map<String, Object> map = new HashMap<String, Object>();
Agencies agency = sdmxExportSession.getAgency();
map.put("id", agency.getId());
map.put("name", agency.getName());
map.put("description",agency.getDescription());
return map;
}
protected void setTabularResourceParameters(TabularResource tr,
SDMXExportSession sdmxImportSession) {
TabularResourceMetadata name = new NameMetadata(sdmxImportSession
.getTableDetail().getName());
TabularResourceMetadata createdAt = new CreationDateMetadata(new Date());
TabularResourceMetadata agencyMeta = new AgencyMetadata(
sdmxImportSession.getTableDetail().getAgency());
TabularResourceMetadata description = new DescriptionMetadata(
sdmxImportSession.getTableDetail().getAgency());
TabularResourceMetadata right = new DescriptionMetadata(
sdmxImportSession.getTableDetail().getRight());
List<TabularResourceMetadata> metas = new ArrayList<TabularResourceMetadata>();
metas.add(name);
metas.add(createdAt);
metas.add(agencyMeta);
metas.add(description);
metas.add(right);
tr.setAllMetadata(metas);
}
public void exportSDMXClientLibraryRequest(
SDMXExportSession sdmxImportSession)
throws SDMXExportWizardException {
try {
HttpSession session = this.getThreadLocalRequest().getSession();
SessionUtil.setSDMXExportSession(session, sdmxImportSession);
service = new TabularDataServiceMock();
OperationInterface oService = service;
TabularResourceInterface trService = service;
List<EligibleOperation> capabilities = service.getCapabilities();
// Import SDMX Codelist takes id 200
EligibleOperation exportSDMXCodelistOperation = getEligibleOperationWithId(
"200", capabilities);
Map<String, Object> parameterInstances = setAgenciesParameters(sdmxImportSession);
OperationInvocation invocation = exportSDMXCodelistOperation
.createOperationInvocation(parameterInstances);
TabularResourceId newTR = trService.createTabularResource();
/*setTabularResourceParameters(newTR, sdmxImportSession);
Task trTask = oService.execute(invocation, newTR.getId());
*/
TRId trId=new TRId();
//trId.setId(String.valueOf(newTR.getId().getValue()));
SessionUtil.setTRId(session, trId);
//SessionUtil.setTRTask(session, trTask);
return;
} catch (Exception e) {
e.printStackTrace();
throw new SDMXExportWizardException(
"Error in Client Library Request: "
+ e.getLocalizedMessage());
}
}
public ExportMonitor getExportMonitor() throws SDMXExportWizardException {
try {
HttpSession session = this.getThreadLocalRequest().getSession();
Task task = SessionUtil.getTRTask(session);
TRId trId = SessionUtil.getTRId(session);
ExportMonitor exportMonitor = new ExportMonitor();
if (trId == null) {
throw new SDMXExportWizardException(
"Error in exportSDMX TabularResource Id null ");
} else {
if (task == null) {
throw new SDMXExportWizardException(
"Error in exportSDMX Task null ");
} else {
ActivityStatus status = task.getStatus();
if (status == null) {
throw new SDMXExportWizardException(
"Error in exportSDMX Status null");
} else {
exportMonitor.setStatus(task.getStatus().ordinal());
exportMonitor.setProgress(task.getProgress());
JobResult result = task.getResult();
if (result == null) {
logger.info("Task Result: "+task.getResult());
} else {
exportMonitor.setError(task.getResult()
.getException());
if (task.getResult().getOutput() == null){
logger.info("Task Result Output: "+task.getResult()
.getOutput());
} else {
if(task.getResult().getOutput().getId() == null) {
logger.info("Task Result Output Id: "+task.getResult()
.getOutput().getId());
} else {
logger.info("Task TableId: "+task.getResult()
.getOutput().getId().getValue());
trId.setTableId(String.valueOf(task.getResult()
.getOutput().getId().getValue()));
SessionUtil.setTRId(session, trId);
}
}
}
}
SessionUtil.setTRTask(session, task);
}
exportMonitor.setTrId(trId);
}
logger.info("getImportMonitor(): "+exportMonitor);
return exportMonitor;
} catch (Exception e) {
e.printStackTrace();
throw new SDMXExportWizardException(
"Error in exportSDMX ExportMonitor: "
+ e.getLocalizedMessage());
}
}
}