Skip to content

Commit

Permalink
Put To JSON Functions in Separate Class
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Jan 15, 2025
1 parent bfcd643 commit 557ff2b
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.vcell.api.common.events.EventWrapper.EventType;
import org.vcell.api.common.events.ExportEventRepresentation;
import org.vcell.api.common.events.SimulationJobStatusEventRepresentation;
import org.vcell.api.utils.DTOOldAPI;
import org.vcell.rest.server.ClientTopicMessageCollector;
import org.vcell.util.Compare;

Expand Down Expand Up @@ -56,8 +57,8 @@ private void newEventMessage(MessageEvent event) {
if (event instanceof ExportEvent) {
ExportEvent exportEvent = (ExportEvent) event;
try {
ExportEventRepresentation exportEventRep = exportEvent.toJsonRep();
ExportEvent event2 = ExportEvent.fromJsonRep(this, exportEventRep);
ExportEventRepresentation exportEventRep = DTOOldAPI.exportEventToJsonRep(exportEvent);
ExportEvent event2 = DTOOldAPI.exportEventFromJsonRep(this, exportEventRep);
if (!Compare.isEqual(event2.getFormat(),exportEvent.getFormat())) {
throw new RuntimeException("Export event round-trip failed");
}
Expand All @@ -73,8 +74,8 @@ private void newEventMessage(MessageEvent event) {
}else if (event instanceof SimulationJobStatusEvent) {
SimulationJobStatusEvent simJobEvent = (SimulationJobStatusEvent)event;
try {
SimulationJobStatusEventRepresentation simJobEventRep = simJobEvent.toJsonRep();
SimulationJobStatusEvent event2 = SimulationJobStatusEvent.fromJsonRep(this, simJobEventRep);
SimulationJobStatusEventRepresentation simJobEventRep = DTOOldAPI.simulationJobStatusEventToJsonRep(simJobEvent);
SimulationJobStatusEvent event2 = DTOOldAPI.simulationJobStatusEventFromJsonRep(this, simJobEventRep);
if (!Compare.isEqual(event2.getJobStatus(),simJobEvent.getJobStatus())) {
throw new RuntimeException("SimulationJobStatus event round-trip failed");
}
Expand Down Expand Up @@ -119,8 +120,8 @@ private void newEventMessage(MessageEvent event) {
}else if (event instanceof DataJobEvent) {
DataJobEvent dataJobEvent = (DataJobEvent)event;
try {
DataJobEventRepresentation dataJobEventRep = dataJobEvent.toJsonRep();
DataJobEvent event2 = DataJobEvent.fromJsonRep(this, dataJobEventRep);
DataJobEventRepresentation dataJobEventRep = DTOOldAPI.dataJobRepToJsonRep(dataJobEvent);
DataJobEvent event2 = DTOOldAPI.dataJobEventFromJsonRep(this, dataJobEventRep);
if (!Compare.isEqual(event2.getDataIdString(),dataJobEvent.getDataIdString())) {
throw new RuntimeException("DataJob event round-trip failed");
}
Expand Down
5 changes: 5 additions & 0 deletions vcell-apiclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,10 @@
<artifactId>vcell-restclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.vcell</groupId>
<artifactId>vcell-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
252 changes: 252 additions & 0 deletions vcell-apiclient/src/main/java/org/vcell/api/utils/DTOOldAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package org.vcell.api.utils;

import cbit.rmi.event.DataJobEvent;
import cbit.rmi.event.ExportEvent;
import cbit.rmi.event.SimulationJobStatusEvent;
import cbit.vcell.export.server.HumanReadableExportData;
import cbit.vcell.export.server.TimeSpecs;
import cbit.vcell.export.server.VariableSpecs;
import cbit.vcell.server.HtcJobID;
import cbit.vcell.server.SimulationExecutionStatus;
import cbit.vcell.server.SimulationJobStatus;
import cbit.vcell.server.SimulationQueueEntryStatus;
import cbit.vcell.solver.Simulation;
import cbit.vcell.solver.VCSimulationIdentifier;
import cbit.vcell.solver.server.SimulationMessage;
import org.vcell.api.common.events.*;
import org.vcell.util.document.KeyValue;
import org.vcell.util.document.User;
import org.vcell.util.document.VCDataJobID;
import org.vcell.util.document.VCellServerID;

import java.util.Date;

public class DTOOldAPI {
public static DataJobEventRepresentation dataJobRepToJsonRep(DataJobEvent dataJobEvent) {
int eventType = dataJobEvent.getEventTypeID();
Double progress = dataJobEvent.getProgress();
String username = dataJobEvent.getVcDataJobID().getJobOwner().getName();
String userkey = dataJobEvent.getVcDataJobID().getJobOwner().getID().toString();
long jobid = dataJobEvent.getVcDataJobID().getJobID();
boolean isBackgroundTask = dataJobEvent.getVcDataJobID().isBackgroundTask();
String dataIdString = dataJobEvent.getDataIdString();
String dataKey = dataJobEvent.getDataKey().toString();

DataJobEventRepresentation rep = new DataJobEventRepresentation(
eventType, progress, username, userkey, jobid, isBackgroundTask, dataIdString, dataKey);
return rep;
}

public static DataJobEvent dataJobEventFromJsonRep(Object eventSource, DataJobEventRepresentation eventRep) {
Double progress = eventRep.progress;
org.vcell.util.document.User owner = new User(eventRep.username,new KeyValue(eventRep.userkey));
VCDataJobID dataJobID = new VCDataJobID(eventRep.jobid, owner, eventRep.isBackgroundTask);
int eventType = eventRep.eventType;
KeyValue dataKey = new KeyValue(eventRep.dataKey);
String dataIdString = eventRep.dataIdString;
DataJobEvent dataJobEvent = new DataJobEvent(dataJobID, eventType, dataKey, dataIdString, progress);
return dataJobEvent;
}

public static ExportEvent exportEventFromJsonRep(Object eventSource, ExportEventRepresentation rep) {
User user = new User(rep.username, new KeyValue(rep.userkey));
TimeSpecs timeSpecs = null;
if (rep.exportTimeSpecs!=null) {
timeSpecs = timeSpecsFromJsonRep(rep.exportTimeSpecs);
}
VariableSpecs variableSpecs = null;
if (rep.exportVariableSpecs!=null) {
variableSpecs = variableSpecsFromJsonRep(rep.exportVariableSpecs);
}
HumanReadableExportData humanReadableExportData1 = null;
if (rep.exportHumanReadableDataSpec!=null){
humanReadableExportData1 = humanReadableExportDataFromJsonRep(rep.exportHumanReadableDataSpec);
}
ExportEvent event = new ExportEvent(
eventSource, rep.jobid, user,
rep.dataIdString, new KeyValue(rep.dataKey), rep.eventType,
rep.format, rep.location, rep.progress,
timeSpecs, variableSpecs);
event.setHumanReadableExportData(humanReadableExportData1);
return event;
}

public static ExportEventRepresentation exportEventToJsonRep(ExportEvent event) {
ExportTimeSpecs exportTimeSpecs = null;
if (event.getTimeSpecs()!=null) {
exportTimeSpecs = exportTimeSpecsToJsonRep(event.getTimeSpecs());
}
ExportVariableSpecs exportVariableSpecs = null;
if (event.getVariableSpecs()!=null) {
exportVariableSpecs = variableSpecsToJsonRep(event.getVariableSpecs());
}
ExportHumanReadableDataSpec exportHumanReadableDataSpec = null;
if (event.getHumanReadableData() != null){
exportHumanReadableDataSpec = humanReadableExportDataToJsonRep(event.getHumanReadableData());
}

return new ExportEventRepresentation(
event.getEventTypeID(), event.getProgress(), event.getFormat(),
event.getLocation(), event.getUser().getName(), event.getUser().getID().toString(), event.getJobID(),
event.getDataIdString(), event.getDataKey().toString(),
exportTimeSpecs, exportVariableSpecs, exportHumanReadableDataSpec);
}

public static ExportTimeSpecs exportTimeSpecsToJsonRep(TimeSpecs timeSpecs) {
ExportTimeSpecs rep = new ExportTimeSpecs(timeSpecs.getBeginTimeIndex(), timeSpecs.getEndTimeIndex(),
timeSpecs.getAllTimes(), timeSpecs.getModeID());
return rep;
}
public static TimeSpecs timeSpecsFromJsonRep(ExportTimeSpecs rep) {
TimeSpecs timeSpecs = new TimeSpecs(rep.beginTimeIndex, rep.endTimeIndex, rep.allTimes, rep.modeID);
return timeSpecs;
}


public static ExportVariableSpecs variableSpecsToJsonRep(VariableSpecs variableSpecs) {
ExportVariableSpecs rep = new ExportVariableSpecs(variableSpecs.getVariableNames(), variableSpecs.getModeID());
return rep;
}

public static VariableSpecs variableSpecsFromJsonRep(ExportVariableSpecs rep) {
VariableSpecs variableSpecs = new VariableSpecs(rep.variableNames, rep.modeID);
return variableSpecs;
}

public static ExportHumanReadableDataSpec humanReadableExportDataToJsonRep(HumanReadableExportData hr) {
return new ExportHumanReadableDataSpec(hr.biomodelName, hr.applicationName, hr.simulationName, hr.differentParameterValues, hr.serverSavedFileName, hr.applicationType,
hr.nonSpatial, hr.subVolume, hr.zSlices, hr.tSlices, hr.numChannels);
}

public static HumanReadableExportData humanReadableExportDataFromJsonRep(ExportHumanReadableDataSpec rep) {
HumanReadableExportData hre = new HumanReadableExportData(rep.simulationName, rep.applicationName, rep.bioModelName, rep.differentParameterValues,
rep.serverSavedFileName, rep.applicationType, rep.nonSpatial, rep.subVolume);
hre.zSlices = rep.zSlices;
hre.tSlices = rep.tSlices;
hre.numChannels = rep.numChannels;
return hre;
}

public static SimulationJobStatusEvent simulationJobStatusEventFromJsonRep(Object eventSource, SimulationJobStatusEventRepresentation eventRep) {
String simid = Simulation.createSimulationID(new KeyValue(eventRep.jobStatus.simulationKey));
int jobIndex = eventRep.jobStatus.jobIndex;
int taskID = eventRep.jobStatus.taskID;
VCellServerID serverID = VCellServerID.getServerID(eventRep.jobStatus.vcellServerID);
KeyValue simkey = new KeyValue(eventRep.jobStatus.simulationKey);
User owner = new User(eventRep.jobStatus.owner_userid,new KeyValue(eventRep.jobStatus.onwer_userkey));
VCSimulationIdentifier vcSimID = new VCSimulationIdentifier(simkey, owner);
Date submitDate = eventRep.jobStatus.submitDate;

SimulationJobStatus.SchedulerStatus schedulerStatus = null;
if (eventRep.jobStatus.schedulerStatus!=null) {
schedulerStatus = SimulationJobStatus.SchedulerStatus.valueOf(eventRep.jobStatus.schedulerStatus.name());
}

HtcJobID htcJobID = null;
Long htcJobNumber = eventRep.jobStatus.htcJobNumber;
SimulationJobStatusRepresentation.BatchSystemType htcBatchSystemType = eventRep.jobStatus.htcBatchSystemType;
if (htcJobNumber!=null) {
htcJobID = new HtcJobID(htcJobNumber.toString(), HtcJobID.BatchSystemType.valueOf(htcBatchSystemType.name()));
}

SimulationMessage simMessage = null;
SimulationMessage.DetailedState detailedState = SimulationMessage.DetailedState.valueOf(eventRep.jobStatus.detailedState.name());
String message = eventRep.jobStatus.detailedStateMessage;
if (detailedState!=null) {
simMessage = SimulationMessage.create(detailedState, message, htcJobID);
}

SimulationQueueEntryStatus simQueueStatus = null;
Date queueDate = eventRep.jobStatus.queueDate;
Integer queuePriority = eventRep.jobStatus.queuePriority;
SimulationJobStatusRepresentation.SimulationQueueID queueId2 = eventRep.jobStatus.queueId;
if (queueDate!=null && queuePriority!=null) {
simQueueStatus = new SimulationQueueEntryStatus(queueDate,queuePriority, SimulationJobStatus.SimulationQueueID.valueOf(queueId2.name()));
}

SimulationExecutionStatus simExeStatus = null;
Date startDate = eventRep.jobStatus.simexe_startDate;
String computeHost = eventRep.jobStatus.computeHost;
Date latestUpdateDate = eventRep.jobStatus.simexe_latestUpdateDate;
Date endDate = eventRep.jobStatus.simexe_endDate;
Boolean hasData = eventRep.jobStatus.hasData;
if (latestUpdateDate!=null) {
simExeStatus = new SimulationExecutionStatus(startDate, computeHost, latestUpdateDate, endDate, hasData, htcJobID);
}

SimulationJobStatus jobStatus = new SimulationJobStatus(
serverID,vcSimID,jobIndex,
submitDate,schedulerStatus,taskID,
simMessage,simQueueStatus,simExeStatus);

Double progress = eventRep.progress;
Double timepoint = eventRep.timePoint;
String username = eventRep.username;
SimulationJobStatusEvent event = new SimulationJobStatusEvent(eventSource, simid, jobStatus, progress, timepoint, username);
return event;
}

public static SimulationJobStatusEventRepresentation simulationJobStatusEventToJsonRep(SimulationJobStatusEvent event) {
String vcellServerID = event.getJobStatus().getServerID().toString();
Date timeDateStamp = event.getJobStatus().getTimeDateStamp();
String simulationKey = event.getJobStatus().getVCSimulationIdentifier().getSimulationKey().toString();
int taskID = event.getJobStatus().getTaskID();
int jobIndex = event.getJobStatus().getJobIndex();
Date submitDate = event.getJobStatus().getSubmitDate();
String owner_userid = event.getUsername();
String onwer_userkey = event.getJobStatus().getVCSimulationIdentifier().getOwner().getID().toString();
SimulationJobStatusRepresentation.SchedulerStatus schedulerStatus =
SimulationJobStatusRepresentation.SchedulerStatus.valueOf(
event.getJobStatus().getSchedulerStatus().name());
SimulationJobStatusRepresentation.DetailedState detailedState =
SimulationJobStatusRepresentation.DetailedState.valueOf(
event.getJobStatus().getSimulationMessage().getDetailedState().name());
String detailedStateMessage = event.getJobStatus().getSimulationMessage().getDisplayMessage();

Long htcJobNumber = null;
String htcComputeServer = null;
SimulationJobStatusRepresentation.BatchSystemType htcBatchSystemType = null;
Date simexe_startDate = null;
Date simexe_latestUpdateDate = null;
Date simexe_endDate = null;
String computeHost = null;
Boolean hasData = null;
SimulationExecutionStatus simExeStatus = event.getJobStatus().getSimulationExecutionStatus();
if (simExeStatus!=null) {
HtcJobID htcJobID = simExeStatus.getHtcJobID();
if (htcJobID!=null) {
htcJobNumber = htcJobID.getJobNumber();
htcComputeServer = htcJobID.getServer();
htcBatchSystemType = SimulationJobStatusRepresentation.BatchSystemType.valueOf(htcJobID.getBatchSystemType().name());
}
simexe_startDate = simExeStatus.getStartDate();
simexe_latestUpdateDate = simExeStatus.getLatestUpdateDate();
simexe_endDate = simExeStatus.getEndDate();
computeHost = simExeStatus.getComputeHost();
hasData = simExeStatus.hasData();
}


Integer queuePriority = null;
Date queueDate = null;
SimulationJobStatusRepresentation.SimulationQueueID queueId = null;
SimulationQueueEntryStatus simQueueStatus = event.getJobStatus().getSimulationQueueEntryStatus();
if (simQueueStatus!=null) {
queuePriority = simQueueStatus.getQueuePriority();
queueDate = simQueueStatus.getQueueDate();
queueId = SimulationJobStatusRepresentation.SimulationQueueID.valueOf(simQueueStatus.getQueueID().name());
}

SimulationJobStatusRepresentation jobStatus = new SimulationJobStatusRepresentation(
vcellServerID, timeDateStamp, simulationKey,
taskID, jobIndex, submitDate, owner_userid, onwer_userkey,
schedulerStatus, detailedState, detailedStateMessage,
htcJobNumber, htcComputeServer, htcBatchSystemType,
queuePriority, queueDate, queueId,
simexe_startDate, simexe_latestUpdateDate, simexe_endDate, computeHost, hasData);
SimulationJobStatusEventRepresentation eventRep = new SimulationJobStatusEventRepresentation(jobStatus, event.getProgress(),
event.getTimepoint(), event.getUsername());
return eventRep;
}

}
28 changes: 0 additions & 28 deletions vcell-core/src/main/java/cbit/rmi/event/DataJobEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

package cbit.rmi.event;

import org.vcell.api.common.events.DataJobEventRepresentation;
import org.vcell.util.document.KeyValue;
import org.vcell.util.document.TimeSeriesJobResults;
import org.vcell.util.document.User;
import org.vcell.util.document.VCDataJobID;

Expand Down Expand Up @@ -117,30 +115,4 @@ public boolean isIntendedFor(User user){
return user.equals(getUser());
}

public DataJobEventRepresentation toJsonRep() {
int eventType = this.eventType;
Double progress = this.progress;
String username = this.vcDataJobID.getJobOwner().getName();
String userkey = this.vcDataJobID.getJobOwner().getID().toString();
long jobid = this.vcDataJobID.getJobID();
boolean isBackgroundTask = this.vcDataJobID.isBackgroundTask();
String dataIdString = this.dataIdString;
String dataKey = this.dataKey.toString();

DataJobEventRepresentation rep = new DataJobEventRepresentation(
eventType, progress, username, userkey, jobid, isBackgroundTask, dataIdString, dataKey);
return rep;
}

public static DataJobEvent fromJsonRep(Object eventSource, DataJobEventRepresentation eventRep) {
Double progress = eventRep.progress;
User owner = new User(eventRep.username,new KeyValue(eventRep.userkey));
VCDataJobID dataJobID = new VCDataJobID(eventRep.jobid, owner, eventRep.isBackgroundTask);
int eventType = eventRep.eventType;
KeyValue dataKey = new KeyValue(eventRep.dataKey);
String dataIdString = eventRep.dataIdString;
DataJobEvent dataJobEvent = new DataJobEvent(dataJobID, eventType, dataKey, dataIdString, progress);
return dataJobEvent;
}

}
Loading

0 comments on commit 557ff2b

Please sign in to comment.