2013-03-04 14:38:25 +01:00
|
|
|
package org.gcube.portlets.user.speciesdiscovery.server.job;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
import javax.persistence.Query;
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
2017-01-11 18:39:11 +01:00
|
|
|
import org.gcube.data.spd.model.service.types.CompleteJobStatus;
|
|
|
|
import org.gcube.data.spd.model.service.types.JobStatus;
|
2013-03-04 14:38:25 +01:00
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.persistence.DaoSession;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.persistence.dao.OccurrenceJobPersistence;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.persistence.dao.ResultRowPersistence;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.service.SpeciesService;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.session.FetchingSession;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.session.SelectableFetchingBuffer;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.stream.StreamExtend;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.util.DateUtil;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.server.util.XStreamUtil;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.shared.DataSource;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.shared.DownloadState;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.shared.JobOccurrencesModel;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.shared.OccurrencesJob;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.shared.OccurrencesSaveEnum;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.shared.ResultRow;
|
|
|
|
import org.gcube.portlets.user.speciesdiscovery.shared.SaveFileFormat;
|
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class OccurrenceJobUtil.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* Jan 10, 2017
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public class OccurrenceJobUtil {
|
|
|
|
|
|
|
|
//STATE RETURNED BY STATUS RESPONSE
|
|
|
|
public static final String COMPLETED = "COMPLETED";
|
|
|
|
public static final String FAILED = "FAILED";
|
|
|
|
public static final String RUNNING = "RUNNING";
|
|
|
|
public static final String PENDING = "PENDING";
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
private static final String ALIASKEY = "key";
|
|
|
|
|
|
|
|
protected static Logger logger = Logger.getLogger(OccurrenceJobUtil.class);
|
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
/**
|
|
|
|
* Convert job.
|
|
|
|
*
|
|
|
|
* @param job the job
|
|
|
|
* @param statusResponse the status response
|
|
|
|
* @param occurrencesJobDao the occurrences job dao
|
|
|
|
* @return the job occurrences model
|
|
|
|
*/
|
|
|
|
public static JobOccurrencesModel convertJob(OccurrencesJob job, CompleteJobStatus statusResponse, OccurrenceJobPersistence occurrencesJobDao) {
|
|
|
|
//TODO SET END TIME
|
2013-03-04 14:38:25 +01:00
|
|
|
JobOccurrencesModel jobOccurrenceModel;
|
|
|
|
DownloadState downloadState = null;
|
|
|
|
long endTime = 0;
|
2013-07-16 19:36:40 +02:00
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
JobStatus status = statusResponse.getStatus();
|
2013-03-04 14:38:25 +01:00
|
|
|
downloadState = getDownloadState(status);
|
|
|
|
logger.trace("jobId: "+job.getId() +" download state: " + downloadState);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//FOR DEBUG
|
|
|
|
// System.out.println("jobId: "+job.getId() +" download state: " + downloadState);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
if(downloadState==null) //Case with exception
|
|
|
|
return null;
|
|
|
|
|
2013-07-16 11:43:25 +02:00
|
|
|
//SET SUBMIT TIME
|
|
|
|
long submitTime = job.getSubmitTime();
|
|
|
|
Date submit = DateUtil.millisecondsToDate(submitTime);
|
2013-03-04 14:38:25 +01:00
|
|
|
// jobSpeciesModel.setStartTime(DateUtil.dateToDateFormatString(start));
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
int completedEntry = 0;
|
|
|
|
if(statusResponse.getCompletedEntries()>0)
|
|
|
|
completedEntry = statusResponse.getCompletedEntries();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
boolean changeStatus = false;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//if status is completed and job was saved, update status as saved
|
|
|
|
if(downloadState.equals(DownloadState.COMPLETED)){
|
|
|
|
if(job.getState().compareTo(DownloadState.SAVED.toString())==0){
|
|
|
|
downloadState = DownloadState.SAVED;
|
|
|
|
changeStatus = true;
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-07-16 11:43:25 +02:00
|
|
|
jobOccurrenceModel = new JobOccurrencesModel(job.getId(), job.getName(), job.getDescription(), downloadState, job.getScientificName(), job.getDataSources(), submit, null,completedEntry, job.getExpectedOccurrence());
|
2013-03-04 14:38:25 +01:00
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
boolean changeEndTime = false;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//UPDATE END TIME
|
|
|
|
if(downloadState.equals(DownloadState.FAILED) || downloadState.equals(DownloadState.COMPLETED)){
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-07-16 11:43:25 +02:00
|
|
|
// if(job.getEndTime()==0){ //UPDATE end time first time only
|
2017-01-11 18:39:11 +01:00
|
|
|
//
|
2013-07-16 11:43:25 +02:00
|
|
|
// logger.trace("UPDATE end time first time only - " + downloadState);
|
|
|
|
// endTime = Calendar.getInstance().getTimeInMillis();
|
|
|
|
// job.setEndTime(endTime);
|
|
|
|
// changeEndTime = true;
|
|
|
|
// }
|
|
|
|
// System.out.println("job "+job);
|
|
|
|
// System.out.println("statusResponse.getEndDate() "+statusResponse.getEndDate());
|
|
|
|
// System.out.println("job.getEndTime() "+job.getEndTime());
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-07-16 11:43:25 +02:00
|
|
|
if(statusResponse.getEndDate()!=null && job.getEndTime()==0){ //UPDATE end time first time only
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
logger.trace("UPDATE end time first time only - " + downloadState);
|
2013-07-16 11:43:25 +02:00
|
|
|
// endTime = Calendar.getInstance().getTimeInMillis();
|
|
|
|
endTime = statusResponse.getEndDate().getTimeInMillis();
|
2013-03-04 14:38:25 +01:00
|
|
|
job.setEndTime(endTime);
|
|
|
|
changeEndTime = true;
|
2013-07-16 11:43:25 +02:00
|
|
|
// speciesJobDao.update(job);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
|
2013-07-16 11:43:25 +02:00
|
|
|
boolean changeStartTime = false;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-07-16 11:43:25 +02:00
|
|
|
//SET START TIME
|
|
|
|
long startTime = job.getStartTime();
|
|
|
|
if(statusResponse.getStartDate()!=null && startTime==0){ //UPDATE start time first time only
|
|
|
|
Date start = DateUtil.millisecondsToDate(statusResponse.getStartDate().getTimeInMillis());
|
|
|
|
// jobSpeciesModel.setStartTime(DateUtil.dateToDateFormatString(start));
|
|
|
|
jobOccurrenceModel.setStartTime(start);
|
|
|
|
changeStartTime = true;
|
|
|
|
}
|
2013-03-04 14:38:25 +01:00
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//UPDATE DAO
|
2013-07-16 11:43:25 +02:00
|
|
|
if(changeStatus || changeEndTime || changeStartTime){
|
2013-03-04 14:38:25 +01:00
|
|
|
job.setState(downloadState.toString());
|
|
|
|
occurrencesJobDao.update(job);
|
|
|
|
}
|
2013-07-16 11:43:25 +02:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error("An error occurred on update the occurrencesJobDao ", e);
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
endTime = job.getEndTime();
|
|
|
|
long elapsedTime = 0;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-07-16 11:43:25 +02:00
|
|
|
//SET END TIME, BECAUSE IT IS CHANGED
|
2013-03-04 14:38:25 +01:00
|
|
|
if(endTime!=0){
|
|
|
|
Date end = DateUtil.millisecondsToDate(endTime);
|
|
|
|
jobOccurrenceModel.setEndTime(end);
|
|
|
|
elapsedTime = endTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elapsedTime = Calendar.getInstance().getTimeInMillis();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
|
|
|
|
//SET ELAPSED TIME
|
2013-07-16 11:43:25 +02:00
|
|
|
jobOccurrenceModel.setElapsedTime(DateUtil.getDifference(submitTime, elapsedTime));
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//OTHERS SET
|
|
|
|
jobOccurrenceModel.setFileFormat(converFileFormat(job.getFileFormat()));
|
|
|
|
jobOccurrenceModel.setCsvType(convertCsvType(job.getCsvType()));
|
|
|
|
jobOccurrenceModel.setByDataSource(job.isByDataSource());
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
return jobOccurrenceModel;
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the download state.
|
|
|
|
*
|
|
|
|
* @param status the status
|
|
|
|
* @return the download state
|
|
|
|
*/
|
|
|
|
public static DownloadState getDownloadState(JobStatus status){
|
|
|
|
|
|
|
|
if(status!=null){
|
|
|
|
switch (status) {
|
|
|
|
case COMPLETED:
|
|
|
|
return DownloadState.COMPLETED;
|
|
|
|
case FAILED:
|
|
|
|
return DownloadState.FAILED;
|
|
|
|
case PENDING:
|
2013-03-04 14:38:25 +01:00
|
|
|
return DownloadState.PENDING;
|
2017-01-11 18:39:11 +01:00
|
|
|
case RUNNING:
|
2013-03-04 14:38:25 +01:00
|
|
|
return DownloadState.ONGOING;
|
2017-01-11 18:39:11 +01:00
|
|
|
default:
|
|
|
|
return null;
|
2013-03-04 14:38:25 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
/**
|
|
|
|
* Convert csv type.
|
|
|
|
*
|
|
|
|
* @param csvType the csv type
|
|
|
|
* @return the occurrences save enum
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static OccurrencesSaveEnum convertCsvType(String csvType) {
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
if(csvType!=null){
|
|
|
|
if(csvType.compareToIgnoreCase(OccurrencesSaveEnum.STANDARD.toString())==0){
|
|
|
|
return OccurrencesSaveEnum.STANDARD;
|
|
|
|
}else if(csvType.compareToIgnoreCase(OccurrencesSaveEnum.OPENMODELLER.toString())==0){
|
|
|
|
return OccurrencesSaveEnum.OPENMODELLER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
/**
|
|
|
|
* Conver file format.
|
|
|
|
*
|
|
|
|
* @param fileFormat the file format
|
|
|
|
* @return the save file format
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static SaveFileFormat converFileFormat(String fileFormat) {
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
if(fileFormat!=null){
|
|
|
|
if(fileFormat.compareToIgnoreCase(SaveFileFormat.CSV.toString())==0){
|
|
|
|
return SaveFileFormat.CSV;
|
|
|
|
}else if(fileFormat.compareToIgnoreCase(SaveFileFormat.DARWIN_CORE.toString())==0){
|
|
|
|
return SaveFileFormat.DARWIN_CORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
/**
|
|
|
|
* Delete occurrence job by id.
|
|
|
|
*
|
|
|
|
* @param jobIdentifier the job identifier
|
|
|
|
* @param occurrencesJobDao the occurrences job dao
|
|
|
|
* @return the int
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static int deleteOccurrenceJobById(String jobIdentifier, OccurrenceJobPersistence occurrencesJobDao){
|
|
|
|
logger.trace("Delete occurrence job id: " + jobIdentifier);
|
|
|
|
|
|
|
|
try{
|
|
|
|
int removed = occurrencesJobDao.deleteItemByIdField(jobIdentifier);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error("An error occured deleteOccurrenceJobById jobId: " + jobIdentifier + " exception: "+e, e);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
logger.trace("job not exists : " +jobIdentifier);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change status occurrence job by id.
|
|
|
|
*
|
|
|
|
* @param jobIdentifier the job identifier
|
|
|
|
* @param state the state
|
|
|
|
* @param occurrencesJobDao the occurrences job dao
|
|
|
|
* @return the int
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static int changeStatusOccurrenceJobById(String jobIdentifier, DownloadState state, OccurrenceJobPersistence occurrencesJobDao){
|
|
|
|
logger.trace("Change status occurrence job id: " + jobIdentifier);
|
|
|
|
// System.out.println("Delete job id: " + jobIdentifier);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
int result = 0;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
try{
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
CriteriaBuilder queryBuilder = occurrencesJobDao.getCriteriaBuilder();
|
|
|
|
CriteriaQuery<Object> cq = queryBuilder.createQuery();
|
|
|
|
Predicate pr1 = queryBuilder.equal(occurrencesJobDao.rootFrom(cq).get(OccurrencesJob.ID_FIELD), jobIdentifier);
|
|
|
|
cq.where(pr1);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
Iterator<OccurrencesJob> iterator = occurrencesJobDao.executeCriteriaQuery(cq).iterator();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
OccurrencesJob job;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
if(iterator.hasNext())
|
|
|
|
job = iterator.next();
|
|
|
|
else
|
|
|
|
return 0;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
job.setState(state.toString());
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
occurrencesJobDao.update(job);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error("An error occured in change status jobId: " + jobIdentifier + " exception: "+e, e );
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
return result;
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of selected key.
|
|
|
|
*
|
|
|
|
* @param searchSession the search session
|
|
|
|
* @return the list of selected key
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static List<String> getListOfSelectedKey(FetchingSession<ResultRow> searchSession) throws Exception{
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
Collection<ResultRow> selectedRows = ((SelectableFetchingBuffer<ResultRow>) searchSession.getBuffer()).getSelected();
|
|
|
|
logger.trace("found "+selectedRows.size()+" selected rows");
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
int count = 0;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
List<String> keys = new ArrayList<String>(selectedRows.size());
|
|
|
|
|
|
|
|
for (ResultRow row:selectedRows) {
|
|
|
|
//ADD KEY ONLY IF IS NOT EQUAL NULL AND SIZE IS > 0
|
|
|
|
if(row.getOccurencesKey()!=null && row.getOccurencesKey().length()>0){
|
|
|
|
keys.add(row.getOccurencesKey());
|
|
|
|
count += row.getOccurencesCount();
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
logger.trace("found "+count+" occurrence points");
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
return keys;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of selected key by data source.
|
|
|
|
*
|
|
|
|
* @param dataSource the data source
|
|
|
|
* @param session the session
|
|
|
|
* @return the list of selected key by data source
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static OccurrenceKeys getListOfSelectedKeyByDataSource(String dataSource, ASLSession session) {
|
|
|
|
logger.trace("getListOfSelectedKeyByDataSource...");
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
OccurrenceKeys occurrenceKeys = new OccurrenceKeys();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
List<String> keys = new ArrayList<String>();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
Iterator<ResultRow> resulRowIt = null;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
int count = 0;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
try{
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
// System.out.println("dasource name: "+dataSource);
|
|
|
|
logger.trace("datasource name: "+dataSource);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
ResultRowPersistence resultRowDao = DaoSession.getResultRowDAO(session);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
/*CriteriaBuilder cb = resultRowDao.getCriteriaBuilder();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
CriteriaQuery<Object> cq = cb.createQuery();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
Predicate pr1 = cb.equal(resultRowDao.rootFrom(cq).get(ResultRow.DATASOURCE_NAME), dataSource);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
Predicate pr2 = cb.equal(resultRowDao.rootFrom(cq).get(ResultRow.SELECTED), true);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
cq.where(cb.and(pr1,pr2));
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
Iterator<ResultRow> resulRowIt = resultRowDao.executeCriteriaQuery(cq).iterator();
|
|
|
|
*/
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
EntityManager em = resultRowDao.createNewManager();
|
|
|
|
try {
|
|
|
|
|
|
|
|
Query query = em.createQuery("select t from ResultRow t where t."+ResultRow.DATASOURCE_NAME +" = '"+dataSource+ "' AND t."+ResultRow.SELECTED+" = true");
|
|
|
|
|
|
|
|
resulRowIt = query.getResultList().iterator();
|
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error in update: "+e.getMessage(), e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
em.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
while(resulRowIt.hasNext()){
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
ResultRow row = resulRowIt.next();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
if(row.getOccurencesKey()!=null && row.getOccurencesKey().length()>0){
|
|
|
|
keys.add(row.getOccurencesKey());
|
|
|
|
count += row.getOccurencesCount();
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
occurrenceKeys.setListKey(keys);
|
|
|
|
occurrenceKeys.setTotalOccurrence(count);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error("error in getListOfSelectedKeyByDataSource "+ e);
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
logger.trace("found "+count+" occurrence points");
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
return occurrenceKeys;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 18:39:11 +01:00
|
|
|
/**
|
|
|
|
* Creates the occurrence job on service by keys.
|
|
|
|
*
|
|
|
|
* @param jobModel the job model
|
|
|
|
* @param taxonomyService the taxonomy service
|
|
|
|
* @param occurrencesJobDao the occurrences job dao
|
|
|
|
* @param keys the keys
|
|
|
|
* @param dataSources the data sources
|
|
|
|
* @param saveFileFormat the save file format
|
|
|
|
* @param csvType the csv type
|
|
|
|
* @param expectedOccurrence the expected occurrence
|
|
|
|
* @return the job occurrences model
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static JobOccurrencesModel createOccurrenceJobOnServiceByKeys(JobOccurrencesModel jobModel,SpeciesService taxonomyService, OccurrenceJobPersistence occurrencesJobDao, List<String> keys, List<DataSource> dataSources, SaveFileFormat saveFileFormat, OccurrencesSaveEnum csvType, int expectedOccurrence) {
|
|
|
|
String serviceJobId = null;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
StreamExtend<String> streamKeys = new StreamExtend<String>(keys.iterator()); //convert
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
String csvTypeString = null;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
try {
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
switch (saveFileFormat) {
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
case CSV:
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
if(csvType.equals(OccurrencesSaveEnum.STANDARD))
|
|
|
|
serviceJobId = taxonomyService.createOccurrenceCSVJob(streamKeys);
|
|
|
|
else if(csvType.equals(OccurrencesSaveEnum.OPENMODELLER))
|
|
|
|
serviceJobId = taxonomyService.createOccurrenceCSVOpenModellerJob(streamKeys);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
if(jobModel.getCsvType()!=null)
|
|
|
|
csvTypeString = jobModel.getCsvType().toString(); //CASE CSV
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
break;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
case DARWIN_CORE:
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
serviceJobId = taxonomyService.createOccurrenceDARWINCOREJob(streamKeys);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
csvTypeString = "";
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
serviceJobId = taxonomyService.createOccurrenceCSVJob(streamKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("An error occured in create new occurrences job on server ",e);
|
|
|
|
return null;
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
|
2013-07-17 18:34:57 +02:00
|
|
|
long submitTimeInMillis = Calendar.getInstance().getTimeInMillis();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
try {
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//STORE INTO DAO
|
2013-07-17 18:34:57 +02:00
|
|
|
OccurrencesJob occurrenceJob = new OccurrencesJob(serviceJobId, jobModel.getJobName(), jobModel.getDescription(), jobModel.getScientificName(), dataSources, DownloadState.PENDING.toString(), "", submitTimeInMillis, 0, 0, jobModel.getFileFormat().toString(),csvTypeString, jobModel.isByDataSource(), convertListKeyIntoStoreXMLString(keys), expectedOccurrence);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//for debug
|
|
|
|
// System.out.println("INTO createOccurrenceJobOnServiceByKeys " + occurrenceJob);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
occurrencesJobDao.insert(occurrenceJob);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-07-17 18:34:57 +02:00
|
|
|
Date start = DateUtil.millisecondsToDate(submitTimeInMillis);
|
2013-07-16 11:43:25 +02:00
|
|
|
jobModel.setSubmitTime(start);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
//FILL MODEL WITH OTHER DATA
|
|
|
|
jobModel.setId(serviceJobId);
|
|
|
|
jobModel.setState(DownloadState.PENDING);
|
|
|
|
jobModel.setEndTime(null);
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
logger.error("An error occured in create new occurrences job on dao object " +e,e);
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
return jobModel;
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert list key into store xml string.
|
|
|
|
*
|
|
|
|
* @param keys the keys
|
|
|
|
* @return the string
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static String convertListKeyIntoStoreXMLString(List<String> keys){
|
|
|
|
String storeKeys = "";
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
KeyStringList keyStringList = new KeyStringList();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
XStreamUtil<KeyStringList> xstreamUtil = new XStreamUtil<KeyStringList>(ALIASKEY,KeyStringList.class);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
for (String key : keys) {
|
2014-04-28 18:51:44 +02:00
|
|
|
// System.out.println("key :"+ key);
|
|
|
|
logger.info("key converted: "+key);
|
2013-03-04 14:38:25 +01:00
|
|
|
keyStringList.addKey(key);
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
storeKeys = xstreamUtil.toXML(keyStringList);
|
|
|
|
|
|
|
|
//FOR DEBUG
|
|
|
|
// System.out.println("stored keys: "+storeKeys);
|
|
|
|
|
|
|
|
return storeKeys;
|
|
|
|
}
|
2017-01-11 18:39:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Revert list key from stored xml string.
|
|
|
|
*
|
|
|
|
* @param storedKeysAsXml the stored keys as xml
|
|
|
|
* @return the list
|
|
|
|
*/
|
2013-03-04 14:38:25 +01:00
|
|
|
public static List<String> revertListKeyFromStoredXMLString(String storedKeysAsXml){
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
List<String> listKey = new ArrayList<String>();
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
XStreamUtil<KeyStringList> xstreamUtil = new XStreamUtil<KeyStringList>(ALIASKEY,KeyStringList.class);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
KeyStringList keyStringList = (KeyStringList) xstreamUtil.fromXML(storedKeysAsXml);
|
2017-01-11 18:39:11 +01:00
|
|
|
|
2013-03-04 14:38:25 +01:00
|
|
|
for (String key : keyStringList.getListKeys()) {
|
|
|
|
// for debug
|
|
|
|
// System.out.println("key :"+ key);
|
|
|
|
listKey.add(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return listKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|