27
27
import edu .kit .datamanager .mappingservice .exception .MappingNotFoundException ;
28
28
import edu .kit .datamanager .mappingservice .exception .MappingServiceException ;
29
29
import edu .kit .datamanager .mappingservice .exception .MappingServiceUserException ;
30
+ import edu .kit .datamanager .mappingservice .plugins .IMappingPlugin ;
30
31
import edu .kit .datamanager .mappingservice .plugins .MappingPluginException ;
31
32
import edu .kit .datamanager .mappingservice .plugins .MappingPluginState ;
32
33
import edu .kit .datamanager .mappingservice .plugins .PluginManager ;
50
51
import java .security .MessageDigest ;
51
52
import java .security .NoSuchAlgorithmException ;
52
53
import java .text .SimpleDateFormat ;
53
- import java .util .Collections ;
54
- import java .util .Date ;
55
- import java .util .Optional ;
54
+ import java .util .*;
56
55
import java .util .concurrent .CompletableFuture ;
57
56
import java .util .regex .Matcher ;
58
57
import java .util .regex .Pattern ;
@@ -91,7 +90,6 @@ public class MappingService {
91
90
*/
92
91
private Path jobsOutputDirectory ;
93
92
94
- private ApplicationProperties applicationProperties ;
95
93
private final MeterRegistry meterRegistry ;
96
94
97
95
/**
@@ -101,9 +99,8 @@ public class MappingService {
101
99
102
100
@ Autowired
103
101
public MappingService (ApplicationProperties applicationProperties , MeterRegistry meterRegistry ) {
104
- this .applicationProperties = applicationProperties ;
105
102
this .meterRegistry = meterRegistry ;
106
- init (this . applicationProperties );
103
+ init (applicationProperties );
107
104
}
108
105
109
106
/**
@@ -118,32 +115,35 @@ public MappingService(ApplicationProperties applicationProperties, MeterRegistry
118
115
*/
119
116
public MappingRecord createMapping (String content , MappingRecord mappingRecord ) throws IOException {
120
117
LOGGER .trace ("Creating mapping with id {}." , mappingRecord .getMappingId ());
121
- // Check for valid mapping ID (should contain at least one non whitespace.
118
+ // Check for valid mapping ID (should contain at least one non whitespace) .
122
119
String mappingId = mappingRecord .getMappingId ();
123
120
if ((mappingId == null ) || (mappingId .isBlank ())) {
124
- String message = String .format ("MappingID shouldn't be empty or contain only whitespaces. You provide '%s'" , mappingId );
121
+ String message = String .format ("MappingID shouldn't be empty or contain only whitespaces. You provided '%s'" , mappingId );
125
122
LOGGER .error (message );
126
123
throw new BadArgumentException (message );
127
124
}
128
125
129
126
String mappingType = mappingRecord .getMappingType ();
130
127
if ((mappingType == null ) || (mappingType .isBlank () || !pluginManager .getPlugins ().containsKey (mappingType ))) {
131
- String message = String .format ("MappingType shouldn't be empty or contain only whitespaces and must be a registered plugin id. You provide '%s'" , mappingType );
128
+ String message = String .format ("MappingType shouldn't be empty or contain only whitespaces and must be a registered plugin id. You provided '%s'" , mappingType );
132
129
LOGGER .error (message );
130
+ Set <Map .Entry <String , IMappingPlugin >> entries = pluginManager .getPlugins ().entrySet ();
131
+ LOGGER .info ("Registered plugins: " );
132
+ for (Map .Entry <String , IMappingPlugin > entry : entries ) {
133
+ LOGGER .info (" * {}" , entry .getKey ());
134
+ }
135
+
133
136
throw new BadArgumentException (message );
134
137
}
135
138
136
139
Iterable <MappingRecord > findMapping = mappingRepo .findByMappingIdIn (Collections .singletonList (mappingRecord .getMappingId ()));
137
140
if (findMapping .iterator ().hasNext ()) {
138
- LOGGER .error ("Unable to create mapping with id {}. Mapping id is alreadyy used." , mappingRecord .getMappingId ());
141
+ LOGGER .error ("Unable to create mapping with id {}. Mapping id is already used." , mappingRecord .getMappingId ());
139
142
mappingRecord = findMapping .iterator ().next ();
140
143
throw new DuplicateMappingException ("Error: Mapping '" + mappingRecord .getMappingType () + "_" + mappingRecord .getMappingId () + "' already exists!" );
141
144
}
142
145
143
- LOGGER .trace ("Saving mapping file." );
144
- saveMappingFile (content , mappingRecord );
145
- LOGGER .trace ("Persisting mapping record." );
146
- MappingRecord result = mappingRepo .save (mappingRecord );
146
+ MappingRecord result = persistMapping (content , mappingRecord );
147
147
LOGGER .trace ("Mapping with id {} successfully created." , result .getMappingId ());
148
148
return mappingRecord ;
149
149
}
@@ -163,10 +163,7 @@ public void updateMapping(String content, MappingRecord mappingRecord) throws Ma
163
163
164
164
LOGGER .trace ("Updating mapping with id {}." , mappingRecord .getMappingId ());
165
165
mappingRecord .setMappingDocumentUri (findMapping .get ().getMappingDocumentUri ());
166
- LOGGER .trace ("Saving mapping file." );
167
- saveMappingFile (content , mappingRecord );
168
- LOGGER .trace ("Persisting mapping record." );
169
- mappingRepo .save (mappingRecord );
166
+ persistMapping (content , mappingRecord );
170
167
LOGGER .trace ("Mapping with id {} successfully updated." , mappingRecord .getMappingId ());
171
168
}
172
169
@@ -189,7 +186,7 @@ public void deleteMapping(MappingRecord mappingRecord) {
189
186
try {
190
187
deleteMappingFile (mappingRecord );
191
188
} catch (IOException e ) {
192
- LOGGER .error (String . format ( "Failed to delete mapping file at %s . Please remove it manually." , mappingRecord .getMappingDocumentUri () ), e );
189
+ LOGGER .error ("Failed to delete mapping file at {} . Please remove it manually." , mappingRecord .getMappingDocumentUri (), e );
193
190
}
194
191
}
195
192
@@ -432,7 +429,11 @@ public JobStatus deleteJobAndAssociatedData(String jobId) {
432
429
//delete output file if file still exists (even after the job was removed from the queue)
433
430
File outputFile = getOutputFile (jobId );
434
431
if (outputFile .exists ()) {
435
- outputFile .delete ();
432
+ if (outputFile .delete ()){
433
+ LOGGER .trace ("Output file {} deleted." , outputFile );
434
+ }else {
435
+ LOGGER .warn ("Output file {} could not be deleted." , outputFile );
436
+ }
436
437
} else {
437
438
LOGGER .debug ("No output file for job {} found." , jobId );
438
439
}
@@ -458,7 +459,11 @@ public JobStatus deleteJobAndAssociatedData(String jobId) {
458
459
459
460
if (jobOutput .exists ()) {
460
461
LOGGER .trace (" - Deleting job output" );
461
- jobOutput .delete ();
462
+ if (jobOutput .delete ()){
463
+ LOGGER .trace (" - Output file {} deleted." , jobOutput );
464
+ }else {
465
+ LOGGER .warn (" ! Output file {} could not be deleted." , jobOutput );
466
+ }
462
467
}
463
468
LOGGER .trace ("Removing job from queue." );
464
469
jobManager .removeJob (jobId );
@@ -488,7 +493,7 @@ private File getOutputFile(String jobId) {
488
493
}
489
494
490
495
/**
491
- * Initalize mappings directory and mappingUtil instance.
496
+ * Initialize mappings directory and mappingUtil instance.
492
497
*
493
498
* @param applicationProperties Properties holding mapping directory
494
499
* setting.
@@ -578,4 +583,11 @@ private String date2String() {
578
583
SimpleDateFormat sdf = new SimpleDateFormat ("_yyyyMMdd_HHmmss" );
579
584
return sdf .format (new Date ());
580
585
}
586
+
587
+ private MappingRecord persistMapping (String content , MappingRecord mappingRecord ) throws IOException {
588
+ LOGGER .trace ("Saving mapping file." );
589
+ saveMappingFile (content , mappingRecord );
590
+ LOGGER .trace ("Persisting mapping record." );
591
+ return mappingRepo .save (mappingRecord );
592
+ }
581
593
}
0 commit comments