summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-be/src/main/java')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverter.java105
1 files changed, 56 insertions, 49 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverter.java
index a5982c1f01..0f9c9be293 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverter.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverter.java
@@ -20,17 +20,17 @@
package org.openecomp.sdc.be.ecomp.converters;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
+import com.google.common.base.Strings;
+import com.google.common.collect.Sets;
+import fj.data.Either;
import org.openecomp.portalsdk.core.restful.domain.EcompRole;
import org.openecomp.portalsdk.core.restful.domain.EcompUser;
import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.user.Role;
-import fj.data.Either;
+import java.util.Iterator;
+import java.util.Objects;
public final class EcompUserConverter {
@@ -38,12 +38,41 @@ public final class EcompUserConverter {
}
public static Either<EcompUser, String> convertUserToEcompUser(User asdcUser) {
- EcompUser convertedUser = new EcompUser();
+ return (Objects.nonNull(asdcUser)) ? Either.left(convertToEcompUser(asdcUser)) : Either.right("User is null");
+ }
+
+
+ public static Either<User, String> convertEcompUserToUser(EcompUser ecompUser) {
+ return (Objects.nonNull(ecompUser)) ? Either.left(convertToUser(ecompUser)) : Either.right("EcompUser is null");
+ }
+
+ private static User convertToUser(EcompUser ecompUser) {
+ User convertedUser = new User();
+
+ convertedUser.setFirstName(ecompUser.getFirstName());
+ convertedUser.setLastName(ecompUser.getLastName());
+ convertedUser.setUserId((!isLoginIdEmpty(ecompUser) ? ecompUser.getLoginId() : ecompUser.getOrgUserId()));
+
+ convertedUser.setEmail(ecompUser.getEmail());
+
+ if (Objects.nonNull(ecompUser.getRoles())) {
+ Iterator<EcompRole> iter = ecompUser.getRoles().iterator();
- if (asdcUser == null) {
- return Either.right("User is null");
+ if (iter.hasNext()) {
+ String updatedRole = EcompRoleConverter.convertEcompRoleToRole(iter.next());
+ convertedUser.setRole(updatedRole);
+ }
}
+ convertedUser.setStatus((ecompUser.isActive()) ? UserStatusEnum.ACTIVE : UserStatusEnum.INACTIVE);
+
+
+ return convertedUser;
+ }
+
+ private static EcompUser convertToEcompUser(User asdcUser) {
+ EcompUser convertedUser = new EcompUser();
+
convertedUser.setFirstName(asdcUser.getFirstName());
convertedUser.setLastName(asdcUser.getLastName());
convertedUser.setLoginId(asdcUser.getUserId());
@@ -56,55 +85,33 @@ public final class EcompUserConverter {
convertedUser.setActive(false);
}
- EcompRole convertedRole = new EcompRole();
- for (Role role : Role.values()) {
- if (role.name().equals(asdcUser.getRole()) || role.toString().equals(asdcUser.getRole())) {
- convertedRole.setName(role.name());
- convertedRole.setId(new Long(role.ordinal()));
- break;
- }
- }
+ EcompRole convertedRole = getEcompRole(asdcUser);
- Set<EcompRole> convertedRoleSet = new HashSet<>();
- convertedRoleSet.add(convertedRole);
- convertedUser.setRoles(convertedRoleSet);
+ convertedUser.setRoles(Sets.newHashSet(convertedRole));
- return Either.left(convertedUser);
+ return convertedUser;
}
- public static Either<User, String> convertEcompUserToUser(EcompUser ecompUser) {
- User convertedUser = new User();
-
- if (ecompUser == null) {
- return Either.right("EcompUser is null");
- }
-
- convertedUser.setFirstName(ecompUser.getFirstName());
- convertedUser.setLastName(ecompUser.getLastName());
-
- if (ecompUser.getLoginId() != null && !ecompUser.getLoginId().isEmpty()) {
- convertedUser.setUserId(ecompUser.getLoginId());
- } else {
- convertedUser.setUserId(ecompUser.getOrgUserId());
- }
-
- convertedUser.setEmail(ecompUser.getEmail());
- if (ecompUser.getRoles() != null) {
- Iterator<EcompRole> iter = ecompUser.getRoles().iterator();
+ private static boolean isLoginIdEmpty(EcompUser user) {
+ return Strings.isNullOrEmpty(user.getLoginId());
+ }
- if (iter.hasNext()) {
- String updatedRole = EcompRoleConverter.convertEcompRoleToRole(iter.next());
- convertedUser.setRole(updatedRole);
+ private static EcompRole getEcompRole(User asdcUser) {
+ EcompRole convertedRole = new EcompRole();
+ for (Role role : Role.values()) {
+ if (isRolesNamesEqual(asdcUser, role)) {
+ convertedRole.setName(role.name());
+ convertedRole.setId((long) role.ordinal());
+ break;
}
}
+ return convertedRole;
+ }
- if (ecompUser.isActive()) {
- convertedUser.setStatus(UserStatusEnum.ACTIVE);
- } else {
- convertedUser.setStatus(UserStatusEnum.INACTIVE);
- }
-
- return Either.left(convertedUser);
+ private static boolean isRolesNamesEqual(User asdcUser, Role role) {
+ String asdcUserRole = asdcUser.getRole();
+ return role.name().equals(asdcUserRole) || role.toString().equals(asdcUserRole);
}
+
}