Skip to content

Commit 24f7726

Browse files
committed
WIP
1 parent 6d88911 commit 24f7726

File tree

10 files changed

+419
-14
lines changed

10 files changed

+419
-14
lines changed

commons/src/main/java/com/deftdevs/bootstrapi/commons/constants/BootstrAPI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
public class BootstrAPI {
44

5-
public static final String ALL = "all";
5+
public static final String _ALL = "_all";
6+
67
public static final String APPLICATION = "application";
78
public static final String APPLICATIONS = "applications";
89
public static final String APPLICATION_LINK = "application-link";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.deftdevs.bootstrapi.commons.rest;
2+
3+
import com.deftdevs.bootstrapi.commons.rest.api._AllResource;
4+
import com.deftdevs.bootstrapi.commons.service.api._AllService;
5+
6+
import javax.inject.Inject;
7+
import javax.ws.rs.core.Response;
8+
9+
public abstract class _AbstractAllResourceImpl<_AllBean>
10+
implements _AllResource<_AllBean> {
11+
12+
private final _AllService<_AllBean> allService;
13+
14+
@Inject
15+
public _AbstractAllResourceImpl(
16+
final _AllService<_AllBean> allService) {
17+
18+
this.allService = allService;
19+
}
20+
21+
public Response setAll(
22+
final _AllBean allBean) {
23+
24+
return Response.ok(allService.setAll(allBean)).build();
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.deftdevs.bootstrapi.commons.rest.api;
2+
3+
import com.deftdevs.bootstrapi.commons.constants.BootstrAPI;
4+
import com.deftdevs.bootstrapi.commons.model.ErrorCollection;
5+
import com.deftdevs.bootstrapi.commons.model.SettingsBean;
6+
import io.swagger.v3.oas.annotations.Operation;
7+
import io.swagger.v3.oas.annotations.media.Content;
8+
import io.swagger.v3.oas.annotations.media.Schema;
9+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
10+
11+
import javax.validation.constraints.NotNull;
12+
import javax.ws.rs.PUT;
13+
import javax.ws.rs.core.Response;
14+
15+
public interface _AllResource<_AllBean> {
16+
17+
@PUT
18+
@Operation(
19+
summary = BootstrAPI._ALL,
20+
responses = {
21+
@ApiResponse(
22+
responseCode = "200", content = @Content(schema = @Schema(implementation = SettingsBean.class)),
23+
description = BootstrAPI._ALL
24+
),
25+
@ApiResponse(
26+
responseCode = "default", content = @Content(schema = @Schema(implementation = ErrorCollection.class)),
27+
description = BootstrAPI._ALL
28+
),
29+
}
30+
)
31+
Response setAll(
32+
@NotNull final _AllBean bean);
33+
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.deftdevs.bootstrapi.commons.service.api;
2+
3+
public interface _AllService<_AllBean> {
4+
5+
/**
6+
* Apply a complete configuration.
7+
*
8+
* @param allBean the configuration to apply
9+
* @return the updated configuration with status
10+
*/
11+
_AllBean setAll(
12+
_AllBean allBean);
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package com.deftdevs.bootstrapi.crowd.model;
22

3+
import com.deftdevs.bootstrapi.commons.model.GroupBean;
34
import com.deftdevs.bootstrapi.commons.model.SettingsBean;
5+
import com.deftdevs.bootstrapi.commons.model.UserBean;
46
import lombok.AllArgsConstructor;
57
import lombok.Data;
68
import lombok.NoArgsConstructor;
79

810
import javax.xml.bind.annotation.XmlElement;
911
import javax.xml.bind.annotation.XmlRootElement;
10-
import java.util.List;
12+
import java.util.Map;
1113

1214
@Data
1315
@NoArgsConstructor
1416
@AllArgsConstructor
1517
@XmlRootElement(name = "all")
16-
public class AllBean {
18+
public class _AllBean {
1719

1820
@XmlElement
1921
private SettingsBean settings;
2022

2123
@XmlElement
22-
private List<ApplicationBean> applications;
24+
private Map<String, UserBean> users;
2325

26+
@XmlElement
27+
private Map<String, GroupBean> groups;
28+
29+
@XmlElement
30+
private Map<String, ApplicationBean> applications;
31+
32+
@XmlElement
33+
private Map<String, _AllBeanConfigStatus> status;
2434
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.deftdevs.bootstrapi.crowd.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import javax.ws.rs.core.Response;
8+
import javax.xml.bind.annotation.XmlElement;
9+
import javax.xml.bind.annotation.XmlRootElement;
10+
11+
@Data
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
@XmlRootElement(name = "configurationStatus")
15+
public class _AllBeanConfigStatus {
16+
17+
@XmlElement
18+
private int status;
19+
20+
@XmlElement
21+
private String message;
22+
23+
@XmlElement
24+
private String details;
25+
26+
public static _AllBeanConfigStatus success() {
27+
return new _AllBeanConfigStatus(Response.Status.OK.getStatusCode(), "Success", null);
28+
}
29+
30+
public static _AllBeanConfigStatus error(Response.Status status, String message, String details) {
31+
return new _AllBeanConfigStatus(status.getStatusCode(), message, details);
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.deftdevs.bootstrapi.crowd.rest;
2+
3+
import com.atlassian.plugins.rest.common.security.SystemAdminOnly;
4+
import com.deftdevs.bootstrapi.commons.constants.BootstrAPI;
5+
import com.deftdevs.bootstrapi.commons.rest._AbstractAllResourceImpl;
6+
import com.deftdevs.bootstrapi.commons.service.api._AllService;
7+
import com.deftdevs.bootstrapi.crowd.model._AllBean;
8+
import io.swagger.v3.oas.annotations.tags.Tag;
9+
import org.springframework.stereotype.Component;
10+
11+
import javax.inject.Inject;
12+
import javax.ws.rs.Consumes;
13+
import javax.ws.rs.Path;
14+
import javax.ws.rs.Produces;
15+
import javax.ws.rs.core.MediaType;
16+
17+
@Path("/")
18+
@Tag(name = BootstrAPI._ALL)
19+
@Consumes(MediaType.APPLICATION_JSON)
20+
@Produces(MediaType.APPLICATION_JSON)
21+
@SystemAdminOnly
22+
@Component
23+
public class _AllResourceImpl extends _AbstractAllResourceImpl<_AllBean> {
24+
25+
@Inject
26+
public _AllResourceImpl(
27+
final _AllService<_AllBean> allService) {
28+
29+
super(allService);
30+
}
31+
32+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
}

crowd/src/main/java/com/deftdevs/bootstrapi/crowd/service/api/AllService.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)