use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.
the class SnomedExportApiTest method executeMultipleExportsAtTheSameTime.
@Test
public void executeMultipleExportsAtTheSameTime() throws Exception {
Promise<Attachment> first = SnomedRequests.rf2().prepareExport().setReleaseType(Rf2ReleaseType.FULL).setCountryNamespaceElement("INT").setRefSetExportLayout(Rf2RefSetExportLayout.COMBINED).setLocales(LOCALES).build(branchPath.getPath()).execute(getBus());
Promise<Attachment> second = SnomedRequests.rf2().prepareExport().setCountryNamespaceElement("INT").setRefSetExportLayout(Rf2RefSetExportLayout.COMBINED).setReleaseType(Rf2ReleaseType.SNAPSHOT).setLocales(LOCALES).build(branchPath.getPath()).execute(getBus());
String message = Promise.all(first, second).then(input -> {
Attachment firstResult = (Attachment) input.get(0);
Attachment secondResult = (Attachment) input.get(1);
InternalAttachmentRegistry fileRegistry = (InternalAttachmentRegistry) ApplicationContext.getServiceForClass(AttachmentRegistry.class);
File firstArchive = fileRegistry.getAttachment(firstResult.getAttachmentId());
File secondArchive = fileRegistry.getAttachment(secondResult.getAttachmentId());
final Map<String, Boolean> firstArchiveMap = ImmutableMap.<String, Boolean>builder().put("sct2_Concept_Full", true).build();
final Map<String, Boolean> secondArchiveMap = ImmutableMap.<String, Boolean>builder().put("sct2_Concept_Snapshot", true).build();
try {
assertArchiveContainsFiles(firstArchive, firstArchiveMap);
assertArchiveContainsFiles(secondArchive, secondArchiveMap);
} catch (Exception e) {
return e.getMessage();
}
fileRegistry.delete(firstResult.getAttachmentId());
fileRegistry.delete(secondResult.getAttachmentId());
return null;
}).fail(input -> input.getMessage()).getSync(2, TimeUnit.MINUTES);
assertNull(message, message);
}
use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.
the class SnomedRf2ExportRestService method export.
@Operation(summary = "Export SNOMED CT content to RF2", description = "Exports SNOMED CT content from the given branch to RF2.")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK") })
@GetMapping
@ResponseBody
public ResponseEntity<?> export(@Parameter(description = "The branch path", required = true) @PathVariable(value = "path") final String branch, @ParameterObject final SnomedRf2ExportConfiguration params, @Parameter(description = "Accepted language tags, in order of preference", example = "en-US;q=0.8,en-GB;q=0.6") @RequestHeader(value = HttpHeaders.ACCEPT_LANGUAGE, defaultValue = "en-US;q=0.8,en-GB;q=0.6", required = false) final String acceptLanguage) {
final Attachment exportedFile = SnomedRequests.rf2().prepareExport().setReleaseType(params.getType() == null ? null : Rf2ReleaseType.getByNameIgnoreCase(params.getType())).setExtensionOnly(params.isExtensionOnly()).setLocales(acceptLanguage).setIncludePreReleaseContent(params.isIncludeUnpublished()).setModules(params.getModuleIds()).setRefSets(params.getRefSetIds()).setCountryNamespaceElement(params.getNamespaceId()).setMaintainerType(Strings.isNullOrEmpty(params.getMaintainerType()) ? null : Rf2MaintainerType.getByNameIgnoreCase(params.getMaintainerType())).setNrcCountryCode(params.getNrcCountryCode()).setTransientEffectiveTime(params.getTransientEffectiveTime()).setStartEffectiveTime(params.getStartEffectiveTime()).setEndEffectiveTime(params.getEndEffectiveTime()).setRefSetExportLayout(params.getRefSetLayout() == null ? null : Rf2RefSetExportLayout.getByNameIgnoreCase(params.getRefSetLayout())).setComponentTypes(params.getComponentTypes()).build(branch).execute(getBus()).getSync();
final File file = ((InternalAttachmentRegistry) attachments).getAttachment(exportedFile.getAttachmentId());
final Resource exportZipResource = new FileSystemResource(file);
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
httpHeaders.setContentDispositionFormData("attachment", exportedFile.getFileName());
// TODO figure out a smart way to cache export results, probably it could be tied to commitTimestamps/versions/etc.
file.deleteOnExit();
return new ResponseEntity<>(exportZipResource, httpHeaders, HttpStatus.OK);
}
use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.
the class ResourcesImportRequest method execute.
@Override
public ImportResponse execute(ServiceProvider context) {
final InternalAttachmentRegistry attachmentRegistry = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
final File file = attachmentRegistry.getAttachment(sourceFile.getAttachmentId());
try {
final List<R> sourceFileContent = loadSourceFileContent(context, file);
final Map<String, R> existingResources = newHashMap(loadExistingResources(context, sourceFileContent));
final ImportDefectAcceptor defectAcceptor = new ImportDefectAcceptor(sourceFile.getFileName());
validateSourceFileContent(context, sourceFileContent, existingResources, defectAcceptor);
List<ImportDefect> defects = defectAcceptor.getDefects();
// Content with validation errors can not be imported
ImportResponse validationResponse = ImportResponse.defects(defects);
if (!validationResponse.getErrors().isEmpty()) {
return validationResponse;
}
final Set<ComponentURI> visitedComponents = Sets.newHashSet();
// Import each resource present in the source file, along with its content
for (final R resource : sourceFileContent) {
final String id = resource.getId();
ComponentURI importedResource = importResource(context, resource, existingResources.get(id));
if (importedResource != null) {
visitedComponents.add(importedResource);
}
visitedComponents.addAll(importContent(context, resource, existingResources.get(id)));
}
return ImportResponse.success(visitedComponents, defects);
} catch (final ApiException e) {
throw e;
} catch (final Exception e) {
String error = "Unexpected error happened during the import of the source file: " + sourceFile.getFileName();
context.log().error(error, e);
return ImportResponse.error(error);
} finally {
if (sourceFile != null && attachmentRegistry != null) {
attachmentRegistry.delete(sourceFile.getAttachmentId());
}
}
}
use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.
the class SnomedRf2ImportRequest method execute.
@Override
public ImportResponse execute(BranchContext context) {
log = LoggerFactory.getLogger("import");
context = context.inject().bind(Logger.class, log).build();
Rf2ImportConfiguration importConfig = new Rf2ImportConfiguration(releaseType, createVersions);
validate(context, importConfig);
final InternalAttachmentRegistry fileReg = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
final File rf2Archive = fileReg.getAttachment(this.rf2Archive.getAttachmentId());
try (Locks locks = Locks.on(context).lock(DatastoreLockContextDescriptions.IMPORT)) {
return doImport(context, rf2Archive, importConfig);
} catch (Exception e) {
if (e instanceof ApiException) {
throw (ApiException) e;
}
throw SnowowlRuntimeException.wrap(e);
}
}
use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.
the class ImportRequest method doExecute.
@Override
public final ImportResponse doExecute(TransactionContext context) {
context.log().info("Importing components from source file '{}'.", this.attachment.getFileName());
InternalAttachmentRegistry iar = null;
try {
iar = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
File attachment = iar.getAttachment(this.attachment.getAttachmentId());
ImportDefectAcceptor defectsAcceptor = new ImportDefectAcceptor(this.attachment.getFileName());
doValidate(context, attachment, defectsAcceptor, context.service(IProgressMonitor.class));
final ImportResponse validationResponse = ImportResponse.defects(defectsAcceptor.getDefects());
if (!validationResponse.getErrors().isEmpty()) {
return validationResponse;
} else {
final Set<ComponentURI> visitedComponents = Sets.newHashSet();
doImport(context, attachment, visitedComponents::add, context.service(IProgressMonitor.class));
context.log().info("Finished importing components from source file '{}'.", this.attachment.getFileName());
return ImportResponse.success(visitedComponents, validationResponse.getDefects());
}
} catch (ApiException e) {
throw e;
} catch (Exception e) {
String error = "Unexpected error happened during the import of the source file: " + attachment.getFileName();
context.log().error(error, e);
return ImportResponse.error(error);
} finally {
if (attachment != null && iar != null) {
iar.delete(attachment.getAttachmentId());
}
}
}
Aggregations