Newer
Older
Juan Fuentes
committed
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import ch.ethz.sis.openbis.generic.asapi.v3.IApplicationServerApi;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.attachment.Attachment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSet;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.update.DataSetUpdate;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.deletion.id.IDeletionId;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.entitytype.EntityKind;
Juan Fuentes
committed
import ch.ethz.sis.openbis.generic.asapi.v3.dto.entitytype.id.EntityTypePermId;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.Experiment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.delete.ExperimentDeletionOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.id.ExperimentIdentifier;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.id.ExperimentPermId;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.id.ProjectIdentifier;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.Sample;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.create.SampleCreation;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.id.SampleIdentifier;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.id.SamplePermId;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.update.SampleUpdate;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.id.SpacePermId;
import ch.ethz.sis.openbis.generic.dssapi.v3.IDataStoreServerApi;
import ethz.ch.MetadataHelper;
import ethz.ch.dataset.DatasetCreationHelper;
import ethz.ch.sample.SampleAuditDataHelper;
import ethz.ch.tag.Tag2SampleTranslator;
Juan Fuentes
committed
public class Experiment2SampleTranslator
{
public static void translate(String sessionToken,
Juan Fuentes
committed
IApplicationServerApi v3,
IDataStoreServerApi v3dss,
Experiment2Sample toMigrate,
Juan Fuentes
committed
boolean COMMIT_CHANGES_TO_OPENBIS) throws Exception {
Juan Fuentes
committed
System.out.println("[START]\t" + toMigrate.getExperimentPermId());
Experiment experiment = MetadataHelper.getExperiment(sessionToken, v3, toMigrate.getExperimentPermId());
Juan Fuentes
committed
// Test mode, only with complete experiments
if(experiment.getAttachments().isEmpty() ||
experiment.getSamples().isEmpty() ||
experiment.getDataSets().isEmpty()) {
return;
}
Juan Fuentes
committed
//
Juan Fuentes
committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
SampleCreation sampleCreation = new SampleCreation();
sampleCreation.setCode(experiment.getCode());
sampleCreation.setSpaceId(new SpacePermId(experiment.getIdentifier().getIdentifier().split("/")[1]));
sampleCreation.setTypeId(new EntityTypePermId(toMigrate.getConfig().getTypeCode()));
ExperimentIdentifier experimentCollectionIdentifier = new ExperimentIdentifier(experiment.getIdentifier().getIdentifier().split("/")[1],
experiment.getIdentifier().getIdentifier().split("/")[2], toMigrate.getConfig().getExperimentToMigrateTo());
sampleCreation.setExperimentId(experimentCollectionIdentifier);
sampleCreation.setProperties(experiment.getProperties());
for(String from:toMigrate.getConfig().getPropertyTypesFromTo().keySet()) {
String to = toMigrate.getConfig().getPropertyTypesFromTo().get(from);
if(experiment.getProperties().containsKey(from)) {
sampleCreation.setProperty(to, experiment.getProperties().get(from));
}
}
// 1. Does the new collection for the sample exists? Create if not
if(COMMIT_CHANGES_TO_OPENBIS && !MetadataHelper.doExperimentExist(v3, sessionToken, experimentCollectionIdentifier.getIdentifier())) {
ProjectIdentifier projectIdentifier = new ProjectIdentifier("/" + experiment.getIdentifier().getIdentifier().split("/")[1] + "/" + experiment.getIdentifier().getIdentifier().split("/")[2]);
v3.createExperiments(sessionToken, Collections.singletonList(MetadataHelper.getMicroscopyExperimentCollectionCreation(projectIdentifier, toMigrate.getConfig().getExperimentToMigrateTo())));
System.out.println("Created Experiment\t" + experimentCollectionIdentifier.getIdentifier());
}
// 2. Does the new sample exists? Create if not
SampleIdentifier sampleIdentifier = new SampleIdentifier(experiment.getIdentifier().getIdentifier());
SamplePermId samplePermId = null;
Sample sample = MetadataHelper.getSample(v3, sessionToken, sampleIdentifier.getIdentifier());
if(COMMIT_CHANGES_TO_OPENBIS && sample == null) {
samplePermId = v3.createSamples(sessionToken, Collections.singletonList(sampleCreation)).get(0);
System.out.println("Created Sample\t" + sampleIdentifier.getIdentifier());
} else {
samplePermId = sample.getPermId();
}
// 3. Does the new sample have assigned as children the samples of the experiment? Assign if not to the new sample and experiment collection.
if(COMMIT_CHANGES_TO_OPENBIS && !experiment.getSamples().isEmpty()) {
Deque<SampleUpdate> toSetNewParentAndCollection = new LinkedList<SampleUpdate>();
for(Sample experimentSample:experiment.getSamples()) {
SampleUpdate update = new SampleUpdate();
update.setSampleId(experimentSample.getPermId());
update.setExperimentId(experimentCollectionIdentifier);
update.getParentIds().add(samplePermId);
toSetNewParentAndCollection.add(update);
System.out.println("Update Children\t" + experimentSample.getIdentifier());
}
MetadataHelper.executeSampleUpdates(sessionToken, v3, COMMIT_CHANGES_TO_OPENBIS, toSetNewParentAndCollection);
}
// 4. Does the new sample have assigned the experiment datasets? Assign if not
if(COMMIT_CHANGES_TO_OPENBIS && !experiment.getDataSets().isEmpty()) {
Deque<DataSetUpdate> toSetNewSampleAndCollection = new LinkedList<DataSetUpdate>();
for(DataSet experimentDataSet:experiment.getDataSets()) {
DataSetUpdate update = new DataSetUpdate();
update.setDataSetId(experimentDataSet.getPermId());
update.setExperimentId(experimentCollectionIdentifier); // Update experiment
// dataSets that are already assigned to a sample, we leave them assigned to them, they should be the containers
// dataSets that are not assigned to a sample should be assigned to their experiment sample
if(experimentDataSet.getSample() == null) {
update.setSampleId(samplePermId);
}
toSetNewSampleAndCollection.add(update);
System.out.println("Update DataSet\t" + experimentDataSet.getPermId());
}
MetadataHelper.executeDataSetUpdates(sessionToken, v3, COMMIT_CHANGES_TO_OPENBIS, toSetNewSampleAndCollection);
}
// 5. Does the new sample have assigned tags as organisational units? Create and add if not.
if(COMMIT_CHANGES_TO_OPENBIS && (sample == null || sample.getParents().isEmpty())) {
List<SamplePermId> tagsAsParentOU = Tag2SampleTranslator.getOrganizationUnitsFromTags(sessionToken, v3, COMMIT_CHANGES_TO_OPENBIS, experiment);
Juan Fuentes
committed
if(tagsAsParentOU != null) {
SampleUpdate sampleUpdate = new SampleUpdate();
sampleUpdate.setSampleId(samplePermId);
for(SamplePermId tagAsParentOU:tagsAsParentOU) {
sampleUpdate.getParentIds().add(tagAsParentOU);
}
System.out.println("Update Tags\t" + tagsAsParentOU);
Deque<SampleUpdate> sampleUpdates = new LinkedList(Arrays.asList(sampleUpdate));
MetadataHelper.executeSampleUpdates(sessionToken, v3, COMMIT_CHANGES_TO_OPENBIS, sampleUpdates);
}
}
// 6. Does the new sample have assigned as datasets the attachments of the experiment? Create if not.
Juan Fuentes
committed
if(COMMIT_CHANGES_TO_OPENBIS && !experiment.getAttachments().isEmpty() && (sample == null || !hasAttachments(sample))) {
Juan Fuentes
committed
for(Attachment attachment:experiment.getAttachments()) {
Juan Fuentes
committed
System.out.println("Add Attachment\t" + attachment.getFileName());
Juan Fuentes
committed
Map<String, String> properties = new HashMap<>();
Juan Fuentes
committed
properties.put("$NAME", attachment.getFileName());
Juan Fuentes
committed
if(attachment.getDescription() != null) {
Juan Fuentes
committed
properties.put("$DESCRIPTION", attachment.getDescription());
Juan Fuentes
committed
}
DatasetCreationHelper.createOneFileDataset(v3dss, sessionToken, EntityKind.SAMPLE, sampleIdentifier.getIdentifier(), "ATTACHMENT", attachment.getFileName(), attachment.getContent(), properties);
Juan Fuentes
committed
}
}
// 7. Add audit data always, repeating updates doesn't hurt
System.out.println("Attach Audit data\t" + toMigrate.getExperimentPermId() + "\t" + sampleIdentifier.getIdentifier());
SampleAuditDataHelper.appendAuditUpdate("openbis_audit_data_update.sql", samplePermId, experiment);
Juan Fuentes
committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
System.out.println("Delete Experiment\t" + toMigrate.getExperimentPermId());
// 8. Delete the migrated experiment
deleteExperiment(sessionToken, v3, COMMIT_CHANGES_TO_OPENBIS, toMigrate.getExperimentPermId());
System.out.println("[FINISH]\t" + toMigrate.getExperimentPermId());
}
private static boolean hasAttachments(Sample sample) {
for(DataSet dataSet:sample.getDataSets()) {
if(dataSet.getType().getCode().equals("ATTACHMENT")) {
return true;
}
}
return false;
}
private static void deleteExperiment(String sessionToken, IApplicationServerApi v3, boolean COMMIT_CHANGES_TO_OPENBIS, ExperimentPermId experimentPermId) {
// Sanity checks before deleting an Experiment
Experiment experiment = MetadataHelper.getExperiment(sessionToken, v3, experimentPermId);
if((experiment.getDataSets().size() + experiment.getSamples().size()) > 0) {
System.out.println("Experiment " + experiment.getIdentifier() + " can't be deleted until DataSets " + experiment.getDataSets().size() + " and Samples " + experiment.getSamples().size() + " are moved. Try again later.");
return;
}
// Deleting Experiments.
if(COMMIT_CHANGES_TO_OPENBIS) {
ExperimentDeletionOptions deleteOptions = new ExperimentDeletionOptions();
deleteOptions.setReason("Microscopy Migration");
IDeletionId deletionId = v3.deleteExperiments(sessionToken, Arrays.asList(experimentPermId), deleteOptions);
v3.confirmDeletions(sessionToken, Arrays.asList(deletionId));
}
}
}