/** * */ package org.gcube.portlets.user.speciesdiscovery.server.job; import java.util.Calendar; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; import org.gcube.data.spd.model.service.types.CompleteJobStatus; import org.gcube.data.spd.model.service.types.JobStatus; import org.gcube.portlets.user.speciesdiscovery.server.persistence.dao.GisLayerJobPersistence; import org.gcube.portlets.user.speciesdiscovery.server.service.SpeciesService; import org.gcube.portlets.user.speciesdiscovery.server.util.DateUtil; import org.gcube.portlets.user.speciesdiscovery.shared.DownloadState; import org.gcube.portlets.user.speciesdiscovery.shared.GisLayerJob; import org.gcube.portlets.user.speciesdiscovery.shared.JobGisLayerModel; /** * The Class GisLinkJobUtil. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * Feb 9, 2017 */ public class GisLayerJobUtil { protected static Logger logger = Logger.getLogger(GisLayerJobUtil.class); /** * Creates the gis layer job by occurrence keys. * * @param occurrenceKeys the occurrence keys * @param taxonomyService the taxonomy service * @param layerTitle the layer title * @param layerDescr the layer descr * @param author the author * @param credits the credits * @param gisLayerJob the gis layer job * @return the job gis layer model * @throws Exception the exception */ public static JobGisLayerModel createGisLayerJobByOccurrenceKeys(List occurrenceKeys, SpeciesService taxonomyService, String layerTitle, String layerDescr, String author, String credits, long totalPoints, GisLayerJobPersistence gisLayerJob) throws Exception { try { logger.trace("Generating Map form Occurrence Keys selected: "+occurrenceKeys.size()); String jobId = taxonomyService.generateGisLayerFromOccurrenceKeys(occurrenceKeys,layerTitle,layerDescr,author,credits); logger.info("generated gis layer jobID: "+jobId); Date submitTime = DateUtil.getDateFormat(Calendar.getInstance()); GisLayerJob gisLJ = new GisLayerJob(jobId, layerTitle, 0, submitTime.getTime(), 0, 0, layerDescr, DownloadState.PENDING.toString(), totalPoints); gisLayerJob.insert(gisLJ); return new JobGisLayerModel(jobId, layerTitle, DownloadState.PENDING,null, submitTime, null, null, layerDescr, 0, totalPoints); } catch (Exception e) { logger.error("An error occurred creating the map", e); throw new Exception(e.getMessage()); } } /** * Convert job. * * @param job the job * @param statusResponse the status response * @param gisLayerJobP the gis layer job p * @return the job gis layer model */ public static JobGisLayerModel convertJob(GisLayerJob job, CompleteJobStatus statusResponse, GisLayerJobPersistence gisLayerJobP){ //TODO SET END TIME JobGisLayerModel jobGisModel; DownloadState downloadState = null; long endTime = 0; JobStatus status = statusResponse.getStatus(); downloadState = getDownloadState(status); logger.trace("gis layer jobId: "+job.getId() +" download state: " + downloadState); if(downloadState==null) //Case with exception return null; //SET SUBMIT TIME long submitTime = job.getSubmitTime(); Date submit = DateUtil.millisecondsToDate(submitTime); // int completedEntry = 0; // if(statusResponse.getCompletedEntries()>0) // completedEntry = statusResponse.getCompletedEntries(); boolean changeStatus = false; //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; } } int completedEntry = 0; if(statusResponse.getCompletedEntries()>0) completedEntry = statusResponse.getCompletedEntries(); //TODO jobGisModel = new JobGisLayerModel(job.getId(), job.getName(), downloadState, completedEntry, job.getTotalPoints()); jobGisModel.setSubmitTime(submit); try{ boolean changeEndTime = false; //UPDATE END TIME if(downloadState.equals(DownloadState.FAILED) || downloadState.equals(DownloadState.COMPLETED)){ if(statusResponse.getEndDate()!=null && job.getEndTime()==0){ //UPDATE end time first time only logger.trace("UPDATE end time first time only - " + downloadState); endTime = statusResponse.getEndDate().getTimeInMillis(); job.setEndTime(endTime); changeEndTime = true; } } boolean changeStartTime = false; //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()); jobGisModel.setStartTime(start); changeStartTime = true; } //UPDATE DAO if(changeStatus || changeEndTime || changeStartTime){ job.setState(downloadState.toString()); gisLayerJobP.update(job); } }catch (Exception e) { logger.error("An error occurred on update the occurrencesJobDao ", e); } endTime = job.getEndTime(); long elapsedTime = 0; //SET END TIME, BECAUSE IT IS CHANGED if(endTime!=0){ Date end = DateUtil.millisecondsToDate(endTime); jobGisModel.setEndTime(end); elapsedTime = endTime; } else elapsedTime = Calendar.getInstance().getTimeInMillis(); //SET ELAPSED TIME jobGisModel.setElapsedTime(DateUtil.getDifference(submitTime, elapsedTime)); return jobGisModel; } /** * 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: return DownloadState.PENDING; case RUNNING: return DownloadState.ONGOING; default: return null; } } return null; } /** * Delete gis layer job by id. * * @param jobIdentifier the job identifier * @param gisLayerJobDao the gis layer job dao * @return the int */ public static int deleteGisLayerJobById(String jobIdentifier, GisLayerJobPersistence gisLayerJobDao){ logger.trace("Delete gis layer job id: " + jobIdentifier); try{ int removed = gisLayerJobDao.deleteItemByIdField(jobIdentifier); return 1; }catch (Exception e) { logger.error("An error occured deleteGisLayerJobById jobId: " + jobIdentifier + " exception: "+e, e); } logger.trace("job not exists : " +jobIdentifier); return 0; } }