|
| 1 | +package com.deftdevs.bootstrapi.crowd.service; |
| 2 | + |
| 3 | +import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService; |
| 4 | +import com.deftdevs.bootstrapi.commons.model.GroupBean; |
| 5 | +import com.deftdevs.bootstrapi.commons.model.UserBean; |
| 6 | +import com.deftdevs.bootstrapi.commons.service.api.UsersService; |
| 7 | +import com.deftdevs.bootstrapi.crowd.model.ApplicationBean; |
| 8 | +import com.deftdevs.bootstrapi.crowd.model._AllBean; |
| 9 | +import com.deftdevs.bootstrapi.crowd.model._AllBeanConfigStatus; |
| 10 | +import com.deftdevs.bootstrapi.crowd.service.api.ApplicationsService; |
| 11 | +import com.deftdevs.bootstrapi.crowd.service.api.CrowdSettingsGeneralService; |
| 12 | +import com.deftdevs.bootstrapi.crowd.service.api.GroupsService; |
| 13 | +import com.deftdevs.bootstrapi.commons.service.api._AllService; |
| 14 | +import org.springframework.stereotype.Component; |
| 15 | + |
| 16 | +import javax.inject.Inject; |
| 17 | +import javax.ws.rs.core.Response; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +@Component |
| 24 | +@ExportAsService(_AllService.class) |
| 25 | +public class _AllServiceImpl implements _AllService<_AllBean> { |
| 26 | + |
| 27 | + private final CrowdSettingsGeneralService settingsService; |
| 28 | + private final UsersService usersService; |
| 29 | + private final GroupsService groupsService; |
| 30 | + private final ApplicationsService applicationsService; |
| 31 | + |
| 32 | + @Inject |
| 33 | + public _AllServiceImpl( |
| 34 | + final CrowdSettingsGeneralService settingsService, |
| 35 | + final UsersService usersService, |
| 36 | + final GroupsService groupsService, |
| 37 | + final ApplicationsService applicationsService) { |
| 38 | + |
| 39 | + this.settingsService = settingsService; |
| 40 | + this.usersService = usersService; |
| 41 | + this.groupsService = groupsService; |
| 42 | + this.applicationsService = applicationsService; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public _AllBean setAll( |
| 47 | + final _AllBean allBean) { |
| 48 | + |
| 49 | + final _AllBean result = new _AllBean(); |
| 50 | + final Map<String, _AllBeanConfigStatus> status = new HashMap<>(); |
| 51 | + |
| 52 | + // Apply settings |
| 53 | + if (allBean.getSettings() != null) { |
| 54 | + try { |
| 55 | + result.setSettings(settingsService.setSettingsGeneral(allBean.getSettings())); |
| 56 | + status.put("settings", _AllBeanConfigStatus.success()); |
| 57 | + } catch (Exception e) { |
| 58 | + status.put("settings", _AllBeanConfigStatus.error( |
| 59 | + Response.Status.INTERNAL_SERVER_ERROR, |
| 60 | + "Failed to apply settings", |
| 61 | + e.getMessage() |
| 62 | + )); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Apply groups (before users, since users may reference groups) |
| 67 | + Map<String, GroupBean> groupsMap = allBean.getGroups(); |
| 68 | + if (groupsMap != null && !groupsMap.isEmpty()) { |
| 69 | + try { |
| 70 | + // Validate group identifiers |
| 71 | + for (Map.Entry<String, GroupBean> entry : groupsMap.entrySet()) { |
| 72 | + String key = entry.getKey(); |
| 73 | + GroupBean group = entry.getValue(); |
| 74 | + if (group.getName() == null) { |
| 75 | + group.setName(key); |
| 76 | + } else if (!key.equals(group.getName())) { |
| 77 | + status.put("groups", _AllBeanConfigStatus.error( |
| 78 | + Response.Status.BAD_REQUEST, |
| 79 | + "Group identifier mismatch", |
| 80 | + String.format("Map key '%s' does not match group name '%s'", key, group.getName()) |
| 81 | + )); |
| 82 | + continue; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!status.containsKey("groups")) { |
| 87 | + List<GroupBean> groupsList = new ArrayList<>(groupsMap.values()); |
| 88 | + List<GroupBean> updatedGroups = groupsService.setGroups(1L, groupsList); |
| 89 | + |
| 90 | + Map<String, GroupBean> resultGroups = new HashMap<>(); |
| 91 | + for (GroupBean group : updatedGroups) { |
| 92 | + resultGroups.put(group.getName(), group); |
| 93 | + } |
| 94 | + result.setGroups(resultGroups); |
| 95 | + status.put("groups", _AllBeanConfigStatus.success()); |
| 96 | + } |
| 97 | + } catch (Exception e) { |
| 98 | + status.put("groups", _AllBeanConfigStatus.error( |
| 99 | + Response.Status.BAD_REQUEST, |
| 100 | + "Failed to apply groups configuration", |
| 101 | + e.getMessage() |
| 102 | + )); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Apply users |
| 107 | + Map<String, UserBean> usersMap = allBean.getUsers(); |
| 108 | + if (usersMap != null && !usersMap.isEmpty()) { |
| 109 | + try { |
| 110 | + // Validate user identifiers |
| 111 | + for (Map.Entry<String, UserBean> entry : usersMap.entrySet()) { |
| 112 | + String key = entry.getKey(); |
| 113 | + UserBean user = entry.getValue(); |
| 114 | + if (user.getUsername() == null) { |
| 115 | + user.setUsername(key); |
| 116 | + } else if (!key.equals(user.getUsername())) { |
| 117 | + status.put("users", _AllBeanConfigStatus.error( |
| 118 | + Response.Status.BAD_REQUEST, |
| 119 | + "User identifier mismatch", |
| 120 | + String.format("Map key '%s' does not match username '%s'", key, user.getUsername()) |
| 121 | + )); |
| 122 | + continue; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (!status.containsKey("users")) { |
| 127 | + List<UserBean> usersList = new ArrayList<>(usersMap.values()); |
| 128 | + List<UserBean> updatedUsers = usersService.setUsers(1L, usersList); |
| 129 | + |
| 130 | + Map<String, UserBean> resultUsers = new HashMap<>(); |
| 131 | + for (UserBean user : updatedUsers) { |
| 132 | + resultUsers.put(user.getUsername(), user); |
| 133 | + } |
| 134 | + result.setUsers(resultUsers); |
| 135 | + status.put("users", _AllBeanConfigStatus.success()); |
| 136 | + } |
| 137 | + } catch (Exception e) { |
| 138 | + status.put("users", _AllBeanConfigStatus.error( |
| 139 | + Response.Status.BAD_REQUEST, |
| 140 | + "Failed to apply users configuration", |
| 141 | + e.getMessage() |
| 142 | + )); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Apply applications |
| 147 | + Map<String, ApplicationBean> appsMap = allBean.getApplications(); |
| 148 | + if (appsMap != null && !appsMap.isEmpty()) { |
| 149 | + try { |
| 150 | + // Validate application identifiers |
| 151 | + for (Map.Entry<String, ApplicationBean> entry : appsMap.entrySet()) { |
| 152 | + String key = entry.getKey(); |
| 153 | + ApplicationBean app = entry.getValue(); |
| 154 | + if (app.getName() == null) { |
| 155 | + app.setName(key); |
| 156 | + } else if (!key.equals(app.getName())) { |
| 157 | + status.put("applications", _AllBeanConfigStatus.error( |
| 158 | + Response.Status.BAD_REQUEST, |
| 159 | + "Application identifier mismatch", |
| 160 | + String.format("Map key '%s' does not match application name '%s'", key, app.getName()) |
| 161 | + )); |
| 162 | + continue; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (!status.containsKey("applications")) { |
| 167 | + List<ApplicationBean> appsList = new ArrayList<>(appsMap.values()); |
| 168 | + List<ApplicationBean> updatedApps = applicationsService.setApplications(appsList); |
| 169 | + |
| 170 | + Map<String, ApplicationBean> resultApps = new HashMap<>(); |
| 171 | + for (ApplicationBean app : updatedApps) { |
| 172 | + resultApps.put(app.getName(), app); |
| 173 | + } |
| 174 | + result.setApplications(resultApps); |
| 175 | + status.put("applications", _AllBeanConfigStatus.success()); |
| 176 | + } |
| 177 | + } catch (Exception e) { |
| 178 | + status.put("applications", _AllBeanConfigStatus.error( |
| 179 | + Response.Status.BAD_REQUEST, |
| 180 | + "Failed to apply applications configuration", |
| 181 | + e.getMessage() |
| 182 | + )); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + result.setStatus(status); |
| 187 | + return result; |
| 188 | + } |
| 189 | +} |
0 commit comments