prepareSandBox(request, brc);
}
protected void prepareSandBox(WebRequest request, BroadleafRequestContext brc) {
AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
if (adminUser == null) {
//clear any sandbox
if (BLCRequestUtils.isOKtoUseSession(request)) {
request.removeAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
}
} else {
SandBox sandBox = null;
if (StringUtils.isNotBlank(request.getParameter(SANDBOX_REQ_PARAM))) {
Long sandBoxId = Long.parseLong(request.getParameter(SANDBOX_REQ_PARAM));
sandBox = sandBoxService.retrieveUserSandBoxForParent(adminUser.getId(), sandBoxId);
if (sandBox == null) {
SandBox approvalOrUserSandBox = sandBoxService.retrieveSandBoxById(sandBoxId);
if (approvalOrUserSandBox.getSandBoxType().equals(SandBoxType.USER)) {
sandBox = approvalOrUserSandBox;
} else {
sandBox = sandBoxService.createUserSandBox(adminUser.getId(), approvalOrUserSandBox);
}
}
}
if (sandBox == null) {
Long previouslySetSandBoxId = null;
if (BLCRequestUtils.isOKtoUseSession(request)) {
previouslySetSandBoxId = (Long) request.getAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR,
WebRequest.SCOPE_GLOBAL_SESSION);
}
if (previouslySetSandBoxId != null) {
sandBox = sandBoxService.retrieveSandBoxById(previouslySetSandBoxId);
}
}
if (sandBox == null) {
List<SandBox> defaultSandBoxes = sandBoxService.retrieveSandBoxesByType(SandBoxType.DEFAULT);
if (defaultSandBoxes.size() > 1) {
throw new IllegalStateException("Only one sandbox should be configured as default");
}
SandBox defaultSandBox;
if (defaultSandBoxes.size() == 1) {
defaultSandBox = defaultSandBoxes.get(0);
} else {
defaultSandBox = sandBoxService.createDefaultSandBox();
}
sandBox = sandBoxService.retrieveUserSandBoxForParent(adminUser.getId(), defaultSandBox.getId());
if (sandBox == null) {
sandBox = sandBoxService.createUserSandBox(adminUser.getId(), defaultSandBox);
}
}
// If the user just changed sandboxes, we want to update the database record.
Long previouslySetSandBoxId = null;
if (BLCRequestUtils.isOKtoUseSession(request)) {
previouslySetSandBoxId = (Long) request.getAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
}
if (previouslySetSandBoxId != null && !sandBox.getId().equals(previouslySetSandBoxId)) {
adminUser.setLastUsedSandBoxId(sandBox.getId());
adminUser = adminSecurityService.saveAdminUser(adminUser);
}
if (BLCRequestUtils.isOKtoUseSession(request)) {
request.setAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR, sandBox.getId(), WebRequest.SCOPE_GLOBAL_SESSION);