Search in sources :

Example 1 with DefaultResourceURIPathResolver

use of com.b2international.snowowl.core.uri.DefaultResourceURIPathResolver in project snow-owl by b2ihealthcare.

the class SnowOwlPlugin method init.

@Override
public void init(SnowOwlConfiguration configuration, Environment env) {
    env.services().registerService(TerminologyRegistry.class, TerminologyRegistry.INSTANCE);
    env.services().registerService(ResourceURIPathResolver.class, new DefaultResourceURIPathResolver(true));
    env.services().registerService(PathTerminologyResourceResolver.class, new PathTerminologyResourceResolver.Default());
    env.services().registerService(TimestampProvider.class, new TimestampProvider.Default());
    env.services().registerService(ResourceTypeConverter.Registry.class, new ResourceTypeConverter.Registry(env.service(ClassPathScanner.class)));
    // configure monitoring support
    final MonitoringConfiguration monitoringConfig = configuration.getModuleConfig(MonitoringConfiguration.class);
    if (monitoringConfig.isEnabled()) {
        final PrometheusMeterRegistry registry = createRegistry(monitoringConfig);
        env.services().registerService(MeterRegistry.class, registry);
    } else {
        // XXX this works like a NOOP registry if you do NOT register any additional registries to it
        env.services().registerService(MeterRegistry.class, new CompositeMeterRegistry());
    }
}
Also used : PrometheusMeterRegistry(io.micrometer.prometheus.PrometheusMeterRegistry) CompositeMeterRegistry(io.micrometer.core.instrument.composite.CompositeMeterRegistry) DefaultResourceURIPathResolver(com.b2international.snowowl.core.uri.DefaultResourceURIPathResolver) TimestampProvider(com.b2international.index.revision.TimestampProvider) ResourceTypeConverter(com.b2international.snowowl.core.ResourceTypeConverter) MonitoringConfiguration(com.b2international.snowowl.core.monitoring.MonitoringConfiguration) PathTerminologyResourceResolver(com.b2international.snowowl.core.repository.PathTerminologyResourceResolver)

Example 2 with DefaultResourceURIPathResolver

use of com.b2international.snowowl.core.uri.DefaultResourceURIPathResolver in project snow-owl by b2ihealthcare.

the class CodeSystemUpgradeRequest method execute.

@Override
public String execute(RepositoryContext context) {
    // get available upgrades
    final CodeSystem currentCodeSystem = CodeSystemRequests.prepareGetCodeSystem(resource.getResourceId()).setExpand(CodeSystem.Expand.AVAILABLE_UPGRADES + "()").build().execute(context);
    if (currentCodeSystem.getUpgradeOf() != null) {
        throw new BadRequestException("Upgrade can not be started on an existing upgrade resource");
    }
    final List<ResourceURI> availableUpgrades = currentCodeSystem.getAvailableUpgrades();
    // report bad request if there are no upgrades available
    if (availableUpgrades.isEmpty()) {
        throw new BadRequestException("There are no upgrades available for resource '%s'.", resource.getResourceId());
    }
    // or the selected extensionOf version is not present as a valid available upgrade
    if (!availableUpgrades.contains(extensionOf)) {
        throw new BadRequestException("Upgrades can only be performed to the next available version dependency.").withDeveloperMessage("Use '%s/<VERSION_ID>', where <VERSION_ID> is one of: '%s'", extensionOf.getResourceId(), availableUpgrades);
    }
    // auto-generate the resourceId if not provided
    // auto-generated upgrade IDs consist of the original Resource's ID and the new extensionOf dependency's path, which is version at this point
    final String upgradeResourceId;
    if (resourceId == null) {
        upgradeResourceId = String.format("%s-%s-UPGRADE", resource.getResourceId(), extensionOf.getPath());
    } else if (resourceId.isBlank()) {
        throw new BadRequestException("'resourceId' property should not be empty, if provided");
    } else {
        BranchNameValidator.DEFAULT.checkName(resourceId);
        upgradeResourceId = resourceId;
    }
    String mergeContentFromBranchPath = currentCodeSystem.getBranchPath();
    // only allow HEAD or valid code system versions
    if (!resource.isHead()) {
        mergeContentFromBranchPath = new DefaultResourceURIPathResolver(false).resolve(context, List.of(resource)).stream().findFirst().get();
    }
    // create the same branch name under the new extensionOf path
    String parentBranch = context.service(ResourceURIPathResolver.class).resolve(context, List.of(extensionOf)).stream().findFirst().get();
    // merge content in the tooling repository from the current resource's to the upgrade resource's branch
    final String upgradeBranch = RepositoryRequests.branching().prepareCreate().setParent(parentBranch).setName(resource.getResourceId()).build(currentCodeSystem.getToolingId()).getRequest().execute(context);
    try {
        // merge branch content from the current code system to the new upgradeBranch
        Merge merge = RepositoryRequests.merging().prepareCreate().setSource(mergeContentFromBranchPath).setTarget(upgradeBranch).setSquash(false).build(currentCodeSystem.getToolingId()).getRequest().execute(context);
        if (merge.getStatus() != Merge.Status.COMPLETED) {
            // report conflicts
            ApiError apiError = merge.getApiError();
            Collection<MergeConflict> conflicts = merge.getConflicts();
            context.log().error("Failed to sync source CodeSystem content to upgrade CodeSystem. Error: {}. Conflicts: {}", apiError.getMessage(), conflicts);
            throw new ConflictException("Upgrade can not be performed due to content synchronization errors.").withAdditionalInfo(Map.of("conflicts", conflicts, "mergeError", apiError.getMessage()));
        }
        // and lastly create the actual CodeSystem so users will be able to browse, access and complete the upgrade
        return CodeSystemRequests.prepareNewCodeSystem().setId(upgradeResourceId).setBranchPath(upgradeBranch).setTitle(String.format("Upgrade of '%s' to '%s'", currentCodeSystem.getTitle(), extensionOf)).setUrl(currentCodeSystem.getUrl() + "?upgrade=" + upgradeResourceId).setLanguage(currentCodeSystem.getLanguage()).setDescription(currentCodeSystem.getDescription()).setStatus("draft").setCopyright(currentCodeSystem.getCopyright()).setOwner(currentCodeSystem.getOwner()).setContact(currentCodeSystem.getContact()).setUsage(currentCodeSystem.getUsage()).setPurpose(currentCodeSystem.getPurpose()).setToolingId(currentCodeSystem.getToolingId()).setExtensionOf(extensionOf).setUpgradeOf(resource).setSettings(currentCodeSystem.getSettings()).commit().setCommitComment(String.format("Start upgrade of '%s' to '%s'", resource, extensionOf)).build().execute(context.openBranch(context, Branch.MAIN_PATH)).getResultAs(String.class);
    } catch (Throwable e) {
        // delete upgrade branch if any exception have been thrown during the upgrade
        RepositoryRequests.branching().prepareDelete(upgradeBranch).build(currentCodeSystem.getToolingId()).getRequest().execute(context);
        throw e;
    }
}
Also used : MergeConflict(com.b2international.snowowl.core.merge.MergeConflict) ResourceURI(com.b2international.snowowl.core.ResourceURI) DefaultResourceURIPathResolver(com.b2international.snowowl.core.uri.DefaultResourceURIPathResolver) Merge(com.b2international.snowowl.core.merge.Merge) ConflictException(com.b2international.commons.exceptions.ConflictException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ApiError(com.b2international.commons.exceptions.ApiError)

Aggregations

DefaultResourceURIPathResolver (com.b2international.snowowl.core.uri.DefaultResourceURIPathResolver)2 ApiError (com.b2international.commons.exceptions.ApiError)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 ConflictException (com.b2international.commons.exceptions.ConflictException)1 TimestampProvider (com.b2international.index.revision.TimestampProvider)1 ResourceTypeConverter (com.b2international.snowowl.core.ResourceTypeConverter)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1 Merge (com.b2international.snowowl.core.merge.Merge)1 MergeConflict (com.b2international.snowowl.core.merge.MergeConflict)1 MonitoringConfiguration (com.b2international.snowowl.core.monitoring.MonitoringConfiguration)1 PathTerminologyResourceResolver (com.b2international.snowowl.core.repository.PathTerminologyResourceResolver)1 CompositeMeterRegistry (io.micrometer.core.instrument.composite.CompositeMeterRegistry)1 PrometheusMeterRegistry (io.micrometer.prometheus.PrometheusMeterRegistry)1