summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverter.java
blob: a5982c1f01eb64ad2ebf7521e16eef37b233830c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.ecomp.converters;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

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;

public final class EcompUserConverter {

    private EcompUserConverter() {
    }

    public static Either<EcompUser, String> convertUserToEcompUser(User asdcUser) {
        EcompUser convertedUser = new EcompUser();

        if (asdcUser == null) {
            return Either.right("User is null");
        }

        convertedUser.setFirstName(asdcUser.getFirstName());
        convertedUser.setLastName(asdcUser.getLastName());
        convertedUser.setLoginId(asdcUser.getUserId());
        convertedUser.setOrgUserId(asdcUser.getUserId());
        convertedUser.setEmail(asdcUser.getEmail());

        if (asdcUser.getStatus().equals(UserStatusEnum.ACTIVE)) {
            convertedUser.setActive(true);
        } else if (asdcUser.getStatus().equals(UserStatusEnum.INACTIVE)) {
            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;
            }
        }

        Set<EcompRole> convertedRoleSet = new HashSet<>();
        convertedRoleSet.add(convertedRole);
        convertedUser.setRoles(convertedRoleSet);

        return Either.left(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();

            if (iter.hasNext()) {
                String updatedRole = EcompRoleConverter.convertEcompRoleToRole(iter.next());
                convertedUser.setRole(updatedRole);
            }
        }

        if (ecompUser.isActive()) {
            convertedUser.setStatus(UserStatusEnum.ACTIVE);
        } else {
            convertedUser.setStatus(UserStatusEnum.INACTIVE);
        }

        return Either.left(convertedUser);
    }
}