792 lines
25 KiB
Java
792 lines
25 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.td.gxtservice.server;
|
|
|
|
import java.nio.charset.Charset;
|
|
import java.text.SimpleDateFormat;
|
|
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.application.framework.core.session.ASLSession;
|
|
import org.gcube.data.analysis.tabulardata.metadata.NoSuchMetadataException;
|
|
import org.gcube.data.analysis.tabulardata.model.table.Table;
|
|
import org.gcube.data.analysis.tabulardata.model.table.TableId;
|
|
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.RightsMetadata;
|
|
import org.gcube.portlets.user.td.gxtservice.client.rpc.TDGXTService;
|
|
import org.gcube.portlets.user.td.gxtservice.server.file.FileUploadSession;
|
|
import org.gcube.portlets.user.td.gxtservice.server.storage.FilesStorage;
|
|
import org.gcube.portlets.user.td.gxtservice.server.trservice.OperationsId;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.Agencies;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.Codelist;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.Dataset;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.TDGXTServiceException;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.TDOpenSession;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.TRId;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.TabResource;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.csv.AvailableCharsetList;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.csv.CSVFileUtil;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.csv.CSVImportMonitor;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.csv.CSVImportSession;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.csv.CSVParserConfiguration;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.csv.CSVRowError;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.file.FileUploadMonitor;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.file.HeaderPresence;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.sdmx.SDMXImportMonitor;
|
|
import org.gcube.portlets.user.td.gxtservice.shared.sdmx.SDMXImportSession;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
|
|
|
/**
|
|
*
|
|
* @author "Giancarlo Panichi" <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class TDGXTServiceImpl extends RemoteServiceServlet implements
|
|
TDGXTService {
|
|
|
|
private static final long serialVersionUID = -5707400086333186368L;
|
|
protected static Logger logger = LoggerFactory
|
|
.getLogger(TDGXTServiceImpl.class);
|
|
|
|
protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
protected TabularDataService service;
|
|
protected HttpSession session;
|
|
protected ASLSession aslSession;
|
|
|
|
public void setTabResource(TabResource tabResource)
|
|
throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
SessionUtil.setTabResource(session, tabResource);
|
|
return;
|
|
|
|
} catch (Exception e) {
|
|
logger.error(
|
|
"Error setting TabResource parameter: "
|
|
+ e.getLocalizedMessage(), e);
|
|
throw new TDGXTServiceException(
|
|
"Error setting TabResource parameter: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public TabResource getTabResource() throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
return SessionUtil.getTabResource(session);
|
|
|
|
} catch (Exception e) {
|
|
logger.error(
|
|
"Error setting TabResource parameter: "
|
|
+ e.getLocalizedMessage(), e);
|
|
throw new TDGXTServiceException(
|
|
"Error setting TabResource parameter: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves the properties of the specified tabular resource
|
|
*/
|
|
/*
|
|
* @Override public TRProperties getTaularResourceProperties(TRId trId)
|
|
* throws TDGXTServiceException { try { HttpSession session =
|
|
* this.getThreadLocalRequest().getSession();
|
|
*
|
|
* SessionUtil.setTRId(session, trId); TabularResourceId id = new
|
|
* TabularResourceId(Long.valueOf(trId .getId())); TabularDataService
|
|
* service = new TabularDataServiceMock();
|
|
*
|
|
* TabularResource tr = service.getTabularResource(id);
|
|
*
|
|
* NameMetadata nameMeta = tr.getMetadata(NameMetadata.class);
|
|
* CreationDateMetadata creationDateMeta = tr
|
|
* .getMetadata(CreationDateMetadata.class); AgencyMetadata agencyMeta =
|
|
* tr.getMetadata(AgencyMetadata.class);
|
|
*
|
|
* String nameTR = nameMeta.getValue(); String agencyTR =
|
|
* agencyMeta.getValue(); Date dateTR = creationDateMeta.getValue(); TableId
|
|
* tableId = tr.getHistory().get(tr.getHistory().size() - 1)
|
|
* .getResultTable().getId();
|
|
*
|
|
* HashMap<String, String> properties = new HashMap<String, String>();
|
|
*
|
|
* properties.put("Id", String.valueOf(tr.getId().getValue()));
|
|
* properties.put("Table id", String.valueOf(tableId.getValue()));
|
|
* properties.put("Name", nameTR); properties.put("Agency", agencyTR);
|
|
* properties.put("Creation Date", sdf.format(dateTR));
|
|
*
|
|
* logger.info("Tabular Resources retrived: " + properties);
|
|
*
|
|
* TRProperties trProperties = new TRProperties();
|
|
* trProperties.setProperties(properties);
|
|
*
|
|
* return trProperties; return null; } catch (Exception e) {
|
|
* logger.error("Error retrieving TabularResources: " +
|
|
* e.getLocalizedMessage()); throw new TDGXTServiceException(
|
|
* "Error retrieving Tabular Resources Properties: " +
|
|
* e.getLocalizedMessage()); }
|
|
*
|
|
* }
|
|
*/
|
|
|
|
@Override
|
|
public void setTDOpenSession(TDOpenSession s) throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
SessionUtil.setTDOpenSession(session, s);
|
|
return;
|
|
|
|
} catch (Exception e) {
|
|
logger.error("Error setting TDOpenSession parameter: "
|
|
+ e.getLocalizedMessage());
|
|
throw new TDGXTServiceException(
|
|
"Error setting TDOpenSession parameter: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
protected TabResource retriveTRMetadataFromService(TabularResource tr, int i)
|
|
throws TDGXTServiceException {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
NameMetadata nameMeta;
|
|
RightsMetadata rightsMeta;
|
|
CreationDateMetadata creationDateMeta;
|
|
AgencyMetadata agencyMeta;
|
|
DescriptionMetadata descriptionMeta;
|
|
try {
|
|
nameMeta = tr.getMetadata(NameMetadata.class);
|
|
creationDateMeta = tr.getMetadata(CreationDateMetadata.class);
|
|
agencyMeta = tr.getMetadata(AgencyMetadata.class);
|
|
descriptionMeta = tr.getMetadata(DescriptionMetadata.class);
|
|
rightsMeta = tr.getMetadata(RightsMetadata.class);
|
|
} catch (NoSuchMetadataException e) {
|
|
e.printStackTrace();
|
|
throw new TDGXTServiceException(
|
|
"Error retriving metadata from service: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
if (nameMeta == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retriving metadata from service: TR nameMeta is null");
|
|
}
|
|
String nameTR = nameMeta.getValue();
|
|
String descriptionTR = "";
|
|
String agencyTR = "";
|
|
String rightsTR = "";
|
|
String dateS = "";
|
|
|
|
if (descriptionMeta != null) {
|
|
descriptionTR = descriptionMeta.getValue();
|
|
}
|
|
if (agencyMeta != null) {
|
|
agencyTR = agencyMeta.getValue();
|
|
}
|
|
if (rightsMeta != null) {
|
|
rightsTR = rightsMeta.getValue();
|
|
}
|
|
if (creationDateMeta != null) {
|
|
Date dateTR = creationDateMeta.getValue();
|
|
dateS = sdf.format(dateTR);
|
|
}
|
|
|
|
TableId tableId = tr.getHistory().get(tr.getHistory().size() - 1)
|
|
.getResultTable().getId();
|
|
TRId trId = new TRId(String.valueOf(tr.getId().getValue()),
|
|
String.valueOf(tableId.getValue()));
|
|
TabResource t = new TabResource(i, nameTR, descriptionTR, agencyTR,
|
|
dateS, rightsTR, trId);
|
|
return t;
|
|
}
|
|
|
|
@Override
|
|
public ArrayList<TabResource> getTabularResources()
|
|
throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
aslSession = SessionUtil.getAslSession(session);
|
|
service = new TabularDataServiceMock();
|
|
|
|
List<TabularResource> trs = service.getTabularResources();
|
|
SessionUtil.setTabularResources(session, trs);
|
|
|
|
ArrayList<TabResource> ltr = new ArrayList<TabResource>();
|
|
|
|
int i;
|
|
for (i = 0; i < trs.size(); i++) {
|
|
TabularResource tr = trs.get(i);
|
|
TabResource t = retriveTRMetadataFromService(tr, i);
|
|
ltr.add(t);
|
|
}
|
|
|
|
logger.debug("Tabular Resources retrived: " + ltr);
|
|
return ltr;
|
|
} catch (Exception e) {
|
|
logger.error("Error retrieving TabularResources: "
|
|
+ e.getLocalizedMessage());
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving TabularResources: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
* @param tabResource
|
|
* @throws TDGXTServiceException
|
|
*/
|
|
|
|
public void removeTabularResource(TabResource tabResource)
|
|
throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
aslSession = SessionUtil.getAslSession(session);
|
|
|
|
if (tabResource == null) {
|
|
logger.error("Error removing TabularResource: tabResource is null");
|
|
throw new TDGXTServiceException(
|
|
"Error removing TabularResource no parameters set");
|
|
}
|
|
TabularDataService service = new TabularDataServiceMock();
|
|
TabularResourceId id = new TabularResourceId(
|
|
Long.valueOf(tabResource.getTrId().getId()));
|
|
service.removeTabularResource(id);
|
|
return;
|
|
} catch (Exception e) {
|
|
logger.error(
|
|
"Error removing TabularResource: "
|
|
+ e.getLocalizedMessage(), e);
|
|
throw new TDGXTServiceException("Error removing TabularResource: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
protected void setTabularResourceMetaData(TabResource tabResource,
|
|
TabularResource serviceTR) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public TabResource createTabularResource(TabResource tabResource)
|
|
throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
aslSession = SessionUtil.getAslSession(session);
|
|
|
|
if (tabResource == null) {
|
|
logger.error("Error creating new TabularResource: tabResource is null");
|
|
throw new TDGXTServiceException(
|
|
"Error creating new TabularResource no parameters set");
|
|
}
|
|
service = new TabularDataServiceMock();
|
|
TabularResource serviceTR = service.createTabularResource();
|
|
Table table = service.getLastTable(serviceTR.getId());
|
|
|
|
TRId trId = new TRId(String.valueOf(serviceTR.getId().getValue()),
|
|
String.valueOf(table.getId().getValue()));
|
|
tabResource.setTrId(trId);
|
|
setTabularResourceMetaData(tabResource, serviceTR);
|
|
return tabResource;
|
|
} catch (Exception e) {
|
|
logger.error(
|
|
"Error creating new TabularResource: "
|
|
+ e.getLocalizedMessage(), e);
|
|
throw new TDGXTServiceException(
|
|
"Error creating new TabularResource: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* jar {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public ArrayList<Codelist> getCodelists() throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
return SessionUtil.retrieveCodelists(session);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new TDGXTServiceException("Error retrieving codelists: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public ArrayList<Dataset> getDatasets() throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
return SessionUtil.retrieveDatasets(session);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new TDGXTServiceException("Error retrieving datasets: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
@Override
|
|
public ArrayList<Agencies> getAgencies() throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
return SessionUtil.retrieveAgencies(session);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new TDGXTServiceException("Error retrieving datasets: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* SDMX Import
|
|
*
|
|
*/
|
|
protected Map<String, Object> retrieveOperationParameters(
|
|
SDMXImportSession sdmxImportSession) {
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
|
Codelist codelist = sdmxImportSession.getSelectedCodelist();
|
|
|
|
map.put("Agency", codelist.getAgencyId());
|
|
map.put("Id", codelist.getId());
|
|
map.put("Version", codelist.getVersion());
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
@Override
|
|
public void importSDMXClientLibraryRequest(
|
|
SDMXImportSession sdmxImportSession) throws TDGXTServiceException {
|
|
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
SessionUtil.setSDMXImportSession(session, sdmxImportSession);
|
|
aslSession = SessionUtil.getAslSession(session);
|
|
|
|
service = new TabularDataServiceMock();
|
|
OperationInterface oService = service;
|
|
TabularResourceInterface trService = service;
|
|
|
|
List<EligibleOperation> capabilities = service.getCapabilities();
|
|
|
|
// Import SDMX Codelist takes id 200
|
|
EligibleOperation importSDMXCodelistOperation = getEligibleOperationWithId(
|
|
OperationsId.SDMXCodelistImport.toString(), capabilities);
|
|
|
|
TabResource importSDMXTabResource = sdmxImportSession
|
|
.getTabResource();
|
|
|
|
TabularResource serviceTR = trService.createTabularResource();
|
|
// Table table=trService.getLastTable(serviceTRId);
|
|
|
|
TRId trId = new TRId(String.valueOf(serviceTR.getId().getValue()));
|
|
importSDMXTabResource.setTrId(trId);
|
|
SessionUtil
|
|
.setSDMXImportTabResource(session, importSDMXTabResource);
|
|
|
|
Map<String, Object> parameterInstances = retrieveOperationParameters(sdmxImportSession);
|
|
|
|
OperationInvocation invocation = importSDMXCodelistOperation
|
|
.createOperationInvocation(parameterInstances);
|
|
|
|
Task trTask = oService.execute(invocation, serviceTR.getId());
|
|
|
|
SessionUtil.setSDMXImportTask(session, trTask);
|
|
return;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new TDGXTServiceException("Error in Client Library Request: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
protected EligibleOperation getEligibleOperationWithId(String op,
|
|
List<EligibleOperation> capabilities) throws TDGXTServiceException {
|
|
for (EligibleOperation operation : capabilities) {
|
|
if (Long.valueOf(op) == operation.getOperationDescriptor()
|
|
.getOperationId().getValue()) {
|
|
return operation;
|
|
}
|
|
;
|
|
|
|
}
|
|
throw new TDGXTServiceException("ElegibleOperation not found");
|
|
|
|
}
|
|
|
|
@Override
|
|
public SDMXImportMonitor getSDMXImportMonitor()
|
|
throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
aslSession = SessionUtil.getAslSession(session);
|
|
Task task = SessionUtil.getSDMXImportTask(session);
|
|
TabResource sdmxImportTabResource = SessionUtil
|
|
.getSDMXImportTabResource(session);
|
|
TRId trId = sdmxImportTabResource.getTrId();
|
|
|
|
SDMXImportMonitor importMonitor = new SDMXImportMonitor();
|
|
if (trId == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error in importSDMX TabularResource Id null");
|
|
} else {
|
|
if (task == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error in importSDMX task null");
|
|
} else {
|
|
ActivityStatus status = task.getStatus();
|
|
if (status == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error in importSDMX Status null");
|
|
} else {
|
|
importMonitor.setStatus(task.getStatus().ordinal());
|
|
importMonitor.setProgress(task.getProgress());
|
|
JobResult result = task.getResult();
|
|
if (result == null) {
|
|
logger.info("Task Result: " + task.getResult());
|
|
} else {
|
|
importMonitor.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()));
|
|
sdmxImportTabResource.setTrId(trId);
|
|
SessionUtil.setSDMXImportTabResource(
|
|
session, sdmxImportTabResource);
|
|
|
|
SessionUtil.setTRId(session, trId);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
SessionUtil.setSDMXImportTask(session, task);
|
|
}
|
|
|
|
importMonitor.setTrId(trId);
|
|
}
|
|
logger.info("getImportMonitor(): " + importMonitor);
|
|
return importMonitor;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new TDGXTServiceException(
|
|
"Error in importSDMX ImportMonitor: "
|
|
+ e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void setSDMXSession(SDMXImportSession sdmxImportSession)
|
|
throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
SessionUtil.setSDMXImportSession(session, sdmxImportSession);
|
|
return;
|
|
|
|
} catch (Exception e) {
|
|
logger.error(
|
|
"Error setting SDMXImportSession parameter: "
|
|
+ e.getLocalizedMessage(), e);
|
|
throw new TDGXTServiceException(
|
|
"Error setting SDMXImportSession parameter: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* CSV Import
|
|
*
|
|
*/
|
|
|
|
@Override
|
|
public void setCSVSession(CSVImportSession importSession)
|
|
throws TDGXTServiceException {
|
|
try {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
SessionUtil.setCSVImportSession(session, importSession);
|
|
return;
|
|
|
|
} catch (Exception e) {
|
|
logger.error(
|
|
"Error setting SDMXImportSession parameter: "
|
|
+ e.getLocalizedMessage(), e);
|
|
throw new TDGXTServiceException(
|
|
"Error setting SDMXImportSession parameter: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public FileUploadMonitor getFileUploadMonitor()
|
|
throws TDGXTServiceException {
|
|
|
|
session = this.getThreadLocalRequest().getSession();
|
|
if (session == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the session: null");
|
|
}
|
|
FileUploadSession fileUploadSession = SessionUtil
|
|
.getFileUploadSession(session);
|
|
if (fileUploadSession == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the fileUploadSession: null");
|
|
}
|
|
return fileUploadSession.getFileUploadMonitor();
|
|
}
|
|
|
|
@Override
|
|
public CSVImportMonitor getCSVImportMonitor() throws TDGXTServiceException {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
if (session == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the session: null");
|
|
}
|
|
logger.info("Session:" + session.getId());
|
|
|
|
FileUploadSession importSession = SessionUtil
|
|
.getFileUploadSession(session);
|
|
if (importSession == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the fileUploadSession: null");
|
|
}
|
|
return importSession.getCsvImportMonitor();
|
|
}
|
|
|
|
@Override
|
|
public AvailableCharsetList getAvailableCharset()
|
|
throws TDGXTServiceException {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
if (session == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the session: null");
|
|
}
|
|
logger.info("Session:" + session.getId());
|
|
String defaultEncoding = Charset.defaultCharset().displayName();
|
|
ArrayList<String> charsetList = new ArrayList<String>(Charset
|
|
.availableCharsets().keySet());
|
|
return new AvailableCharsetList(charsetList, defaultEncoding);
|
|
}
|
|
|
|
@Override
|
|
public ArrayList<String> configureCSVParser(String encoding,
|
|
HeaderPresence headerPresence, char delimiter, char comment)
|
|
throws TDGXTServiceException {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
if (session == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the session: null");
|
|
}
|
|
logger.info("Session:" + session.getId());
|
|
logger.info("configureCSVParser encoding: " + encoding
|
|
+ " headerPresence: " + headerPresence + " delimiter: "
|
|
+ delimiter + " comment: " + comment);
|
|
|
|
FileUploadSession fileUploadSession = SessionUtil
|
|
.getFileUploadSession(session);
|
|
if (fileUploadSession == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the fileUploadSession: null");
|
|
}
|
|
CSVParserConfiguration parserConfiguration = fileUploadSession
|
|
.getParserConfiguration();
|
|
if (parserConfiguration == null) {
|
|
parserConfiguration = new CSVParserConfiguration(
|
|
Charset.forName(encoding), delimiter, comment,
|
|
headerPresence);
|
|
fileUploadSession.setParserConfiguration(parserConfiguration);
|
|
} else {
|
|
parserConfiguration.update(encoding, delimiter, comment,
|
|
headerPresence);
|
|
}
|
|
SessionUtil.setFileUploadSession(session, fileUploadSession);
|
|
try {
|
|
return CSVFileUtil.getHeader(fileUploadSession.getCsvFile(),
|
|
fileUploadSession.getParserConfiguration());
|
|
} catch (Exception e) {
|
|
logger.error("Error retrieving the CSV header", e);
|
|
throw new TDGXTServiceException(
|
|
"Error calculating the CSV header: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ArrayList<CSVRowError> checkCSV(long errorsLimit)
|
|
throws TDGXTServiceException {
|
|
|
|
session = this.getThreadLocalRequest().getSession();
|
|
if (session == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the session: null");
|
|
}
|
|
logger.info("Session:" + session.getId());
|
|
FileUploadSession fileUploadSession = SessionUtil
|
|
.getFileUploadSession(session);
|
|
if (fileUploadSession == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the fileUploadSession: null");
|
|
}
|
|
|
|
try {
|
|
return CSVFileUtil.checkCSV(fileUploadSession.getCsvFile(),
|
|
fileUploadSession.getParserConfiguration(), errorsLimit);
|
|
} catch (Exception e) {
|
|
logger.error("Error checking the CSV file", e);
|
|
throw new TDGXTServiceException("Error checking the CSV file: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void startCSVImport(boolean[] columnToImportMask)
|
|
throws TDGXTServiceException {
|
|
session = this.getThreadLocalRequest().getSession();
|
|
if (session == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the session: null");
|
|
}
|
|
logger.info("Session:" + session.getId());
|
|
|
|
ASLSession aslSession = SessionUtil.getAslSession(session);
|
|
if (aslSession == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the asl session: null");
|
|
}
|
|
String user = aslSession.getUsername();
|
|
logger.info("Session User:" + user);
|
|
|
|
FileUploadSession fileUploadSession = SessionUtil
|
|
.getFileUploadSession(session);
|
|
if (fileUploadSession == null) {
|
|
throw new TDGXTServiceException(
|
|
"Error retrieving the fileUploadSession: null");
|
|
}
|
|
|
|
importCSVFileOnService(user, fileUploadSession, columnToImportMask);
|
|
|
|
|
|
}
|
|
|
|
protected Map<String, Object> csvImportFileParameter(
|
|
String fileUrlOnStorage, FileUploadSession fileUploadSession,
|
|
boolean[] columnToImportMask) {
|
|
final String ENCODING = "encoding";
|
|
final String HASHEADER = "hasHeader";
|
|
final String SEPARATOR = "separator";
|
|
final String URL = "url";
|
|
|
|
Map<String, Object> parameterInstances = new HashMap<String, Object>();
|
|
parameterInstances.put(URL, fileUrlOnStorage);
|
|
parameterInstances.put(SEPARATOR, fileUploadSession
|
|
.getParserConfiguration().getDelimiter());// ','
|
|
parameterInstances.put(ENCODING, fileUploadSession
|
|
.getParserConfiguration().getCharset());// "UTF-8"
|
|
boolean hasHeader = true;
|
|
if (fileUploadSession.getParserConfiguration().getHeaderPresence() == HeaderPresence.NONE) {
|
|
hasHeader = false;
|
|
}
|
|
parameterInstances.put(HASHEADER, hasHeader);// true
|
|
return parameterInstances;
|
|
}
|
|
|
|
protected void importCSVFileOnService(String user,
|
|
FileUploadSession fileUploadSession, boolean[] columnToImportMask)
|
|
throws TDGXTServiceException {
|
|
FilesStorage filesStorage = new FilesStorage();
|
|
|
|
String fileUrlOnStorage = filesStorage.storageCSVTempFile(user,
|
|
fileUploadSession.getCsvFile());
|
|
|
|
Map<String, Object> parameterInstance = csvImportFileParameter(
|
|
fileUrlOnStorage, fileUploadSession, columnToImportMask);
|
|
|
|
service = new TabularDataServiceMock();
|
|
|
|
List<EligibleOperation> capabilities = service.getCapabilities();
|
|
|
|
// Import CSV file
|
|
EligibleOperation importCSVFileOperation = getEligibleOperationWithId(
|
|
OperationsId.CSVImport.toString(), capabilities);
|
|
|
|
OperationInvocation oi = importCSVFileOperation
|
|
.createOperationInvocation(parameterInstance);
|
|
TabularResource tabularResource = service.createTabularResource();
|
|
|
|
Task trTask;
|
|
try {
|
|
trTask = service.execute(oi, tabularResource.getId());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new TDGXTServiceException(
|
|
"Tabular Data Service error creating TabularResource: "
|
|
+ e.getLocalizedMessage());
|
|
}
|
|
|
|
SessionUtil.setCSVImportFileTask(session, trTask);
|
|
|
|
}
|
|
}
|