From bbe9ece3ca26b22ba5aee52bab34a3d78fc02d50 Mon Sep 17 00:00:00 2001 From: burdziak Date: Mon, 18 Mar 2019 14:34:29 +0100 Subject: Fix sonar issues in ProfileController Change-Id: I454d8a942cac079de5b899bcb00139de84ac115d Issue-ID: PORTAL-523 Signed-off-by: burdziak --- .../controller/core/ProfileController.java | 140 +++++++++++---------- 1 file changed, 74 insertions(+), 66 deletions(-) diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java index c7edfb81..24ea203f 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java @@ -75,6 +75,17 @@ public class ProfileController extends RestrictedBaseController { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileController.class); + private static final String PROFILE_ID = "profile_id"; + private static final String PROFILEID = "profileId"; + private static final String STATE_LIST = "stateList"; + private static final String COUNTRIES = "countries"; + private static final String TIME_ZONES = "timeZones"; + private static final String AVAILABLE_ROLES= "availableRoles"; + private static final String PROFILE = "profile"; + private static final String PROFILE_CONTAINER = "ProfileController.save"; + private static final String UTF8 = "UTF-8"; + private static final String APPLICATION_JSON = "application / json"; + @Autowired private UserProfileService service; @@ -83,8 +94,6 @@ public class ProfileController extends RestrictedBaseController { @Autowired private RoleService roleService; - - private String viewName; @RequestMapping(value = { "/profile" }, method = RequestMethod.GET) public ModelAndView profile(HttpServletRequest request) throws IOException { @@ -98,25 +107,25 @@ public class ProfileController extends RestrictedBaseController { profile = UserUtils.getUserSession(request); profileId = profile.getId(); } else { - profileId = Long.parseLong(request.getParameter("profile_id")); + profileId = Long.parseLong(request.getParameter(PROFILE_ID)); profile = userService.getUser(String.valueOf(profileId)); } try { - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "profile: failed to write JSON", e); } - return new ModelAndView("profile", "model", model); + return new ModelAndView(PROFILE, "model", model); } @RequestMapping(value = { "/self_profile" }, method = RequestMethod.GET) - public ModelAndView selfProfile(HttpServletRequest request) throws Exception{ + public ModelAndView selfProfile(HttpServletRequest request) { Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); @@ -124,16 +133,16 @@ public class ProfileController extends RestrictedBaseController { User user = UserUtils.getUserSession(request); User profile = UserUtils.getUserSession(request); try { - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "self_profile: failed to write JSON", e); } - return new ModelAndView("profile", "model", model); + return new ModelAndView(PROFILE, "model", model); } @RequestMapping(value = { "/get_self_profile" }, method = RequestMethod.GET) @@ -145,12 +154,12 @@ public class ProfileController extends RestrictedBaseController { Long profileId = null; User profile = UserUtils.getUserSession(request); try { - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); @@ -172,15 +181,15 @@ public class ProfileController extends RestrictedBaseController { profile = UserUtils.getUserSession(request); profileId = profile.getId(); } else { - profileId = Long.parseLong(request.getParameter("profile_id")); + profileId = Long.parseLong(request.getParameter(PROFILE_ID)); profile = userService.getUser(String.valueOf(profileId)); } - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); @@ -192,41 +201,41 @@ public class ProfileController extends RestrictedBaseController { @RequestMapping(value = { "/profile/saveProfile" }, method = RequestMethod.POST) public ModelAndView saveProfile(HttpServletRequest request, HttpServletResponse response) { - logger.info(EELFLoggerDelegate.debugLogger, "ProfileController.save"); + logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); - User user = mapper.readValue(root.get("profile").toString(), User.class); + User user = mapper.readValue(root.get(PROFILE).toString(), User.class); String selectedCountry = mapper.readValue(root.get("selectedCountry").toString(), String.class); String selectedState = mapper.readValue(root.get("selectedState").toString(), String.class); String selectedTimeZone = mapper.readValue(root.get("selectedTimeZone").toString(), String.class); - Long profileId = Long.parseLong(request.getParameter("profile_id")); + Long profileId = Long.parseLong(request.getParameter(PROFILE_ID)); - User domainUser = (User) userService.getUser(String.valueOf(profileId)); + User domainUser = userService.getUser(String.valueOf(profileId)); user.setPseudoRoles(domainUser.getPseudoRoles()); user.setUserApps(domainUser.getUserApps()); - if (!selectedCountry.equals("")) { + if (!selectedCountry.isEmpty()) { user.setCountry(selectedCountry); } - if (!selectedState.equals("")) { + if (!selectedState.isEmpty()) { user.setState(selectedState); } - if (!selectedTimeZone.equals("")) { + if (!selectedTimeZone.isEmpty()) { user.setTimeZoneId(Long.parseLong(selectedTimeZone)); } service.saveUser(user); logger.info(EELFLoggerDelegate.auditLogger, "Save profile for user {}", profileId); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); + response.setCharacterEncoding(UTF8); + response.setContentType(APPLICATION_JSON); PrintWriter out = response.getWriter(); out.write("" + profileId); return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "saveProfile failed", e); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); try { PrintWriter out = response.getWriter(); out.write("An error occurred in the saveProfile ()"); @@ -240,7 +249,7 @@ public class ProfileController extends RestrictedBaseController { @RequestMapping(value = { "/profile/removeRole" }, method = RequestMethod.POST) public ModelAndView removeRole(HttpServletRequest request, HttpServletResponse response) throws IOException { - logger.info(EELFLoggerDelegate.debugLogger, "ProfileController.save"); + logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER); try { ObjectMapper mapper = new ObjectMapper(); @@ -248,7 +257,7 @@ public class ProfileController extends RestrictedBaseController { JsonNode root = mapper.readTree(request.getReader()); Role role = mapper.readValue(root.get("role").toString(), Role.class); - String profileId = request.getParameter("profile_id"); + String profileId = request.getParameter(PROFILE_ID); User domainUser = userService.getUser(profileId); @@ -262,14 +271,14 @@ public class ProfileController extends RestrictedBaseController { } logger.info(EELFLoggerDelegate.auditLogger, "Remove role " + role.getId() + " from user " + profileId); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + response.setContentType(APPLICATION_JSON); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); Map model = new HashMap<>(); - model.put("profile", mapper.writeValueAsString(domainUser)); + model.put(PROFILE, mapper.writeValueAsString(domainUser)); JSONObject j = new JSONObject(mapper.writeValueAsString(domainUser)); out.write(j.toString()); @@ -277,7 +286,7 @@ public class ProfileController extends RestrictedBaseController { return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "removeRole failed", e); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred in the removeRole ()"); return null; @@ -287,14 +296,14 @@ public class ProfileController extends RestrictedBaseController { @RequestMapping(value = { "/profile/addNewRole" }, method = RequestMethod.POST) public ModelAndView addNewRole(HttpServletRequest request, HttpServletResponse response) throws IOException { - logger.info(EELFLoggerDelegate.debugLogger, "ProfileController.save" ); + logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER ); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); Role role = mapper.readValue(root.get("role").toString(), Role.class); - String profileId = request.getParameter("profile_id"); + String profileId = request.getParameter(PROFILE_ID); User domainUser = userService.getUser(profileId); domainUser.addRole(role); service.saveUser(domainUser); @@ -305,13 +314,13 @@ public class ProfileController extends RestrictedBaseController { } logger.info(EELFLoggerDelegate.auditLogger, "Add new role " + role.getName() + " to user " + profileId); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + response.setContentType(APPLICATION_JSON); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); Map model = new HashMap<>(); - model.put("profile", mapper.writeValueAsString(domainUser)); + model.put(PROFILE, mapper.writeValueAsString(domainUser)); JSONObject j = new JSONObject(mapper.writeValueAsString(domainUser)); out.write(j.toString()); @@ -319,8 +328,8 @@ public class ProfileController extends RestrictedBaseController { return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "addNewRole failed", e); - response.setCharacterEncoding("UTF-8"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred in the addNewRole ()"); return null; @@ -328,16 +337,6 @@ public class ProfileController extends RestrictedBaseController { } - @Override - public String getViewName() { - return viewName; - } - - @Override - public void setViewName(String viewName) { - this.viewName = viewName; - } - @SuppressWarnings("rawtypes") public List getStates() { return AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"); @@ -358,4 +357,13 @@ public class ProfileController extends RestrictedBaseController { return roleService.getAvailableRoles(requestedLoginId); } + @Override + public String getViewName() { + return viewName; + } + + @Override + public void setViewName(String viewName) { + this.viewName = viewName; + } } \ No newline at end of file -- cgit 1.2.3-korg