11package org .ihtsdo .rvf .core .service ;
22
3+ import jakarta .annotation .PostConstruct ;
4+ import jakarta .annotation .Resource ;
35import org .apache .commons .dbcp .BasicDataSource ;
46import org .apache .commons .io .FileUtils ;
57import org .apache .commons .io .IOUtils ;
2022import org .springframework .core .io .ResourceLoader ;
2123import org .springframework .stereotype .Service ;
2224
23- import jakarta .annotation .PostConstruct ;
24- import jakarta .annotation .Resource ;
2525import java .io .*;
2626import java .nio .file .Files ;
2727import java .nio .file .LinkOption ;
@@ -150,7 +150,7 @@ public String getRVFVersion(String product, String releaseVersion) {
150150 * for uploading prospective releases since they do not need to be stored for later use.
151151 *
152152 */
153- public boolean uploadPublishedReleaseData (final InputStream inputStream , final String fileName , final String product , final String version ) throws BusinessServiceException {
153+ public boolean uploadPublishedReleaseData (final InputStream inputStream , final String fileName , final String product , final String version , final List < String > excludedRF2Files ) throws BusinessServiceException {
154154 // copy release pack zip to data location
155155 logger .info (RECEIVING_RELEASE_DATA_INFO_MSG , fileName );
156156 final File fileDestination = new File (sctDataFolder .getAbsolutePath (), fileName );
@@ -170,14 +170,14 @@ public boolean uploadPublishedReleaseData(final InputStream inputStream, final S
170170 logger .info ("Release version is already known in RVF and the existing one will be deleted and reloaded: {}" , rvfVersion );
171171 }
172172 List <String > rf2FilesLoaded = new ArrayList <>();
173- String schemaName = loadSnomedData (rvfVersion , rf2FilesLoaded , fileDestination );
173+ String schemaName = loadSnomedData (rvfVersion , rf2FilesLoaded , excludedRF2Files , fileDestination );
174174 logger .info ("schemaName = {}" , schemaName );
175175 schemaNames .add (schemaName );
176176 return true ;
177177 }
178178
179179
180- public boolean uploadReleaseDataIntoDB (final InputStream inputStream , String fileName , String schemaName ) throws BusinessServiceException {
180+ public boolean uploadReleaseDataIntoDB (final InputStream inputStream , String fileName , String schemaName , List < String > excludedRF2Files ) throws BusinessServiceException {
181181 // copy release pack zip to data location
182182 logger .info (RECEIVING_RELEASE_DATA_INFO_MSG , fileName );
183183 final File fileDestination = new File (sctDataFolder .getAbsolutePath (), fileName );
@@ -197,22 +197,22 @@ public boolean uploadReleaseDataIntoDB(final InputStream inputStream, String fil
197197 }
198198 logger .info ("Loading data into schema {}" , schemaName );
199199 List <String > rf2FilesLoaded = new ArrayList <>();
200- loadSnomedData (schemaName , rf2FilesLoaded , fileDestination );
200+ loadSnomedData (schemaName , rf2FilesLoaded , excludedRF2Files , fileDestination );
201201 logger .info ("schemaName = {}" , schemaName );
202202 return true ;
203203 }
204204
205- public boolean uploadPublishedReleaseData (final File releasePackZip , final String product , final String version ) throws BusinessServiceException {
205+ public boolean uploadPublishedReleaseData (final File releasePackZip , final String product , final String version , final List < String > excludedRF2Files ) throws BusinessServiceException {
206206 boolean result = false ;
207207 try (InputStream inputStream = new FileInputStream (releasePackZip )) {
208- result = uploadPublishedReleaseData (inputStream , releasePackZip .getName (), product , version );
208+ result = uploadPublishedReleaseData (inputStream , releasePackZip .getName (), product , version , excludedRF2Files );
209209 } catch (final IOException e ) {
210210 logger .error ("Error during upload release:" + releasePackZip .getName (), e );
211211 }
212212 return result ;
213213 }
214214
215- public String loadSnomedData (final String versionName , List <String > rf2FilesLoaded , final File ... zipDataFile ) throws BusinessServiceException {
215+ public String loadSnomedData (final String versionName , List <String > rf2FilesLoaded , List < String > excludedRF2Files , final File ... zipDataFile ) throws BusinessServiceException {
216216 File outputFolder = null ;
217217 final String createdSchemaName = versionName .startsWith (RVF_DB_PREFIX ) ? versionName : RVF_DB_PREFIX + versionName ;
218218 final long startTime = Calendar .getInstance ().getTimeInMillis ();
@@ -232,7 +232,7 @@ public String loadSnomedData(final String versionName, List<String> rf2FilesLoad
232232 ZipFileUtils .extractFilesFromZipToOneFolder (zipFile , outputFolder .getAbsolutePath ());
233233 }
234234 createSchema (createdSchemaName );
235- loadReleaseFilesToDB (outputFolder , rvfDynamicDataSource , rf2FilesLoaded , createdSchemaName );
235+ loadReleaseFilesToDB (outputFolder , rvfDynamicDataSource , rf2FilesLoaded , createdSchemaName , excludedRF2Files );
236236 } catch (final RVFExecutionException | IOException e ) {
237237 List <String > fileNames = Arrays .stream (zipDataFile ).map (File ::getName ).toList ();
238238 final String errorMsg = String .format ("Error while loading file %s into version %s" , Arrays .toString (fileNames .toArray ()), versionName );
@@ -245,10 +245,18 @@ public String loadSnomedData(final String versionName, List<String> rf2FilesLoad
245245 return createdSchemaName ;
246246 }
247247
248- private void loadReleaseFilesToDB (final File rf2TextFilesDir , final RvfDynamicDataSource dataSource , List <String > rf2FilesLoaded , String schemaName ) throws RVFExecutionException {
248+ private void loadReleaseFilesToDB (final File rf2TextFilesDir , final RvfDynamicDataSource dataSource , List <String > rf2FilesLoaded , String schemaName , List < String > excludedRF2Files ) throws RVFExecutionException {
249249 if (rf2TextFilesDir != null ) {
250+ final List <String > allExcludedRF2Files = new ArrayList <>();
251+ if (excludedRF2Files != null ) {
252+ allExcludedRF2Files .addAll (excludedRF2Files );
253+ for (String excludedFile : excludedRF2Files ) {
254+ allExcludedRF2Files .add (excludedFile .replace ("Delta_" , "Full_" ).replace ("Delta-" , "Full-" ));
255+ allExcludedRF2Files .add (excludedFile .replace ("Delta_" , "Snapshot_" ).replace ("Delta-" , "Snapshot-" ));
256+ }
257+ }
250258 final String [] rf2Files = rf2TextFilesDir .list ((dir , name ) ->
251- name .endsWith (".txt" ) && (name .startsWith ("der2" ) || name .startsWith ("sct2" ) || name .startsWith ("xder2" ) || name .startsWith ("xsct2" ))
259+ name .endsWith (".txt" ) && (name .startsWith ("der2" ) || name .startsWith ("sct2" ) || name .startsWith ("xder2" ) || name .startsWith ("xsct2" )) && ! isExcludedFile ( name , allExcludedRF2Files )
252260 );
253261 if (rf2Files != null && rf2Files .length > 0 ) {
254262 final ReleaseFileDataLoader dataLoader = new ReleaseFileDataLoader (dataSource , schemaName , new MySqlDataTypeConverter ());
@@ -257,6 +265,16 @@ private void loadReleaseFilesToDB(final File rf2TextFilesDir, final RvfDynamicDa
257265 }
258266 }
259267
268+ private boolean isExcludedFile (String name , List <String > allExcludedRF2Files ) {
269+ if (allExcludedRF2Files == null || allExcludedRF2Files .isEmpty ()) return false ;
270+ String cleanFileName = name ;
271+ if (cleanFileName .startsWith ("x" )) {
272+ cleanFileName = name .substring (1 );
273+ }
274+ String filenameToCheck = cleanFileName .replace ("sct2" , "rel2" ).replace ("der2" , "rel2" );
275+ return allExcludedRF2Files .stream ().anyMatch (excludedRF2File -> filenameToCheck .contains (excludedRF2File .replaceAll ("_\\ d{8}.txt$" , "" )));
276+ }
277+
260278 public boolean isKnownRelease (String releaseVersion ) {
261279 if (releaseVersion == null || releaseVersion .endsWith (ZIP_FILE_EXTENSION )) {
262280 return false ;
@@ -594,12 +612,12 @@ public boolean restoreReleaseFromBinaryArchive(String schemaName) throws IOExcep
594612 }
595613 }
596614
597- public boolean uploadPublishedReleaseFromStore (String releaseFilename , String schemaName ) throws BusinessServiceException {
615+ public boolean uploadPublishedReleaseFromStore (String releaseFilename , String schemaName , List < String > excludedRF2Files ) throws BusinessServiceException {
598616 // check local disk first
599617 if (!releaseStorageConfig .isUseCloud ()) {
600618 File uploadedFile = new File (sctDataFolder , releaseFilename );
601619 if (uploadedFile .exists () && uploadedFile .canRead ()) {
602- loadSnomedData (schemaName , new ArrayList <>(), uploadedFile );
620+ loadSnomedData (schemaName , new ArrayList <>(), excludedRF2Files , uploadedFile );
603621 return true ;
604622 }
605623 }
@@ -610,7 +628,7 @@ public boolean uploadPublishedReleaseFromStore(String releaseFilename, String sc
610628 } catch (IOException e ) {
611629 throw new BusinessServiceException ("Failed to read file " + releaseFilename + " via " + releaseStorageConfig .toString (), e );
612630 }
613- uploadReleaseDataIntoDB (inputStream , releaseFilename , schemaName );
631+ uploadReleaseDataIntoDB (inputStream , releaseFilename , schemaName , excludedRF2Files );
614632 return true ;
615633 }
616634
0 commit comments