Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / ecomp / converters / EcompUserConverter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.ecomp.converters;
22
23 import fj.data.Either;
24 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
25 import org.onap.portalsdk.core.restful.domain.EcompRole;
26 import org.onap.portalsdk.core.restful.domain.EcompUser;
27 import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.user.Role;
30
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.Set;
34
35 public final class EcompUserConverter {
36
37     private EcompUserConverter() {
38     }
39
40     public static Either<EcompUser, String> convertUserToEcompUser(User asdcUser) {
41         EcompUser convertedUser = new EcompUser();
42
43         if (asdcUser == null) {
44             return Either.right("User is null");
45         }
46
47         convertedUser.setFirstName(asdcUser.getFirstName());
48         convertedUser.setLastName(asdcUser.getLastName());
49         convertedUser.setLoginId(asdcUser.getUserId());
50         convertedUser.setOrgUserId(asdcUser.getUserId());
51         convertedUser.setEmail(asdcUser.getEmail());
52
53         if (asdcUser.getStatus().equals(UserStatusEnum.ACTIVE)) {
54             convertedUser.setActive(true);
55         } else if (asdcUser.getStatus().equals(UserStatusEnum.INACTIVE)) {
56             convertedUser.setActive(false);
57         }
58
59         EcompRole convertedRole = new EcompRole();
60         for (Role role : Role.values()) {
61             if (role.name().equals(asdcUser.getRole()) || role.toString().equals(asdcUser.getRole())) {
62                 convertedRole.setName(role.name());
63                 convertedRole.setId(new Long(role.ordinal()));
64                 break;
65             }
66         }
67
68         Set<EcompRole> convertedRoleSet = new HashSet<>();
69         convertedRoleSet.add(convertedRole);
70         convertedUser.setRoles(convertedRoleSet);
71
72         return Either.left(convertedUser);
73     }
74
75     public static User convertEcompUserToUser(EcompUser ecompUser) throws PortalAPIException {
76         User convertedUser = new User();
77
78         if (ecompUser == null) {
79             throw new PortalAPIException("ecomp user is null");
80         }
81
82         convertedUser.setFirstName(ecompUser.getFirstName());
83         convertedUser.setLastName(ecompUser.getLastName());
84
85         if (ecompUser.getLoginId() != null && !ecompUser.getLoginId().isEmpty()) {
86             convertedUser.setUserId(ecompUser.getLoginId());
87         } else {
88             convertedUser.setUserId(ecompUser.getOrgUserId());
89         }
90
91         convertedUser.setEmail(ecompUser.getEmail());
92
93         if (ecompUser.getRoles() != null) {
94             Iterator<EcompRole> iter = ecompUser.getRoles().iterator();
95
96             if (iter.hasNext()) {
97                 String updatedRole = EcompRoleConverter.convertEcompRoleToRole(iter.next());
98                 convertedUser.setRole(updatedRole);
99             }
100         }
101
102         if (ecompUser.isActive()) {
103             convertedUser.setStatus(UserStatusEnum.ACTIVE);
104         } else {
105             convertedUser.setStatus(UserStatusEnum.INACTIVE);
106         }
107
108         return convertedUser;
109     }
110 }