32
32
import java .io .File ;
33
33
import java .io .IOException ;
34
34
import java .net .URI ;
35
- import java .net .URISyntaxException ;
36
35
import java .nio .charset .StandardCharsets ;
37
36
import java .nio .file .Files ;
38
37
import java .nio .file .Path ;
39
38
import java .nio .file .Paths ;
40
39
import java .security .MessageDigest ;
41
40
import java .security .NoSuchAlgorithmException ;
42
41
import java .text .SimpleDateFormat ;
43
- import java .util .*;
42
+ import java .util .Collections ;
43
+ import java .util .Date ;
44
+ import java .util .Optional ;
44
45
45
46
/**
46
47
* Service for managing mappings.
@@ -63,7 +64,7 @@ public class MappingService {
63
64
private final static Logger LOGGER = LoggerFactory .getLogger (MappingService .class );
64
65
65
66
@ Autowired
66
- public MappingService (ApplicationProperties applicationProperties ) throws URISyntaxException {
67
+ public MappingService (ApplicationProperties applicationProperties ) {
67
68
init (applicationProperties );
68
69
}
69
70
@@ -74,9 +75,7 @@ public MappingService(ApplicationProperties applicationProperties) throws URISyn
74
75
* @param mappingRecord record of the mapping
75
76
*/
76
77
public void createMapping (String content , MappingRecord mappingRecord ) throws IOException {
77
- // Right now only one mapping per mappingID is allowed. May change in future.
78
- // Optional<MappingRecord> findMapping = mappingRepo.findByMappingIdAndMappingType(mappingRecord.getMappingId(), mappingRecord.getMappingType());
79
- Iterable <MappingRecord > findMapping = mappingRepo .findByMappingIdInOrMappingTypeIn (Arrays .asList (mappingRecord .getMappingId ()), Arrays .asList ((String ) null ));
78
+ Iterable <MappingRecord > findMapping = mappingRepo .findByMappingIdIn (Collections .singletonList (mappingRecord .getMappingId ()));
80
79
if (findMapping .iterator ().hasNext ()) {
81
80
mappingRecord = findMapping .iterator ().next ();
82
81
throw new MappingException ("Error: Mapping '" + mappingRecord .getMappingType () + "_" + mappingRecord .getMappingId () + "' already exists!" );
@@ -92,8 +91,8 @@ public void createMapping(String content, MappingRecord mappingRecord) throws IO
92
91
* @param mappingRecord record of the mapping
93
92
*/
94
93
public void updateMapping (String content , MappingRecord mappingRecord ) throws IOException {
95
- Optional <MappingRecord > findMapping = mappingRepo .findByMappingIdAndMappingType (mappingRecord .getMappingId (), mappingRecord . getMappingType ());
96
- if (! findMapping .isPresent ()) {
94
+ Optional <MappingRecord > findMapping = mappingRepo .findByMappingId (mappingRecord .getMappingId ());
95
+ if (findMapping .isEmpty ()) {
97
96
throw new MappingException ("Error: Mapping '" + mappingRecord .getMappingType () + "_" + mappingRecord .getMappingId () + "' doesn't exist!" );
98
97
}
99
98
mappingRecord .setMappingDocumentUri (findMapping .get ().getMappingDocumentUri ());
@@ -107,8 +106,8 @@ public void updateMapping(String content, MappingRecord mappingRecord) throws IO
107
106
* @param mappingRecord record of the mapping
108
107
*/
109
108
public void deleteMapping (MappingRecord mappingRecord ) throws IOException {
110
- Optional <MappingRecord > findMapping = mappingRepo .findByMappingIdAndMappingType (mappingRecord .getMappingId (), mappingRecord . getMappingType ());
111
- if (! findMapping .isPresent ()) {
109
+ Optional <MappingRecord > findMapping = mappingRepo .findByMappingId (mappingRecord .getMappingId ());
110
+ if (findMapping .isEmpty ()) {
112
111
throw new MappingException ("Error: Mapping '" + mappingRecord .getMappingType () + "_" + mappingRecord .getMappingId () + "' doesn't exist!" );
113
112
}
114
113
mappingRecord = findMapping .get ();
@@ -120,24 +119,22 @@ public void deleteMapping(MappingRecord mappingRecord) throws IOException {
120
119
* Execute mapping and get the location of result file. If no according
121
120
* mapping is found the src file will be returned.
122
121
*
123
- * @param contentUrl Content of the src file.
124
- * @param mappingId filename of the mapping
125
- * @param mappingType type of the mapping.
122
+ * @param contentUrl Content of the src file.
123
+ * @param mappingId id of the mapping
126
124
* @return Path to result file.
127
125
*/
128
- public Optional <Path > executeMapping (URI contentUrl , String mappingId , String mappingType ) throws MappingPluginException {
129
- Optional <Path > returnValue = Optional . empty () ;
126
+ public Optional <Path > executeMapping (URI contentUrl , String mappingId ) throws MappingPluginException {
127
+ Optional <Path > returnValue ;
130
128
Optional <Path > download = FileUtil .downloadResource (contentUrl );
131
- MappingRecord mappingRecord = null ;
129
+ MappingRecord mappingRecord ;
132
130
133
131
if (download .isPresent ()) {
134
- LOGGER .trace ("Execute Mapping for '{}', and mapping '{}/{} '." , contentUrl .toString (), mappingId , mappingType );
132
+ LOGGER .trace ("Execute Mapping for '{}', and mapping '{}'." , contentUrl .toString (), mappingId );
135
133
Path srcFile = download .get ();
136
134
// Get mapping file
137
- Optional <MappingRecord > optionalMappingRecord = mappingRepo .findByMappingIdAndMappingType (mappingId , mappingType );
135
+ Optional <MappingRecord > optionalMappingRecord = mappingRepo .findByMappingId (mappingId );
138
136
if (optionalMappingRecord .isPresent ()) {
139
137
mappingRecord = optionalMappingRecord .get ();
140
- // mappingRecord.getMappingDocumentUri();
141
138
Path mappingFile = Paths .get (mappingRecord .getMappingDocumentUri ());
142
139
// execute mapping
143
140
Path resultFile ;
@@ -150,42 +147,18 @@ public Optional<Path> executeMapping(URI contentUrl, String mappingId, String ma
150
147
returnValue = Optional .of (srcFile );
151
148
}
152
149
} else {
153
- String message = contentUrl != null ? "Error: Downloading content from '" + contentUrl . toString () + "'!" : "Error: No URL provided!" ;
150
+ String message = contentUrl != null ? "Error: Downloading content from '" + contentUrl + "'!" : "Error: No URL provided!" ;
154
151
throw new MappingException (message );
155
152
}
156
153
return returnValue ;
157
154
}
158
155
159
- /**
160
- * Execute mapping(s) and get the location of result file.
161
- *
162
- * @param contentUrl Content of the src file.
163
- * @param mappingId filename of the mapping
164
- * @return List of paths to all result files.
165
- */
166
- public List <Path > executeMapping (URI contentUrl , String mappingId ) throws MappingPluginException {
167
- List <Path > returnValue = new ArrayList <>();
168
- String noMappingType = null ;
169
-
170
- Iterator <MappingRecord > findMapping = mappingRepo .findByMappingIdInOrMappingTypeIn (Arrays .asList (mappingId ), Arrays .asList (noMappingType )).iterator ();
171
- String mappingType = null ;
172
- if (findMapping .hasNext ()) {
173
- mappingType = findMapping .next ().getMappingType ();
174
- }
175
- Optional <Path > executeMapping = executeMapping (contentUrl , mappingId , mappingType );
176
- if (executeMapping .isPresent ()) {
177
- returnValue .add (executeMapping .get ());
178
- }
179
-
180
- return returnValue ;
181
- }
182
-
183
156
/**
184
157
* Initalize mappings directory and mappingUtil instance.
185
158
*
186
159
* @param applicationProperties Properties holding mapping directory setting.
187
160
*/
188
- private void init (ApplicationProperties applicationProperties ) throws URISyntaxException {
161
+ private void init (ApplicationProperties applicationProperties ) {
189
162
if ((applicationProperties != null ) && (applicationProperties .getMappingsLocation () != null )) {
190
163
try {
191
164
mappingsDirectory = Files .createDirectories (new File (applicationProperties .getMappingsLocation ().getPath ()).getAbsoluteFile ().toPath ());
@@ -205,15 +178,15 @@ private void init(ApplicationProperties applicationProperties) throws URISyntaxE
205
178
* @throws IOException error writing file.
206
179
*/
207
180
private void saveMappingFile (String content , MappingRecord mapping ) throws IOException {
208
- Path newMappingFile = null ;
181
+ Path newMappingFile ;
209
182
if ((content != null ) && (mapping != null ) && (mapping .getMappingId () != null ) && (mapping .getMappingType () != null )) {
210
183
LOGGER .debug ("Storing mapping file with id '{}' and type '{}'" , mapping .getMappingId (), mapping .getMappingType ());
211
184
LOGGER .trace ("Content of mapping: '{}'" , content );
212
185
try {
213
186
// 'delete' old file
214
187
deleteMappingFile (mapping );
215
188
newMappingFile = Paths .get (mappingsDirectory .toString (), mapping .getMappingId () + "_" + mapping .getMappingType () + ".mapping" );
216
- LOGGER .trace ("Write content to '{}'" , newMappingFile . toString () );
189
+ LOGGER .trace ("Write content to '{}'" , newMappingFile );
217
190
FileUtils .writeStringToFile (newMappingFile .toFile (), content , StandardCharsets .UTF_8 );
218
191
mapping .setMappingDocumentUri (newMappingFile .toString ());
219
192
byte [] data = content .getBytes ();
@@ -249,7 +222,7 @@ private void deleteMappingFile(MappingRecord mapping) throws IOException {
249
222
Path deleteFile = Paths .get (mapping .getMappingDocumentUri ());
250
223
if (deleteFile .toFile ().exists ()) {
251
224
Path newFileName = Paths .get (deleteFile .getParent ().toString (), deleteFile .getFileName () + date2String ());
252
- LOGGER .trace ("Move mapping file fo '{}'" , newFileName . toString () );
225
+ LOGGER .trace ("Move mapping file fo '{}'" , newFileName );
253
226
FileUtils .moveFile (deleteFile .toFile (), newFileName .toFile ());
254
227
}
255
228
}
0 commit comments