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;
}
Aggregations