Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the getSysUserId Method into a Single Class #1303

Merged
merged 8 commits into from
Jan 8, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import org.openelisglobal.common.form.BaseForm;
import org.openelisglobal.common.log.LogEvent;
import org.openelisglobal.common.util.ConfigurationProperties;
import org.openelisglobal.common.util.ControllerUtills;
import org.openelisglobal.common.util.StringUtil;
import org.openelisglobal.internationalization.MessageUtil;
import org.openelisglobal.login.dao.UserModuleService;
import org.openelisglobal.login.valueholder.UserSessionData;
import org.openelisglobal.view.PageBuilderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -28,7 +28,7 @@
import org.springframework.web.servlet.support.RequestContextUtils;

@Component
public abstract class BaseController implements IActionConstants {
public abstract class BaseController extends ControllerUtills implements IActionConstants {

// Request being autowired appears to be threadsafe because of how Spring
// handles autowiring, despite all controllers being singletons
Expand Down Expand Up @@ -183,17 +183,6 @@ protected void setPageTitles(HttpServletRequest request, BaseForm form) {
}
}

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
if (usd == null) {
usd = (UserSessionData) request.getAttribute(USER_SESSION_DATA);
if (usd == null) {
return null;
}
}
return String.valueOf(usd.getSystemUserId());
}

protected void setSuccessFlag(HttpServletRequest request, boolean success) {
request.setAttribute(FWD_SUCCESS, success);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
package org.openelisglobal.common.rest;

import javax.servlet.http.HttpServletRequest;
import org.openelisglobal.common.action.IActionConstants;
import org.openelisglobal.login.valueholder.UserSessionData;
import org.openelisglobal.common.util.ControllerUtills;
import org.springframework.stereotype.Component;

@Component
public class BaseRestController implements IActionConstants {
public class BaseRestController extends ControllerUtills implements IActionConstants {

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
if (usd == null) {
usd = (UserSessionData) request.getAttribute(USER_SESSION_DATA);
if (usd == null) {
return null;
}
}
return String.valueOf(usd.getSystemUserId());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/openelisglobal/common/util/ControllerUtills.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.openelisglobal.common.util;

import javax.servlet.http.HttpServletRequest;
import org.openelisglobal.login.valueholder.UserSessionData;

public class ControllerUtills {

private static final String USER_SESSION_DATA = "userSessionData";
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved
if (usd == null) {
usd = (UserSessionData) request.getAttribute(USER_SESSION_DATA);
if (usd == null) {
return null;
}
}
return String.valueOf(usd.getSystemUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import nl.martijndwars.webpush.PushService;
import org.apache.http.HttpResponse;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.openelisglobal.login.valueholder.UserSessionData;
import org.openelisglobal.common.util.ControllerUtills;
import org.openelisglobal.notifications.dao.NotificationDAO;
import org.openelisglobal.notifications.dao.NotificationSubscriptionDAO;
import org.openelisglobal.notifications.entity.Notification;
Expand All @@ -31,12 +31,11 @@

@RequestMapping("/rest")
@RestController
public class NotificationRestController {
public class NotificationRestController extends ControllerUtills {
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved

private final NotificationDAO notificationDAO;
private final SystemUserService systemUserService;
private final NotificationSubscriptionDAO notificationSubscriptionDAO;
private static final String USER_SESSION_DATA = "userSessionData";

@Autowired
private ConfigurableEnvironment env;
Expand Down Expand Up @@ -210,15 +209,4 @@ public ResponseEntity<?> unsubscribe(HttpServletRequest request) {

return ResponseEntity.ok().body("Unsubscribed successfully");
}

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
if (usd == null) {
usd = (UserSessionData) request.getAttribute(USER_SESSION_DATA);
if (usd == null) {
return null;
}
}
return String.valueOf(usd.getSystemUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.GenericValidator;
import org.openelisglobal.common.services.DisplayListService;
import org.openelisglobal.login.valueholder.UserSessionData;
import org.openelisglobal.common.util.ControllerUtills;
import org.openelisglobal.patient.action.bean.PatientSearch;
import org.openelisglobal.qaevent.form.NonConformingEventForm;
import org.openelisglobal.qaevent.service.NCEventService;
Expand All @@ -26,12 +26,10 @@

@RestController
@RequestMapping(value = "/rest")
public class NonConformingEventsCorrectionActionRestController {
public class NonConformingEventsCorrectionActionRestController extends ControllerUtills {
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved

private NCEventService ncEventService = SpringContext.getBean(NCEventService.class);

private static final String USER_SESSION_DATA = "userSessionData";

@Autowired
private NonConformingEventWorker nonConformingEventWorker;

Expand Down Expand Up @@ -90,15 +88,4 @@ public ResponseEntity<?> updateNCECorretiveActionForm(@RequestBody NonConforming
return ResponseEntity.ok().body(Map.of("success", false));
}
}

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
if (usd == null) {
usd = (UserSessionData) request.getAttribute(USER_SESSION_DATA);
if (usd == null) {
return null;
}
}
return String.valueOf(usd.getSystemUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.openelisglobal.common.rest.bean.NceSampleItemInfo;
import org.openelisglobal.common.services.DisplayListService;
import org.openelisglobal.common.services.RequesterService;
import org.openelisglobal.common.util.ControllerUtills;
import org.openelisglobal.common.util.DateUtil;
import org.openelisglobal.login.valueholder.UserSessionData;
import org.openelisglobal.qaevent.form.NonConformingEventForm;
import org.openelisglobal.qaevent.service.NceCategoryService;
import org.openelisglobal.qaevent.valueholder.NcEvent;
Expand All @@ -33,7 +33,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReportNonConformEventsRestController {
public class ReportNonConformEventsRestController extends ControllerUtills {
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved

private final SampleService sampleService;
private final SampleItemService sampleItemService;
Expand All @@ -42,8 +42,6 @@ public class ReportNonConformEventsRestController {
private final NceCategoryService nceCategoryService;
private final RequesterService requesterService;

private static final String USER_SESSION_DATA = "userSessionData";

@Autowired
private SystemUserService systemUserService;

Expand Down Expand Up @@ -175,17 +173,6 @@ private NceSampleInfo addSample(Sample sample) {
return sampleInfo;
}

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
if (usd == null) {
usd = (UserSessionData) request.getAttribute(USER_SESSION_DATA);
if (usd == null) {
return null;
}
}
return String.valueOf(usd.getSystemUserId());
}

private Sample getSampleForLabNumber(String labNumber) throws LIMSInvalidConfigurationException {
return sampleService.getSampleByAccessionNumber(labNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.openelisglobal.common.services.DisplayListService;
import org.openelisglobal.common.util.ControllerUtills;
import org.openelisglobal.common.util.DateUtil;
import org.openelisglobal.login.valueholder.UserSessionData;
import org.openelisglobal.qaevent.form.NonConformingEventForm;
import org.openelisglobal.qaevent.service.NCEventService;
import org.openelisglobal.qaevent.service.NceCategoryService;
Expand All @@ -29,7 +29,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ViewNonConformEventsRestController {
public class ViewNonConformEventsRestController extends ControllerUtills {
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved

@Autowired
private NCEventService ncEventService;
Expand All @@ -46,8 +46,6 @@ public class ViewNonConformEventsRestController {
@Autowired
private SampleItemService sampleItemService;

private static final String USER_SESSION_DATA = "userSessionData";

private final NonConformingEventWorker nonConformingEventWorker;

public ViewNonConformEventsRestController(NonConformingEventWorker nonConformingEventWorker) {
Expand Down Expand Up @@ -120,15 +118,4 @@ public ResponseEntity<?> postReportNonConformingEvent(@RequestBody NonConforming
.body("An error occurred while processing the request." + e);
}
}

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
if (usd == null) {
usd = (UserSessionData) request.getAttribute(USER_SESSION_DATA);
if (usd == null) {
return null;
}
}
return String.valueOf(usd.getSystemUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.openelisglobal.common.action.IActionConstants;
import org.openelisglobal.common.exception.LIMSRuntimeException;
import org.openelisglobal.common.log.LogEvent;
import org.openelisglobal.common.util.ControllerUtills;
import org.openelisglobal.common.validator.BaseErrors;
import org.openelisglobal.login.valueholder.UserSessionData;
import org.openelisglobal.patient.action.IPatientUpdate;
Expand All @@ -39,7 +40,7 @@

@Service
@Scope("prototype")
public class PatientManagementUpdate implements IPatientUpdate {
public class PatientManagementUpdate extends ControllerUtills implements IPatientUpdate {

private String currentUserId;
protected Patient patient;
Expand Down Expand Up @@ -78,11 +79,6 @@ public void initializeGlobalVariables() {
}
}

protected String getSysUserId(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(IActionConstants.USER_SESSION_DATA);
return String.valueOf(usd.getSystemUserId());
}

public void setSysUserIdFromRequest(HttpServletRequest request) {
UserSessionData usd = (UserSessionData) request.getSession().getAttribute(IActionConstants.USER_SESSION_DATA);
currentUserId = String.valueOf(usd.getSystemUserId());
Expand Down