From 7f5a837be19d55a9d813cfe041d8563132cdfde3 Mon Sep 17 00:00:00 2001 From: Brunner-Leo <46557560+Brunner-Leo@users.noreply.github.com> Date: Fri, 7 May 2021 08:53:54 +0200 Subject: [PATCH] ATLDEV-304 Refactor user information (#48) --- pom.xml | 2 +- .../jira/timesheet/rest/UserRest.java | 49 +++- .../timesheet/services/TimesheetService.java | 6 + .../services/impl/TimesheetServiceImpl.java | 12 + src/main/resources/js/user_information.js | 260 ++++++++---------- src/main/resources/user_information.vm | 184 +++++++++---- .../MySampleDatabaseUpdater.java | 26 +- .../jira/timesheet/rest/UserRestTest.java | 65 ++++- 8 files changed, 397 insertions(+), 207 deletions(-) diff --git a/pom.xml b/pom.xml index 6435ea94..34243edd 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 4.0.0 org.catrobat.jira timesheet - 2.4.1 + 2.4.2 International Catrobat Association diff --git a/src/main/java/org/catrobat/jira/timesheet/rest/UserRest.java b/src/main/java/org/catrobat/jira/timesheet/rest/UserRest.java index e79f7550..ecc7e9e0 100644 --- a/src/main/java/org/catrobat/jira/timesheet/rest/UserRest.java +++ b/src/main/java/org/catrobat/jira/timesheet/rest/UserRest.java @@ -43,6 +43,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.sql.Time; import java.time.LocalDate; import java.util.*; import java.util.concurrent.TimeUnit; @@ -126,18 +127,20 @@ public Response getUsers(@Context HttpServletRequest request) { @Path("/getUserInformation") @Produces(MediaType.APPLICATION_JSON) public Response getUserInformation(@Context HttpServletRequest request) { - - logger.debug("1 /getUserInformation reached"); - Date date = new Date(); - Response response = permissionService.checkRootPermission(); if (response != null) { return response; } + List jsonUserInformationList = timesheetsToJson(timesheetService.all()); + + return Response.ok(jsonUserInformationList).build(); + } + private List timesheetsToJson(List timesheetList) + { List jsonUserInformationList = new ArrayList<>(); - for (Timesheet timesheet : timesheetService.all()) { + for (Timesheet timesheet : timesheetList) { ApplicationUser u = ComponentAccessor.getUserManager().getUserByKey(timesheet.getUserKey()); if (u == null) { @@ -191,15 +194,43 @@ public Response getUserInformation(@Context HttpServletRequest request) { jsonUserInformationList.add(jsonUserInformation); } + return jsonUserInformationList; + } - Date date1 = new Date(); - long diffInMillies = date1.getTime() - date.getTime(); - TimeUnit timeUnit = TimeUnit.SECONDS; - logger.debug("2 /getUserInformation diffInMillies/diffInSeconds: " + diffInMillies + "/" + timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS)); + @GET + @Path("/getUserInformation/{State}") + @Produces(MediaType.APPLICATION_JSON) + public Response getUserInformationWithState(@Context HttpServletRequest request, @PathParam("State") String state) { + Response response = permissionService.checkRootPermission(); + if (response != null) { + return response; + } + List jsonUserInformationList = timesheetsToJson(timesheetService.allWithState(state)); return Response.ok(jsonUserInformationList).build(); } + @GET + @Path("/getUserInformationStats") + @Produces(MediaType.APPLICATION_JSON) + public Response getUserInformationStats(@Context HttpServletRequest request) { + Response response = permissionService.checkRootPermission(); + if (response != null) { + return response; + } + + List states = Arrays.asList("ACTIVE", "INACTIVE", "AUTO_INACTIVE", "INACTIVE_OFFLINE", "DISABLED", "DONE"); + + List userInformationStats = new ArrayList<>(); + + for (String state : states) { + userInformationStats.add(timesheetService.getStateAmount(state)); + } + + return Response.ok(userInformationStats).build(); + } + + @GET @Path("/getUsersForCoordinator/{currentTimesheetID}") @Produces(MediaType.APPLICATION_JSON) diff --git a/src/main/java/org/catrobat/jira/timesheet/services/TimesheetService.java b/src/main/java/org/catrobat/jira/timesheet/services/TimesheetService.java index d3f7f7cc..f221cee7 100644 --- a/src/main/java/org/catrobat/jira/timesheet/services/TimesheetService.java +++ b/src/main/java/org/catrobat/jira/timesheet/services/TimesheetService.java @@ -57,6 +57,12 @@ Timesheet add(String userKey, String displayName, @NotNull List all(); + @NotNull + List allWithState(String state); + + @NotNull + int getStateAmount(String state); + @Nullable Timesheet updateTimesheetEnableState(int timesheetID, Boolean isEnabled) throws ServiceException; diff --git a/src/main/java/org/catrobat/jira/timesheet/services/impl/TimesheetServiceImpl.java b/src/main/java/org/catrobat/jira/timesheet/services/impl/TimesheetServiceImpl.java index 7bce6dd8..8e8d5abc 100644 --- a/src/main/java/org/catrobat/jira/timesheet/services/impl/TimesheetServiceImpl.java +++ b/src/main/java/org/catrobat/jira/timesheet/services/impl/TimesheetServiceImpl.java @@ -132,6 +132,18 @@ public List all() { return newArrayList(ao.find(Timesheet.class)); } + @NotNull + @Override + public List allWithState(String state) { + return newArrayList(ao.find(Timesheet.class, "STATE = ?", state)); + } + + @NotNull + @Override + public int getStateAmount(String state) { + return ao.find(Timesheet.class, "STATE = ?", state).length; + } + @Nullable @Override public Timesheet updateTimesheetEnableState(int timesheetID, Boolean isEnabled) throws ServiceException { diff --git a/src/main/resources/js/user_information.js b/src/main/resources/js/user_information.js index 3d717843..4dd768c1 100644 --- a/src/main/resources/js/user_information.js +++ b/src/main/resources/js/user_information.js @@ -40,11 +40,6 @@ AJS.toInit(function () { } function populateTable(userInformation) { - AJS.$(".loadingDiv").show(); - AJS.$("#user-information-table-content").empty(); - AJS.$("#done-user-info-table-content").empty(); - AJS.$("#disabled-user-info-table-content").empty(); - //sort by username userInformation.sort(dynamicSort("userName")); @@ -82,55 +77,55 @@ AJS.toInit(function () { var current_state = userInformation[i].state; var current_state_color = "black"; + var table = ""; switch(current_state){ case "ACTIVE": current_state_color = "green"; + table = "#active-user-table-content"; break; case "DISABLED": current_state_color = "red"; + table = "#disabled-user-table-content"; break; case "INACTIVE" : current_state_color = "goldenRod"; + table = "#inactive-user-table-content"; break; case "AUTO_INACTIVE": current_state_color = "goldenRod"; + table = "#autoinactive-user-table-content"; break; case "DONE": current_state_color = "grey"; + table = "#done-user-table-content"; + break; + case "INACTIVE_OFFLINE": + current_state_color = "goldenRod"; + table = "#inactiveoffline-user-table-content"; break; } - var enabledColumn = "" + dropdown; - var row = "" + - "" + + var enabledColumn = "" + dropdown; + var row = "" + + "" + "" + userInformation[i].userName + " "+ - "" + userInformation[i].teams + + "" + userInformation[i].teams + "" + userInformation[i].state + - "" + inactiveEndDate + - "" + userInformation[i].remainingHours + - "" + userInformation[i].targetTotalHours + - "" + userInformation[i].totalHours + - "" + userInformation[i].hoursPerHalfYear + - "" + userInformation[i].hoursPerMonitoringPeriod + - "" + firstEntryDate + - "" + latestEntryDate + - "" + userInformation[i].latestEntryDescription + + "" + inactiveEndDate + + "" + userInformation[i].remainingHours + + "" + userInformation[i].targetTotalHours + + "" + userInformation[i].totalHours + + "" + userInformation[i].hoursPerHalfYear + + "" + userInformation[i].hoursPerMonitoringPeriod + + "" + firstEntryDate + + "" + latestEntryDate + + "" + userInformation[i].latestEntryDescription + enabledColumn + ""; - - if (userInformation[i].state === "DONE") { - AJS.$("#done-user-info-table-content").append(row); - } - else if (userInformation[i].state === "DISABLED") { - AJS.$("#disabled-user-info-table-content").append(row); - } - else { - AJS.$("#user-information-table-content").append(row); - } - - var timesheetID = userInformation[i].timesheetID; - - setupDropdownButton(timesheetID, enabled); + + AJS.$(table).append(row); + + setupDropdownButton(userInformation[i].timesheetID, enabled); } AJS.$(".view-timesheet-button").on("click", function (e) { @@ -144,53 +139,6 @@ AJS.toInit(function () { window.open(AJS.params.baseURL + "/secure/ViewProfile.jspa?name=" + user_name, "_blank"); }); - - AJS.$("#user-information-table").trigger("update"); - AJS.$("#disabled-users-table").trigger("update"); - AJS.$("#done-users-table").trigger("update"); - - AJS.$("#timesheet-user-statistics").empty(); - - var numberTotal = 0; - var numberActive = 0; - var numberInActive = 0; - var numberAutoInActive = 0; - var numberInActiveOffline = 0; - var numberDisabled = 0; - var numberDone = 0; - - for (var i = 0; i < userInformation.length; i++) { - - numberTotal++; - - if (userInformation[i].state === "ACTIVE") - numberActive++; - else if (userInformation[i].state === "INACTIVE") - numberInActive++; - else if (userInformation[i].state === "AUTO_INACTIVE") - numberAutoInActive++; - else if (userInformation[i].state === "INACTIVE_OFFLINE") - numberInActiveOffline++; - else if (userInformation[i].state === "DISABLED") - numberDisabled++; - else if (userInformation[i].state === "DONE") - numberDone++; - - } - - var row = "" + "Total Number of Timesheets: " + numberTotal + "" + - "" + "Active Timesheets: " + numberActive + "" + - "" + "Auto Inactive Timesheets: " + numberAutoInActive + "" + - "" + "Inactive Timesheets: " + numberInActive + "" + - "" + - - "" + "Disabled Timesheets: " + numberDisabled + "" + - "" + "InactiveOffline Timesheets: " + numberInActiveOffline + "" + - "" + "Done Timesheets: " + numberDone + "" + - ""; - - AJS.$("#timesheet-user-statistics").append(row); - AJS.$(".loadingDiv").hide(); } @@ -243,11 +191,8 @@ AJS.toInit(function () { setupDropdownButton(timesheetID, enabled); - if (enabled) { - AJS.$("#state" + timesheetID).text("ACTIVE"); - } else { - AJS.$("#state" + timesheetID).text("DISABLED"); - } + AJS.$("#tr" + timesheetID).remove(); + AJS.$(".loadingDiv").hide(); }, error: function (error) { @@ -262,76 +207,70 @@ AJS.toInit(function () { }); } - function updateTimesheetStatus() { - - var data = []; - - for (var i = 0; i < users.length; i++) { - var tempData = {}; - tempData.timesheetID = users[i].timesheetID; - var tmpCheckBox = AJS.$("#checkBox" + users[i].timesheetID); - tempData.isEnabled = tmpCheckBox.prop("checked"); - - data.push(tempData); - } - + function populateTableWrapper(state) { AJS.$(".loadingDiv").show(); - AJS.$.ajax({ - url: restBaseUrl + 'timesheets/updateEnableStates', - type: "POST", - contentType: "application/json", - data: JSON.stringify(data), - processData: false, - success: function () { - if(fetchingErrorMessage) - fetchingErrorMessage.closeMessage(); - if(errorMessage) - errorMessage.closeMessage(); - AJS.messages.success({ - title: "Success!", - body: "Timesheet 'enabled states' updated.", - fadeout: true, - delay: 5000, - duration: 5000 - }); - AJS.$(".loadingDiv").hide(); - }, - error: function (error) { - if(errorMessage) - errorMessage.closeMessage(); - errorMessage = AJS.messages.error({ - title: "Error!", - body: error.responseText - }); - AJS.$(".loadingDiv").hide(); - } - }); - } - - function fetchData() { var userInformationFetched = AJS.$.ajax({ type: 'GET', - url: restBaseUrl + 'user/getUserInformation', + url: restBaseUrl + 'user/getUserInformation/' + state, contentType: "application/json" }); + AJS.$.when(userInformationFetched) + .done(populateTable) + .fail(function (error) { + if(fetchingErrorMessage) + fetchingErrorMessage.closeMessage(); + fetchingErrorMessage = AJS.messages.error({ + title: 'There was an error while fetching the required data.', + body: '

Reason: ' + error.responseText + '

' + }); + console.log(error); + }); + } - var monitoringFetched = AJS.$.ajax({ + function populateStatsWrapper() { + AJS.$(".loadingDiv").show(); + var stateAmounts = AJS.$.ajax({ type: 'GET', - url: restBaseUrl + 'monitoring/getMonitoring', - contentType: "application/json" + url: restBaseUrl + 'user/getUserInformationStats', + contentType: "text/plain" }); - AJS.$.when(userInformationFetched) - .done(populateTable) + AJS.$.when(stateAmounts) + .done(populateStats) .fail(function (error) { - if(fetchingErrorMessage) - fetchingErrorMessage.closeMessage(); + if(fetchingErrorMessage) + fetchingErrorMessage.closeMessage(); fetchingErrorMessage = AJS.messages.error({ title: 'There was an error while fetching the required data.', body: '

Reason: ' + error.responseText + '

' }); console.log(error); }); + } + + function populateStats(amounts) { + var sum = 0; + var style = " style=\"text-align:right\""; + var stat_list = ["Active", "Inactive", "Auto-Inactive", + "Inactive-Offline", "Disabled", "Done", "Total Number of Timesheets"] + + for (var i = 0; i < amounts.length; i++) { + console.log(amounts[i]); + var row_stat = "" + stat_list[i] + "" + amounts[i] + ""; + AJS.$("#timesheet-user-statistics").append(row_stat); + sum += amounts[i]; + } + + AJS.$("#timesheet-user-statistics-total").append("" + stat_list[stat_list.length - 1] + "" + sum + ""); + AJS.$(".loadingDiv").hide(); + } + + function fetchMonitoring() { + var monitoringFetched = AJS.$.ajax({ + type: 'GET', + url: restBaseUrl + 'monitoring/getMonitoring', + contentType: "application/json" + }); AJS.$.when(monitoringFetched) .fail(function (error) { @@ -345,5 +284,48 @@ AJS.toInit(function () { }); } - fetchData(); + function setupClickListeners() { + AJS.$(".loadingDiv").hide(); + + AJS.$("#li-stats-tab").click(function(){ + AJS.$("#timesheet-user-statistics").empty() + AJS.$("#timesheet-user-statistics-total").empty() + populateStatsWrapper(); + }) + + AJS.$("#li-active-tab").click(function(){ + AJS.$("#active-user-table-content").empty() + populateTableWrapper("ACTIVE"); + }) + + AJS.$("#li-inactive-tab").click(function(){ + AJS.$("#inactive-user-table-content").empty() + populateTableWrapper("INACTIVE"); + }) + + AJS.$("#li-autoinactive-tab").click(function(){ + AJS.$("#autoinactive-user-table-content").empty() + populateTableWrapper("AUTO_INACTIVE"); + }) + + AJS.$("#li-inactiveoffline-tab").click(function(){ + AJS.$("#inactiveoffline-user-table-content").empty() + populateTableWrapper("INACTIVE_OFFLINE"); + }) + + AJS.$("#li-disabled-tab").click(function(){ + AJS.$("#disabled-user-table-content").empty() + populateTableWrapper("DISABLED"); + }) + + AJS.$("#li-done-tab").click(function(){ + AJS.$("#done-user-table-content").empty() + populateTableWrapper("DONE"); + }) + } + + setupClickListeners(); + populateStatsWrapper(); + fetchMonitoring(); + }); diff --git a/src/main/resources/user_information.vm b/src/main/resources/user_information.vm index 0bc4f7c7..c4854e84 100644 --- a/src/main/resources/user_information.vm +++ b/src/main/resources/user_information.vm @@ -20,30 +20,11 @@ $webResourceManager.requireResource("org.catrobat.jira.timesheet:user-information-resources") - - - - - - - -