ApiUserProfile.java
2.09 KB
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
package com.srh.api.model;
import com.srh.api.error.exception.DuplicateValueException;
import com.srh.api.error.exception.RelationshipNotFoundException;
import lombok.Data;
import lombok.SneakyThrows;
import java.util.List;
@Data
public class ApiUserProfile {
private ApiUser apiUser;
private Profile profile;
public ApiUserProfile(ApiUser apiUser, Profile profile) {
this.apiUser = apiUser;
this.profile = profile;
}
@SneakyThrows
public void addEntities() {
addApiUserInProfile();
addProfileInApiUser();
}
@SneakyThrows
public void removeEntities() {
removeApiUserInProfile();
removeProfileInApiUser();
}
@SneakyThrows
private void addApiUserInProfile() {
List<ApiUser> apiUsersInProfile = getApiUserListInProfile();
if (apiUsersInProfile.contains(apiUser))
throw new DuplicateValueException("ApiUser link already exists");
apiUsersInProfile.add(apiUser);
}
@SneakyThrows
private void addProfileInApiUser() {
List<Profile> profilesInApiUser = getProfileListInApiUser();
if (profilesInApiUser.contains(profile))
throw new DuplicateValueException("Profile link already exists");
profilesInApiUser.add(profile);
}
@SneakyThrows
private void removeApiUserInProfile() {
List<ApiUser> apiUsersInProfile = profile.getApiUsers();
if (!apiUsersInProfile.contains(apiUser))
throw new RelationshipNotFoundException("ApiUser not exist in Profile");
apiUsersInProfile.remove(apiUser);
}
@SneakyThrows
private void removeProfileInApiUser() {
List<Profile> profilesInApiUser = apiUser.getProfiles();
if (!profilesInApiUser.contains(profile))
throw new RelationshipNotFoundException("Profile not exist in ApiUser");
profilesInApiUser.remove(profile);
}
private List<ApiUser> getApiUserListInProfile() {
return profile.getApiUsers();
}
private List<Profile> getProfileListInApiUser() {
return apiUser.getProfiles();
}
}