7
7
import ch .systemsx .cisd .etlserver .registrator .api .v2 .IDataSetRegistrationTransactionV2 ;
8
8
import ch .systemsx .cisd .openbis .dss .generic .shared .api .internal .v2 .ISampleImmutable ;
9
9
import java .io .File ;
10
+ import java .io .IOException ;
11
+ import java .nio .file .Files ;
10
12
import life .qbic .registration .openbis .exceptions .fail .MeasurementHasDataException ;
11
13
import life .qbic .registration .openbis .exceptions .fail .UnknownSampleTypeException ;
12
14
import life .qbic .registration .openbis .types .QDatasetType ;
@@ -50,8 +52,9 @@ public boolean shouldRetryProcessing(DataSetRegistrationContext context, Excepti
50
52
51
53
@ Override
52
54
public void process (IDataSetRegistrationTransactionV2 transaction ) {
55
+ File provenanceFile = new File (transaction .getIncoming (), PROVENANCE_FILE_NAME );
53
56
DataSetProvenance dataSetProvenance = ProvenanceParser .parseProvenanceJson (
54
- new File ( transaction . getIncoming (), PROVENANCE_FILE_NAME ) );
57
+ provenanceFile );
55
58
56
59
String measurementId = dataSetProvenance .measurementId ();
57
60
@@ -68,8 +71,34 @@ public void process(IDataSetRegistrationTransactionV2 transaction) {
68
71
newDataSet .setPropertyValue (QPropertyType .Q_TASK_ID .getOpenBisPropertyName (), dataSetProvenance .taskId ());
69
72
QDatasetType qDatasetType = getDatasetType (measurementSample );
70
73
newDataSet .setDataSetType (qDatasetType .name ());
74
+ moveFiles (transaction , newDataSet , provenanceFile );
71
75
72
- transaction .moveFile (transaction .getIncoming ().getAbsolutePath (), newDataSet );
76
+ }
77
+
78
+ private void moveFiles (IDataSetRegistrationTransactionV2 transactionV2 , IDataSet dataSet , File provenanceFile ) {
79
+
80
+ byte [] buffer = null ;
81
+ try {
82
+ buffer = Files .readAllBytes (provenanceFile .toPath ().toAbsolutePath ());
83
+ Files .delete (provenanceFile .toPath ());
84
+ transactionV2 .moveFile (transactionV2 .getIncoming ().getAbsolutePath (), dataSet );
85
+ } catch (RuntimeException e ) {
86
+ //recover provenance file
87
+ try {
88
+ Files .write (provenanceFile .toPath ().toAbsolutePath (), buffer );
89
+ } catch (IOException ex ) {
90
+ throw new RuntimeException (ex );
91
+ }
92
+ throw e ;
93
+ } catch (IOException e ) {
94
+ //recover provenance file
95
+ try {
96
+ Files .write (provenanceFile .toPath ().toAbsolutePath (), buffer );
97
+ } catch (IOException ex ) {
98
+ throw new RuntimeException (ex );
99
+ }
100
+ throw new RuntimeException (e );
101
+ }
73
102
}
74
103
75
104
private static QDatasetType getDatasetType (ISampleImmutable measurementSample ) {
0 commit comments