use of io.gravitee.rest.api.model.settings.ApiPrimaryOwnerMode in project gravitee-management-rest-api by gravitee-io.
the class ApiServiceImpl method findPrimaryOwner.
public PrimaryOwnerEntity findPrimaryOwner(JsonNode apiDefinition, String userId) {
ApiPrimaryOwnerMode poMode = ApiPrimaryOwnerMode.valueOf(this.parameterService.find(Key.API_PRIMARY_OWNER_MODE, ParameterReferenceType.ENVIRONMENT));
PrimaryOwnerEntity primaryOwnerFromDefinition = findPrimaryOwnerFromApiDefinition(apiDefinition);
switch(poMode) {
case USER:
if (primaryOwnerFromDefinition == null || ApiPrimaryOwnerMode.GROUP.name().equals(primaryOwnerFromDefinition.getType())) {
return new PrimaryOwnerEntity(userService.findById(userId));
}
if (ApiPrimaryOwnerMode.USER.name().equals(primaryOwnerFromDefinition.getType())) {
try {
return new PrimaryOwnerEntity(userService.findById(primaryOwnerFromDefinition.getId()));
} catch (UserNotFoundException unfe) {
return new PrimaryOwnerEntity(userService.findById(userId));
}
}
break;
case GROUP:
if (primaryOwnerFromDefinition == null) {
return getFirstPoGroupUserBelongsTo(userId);
}
if (ApiPrimaryOwnerMode.GROUP.name().equals(primaryOwnerFromDefinition.getType())) {
try {
return new PrimaryOwnerEntity(groupService.findById(primaryOwnerFromDefinition.getId()));
} catch (GroupNotFoundException unfe) {
return getFirstPoGroupUserBelongsTo(userId);
}
}
if (ApiPrimaryOwnerMode.USER.name().equals(primaryOwnerFromDefinition.getType())) {
try {
final String poUserId = primaryOwnerFromDefinition.getId();
userService.findById(poUserId);
final Set<GroupEntity> poGroupsOfPoUser = groupService.findByUser(poUserId).stream().filter(group -> group.getApiPrimaryOwner() != null && !group.getApiPrimaryOwner().isEmpty()).collect(toSet());
if (poGroupsOfPoUser.isEmpty()) {
return getFirstPoGroupUserBelongsTo(userId);
}
return new PrimaryOwnerEntity(poGroupsOfPoUser.iterator().next());
} catch (UserNotFoundException unfe) {
return getFirstPoGroupUserBelongsTo(userId);
}
}
break;
case HYBRID:
default:
if (primaryOwnerFromDefinition == null) {
return new PrimaryOwnerEntity(userService.findById(userId));
}
if (ApiPrimaryOwnerMode.GROUP.name().equals(primaryOwnerFromDefinition.getType())) {
try {
return new PrimaryOwnerEntity(groupService.findById(primaryOwnerFromDefinition.getId()));
} catch (GroupNotFoundException unfe) {
try {
return getFirstPoGroupUserBelongsTo(userId);
} catch (NoPrimaryOwnerGroupForUserException ex) {
return new PrimaryOwnerEntity(userService.findById(userId));
}
}
}
if (ApiPrimaryOwnerMode.USER.name().equals(primaryOwnerFromDefinition.getType())) {
try {
return new PrimaryOwnerEntity(userService.findById(primaryOwnerFromDefinition.getId()));
} catch (UserNotFoundException unfe) {
return new PrimaryOwnerEntity(userService.findById(userId));
}
}
break;
}
return new PrimaryOwnerEntity(userService.findById(userId));
}
Aggregations