Search in sources :

Example 1 with DuplicateApiEntity

use of io.gravitee.rest.api.model.api.DuplicateApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiDuplicatorServiceImpl method duplicate.

@Override
public ApiEntity duplicate(final ApiEntity apiEntity, final DuplicateApiEntity duplicateApiEntity, String organizationId, String environmentId) {
    requireNonNull(apiEntity, "Missing ApiEntity");
    final String apiId = apiEntity.getId();
    LOGGER.debug("Duplicate API {}", apiId);
    final UpdateApiEntity newApiEntity = convert(apiEntity);
    final Proxy proxy = apiEntity.getProxy();
    proxy.setVirtualHosts(singletonList(new VirtualHost(duplicateApiEntity.getContextPath())));
    newApiEntity.setProxy(proxy);
    newApiEntity.setVersion(duplicateApiEntity.getVersion() == null ? apiEntity.getVersion() : duplicateApiEntity.getVersion());
    if (duplicateApiEntity.getFilteredFields().contains("groups")) {
        newApiEntity.setGroups(null);
    } else {
        newApiEntity.setGroups(apiEntity.getGroups());
    }
    final ApiEntity duplicatedApi = apiService.createWithApiDefinition(newApiEntity, getAuthenticatedUsername(), null);
    if (!duplicateApiEntity.getFilteredFields().contains("members")) {
        final Set<MembershipEntity> membershipsToDuplicate = membershipService.getMembershipsByReference(io.gravitee.rest.api.model.MembershipReferenceType.API, apiId);
        RoleEntity primaryOwnerRole = roleService.findPrimaryOwnerRoleByOrganization(organizationId, RoleScope.API);
        if (primaryOwnerRole != null) {
            String primaryOwnerRoleId = primaryOwnerRole.getId();
            membershipsToDuplicate.forEach(membership -> {
                String roleId = membership.getRoleId();
                if (!primaryOwnerRoleId.equals(roleId)) {
                    membershipService.addRoleToMemberOnReference(io.gravitee.rest.api.model.MembershipReferenceType.API, duplicatedApi.getId(), membership.getMemberType(), membership.getMemberId(), roleId);
                }
            });
        }
    }
    if (!duplicateApiEntity.getFilteredFields().contains("pages")) {
        final List<PageEntity> pages = pageService.search(new PageQuery.Builder().api(apiId).build(), true, environmentId);
        pageService.duplicatePages(pages, environmentId, duplicatedApi.getId());
    }
    if (!duplicateApiEntity.getFilteredFields().contains("plans")) {
        final Set<PlanEntity> plans = planService.findByApi(apiId);
        planService.duplicatePlans(plans, environmentId, duplicatedApi.getId());
    }
    return duplicatedApi;
}
Also used : UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) DuplicateApiEntity(io.gravitee.rest.api.model.api.DuplicateApiEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) Proxy(io.gravitee.definition.model.Proxy) VirtualHost(io.gravitee.definition.model.VirtualHost)

Aggregations

Proxy (io.gravitee.definition.model.Proxy)1 VirtualHost (io.gravitee.definition.model.VirtualHost)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 DuplicateApiEntity (io.gravitee.rest.api.model.api.DuplicateApiEntity)1 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)1 PageQuery (io.gravitee.rest.api.model.documentation.PageQuery)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1