2727import edu .kit .datamanager .mappingservice .exception .MappingNotFoundException ;
2828import edu .kit .datamanager .mappingservice .exception .MappingServiceException ;
2929import edu .kit .datamanager .mappingservice .exception .MappingServiceUserException ;
30+ import edu .kit .datamanager .mappingservice .plugins .IMappingPlugin ;
3031import edu .kit .datamanager .mappingservice .plugins .MappingPluginException ;
3132import edu .kit .datamanager .mappingservice .plugins .MappingPluginState ;
3233import edu .kit .datamanager .mappingservice .plugins .PluginManager ;
5051import java .security .MessageDigest ;
5152import java .security .NoSuchAlgorithmException ;
5253import java .text .SimpleDateFormat ;
53- import java .util .Collections ;
54- import java .util .Date ;
55- import java .util .Optional ;
54+ import java .util .*;
5655import java .util .concurrent .CompletableFuture ;
5756import java .util .regex .Matcher ;
5857import java .util .regex .Pattern ;
@@ -91,7 +90,6 @@ public class MappingService {
9190 */
9291 private Path jobsOutputDirectory ;
9392
94- private ApplicationProperties applicationProperties ;
9593 private final MeterRegistry meterRegistry ;
9694
9795 /**
@@ -101,9 +99,8 @@ public class MappingService {
10199
102100 @ Autowired
103101 public MappingService (ApplicationProperties applicationProperties , MeterRegistry meterRegistry ) {
104- this .applicationProperties = applicationProperties ;
105102 this .meterRegistry = meterRegistry ;
106- init (this . applicationProperties );
103+ init (applicationProperties );
107104 }
108105
109106 /**
@@ -118,32 +115,35 @@ public MappingService(ApplicationProperties applicationProperties, MeterRegistry
118115 */
119116 public MappingRecord createMapping (String content , MappingRecord mappingRecord ) throws IOException {
120117 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) .
122119 String mappingId = mappingRecord .getMappingId ();
123120 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 );
125122 LOGGER .error (message );
126123 throw new BadArgumentException (message );
127124 }
128125
129126 String mappingType = mappingRecord .getMappingType ();
130127 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 );
132129 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+
133136 throw new BadArgumentException (message );
134137 }
135138
136139 Iterable <MappingRecord > findMapping = mappingRepo .findByMappingIdIn (Collections .singletonList (mappingRecord .getMappingId ()));
137140 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 ());
139142 mappingRecord = findMapping .iterator ().next ();
140143 throw new DuplicateMappingException ("Error: Mapping '" + mappingRecord .getMappingType () + "_" + mappingRecord .getMappingId () + "' already exists!" );
141144 }
142145
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 );
147147 LOGGER .trace ("Mapping with id {} successfully created." , result .getMappingId ());
148148 return mappingRecord ;
149149 }
@@ -163,10 +163,7 @@ public void updateMapping(String content, MappingRecord mappingRecord) throws Ma
163163
164164 LOGGER .trace ("Updating mapping with id {}." , mappingRecord .getMappingId ());
165165 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 );
170167 LOGGER .trace ("Mapping with id {} successfully updated." , mappingRecord .getMappingId ());
171168 }
172169
@@ -189,7 +186,7 @@ public void deleteMapping(MappingRecord mappingRecord) {
189186 try {
190187 deleteMappingFile (mappingRecord );
191188 } 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 );
193190 }
194191 }
195192
@@ -432,7 +429,11 @@ public JobStatus deleteJobAndAssociatedData(String jobId) {
432429 //delete output file if file still exists (even after the job was removed from the queue)
433430 File outputFile = getOutputFile (jobId );
434431 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+ }
436437 } else {
437438 LOGGER .debug ("No output file for job {} found." , jobId );
438439 }
@@ -458,7 +459,11 @@ public JobStatus deleteJobAndAssociatedData(String jobId) {
458459
459460 if (jobOutput .exists ()) {
460461 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+ }
462467 }
463468 LOGGER .trace ("Removing job from queue." );
464469 jobManager .removeJob (jobId );
@@ -488,7 +493,7 @@ private File getOutputFile(String jobId) {
488493 }
489494
490495 /**
491- * Initalize mappings directory and mappingUtil instance.
496+ * Initialize mappings directory and mappingUtil instance.
492497 *
493498 * @param applicationProperties Properties holding mapping directory
494499 * setting.
@@ -578,4 +583,11 @@ private String date2String() {
578583 SimpleDateFormat sdf = new SimpleDateFormat ("_yyyyMMdd_HHmmss" );
579584 return sdf .format (new Date ());
580585 }
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+ }
581593}
0 commit comments