Search in sources :

Example 1 with Builder

use of com.b2international.snowowl.core.internal.ResourceDocument.Builder in project snow-owl by b2ihealthcare.

the class BaseResourceCreateRequest method createResourceDocument.

private ResourceDocument createResourceDocument(TransactionContext context, List<String> bundleAncestorIds) {
    final Builder builder = ResourceDocument.builder().resourceType(getResourceType()).id(id).url(url).title(title).language(language).description(description).status(status == null ? "draft" : status).copyright(copyright).owner(Optional.ofNullable(owner).orElseGet(() -> context.service(User.class).getUsername())).contact(contact).usage(usage).purpose(purpose).bundleAncestorIds(bundleAncestorIds).bundleId(bundleId);
    completeResource(builder);
    return builder.build();
}
Also used : Builder(com.b2international.snowowl.core.internal.ResourceDocument.Builder)

Example 2 with Builder

use of com.b2international.snowowl.core.internal.ResourceDocument.Builder in project snow-owl by b2ihealthcare.

the class BaseResourceUpdateRequest method updateBundle.

private boolean updateBundle(TransactionContext context, String resourceId, String oldBundleId, Builder updated) {
    if (bundleId == null || bundleId.equals(oldBundleId)) {
        return false;
    }
    final List<String> bundleAncestorIds = getBundleAncestorIds(context, resourceId);
    updated.bundleId(bundleId);
    updated.bundleAncestorIds(bundleAncestorIds);
    // Update bundle ancestor IDs on all descendants of the resource (their bundle ID does not change)
    final Iterator<Resource> descendants = ResourceRequests.prepareSearch().filterByBundleAncestorId(resourceId).setLimit(5_000).stream(context).flatMap(Resources::stream).iterator();
    final Multimap<String, Resource> resourcesByParentId = Multimaps.index(descendants, Resource::getBundleId);
    // Calculate new ancestor ID list for direct children of this resource first
    final ImmutableList.Builder<String> ancestorIdsOfParent = ImmutableList.<String>builder().addAll(bundleAncestorIds);
    if (!IComponent.ROOT_ID.equals(bundleId)) {
        ancestorIdsOfParent.add(bundleId);
    }
    final Map<String, List<String>> newAncestorIdsByParentId = newHashMap(Map.of(resourceId, ancestorIdsOfParent.build()));
    // Start processing ancestor ID lists with the direct children of the resource
    final Deque<Map.Entry<String, Collection<Resource>>> toProcess = new ArrayDeque<>();
    toProcess.addLast(new AbstractMap.SimpleImmutableEntry<>(resourceId, resourcesByParentId.get(resourceId)));
    while (!toProcess.isEmpty()) {
        final Entry<String, Collection<Resource>> entry = toProcess.removeFirst();
        final String parentId = entry.getKey();
        final Collection<Resource> resources = entry.getValue();
        /*
			 * XXX: We will use the same ancestor ID list for all sibling resources. The
			 * call removes the entry from the map as it will never be read again after this
			 * iteration.
			 */
        final List<String> newAncestorIds = newAncestorIdsByParentId.remove(parentId);
        for (final Resource current : resources) {
            final String id = current.getId();
            final ResourceDocument resource = context.lookup(id, ResourceDocument.class);
            final ResourceDocument.Builder resourceBuilder = ResourceDocument.builder(resource);
            if (!Objects.equals(resource.getBundleAncestorIds(), newAncestorIds)) {
                resourceBuilder.bundleAncestorIds(newAncestorIds);
                context.update(resource, resourceBuilder.build());
            }
            final Collection<Resource> next = resourcesByParentId.get(id);
            if (!next.isEmpty()) {
                /*
					 * If the current resource has any children, make a note that we have to update
					 * bundleAncestorIds for them as well. The bundleId for these resources remains
					 * "id".
					 */
                final List<String> nextAncestorIds = ImmutableList.<String>builder().addAll(newAncestorIds).add(parentId).build();
                newAncestorIdsByParentId.put(id, nextAncestorIds);
                toProcess.add(new AbstractMap.SimpleImmutableEntry<>(id, next));
            }
        }
    }
    return true;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Resource(com.b2international.snowowl.core.Resource) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Builder(com.b2international.snowowl.core.internal.ResourceDocument.Builder) Entry(java.util.Map.Entry) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

Builder (com.b2international.snowowl.core.internal.ResourceDocument.Builder)2 Resource (com.b2international.snowowl.core.Resource)1 ResourceDocument (com.b2international.snowowl.core.internal.ResourceDocument)1 ImmutableList (com.google.common.collect.ImmutableList)1 Entry (java.util.Map.Entry)1