use of com.sdicons.json.model.JSONInteger in project Gemma by PavlidisLab.
the class ExpressionExperimentFormController method updateBioMaterialMap.
/**
* Change the relationship between bioassays and biomaterials.
*
* @param request request
* @param expressionExperiment ee
* @return true if there were changes
*/
private boolean updateBioMaterialMap(HttpServletRequest request, ExpressionExperiment expressionExperiment) {
// parse JSON-serialized map
String jsonSerialization = request.getParameter("assayToMaterialMap");
// convert back to a map
Map<String, JSONValue> bioAssayMap;
try (StringInputStream aStream = new StringInputStream(jsonSerialization)) {
JSONParser parser = new JSONParser(aStream);
bioAssayMap = ((JSONObject) parser.nextValue()).getValue();
} catch (IOException | ANTLRException e) {
throw new RuntimeException(e);
}
Map<BioAssay, BioMaterial> deleteAssociations = new HashMap<>();
Set<Entry<String, JSONValue>> bioAssays = bioAssayMap.entrySet();
boolean anyChanges = false;
int newBioMaterialCount = 0;
for (Entry<String, JSONValue> entry : bioAssays) {
// if it is, skip over this entry
if (entry.getKey().equalsIgnoreCase("nullElement")) {
continue;
}
Long bioAssayId = Long.parseLong(entry.getKey());
JSONValue value = entry.getValue();
Long newMaterialId;
if (value.isString()) {
newMaterialId = Long.parseLong(((JSONString) value).getValue());
} else {
newMaterialId = ((JSONInteger) value).getValue().longValue();
}
// maybe we need to do
BioAssay bioAssay = bioAssayService.load(bioAssayId);
if (bioAssay == null) {
throw new IllegalArgumentException("Bioassay with id=" + bioAssayId + " was not associated with the experiment");
}
BioMaterial currentBioMaterial = bioAssay.getSampleUsed();
if (newMaterialId.equals(currentBioMaterial.getId())) {
// / no change
continue;
}
BioMaterial newMaterial;
if (newMaterialId < 0) {
// This kludge signifies that it is a 'brand new' biomaterial.
newMaterial = bioMaterialService.copy(currentBioMaterial);
newMaterial.setName("Modeled after " + currentBioMaterial.getName());
newMaterial.getFactorValues().clear();
newMaterial = (BioMaterial) persisterHelper.persist(newMaterial);
newBioMaterialCount++;
} else {
// FIXME can we just use this from the experiment, probably no need to fetch it again.
newMaterial = bioMaterialService.load(newMaterialId);
if (newMaterial == null) {
throw new IllegalArgumentException("BioMaterial with id=" + newMaterialId + " could not be loaded");
}
}
anyChanges = true;
BaseFormController.log.info("Associating " + bioAssay + " with " + newMaterial);
bioAssayService.addBioMaterialAssociation(bioAssay, newMaterial);
}
if (anyChanges) {
/*
* FIXME Decide if we need to remove the biomaterial -> factor value associations, it could be completely
* fouled up.
*/
BaseFormController.log.info("There were changes to the BioMaterial -> BioAssay map");
this.audit(expressionExperiment, BioMaterialMappingUpdate.Factory.newInstance(), // remove unnecessary biomaterial associations
newBioMaterialCount + " biomaterials");
Collection<BioAssay> deleteKeys = deleteAssociations.keySet();
for (BioAssay assay : deleteKeys) {
/*
* BUG: if this fails, we end up with a useless extra biomaterial associated with the bioassay.
*/
bioAssayService.removeBioMaterialAssociation(assay, deleteAssociations.get(assay));
}
} else {
BaseFormController.log.info("There were no changes to the BioMaterial -> BioAssay map");
}
return anyChanges;
}
Aggregations