From d2c1bd5c4062a94d609999ff70821b13f608f2e3 Mon Sep 17 00:00:00 2001 From: Jodicd Date: Sun, 14 Feb 2021 14:01:31 -0600 Subject: [PATCH 01/15] JM - Intial Commit --- README-Jodi.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README-Jodi.md diff --git a/README-Jodi.md b/README-Jodi.md new file mode 100644 index 00000000..63b6d5ac --- /dev/null +++ b/README-Jodi.md @@ -0,0 +1,3 @@ +# Jodi Mitchell + +This is my Readme for my first commit. From 4c4d279dad5df976daaa0c1529f51b30495db6d0 Mon Sep 17 00:00:00 2001 From: Jodicd Date: Tue, 16 Feb 2021 13:10:38 -0600 Subject: [PATCH 02/15] Added Sprint2 Tasks --- pom.xml | 12 -- .../activeUsers/ActiveUserDeleteCommand.java | 38 ++++ .../ValidateActiveUserCommand.java | 41 ++++ .../employees/ActiveEmployeeExistsQuery.java | 21 ++ .../employees/EmployeeCreateCommand.java | 89 +++++++++ .../employees/EmployeeDeleteCommand.java | 42 ++++ .../commands/employees/EmployeeQuery.java | 41 ++++ .../employees/EmployeeSignInCommand.java | 108 ++++++++++ .../employees/EmployeeUpdateCommand.java | 80 ++++++++ .../employees/helpers/EmployeeHelper.java | 34 ++++ .../controllers/BaseRestController.java | 89 +++++++++ .../controllers/BaseRouteController.java | 117 +++++++++++ .../EmployeeDetailRouteController.java | 145 ++++++++++++++ .../controllers/EmployeeRestController.java | 100 ++++++++++ .../controllers/MainMenuRouteController.java | 44 +++++ .../ProductDetailRouteController.java | 62 +++++- .../ProductListingRouteController.java | 31 ++- .../controllers/ProductRestController.java | 49 ++++- .../controllers/SignInRestController.java | 34 ++++ .../controllers/SignInRouteController.java | 89 +++++++++ .../enums/QueryParameterMessages.java | 45 +++++ .../enums/QueryParameterNames.java | 3 +- .../controllers/enums/ViewModelNames.java | 6 +- .../controllers/enums/ViewNames.java | 5 +- .../uark/registerapp/models/api/Employee.java | 145 ++++++++++++++ .../models/api/EmployeeSignIn.java | 28 +++ .../registerapp/models/api/EmployeeType.java | 55 ++++++ .../models/entities/ActiveUserEntity.java | 91 +++++++++ .../models/entities/EmployeeEntity.java | 164 ++++++++++++++++ .../models/enums/EmployeeClassification.java | 58 ++++++ .../repositories/ActiveUserRepository.java | 13 ++ .../repositories/EmployeeRepository.java | 15 ++ src/main/resources/static/images/Sign-Out.png | Bin 0 -> 417 bytes .../static/scripts/employeeDetail.js | 184 ++++++++++++++++++ src/main/resources/static/scripts/mainMenu.js | 43 ++++ src/main/resources/static/scripts/master.js | 26 +++ src/main/resources/static/scripts/signIn.js | 43 ++++ src/main/resources/static/styles/master.css | 18 ++ .../resources/templates/employeeDetail.html | 104 ++++++++++ src/main/resources/templates/mainMenu.html | 62 ++++++ .../resources/templates/productDetail.html | 10 +- .../resources/templates/productListing.html | 6 +- src/main/resources/templates/signIn.html | 41 ++++ 43 files changed, 2396 insertions(+), 35 deletions(-) create mode 100644 src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java create mode 100644 src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/BaseRestController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/SignInRestController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java create mode 100644 src/main/java/edu/uark/registerapp/models/api/Employee.java create mode 100644 src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java create mode 100644 src/main/java/edu/uark/registerapp/models/api/EmployeeType.java create mode 100644 src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java create mode 100644 src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java create mode 100644 src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java create mode 100644 src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java create mode 100644 src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java create mode 100644 src/main/resources/static/images/Sign-Out.png create mode 100644 src/main/resources/static/scripts/employeeDetail.js create mode 100644 src/main/resources/static/scripts/mainMenu.js create mode 100644 src/main/resources/static/scripts/signIn.js create mode 100644 src/main/resources/templates/employeeDetail.html create mode 100644 src/main/resources/templates/mainMenu.html create mode 100644 src/main/resources/templates/signIn.html diff --git a/pom.xml b/pom.xml index 330a0022..1f4ec523 100644 --- a/pom.xml +++ b/pom.xml @@ -46,18 +46,6 @@ commons-lang3 - - - org.springframework.boot spring-boot-devtools diff --git a/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java b/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java new file mode 100644 index 00000000..ccd22837 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java @@ -0,0 +1,38 @@ +package edu.uark.registerapp.commands.activeUsers; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import edu.uark.registerapp.commands.VoidCommandInterface; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.repositories.ActiveUserRepository; + +@Service +public class ActiveUserDeleteCommand implements VoidCommandInterface { + @Transactional + @Override + public void execute() { + final Optional activeUserEntity = + this.activeUserRepository.findBySessionKey(this.sessionKey); + + if (activeUserEntity.isPresent()) { + this.activeUserRepository.delete(activeUserEntity.get()); + } + } + + // Properties + private String sessionKey; + public String getSessionKey() { + return this.sessionKey; + } + public ActiveUserDeleteCommand setSessionKey(final String sessionKey) { + this.sessionKey = sessionKey; + return this; + } + + @Autowired + private ActiveUserRepository activeUserRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java b/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java new file mode 100644 index 00000000..d04784cb --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java @@ -0,0 +1,41 @@ +package edu.uark.registerapp.commands.activeUsers; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.repositories.ActiveUserRepository; + +@Service +public class ValidateActiveUserCommand implements ResultCommandInterface { + @Override + public ActiveUserEntity execute() { + final Optional activeUserEntity = + this.activeUserRepository.findBySessionKey(this.sessionKey); + + if (!activeUserEntity.isPresent()) { + throw new UnauthorizedException(); + } + + return activeUserEntity.get(); + } + + // Properties + private String sessionKey; + + public String getSessionKey() { + return this.sessionKey; + } + + public ValidateActiveUserCommand setSessionKey(final String sessionKey) { + this.sessionKey = sessionKey; + return this; + } + + @Autowired + private ActiveUserRepository activeUserRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java new file mode 100644 index 00000000..9581f662 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java @@ -0,0 +1,21 @@ +package edu.uark.registerapp.commands.employees; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.VoidCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class ActiveEmployeeExistsQuery implements VoidCommandInterface { + @Override + public void execute() { + if (!this.employeeRepository.existsByIsActive(true)) { + throw new NotFoundException("Employee"); + } + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java new file mode 100644 index 00000000..0da298fb --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java @@ -0,0 +1,89 @@ +package edu.uark.registerapp.commands.employees; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeCreateCommand implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + if (this.isInitialEmployee) { + this.apiEmployee.setClassification( + EmployeeClassification.GENERAL_MANAGER.getClassification()); + } + + // Create a new ENTITY object from the API object details. + final EmployeeEntity employeeEntity = + this.employeeRepository.save(new EmployeeEntity(this.apiEmployee)); + + // Synchronize information generated by the database upon INSERT. + this.apiEmployee.setId(employeeEntity.getId()); + // Only send the password over the network when modifying the database. + this.apiEmployee.setPassword(StringUtils.EMPTY); + this.apiEmployee.setCreatedOn(employeeEntity.getCreatedOn()); + this.apiEmployee.setEmployeeId( + EmployeeHelper.padEmployeeId( + employeeEntity.getEmployeeId())); + + return this.apiEmployee; + } + + // Helper methods + private void validateProperties() { + if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { + throw new UnprocessableEntityException("first name"); + } + if (StringUtils.isBlank(this.apiEmployee.getLastName())) { + throw new UnprocessableEntityException("last name"); + } + if (StringUtils.isBlank(this.apiEmployee.getPassword())) { + throw new UnprocessableEntityException("password"); + } + + if (!this.isInitialEmployee + && (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED)) { + + throw new UnprocessableEntityException("classification"); + } + } + + // Properties + private Employee apiEmployee; + public Employee getApiEmployee() { + return this.apiEmployee; + } + public EmployeeCreateCommand setApiEmployee(final Employee apiEmployee) { + this.apiEmployee = apiEmployee; + return this; + } + + private boolean isInitialEmployee; + public boolean getIsInitialEmployee() { + return this.isInitialEmployee; + } + public EmployeeCreateCommand setIsInitialEmployee( + final boolean isInitialEmployee + ) { + + this.isInitialEmployee = isInitialEmployee; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; + + public EmployeeCreateCommand() { + this.isInitialEmployee = false; + } +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java new file mode 100644 index 00000000..da485479 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java @@ -0,0 +1,42 @@ +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import edu.uark.registerapp.commands.VoidCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeDeleteCommand implements VoidCommandInterface { + @Transactional + @Override + public void execute() { + final Optional employeeEntity = + this.employeeRepository.findById(this.employeeId); + + if (!employeeEntity.isPresent()) { // No record with the associated record ID exists in the database. + throw new NotFoundException("Product"); + } + + this.employeeRepository.delete(employeeEntity.get()); + } + + // Properties + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public EmployeeDeleteCommand setEmployeeId(final UUID productId) { + this.employeeId = productId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java new file mode 100644 index 00000000..0fcfb8e8 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java @@ -0,0 +1,41 @@ +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeQuery implements ResultCommandInterface { + @Override + public Employee execute() { + final Optional employeeEntity = + this.employeeRepository.findById(this.employeeId); + + if (employeeEntity.isPresent()) { + return new Employee(employeeEntity.get()); + } else { + throw new NotFoundException("Employee"); + } + } + + // Properties + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public EmployeeQuery setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java new file mode 100644 index 00000000..ad757154 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java @@ -0,0 +1,108 @@ +package edu.uark.registerapp.commands.employees; + +import java.util.Arrays; +import java.util.Optional; + +import javax.transaction.Transactional; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.api.EmployeeSignIn; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.ActiveUserRepository; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeSignInCommand implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + return new Employee(this.SignInEmployee()); + } + + // Helper methods + private void validateProperties() { + if (StringUtils.isBlank(this.employeeSignIn.getEmployeeId())) { + throw new UnprocessableEntityException("employee ID"); + } + try { + Integer.parseInt(this.employeeSignIn.getEmployeeId()); + } catch (final NumberFormatException e) { + throw new UnprocessableEntityException("employee ID"); + } + if (StringUtils.isBlank(this.employeeSignIn.getPassword())) { + throw new UnprocessableEntityException("password"); + } + } + + @Transactional + private EmployeeEntity SignInEmployee() { + final Optional employeeEntity = + this.employeeRepository.findByEmployeeId( + Integer.parseInt(this.employeeSignIn.getEmployeeId())); + + if (!employeeEntity.isPresent() + || !Arrays.equals( + employeeEntity.get().getPassword(), + EmployeeHelper.hashPassword(this.employeeSignIn.getPassword())) + ) { + + throw new UnauthorizedException(); + } + + final Optional activeUserEntity = + this.activeUserRepository + .findByEmployeeId(employeeEntity.get().getId()); + + if (!activeUserEntity.isPresent()) { + this.activeUserRepository.save( + (new ActiveUserEntity()) + .setSessionKey(this.sessionId) + .setEmployeeId(employeeEntity.get().getId()) + .setClassification( + employeeEntity.get().getClassification()) + .setName( + employeeEntity.get().getFirstName() + .concat(" ") + .concat(employeeEntity.get().getLastName()))); + } else { + this.activeUserRepository.save( + activeUserEntity.get().setSessionKey(this.sessionId)); + } + + return employeeEntity.get(); + } + + // Properties + private EmployeeSignIn employeeSignIn; + public EmployeeSignIn getEmployeeSignIn() { + return this.employeeSignIn; + } + public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn) { + this.employeeSignIn = employeeSignIn; + return this; + } + + private String sessionId; + public String getSessionId() { + return this.sessionId; + } + public EmployeeSignInCommand setSessionId(final String sessionId) { + this.sessionId = sessionId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; + @Autowired + private ActiveUserRepository activeUserRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java new file mode 100644 index 00000000..0fd64d7a --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java @@ -0,0 +1,80 @@ +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import javax.transaction.Transactional; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeUpdateCommand implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + this.updateEmployeeEntity(); + + return this.apiEmployee; + } + + // Helper methods + private void validateProperties() { + if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { + throw new UnprocessableEntityException("first name"); + } + if (StringUtils.isBlank(this.apiEmployee.getLastName())) { + throw new UnprocessableEntityException("last name"); + } + if (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED) { + throw new UnprocessableEntityException("classification"); + } + } + + @Transactional + private void updateEmployeeEntity() { + final Optional queriedEmployeeEntity = + this.employeeRepository.findById(this.employeeId); + + if (!queriedEmployeeEntity.isPresent()) { + throw new NotFoundException("Employee"); // No record with the associated record ID exists in the database. + } + + this.apiEmployee = queriedEmployeeEntity.get() + .synchronize(this.apiEmployee); // Synchronize any incoming changes for UPDATE to the database. + + this.employeeRepository.save(queriedEmployeeEntity.get()); // Write, via an UPDATE, any changes to the database. + } + + // Properties + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public EmployeeUpdateCommand setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + private Employee apiEmployee; + public Employee getApiEmployee() { + return this.apiEmployee; + } + public EmployeeUpdateCommand setApiEmployee(final Employee apiEmployee) { + this.apiEmployee = apiEmployee; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java new file mode 100644 index 00000000..e6f54d73 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java @@ -0,0 +1,34 @@ +package edu.uark.registerapp.commands.employees.helpers; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.apache.commons.lang3.StringUtils; + +public class EmployeeHelper { + public static String padEmployeeId(final int employeeId) { + final String employeeIdAsString = Integer.toString(employeeId); + + return ((employeeIdAsString.length() < EMPLOYEE_ID_MAXIMUM_LENGTH) + ? StringUtils.leftPad( + employeeIdAsString, + EMPLOYEE_ID_MAXIMUM_LENGTH, + "0") + : employeeIdAsString); + } + + public static byte[] hashPassword(final String password) { + try { + final MessageDigest messageDigest = + MessageDigest.getInstance("SHA-256"); + + messageDigest.update(password.getBytes()); + + return messageDigest.digest(); + } catch (final NoSuchAlgorithmException e) { + return new byte[0]; + } + } + + private static final int EMPLOYEE_ID_MAXIMUM_LENGTH = 5; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java b/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java new file mode 100644 index 00000000..0f39f55f --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java @@ -0,0 +1,89 @@ +package edu.uark.registerapp.controllers; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand; +import edu.uark.registerapp.commands.exceptions.ConflictException; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.controllers.enums.QueryParameterMessages; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.ApiResponse; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; + +public class BaseRestController extends BaseController { + protected ApiResponse redirectSessionNotActive( + final HttpServletResponse response + ) { + + response.setStatus(HttpStatus.FOUND.value()); + return (new ApiResponse()) + .setRedirectUrl( + ViewNames.SIGN_IN.getRoute().concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.SESSION_NOT_ACTIVE.getKeyAsString()))); + } + + protected ApiResponse redirectUserNotElevated( + final HttpServletRequest request, + final HttpServletResponse response + ) { + + return this.redirectUserNotElevated(request, response, ViewNames.MAIN_MENU.getRoute()); + } + + protected ApiResponse redirectUserNotElevated( + final HttpServletRequest request, + final HttpServletResponse response, + final String redirectRoute + ) { + + try { + final ActiveUserEntity activeUserEntity = + this.validateActiveUserCommand + .setSessionKey(request.getSession().getId()) + .execute(); + + if (activeUserEntity == null) { + return this.redirectSessionNotActive(response); + } else if (!EmployeeClassification.isElevatedUser(activeUserEntity.getClassification())) { + response.setStatus(HttpStatus.FOUND.value()); + + return (new ApiResponse()) + .setRedirectUrl( + redirectRoute.concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.NO_PERMISSIONS_FOR_ACTION.getKeyAsString()))); + } + } catch (final UnauthorizedException e) { + return this.redirectSessionNotActive(response); + } + + return new ApiResponse(); + } + + @ExceptionHandler({ + ConflictException.class, + NotFoundException.class, + UnauthorizedException.class, + UnprocessableEntityException.class + }) + public @ResponseBody ApiResponse handleError(final Exception e) { + return (new ApiResponse()).setErrorMessage(e.getMessage()); + } + + // Properties + @Autowired + private ValidateActiveUserCommand validateActiveUserCommand; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java b/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java new file mode 100644 index 00000000..58774f84 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java @@ -0,0 +1,117 @@ +package edu.uark.registerapp.controllers; + +import java.util.Map; +import java.util.Optional; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.QueryParameterMessages; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; + +public abstract class BaseRouteController extends BaseController { + protected ModelAndView setErrorMessageFromQueryString( + ModelAndView modelAndView, + final Map queryParameters + ) { + + if (!queryParameters.containsKey(QueryParameterNames.ERROR_CODE.getValue())) { + return modelAndView; + } + + try { + modelAndView = + this.setErrorMessageFromQueryString( + modelAndView, + Integer.parseInt( + queryParameters.get( + QueryParameterNames.ERROR_CODE.getValue()))); + } catch (final NumberFormatException e) { } + + return modelAndView; + } + protected ModelAndView setErrorMessageFromQueryString( + final ModelAndView modelAndView, + final Optional errorCode + ) { + + if (!errorCode.isPresent()) { + return modelAndView; + } + + return this.setErrorMessageFromQueryString(modelAndView, errorCode.get()); + } + + protected Optional getCurrentUser( + final HttpServletRequest request + ) { + + try { + return Optional.of( + this.validateActiveUserCommand + .setSessionKey(request.getSession().getId()) + .execute()); + } catch (final UnauthorizedException e) { + return Optional.ofNullable(null); + } + } + + protected ModelAndView buildInvalidSessionResponse() { + return new ModelAndView( + REDIRECT_PREPEND.concat( + ViewNames.SIGN_IN.getRoute().concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.SESSION_NOT_ACTIVE.getKeyAsString())))); + } + + protected boolean isElevatedUser(final ActiveUserEntity activeUserEntity) { + return EmployeeClassification.isElevatedUser( + activeUserEntity.getClassification()); + } + + protected ModelAndView buildNoPermissionsResponse() { + return this.buildNoPermissionsResponse(ViewNames.MAIN_MENU.getRoute()); + } + + protected ModelAndView buildNoPermissionsResponse(final String redirectRoute) { + return new ModelAndView( + REDIRECT_PREPEND.concat( + redirectRoute.concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.NO_PERMISSIONS_TO_VIEW.getKeyAsString())))); + } + + protected static final String REDIRECT_PREPEND = "redirect:"; + + // Helper methods + private ModelAndView setErrorMessageFromQueryString( + final ModelAndView modelAndView, + final int errorCode + ) { + + final String errorMessage = QueryParameterMessages.mapMessage(errorCode); + + if (!StringUtils.isBlank(errorMessage)) { + modelAndView.addObject( + ViewModelNames.ERROR_MESSAGE.getValue(), + errorMessage); + } + + return modelAndView; + } + + // Properties + @Autowired + private ValidateActiveUserCommand validateActiveUserCommand; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java b/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java new file mode 100644 index 00000000..01397b4c --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java @@ -0,0 +1,145 @@ +package edu.uark.registerapp.controllers; + +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; +import edu.uark.registerapp.commands.employees.EmployeeQuery; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.api.EmployeeType; +import edu.uark.registerapp.models.entities.ActiveUserEntity; + +@Controller +@RequestMapping(value = "/employeeDetail") +public class EmployeeDetailRouteController extends BaseRouteController { + @RequestMapping(method = RequestMethod.GET) + public ModelAndView start( + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final boolean activeUserExists = this.activeUserExists(); + + if (activeUserExists) { + final Optional activeUserEntity = + this.getCurrentUser(request); + + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } else if (!this.isElevatedUser(activeUserEntity.get())) { + return this.buildNoPermissionsResponse(); + } + } + + return this.buildStartResponse(!activeUserExists, queryParameters); + } + + @RequestMapping(value = "/{employeeId}", method = RequestMethod.GET) + public ModelAndView startWithEmployee( + @PathVariable final UUID employeeId, + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final Optional activeUserEntity = + this.getCurrentUser(request); + + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } else if (!this.isElevatedUser(activeUserEntity.get())) { + return this.buildNoPermissionsResponse(); + } + + return this.buildStartResponse(employeeId, queryParameters); + } + + // Helper methods + private boolean activeUserExists() { + try { + this.activeEmployeeExistsQuery.execute(); + return true; + } catch (final NotFoundException e) { + return false; + } + } + + private ModelAndView buildStartResponse( + final boolean isInitialEmployee, + final Map queryParameters + ) { + + return this.buildStartResponse( + isInitialEmployee, + (new UUID(0, 0)), + queryParameters); + } + + private ModelAndView buildStartResponse( + final UUID employeeId, + final Map queryParameters + ) { + + return this.buildStartResponse(false, employeeId, queryParameters); + } + + private ModelAndView buildStartResponse( + final boolean isInitialEmployee, + final UUID employeeId, + final Map queryParameters + ) { + + ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.EMPLOYEE_DETAIL.getViewName()), + queryParameters); + + if (employeeId.equals(new UUID(0, 0))) { + modelAndView.addObject( + ViewModelNames.EMPLOYEE.getValue(), + (new Employee()).setIsInitialEmployee(isInitialEmployee)); + } else { + try { + modelAndView.addObject( + ViewModelNames.EMPLOYEE.getValue(), + this.employeeQuery + .setEmployeeId(employeeId) + .execute() + .setIsInitialEmployee(isInitialEmployee)); + } catch (final Exception e) { + modelAndView.addObject( + ViewModelNames.ERROR_MESSAGE.getValue(), + e.getMessage()); + modelAndView.addObject( + ViewModelNames.EMPLOYEE.getValue(), + (new Employee()).setIsInitialEmployee(isInitialEmployee)); + } + } + + modelAndView.addObject( + ViewModelNames.EMPLOYEE_TYPES.getValue(), + EmployeeType.allEmployeeTypes()); + + return modelAndView; + } + + // Properties + @Autowired + private EmployeeQuery employeeQuery; + + @Autowired + private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java b/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java new file mode 100644 index 00000000..f9569b14 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java @@ -0,0 +1,100 @@ +package edu.uark.registerapp.controllers; + +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; +import edu.uark.registerapp.commands.employees.EmployeeCreateCommand; +import edu.uark.registerapp.commands.employees.EmployeeUpdateCommand; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.ApiResponse; +import edu.uark.registerapp.models.api.Employee; + +@RestController +@RequestMapping(value = "/api/employee") +public class EmployeeRestController extends BaseRestController { + @RequestMapping(value = "/", method = RequestMethod.POST) + public @ResponseBody ApiResponse createEmployee( + @RequestBody final Employee employee, + final HttpServletRequest request, + final HttpServletResponse response + ) { + + boolean isInitialEmployee = false; + ApiResponse canCreateEmployeeResponse; + + try { + this.activeEmployeeExistsQuery.execute(); + + canCreateEmployeeResponse = + this.redirectUserNotElevated(request, response); + } catch (final NotFoundException e) { + isInitialEmployee = true; + canCreateEmployeeResponse = new ApiResponse(); + } + + if (!canCreateEmployeeResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { + return canCreateEmployeeResponse; + } + + final Employee createdEmployee = + this.employeeCreateCommand + .setApiEmployee(employee) + .setIsInitialEmployee(isInitialEmployee) + .execute(); + + if (isInitialEmployee) { + createdEmployee + .setRedirectUrl( + ViewNames.SIGN_IN.getRoute().concat( + this.buildInitialQueryParameter( + QueryParameterNames.EMPLOYEE_ID.getValue(), + createdEmployee.getEmployeeId()))); + } + + return createdEmployee.setIsInitialEmployee(isInitialEmployee); + } + + @RequestMapping(value = "/{employeeId}", method = RequestMethod.PATCH) + public @ResponseBody ApiResponse updateEmployee( + @PathVariable final UUID employeeId, + @RequestBody final Employee employee, + final HttpServletRequest request, + final HttpServletResponse response + ) { + + final ApiResponse elevatedUserResponse = + this.redirectUserNotElevated(request, response); + if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { + return elevatedUserResponse; + } + + return this.employeeUpdateCommand + .setEmployeeId(employeeId) + .setApiEmployee(employee) + .execute(); + } + + // Properties + @Autowired + private EmployeeCreateCommand employeeCreateCommand; + + @Autowired + private EmployeeUpdateCommand employeeUpdateCommand; + + @Autowired + private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java b/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java new file mode 100644 index 00000000..7f28f3be --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java @@ -0,0 +1,44 @@ +package edu.uark.registerapp.controllers; + +import java.util.Map; +import java.util.Optional; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.entities.ActiveUserEntity; + +@Controller +@RequestMapping(value = "/mainMenu") +public class MainMenuRouteController extends BaseRouteController { + @RequestMapping(method = RequestMethod.GET) + public ModelAndView start( + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final Optional activeUserEntity = + this.getCurrentUser(request); + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } + + ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.MAIN_MENU.getViewName()), + queryParameters); + + modelAndView.addObject( + ViewModelNames.IS_ELEVATED_USER.getValue(), + this.isElevatedUser(activeUserEntity.get())); + + return modelAndView; + } +} diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java b/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java index f6f35713..f6c4edc2 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java @@ -1,35 +1,81 @@ package edu.uark.registerapp.controllers; +import java.util.Map; +import java.util.Optional; import java.util.UUID; +import javax.servlet.http.HttpServletRequest; + import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import edu.uark.registerapp.commands.products.ProductQuery; import edu.uark.registerapp.controllers.enums.ViewModelNames; import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.Product; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; @Controller @RequestMapping(value = "/productDetail") -public class ProductDetailRouteController { +public class ProductDetailRouteController extends BaseRouteController { @RequestMapping(method = RequestMethod.GET) - public ModelAndView start() { - return (new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName())) - .addObject( - ViewModelNames.PRODUCT.getValue(), - (new Product()).setLookupCode(StringUtils.EMPTY).setCount(0)); + public ModelAndView start( + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + final Optional activeUserEntity = + this.getCurrentUser(request); + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } else if (!this.isElevatedUser(activeUserEntity.get())) { + return this.buildNoPermissionsResponse( + ViewNames.PRODUCT_LISTING.getRoute()); + } + + final ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()), + queryParameters); + + modelAndView.addObject( + ViewModelNames.IS_ELEVATED_USER.getValue(), + true); + modelAndView.addObject( + ViewModelNames.PRODUCT.getValue(), + (new Product()).setLookupCode(StringUtils.EMPTY).setCount(0)); + + return modelAndView; } @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ModelAndView startWithProduct(@PathVariable final UUID productId) { + public ModelAndView startWithProduct( + @PathVariable final UUID productId, + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final Optional activeUserEntity = + this.getCurrentUser(request); + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } + final ModelAndView modelAndView = - new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()); + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()), + queryParameters); + + modelAndView.addObject( + ViewModelNames.IS_ELEVATED_USER.getValue(), + EmployeeClassification.isElevatedUser( + activeUserEntity.get().getClassification())); try { modelAndView.addObject( diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java b/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java index 3c2a1178..ed9a3414 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java @@ -1,23 +1,46 @@ package edu.uark.registerapp.controllers; +import java.util.Map; +import java.util.Optional; + +import javax.servlet.http.HttpServletRequest; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import edu.uark.registerapp.commands.products.ProductsQuery; import edu.uark.registerapp.controllers.enums.ViewModelNames; import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.Product; +import edu.uark.registerapp.models.entities.ActiveUserEntity; @Controller -@RequestMapping(value = "/") -public class ProductListingRouteController { +@RequestMapping(value = "/productListing") +public class ProductListingRouteController extends BaseRouteController { @RequestMapping(method = RequestMethod.GET) - public ModelAndView showProductListing() { + public ModelAndView showProductListing( + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final Optional activeUserEntity = + this.getCurrentUser(request); + if (!activeUserEntity.isPresent()) { + return buildInvalidSessionResponse(); + } + ModelAndView modelAndView = - new ModelAndView(ViewNames.PRODUCT_LISTING.getViewName()); + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.PRODUCT_LISTING.getViewName()), + queryParameters); + + modelAndView.addObject( + ViewModelNames.IS_ELEVATED_USER.getValue(), + this.isElevatedUser(activeUserEntity.get())); try { modelAndView.addObject( diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java b/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java index 0c6c5f62..9f1be30f 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java @@ -2,6 +2,10 @@ import java.util.UUID; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -13,17 +17,30 @@ import edu.uark.registerapp.commands.products.ProductCreateCommand; import edu.uark.registerapp.commands.products.ProductDeleteCommand; import edu.uark.registerapp.commands.products.ProductUpdateCommand; +import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.ApiResponse; import edu.uark.registerapp.models.api.Product; @RestController @RequestMapping(value = "/api/product") -public class ProductRestController { +public class ProductRestController extends BaseRestController { @RequestMapping(value = "/", method = RequestMethod.POST) public @ResponseBody ApiResponse createProduct( - @RequestBody final Product product + @RequestBody final Product product, + final HttpServletRequest request, + final HttpServletResponse response ) { + final ApiResponse elevatedUserResponse = + this.redirectUserNotElevated( + request, + response, + ViewNames.PRODUCT_LISTING.getRoute()); + + if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { + return elevatedUserResponse; + } + return this.productCreateCommand .setApiProduct(product) .execute(); @@ -32,9 +49,21 @@ public class ProductRestController { @RequestMapping(value = "/{productId}", method = RequestMethod.PUT) public @ResponseBody ApiResponse updateProduct( @PathVariable final UUID productId, - @RequestBody final Product product + @RequestBody final Product product, + final HttpServletRequest request, + final HttpServletResponse response ) { + final ApiResponse elevatedUserResponse = + this.redirectUserNotElevated( + request, + response, + ViewNames.PRODUCT_LISTING.getRoute()); + + if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { + return elevatedUserResponse; + } + return this.productUpdateCommand .setProductId(productId) .setApiProduct(product) @@ -43,9 +72,21 @@ public class ProductRestController { @RequestMapping(value = "/{productId}", method = RequestMethod.DELETE) public @ResponseBody ApiResponse deleteProduct( - @PathVariable final UUID productId + @PathVariable final UUID productId, + final HttpServletRequest request, + final HttpServletResponse response ) { + final ApiResponse elevatedUserResponse = + this.redirectUserNotElevated( + request, + response, + ViewNames.PRODUCT_LISTING.getRoute()); + + if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { + return elevatedUserResponse; + } + this.productDeleteCommand .setProductId(productId) .execute(); diff --git a/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java b/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java new file mode 100644 index 00000000..8ec7ef25 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java @@ -0,0 +1,34 @@ +package edu.uark.registerapp.controllers; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import edu.uark.registerapp.commands.activeUsers.ActiveUserDeleteCommand; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.ApiResponse; + +@RestController +@RequestMapping(value = "/api") +public class SignInRestController extends BaseRestController { + @RequestMapping(value="/signOut", method = RequestMethod.DELETE) + public @ResponseBody ApiResponse removeActiveUser( + final HttpServletRequest request + ) { + + this.activeUserDeleteCommand + .setSessionKey(request.getSession().getId()) + .execute(); + + return (new ApiResponse()) + .setRedirectUrl(ViewNames.SIGN_IN.getRoute()); + } + + // Properties + @Autowired + private ActiveUserDeleteCommand activeUserDeleteCommand; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java b/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java new file mode 100644 index 00000000..749ace20 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java @@ -0,0 +1,89 @@ +package edu.uark.registerapp.controllers; + +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; +import edu.uark.registerapp.commands.employees.EmployeeSignInCommand; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.EmployeeSignIn; + +@Controller +@RequestMapping(value = "/") +public class SignInRouteController extends BaseRouteController { + @RequestMapping(method = RequestMethod.GET) + public ModelAndView showSignIn( + @RequestParam final Map queryParameters + ) { + + try { + this.activeEmployeeExistsQuery.execute(); + } catch (NotFoundException e) { + return new ModelAndView( + REDIRECT_PREPEND.concat( + ViewNames.EMPLOYEE_DETAIL.getRoute())); + } + + ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.SIGN_IN.getViewName()), + queryParameters); + + if (queryParameters.containsKey(QueryParameterNames.EMPLOYEE_ID.getValue())) { + modelAndView.addObject( + ViewModelNames.EMPLOYEE_ID.getValue(), + queryParameters.get(QueryParameterNames.EMPLOYEE_ID.getValue())); + } + + return modelAndView; + } + + @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + public ModelAndView performSignIn( + EmployeeSignIn employeeSignIn, + HttpServletRequest request + ) { + + try { + this.employeeSignInCommand + .setSessionId(request.getSession().getId()) + .setEmployeeSignIn(employeeSignIn) + .execute(); + } catch (Exception e) { + ModelAndView modelAndView = + new ModelAndView(ViewNames.SIGN_IN.getViewName()); + + modelAndView.addObject( + ViewModelNames.ERROR_MESSAGE.getValue(), + e.getMessage()); + modelAndView.addObject( + ViewModelNames.EMPLOYEE_ID.getValue(), + employeeSignIn.getEmployeeId()); + + return modelAndView; + } + + return new ModelAndView( + REDIRECT_PREPEND.concat( + ViewNames.MAIN_MENU.getRoute())); + } + + // Properties + @Autowired + private EmployeeSignInCommand employeeSignInCommand; + + @Autowired + private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java new file mode 100644 index 00000000..3f1bed87 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java @@ -0,0 +1,45 @@ +package edu.uark.registerapp.controllers.enums; + +import java.util.HashMap; +import java.util.Map; + +public enum QueryParameterMessages { + NOT_DEFINED(-1, ""), + SESSION_NOT_ACTIVE(1001, "The current user's session is no longer active."), + NO_PERMISSIONS_TO_VIEW(1101, "You do not have permission to view this resource."), + NO_PERMISSIONS_FOR_ACTION(1102, "You do not have permission to perform this action."); + + public int getKey() { + return this.key; + } + public String getKeyAsString() { + return Integer.toString(this.key); + } + public String getMessage() { + return this.message; + } + + public static String mapMessage(final int key) { + if (valueMap == null) { + valueMap = new HashMap(); + + for (final QueryParameterMessages status : QueryParameterMessages.values()) { + valueMap.put(status.getKey(), status.getMessage()); + } + } + + return (valueMap.containsKey(key) + ? valueMap.get(key) + : QueryParameterMessages.NOT_DEFINED.getMessage()); + } + + private int key; + private String message; + + private static Map valueMap = null; + + private QueryParameterMessages(final int key, final String message) { + this.key = key; + this.message = message; + } +} diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java index 988a9c79..d20b0100 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java @@ -2,7 +2,8 @@ public enum QueryParameterNames { NOT_DEFINED(""), - ERROR_CODE("errorCode"); + ERROR_CODE("errorCode"), + EMPLOYEE_ID("employeeId"); public String getValue() { return value; diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java index 7c39b102..fefde2fd 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java @@ -3,8 +3,12 @@ public enum ViewModelNames { NOT_DEFINED(""), ERROR_MESSAGE("errorMessage"), + IS_ELEVATED_USER("isElevatedUser"), PRODUCTS("products"), // Product listing - PRODUCT("product"); // Product detail + PRODUCT("product"), // Product detail + EMPLOYEE_ID("employeeId"), // Sign in + EMPLOYEE("employee"), // Employee detail + EMPLOYEE_TYPES("employeeTypes"); public String getValue() { return value; diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java index cd1a1246..3e3e2ca2 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java @@ -1,8 +1,11 @@ package edu.uark.registerapp.controllers.enums; public enum ViewNames { + SIGN_IN("signIn", "/"), + MAIN_MENU("mainMenu"), PRODUCT_DETAIL("productDetail"), - PRODUCT_LISTING("productListing", "/"); + EMPLOYEE_DETAIL("employeeDetail"), + PRODUCT_LISTING("productListing"); public String getRoute() { return this.route; diff --git a/src/main/java/edu/uark/registerapp/models/api/Employee.java b/src/main/java/edu/uark/registerapp/models/api/Employee.java new file mode 100644 index 00000000..6e313bfc --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/api/Employee.java @@ -0,0 +1,145 @@ +package edu.uark.registerapp.models.api; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.UUID; + +import org.apache.commons.lang3.StringUtils; + +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.models.entities.EmployeeEntity; + +public class Employee extends ApiResponse { + private UUID id; + public UUID getId() { + return this.id; + } + public Employee setId(final UUID id) { + this.id = id; + return this; + } + + private String employeeId; + public String getEmployeeId() { + return this.employeeId; + } + public Employee setEmployeeId(final int employeeId) { + this.employeeId = EmployeeHelper.padEmployeeId(employeeId); + return this; + } + public Employee setEmployeeId(final String employeeId) { + this.employeeId = employeeId; + return this; + } + + private String firstName; + public String getFirstName() { + return this.firstName; + } + public Employee setFirstName(final String firstName) { + this.firstName = firstName; + return this; + } + + private String lastName; + public String getLastName() { + return this.lastName; + } + public Employee setLastName(final String lastName) { + this.lastName = lastName; + return this; + } + + private String password; + public String getPassword() { + return this.password; + } + public Employee setPassword(final String password) { + this.password = password; + return this; + } + + private boolean isActive; + public boolean getIsActive() { + return this.isActive; + } + public Employee setIsActive(final boolean isActive) { + this.isActive = isActive; + return this; + } + + private int classification; + public int getClassification() { + return this.classification; + } + public Employee setClassification(final int classification) { + this.classification = classification; + return this; + } + + private UUID managerId; + public UUID getManagerId() { + return this.managerId; + } + public Employee setManagerId(final UUID managerId) { + this.managerId = managerId; + return this; + } + + private String createdOn; + public String getCreatedOn() { + return this.createdOn; + } + public Employee setCreatedOn(final String createdOn) { + this.createdOn = createdOn; + return this; + } + public Employee setCreatedOn(final LocalDateTime createdOn) { + this.createdOn = + createdOn.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); + + return this; + } + + private boolean isInitialEmployee; + public boolean getIsInitialEmployee() { + return this.isInitialEmployee; + } + public Employee setIsInitialEmployee(final boolean isInitialEmployee) { + this.isInitialEmployee = isInitialEmployee; + return this; + } + + public Employee() { + super(); + + this.isActive = true; + this.id = new UUID(0, 0); + this.classification = -1; + this.isInitialEmployee = false; + this.managerId = new UUID(0, 0); + this.lastName = StringUtils.EMPTY; + this.password = StringUtils.EMPTY; + this.firstName = StringUtils.EMPTY; + this.employeeId = StringUtils.EMPTY; + + this.setCreatedOn(LocalDateTime.now()); + } + + public Employee(final EmployeeEntity employeeEntity) { + super(false); + + this.isInitialEmployee = false; + this.id = employeeEntity.getId(); + this.password = StringUtils.EMPTY; + this.isActive = employeeEntity.getIsActive(); + this.lastName = employeeEntity.getLastName(); + this.firstName = employeeEntity.getFirstName(); + this.managerId = employeeEntity.getManagerId(); + this.classification = employeeEntity.getClassification(); + this.employeeId = + EmployeeHelper.padEmployeeId(employeeEntity.getEmployeeId()); + + this.setCreatedOn(employeeEntity.getCreatedOn()); + } +} diff --git a/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java b/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java new file mode 100644 index 00000000..e01e4f6c --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java @@ -0,0 +1,28 @@ +package edu.uark.registerapp.models.api; + +import org.apache.commons.lang3.StringUtils; + +public class EmployeeSignIn { + private String employeeId; + public String getEmployeeId() { + return this.employeeId; + } + public EmployeeSignIn setEmployeeId(final String employeeId) { + this.employeeId = employeeId; + return this; + } + + private String password; + public String getPassword() { + return this.password; + } + public EmployeeSignIn setPassword(final String password) { + this.password = password; + return this; + } + + public EmployeeSignIn() { + this.password = StringUtils.EMPTY; + this.employeeId = StringUtils.EMPTY; + } +} diff --git a/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java b/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java new file mode 100644 index 00000000..fa9e8421 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java @@ -0,0 +1,55 @@ +package edu.uark.registerapp.models.api; + +import org.apache.commons.lang3.StringUtils; + +import edu.uark.registerapp.models.enums.EmployeeClassification; + +public class EmployeeType { + private int classification; + public int getClassification() { + return this.classification; + } + public EmployeeType setClassification(final int classification) { + this.classification = classification; + return this; + } + + private String displayLabel; + + public String getDisplayLabel() { + return this.displayLabel; + } + + public EmployeeType setDisplayLabel(final String displayLabel) { + this.displayLabel = displayLabel; + return this; + } + + public static EmployeeType[] allEmployeeTypes() { + final EmployeeClassification[] employeeClassifications = + EmployeeClassification.values(); + final EmployeeType[] employeeTypes = + new EmployeeType[employeeClassifications.length]; + + for (int i = 0; i < employeeClassifications.length; i++) { + employeeTypes[i] = new EmployeeType(employeeClassifications[i]); + } + + return employeeTypes; + } + + public EmployeeType() { + this(-1, StringUtils.EMPTY); + } + + public EmployeeType(final EmployeeClassification employeeClassification) { + this( + employeeClassification.getClassification(), + employeeClassification.getDisplayLabel()); + } + + public EmployeeType(final int classification, final String displayLabel) { + this.displayLabel = displayLabel; + this.classification = classification; + } +} diff --git a/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java new file mode 100644 index 00000000..6eba3528 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java @@ -0,0 +1,91 @@ +package edu.uark.registerapp.models.entities; + +import java.time.LocalDateTime; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.annotations.Generated; +import org.hibernate.annotations.GenerationTime; + +@Entity +@Table(name="activeuser") +public class ActiveUserEntity { + @Id + @Column(name="id", updatable = false) + @GeneratedValue(strategy=GenerationType.AUTO) + private final UUID id; + + public UUID getId() { + return this.id; + } + + @Column(name = "employeeid") + private UUID employeeId; + + public UUID getEmployeeId() { + return this.employeeId; + } + + public ActiveUserEntity setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + @Column(name = "name") + private String name; + + public String getName() { + return this.name; + } + + public ActiveUserEntity setName(final String name) { + this.name = name; + return this; + } + + @Column(name = "classification") + private int classification; + + public int getClassification() { + return this.classification; + } + + public ActiveUserEntity setClassification(final int classification) { + this.classification = classification; + return this; + } + + @Column(name = "sessionkey") + private String sessionKey; + + public String getSessionKey() { + return this.sessionKey; + } + + public ActiveUserEntity setSessionKey(final String sessionKey) { + this.sessionKey = sessionKey; + return this; + } + + @Column(name="createdon", insertable=false, updatable = false) + @Generated(GenerationTime.INSERT) + private LocalDateTime createdOn; + public LocalDateTime getCreatedOn() { + return this.createdOn; + } + + public ActiveUserEntity() { + this.id = new UUID(0, 0); + this.classification = -1; + this.name = StringUtils.EMPTY; + this.employeeId = new UUID(0, 0); + this.sessionKey = StringUtils.EMPTY; + } +} diff --git a/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java b/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java new file mode 100644 index 00000000..324e5b31 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java @@ -0,0 +1,164 @@ +package edu.uark.registerapp.models.entities; + +import java.time.LocalDateTime; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.annotations.Generated; +import org.hibernate.annotations.GenerationTime; + +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.models.api.Employee; + +@Entity +@Table(name="employee") +public class EmployeeEntity { + @Id + @Column(name="id", updatable = false) + @GeneratedValue(strategy=GenerationType.AUTO) + private final UUID id; + + public UUID getId() { + return this.id; + } + + @Column(name = "employeeid", insertable = false, updatable = false) + @Generated(GenerationTime.INSERT) + private int employeeId; + + public int getEmployeeId() { + return this.employeeId; + } + + @Column(name = "firstname") + private String firstName; + + public String getFirstName() { + return this.firstName; + } + + public EmployeeEntity setFirstName(final String firstName) { + this.firstName = firstName; + return this; + } + + @Column(name = "lastname") + private String lastName; + + public String getLastName() { + return this.lastName; + } + + public EmployeeEntity setLastName(final String lastName) { + this.lastName = lastName; + return this; + } + + @Column(name = "password") + private byte[] password; + + public byte[] getPassword() { + return this.password; + } + + public EmployeeEntity setPassword(final byte[] password) { + this.password = password; + return this; + } + + @Column(name = "active") + private boolean isActive; + + public boolean getIsActive() { + return this.isActive; + } + + public EmployeeEntity setIsActive(final boolean isActive) { + this.isActive = isActive; + return this; + } + + @Column(name = "classification") + private int classification; + + public int getClassification() { + return this.classification; + } + + public EmployeeEntity setClassification(final int classification) { + this.classification = classification; + return this; + } + + @Column(name = "managerid") + private UUID managerId; + + public UUID getManagerId() { + return this.managerId; + } + + public EmployeeEntity setManagerId(final UUID managerId) { + this.managerId = managerId; + return this; + } + + @Column(name = "createdon", insertable = false, updatable = false) + @Generated(GenerationTime.INSERT) + private LocalDateTime createdOn; + + public LocalDateTime getCreatedOn() { + return this.createdOn; + } + + public Employee synchronize(final Employee apiEmployee) { + this.setIsActive(apiEmployee.getIsActive()); + this.setLastName(apiEmployee.getLastName()); + this.setFirstName(apiEmployee.getFirstName()); + this.setClassification(apiEmployee.getClassification()); + if (apiEmployee.getManagerId() != null) { + this.setManagerId(apiEmployee.getManagerId()); + } + if (!StringUtils.isBlank(apiEmployee.getPassword())) { + this.setPassword( + EmployeeHelper.hashPassword( + apiEmployee.getPassword())); + } + + apiEmployee.setId(this.getId()); + apiEmployee.setCreatedOn(this.getCreatedOn()); + apiEmployee.setEmployeeId(this.getEmployeeId()); + + return apiEmployee; + } + + public EmployeeEntity() { + this.employeeId = -1; + this.isActive = false; + this.id = new UUID(0, 0); + this.classification = -1; + this.password = new byte[0]; + this.managerId = new UUID(0, 0); + this.lastName = StringUtils.EMPTY; + this.firstName = StringUtils.EMPTY; + } + + public EmployeeEntity(final Employee apiEmployee) { + this.id = new UUID(0, 0); + this.isActive = apiEmployee.getIsActive(); + this.lastName = apiEmployee.getLastName(); + this.firstName = apiEmployee.getFirstName(); + this.classification = apiEmployee.getClassification(); + this.password = EmployeeHelper.hashPassword(apiEmployee.getPassword()); + this.managerId = ( + (apiEmployee.getManagerId() != null) + ? apiEmployee.getManagerId() + : new UUID(0, 0)); + } +} diff --git a/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java b/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java new file mode 100644 index 00000000..751c80ef --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java @@ -0,0 +1,58 @@ +package edu.uark.registerapp.models.enums; + +import java.util.HashMap; +import java.util.Map; + +public enum EmployeeClassification { + NOT_DEFINED(-1, "Not Selected"), + CASHIER(101, "Cashier"), + SHIFT_MANAGER(501, "Shift Manager"), + GENERAL_MANAGER(701, "General Manager"); + + public int getClassification() { + return this.classification; + } + + public String getDisplayLabel() { + return this.displayLabel; + } + + public static EmployeeClassification map(final int key) { + if (valueMap == null) { + valueMap = new HashMap(); + + for (final EmployeeClassification employeeClassification : EmployeeClassification.values()) { + valueMap.put( + employeeClassification.getClassification(), + employeeClassification); + } + } + + return ((valueMap.containsKey(key) + ? valueMap.get(key) + : EmployeeClassification.NOT_DEFINED)); + } + + public static boolean isElevatedUser(final int classification) { + final EmployeeClassification employeeClassification = + EmployeeClassification.map(classification); + + return ( + (employeeClassification == EmployeeClassification.GENERAL_MANAGER) + || (employeeClassification == EmployeeClassification.SHIFT_MANAGER)); + } + + private int classification; + private String displayLabel; + + private static Map valueMap = null; + + private EmployeeClassification( + final int classification, + final String displayLabel + ) { + + this.displayLabel = displayLabel; + this.classification = classification; + } +} diff --git a/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java new file mode 100644 index 00000000..53746cee --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java @@ -0,0 +1,13 @@ +package edu.uark.registerapp.models.repositories; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +import edu.uark.registerapp.models.entities.ActiveUserEntity; + +public interface ActiveUserRepository extends CrudRepository { + Optional findByEmployeeId(UUID employeeId); + Optional findBySessionKey(String sessionKey); +} diff --git a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java new file mode 100644 index 00000000..8ba4b81f --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java @@ -0,0 +1,15 @@ +package edu.uark.registerapp.models.repositories; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +import edu.uark.registerapp.models.entities.EmployeeEntity; + +public interface EmployeeRepository extends CrudRepository { + boolean existsByIsActive(boolean isActive); + boolean existsByEmployeeId(int employeeId); + Optional findById(UUID id); + Optional findByEmployeeId(int employeeId); +} diff --git a/src/main/resources/static/images/Sign-Out.png b/src/main/resources/static/images/Sign-Out.png new file mode 100644 index 0000000000000000000000000000000000000000..82fe8695ab00a4cdbd2b3f95f065fd7acbd108ec GIT binary patch literal 417 zcmV;S0bc%zP)7GIp>_a(3m+}_QaKE&z3o3q#8Dyh{QRup`m9ZI--K<7}2X0QOT_6(UfB~ zfFo0iTM!k^g5q{n0XmBNtqSl%ad9?U37>C(0R|XgKr-NscF2HNf}kBL;Dz{~wmv|? zI=>#2+PS61|;Y%W<1K>^~fyX^@&QOFD-2!R*p^-#O=;bI$z%Lh{J>>1jl;00000 LNkvXXu0mjf0)eri literal 0 HcmV?d00001 diff --git a/src/main/resources/static/scripts/employeeDetail.js b/src/main/resources/static/scripts/employeeDetail.js new file mode 100644 index 00000000..b5d7d2f8 --- /dev/null +++ b/src/main/resources/static/scripts/employeeDetail.js @@ -0,0 +1,184 @@ +let hideEmployeeSavedAlertTimer = undefined; + +document.addEventListener("DOMContentLoaded", () => { + document.getElementById("saveButton") + .addEventListener("click", saveActionClick); + + const employeeFirstNameEditElement = + getEmployeeFirstNameEditElement(); + employeeFirstNameEditElement.focus(); + employeeFirstNameEditElement.select(); +}); + +// Save +function saveActionClick(event) { + if (!validateSave()) { + return; + } + + const saveActionElement = event.target; + saveActionElement.disabled = true; + + const employeeId = getEmployeeId(); + const employeeIdIsDefined = (employeeId.trim() !== ""); + const saveActionUrl = ("/api/employee/" + + (employeeIdIsDefined ? employeeId : "")); + const saveEmployeeRequest = { + id: employeeId, + managerId: getEmployeeManagerId(), + lastName: getEmployeeLastNameEditElement().value, + password: getEmployeePasswordEditElement().value, + firstName: getEmployeeFirstNameEditElement().value, + classification: getEmployeeTypeSelectElement().value + }; + + if (employeeIdIsDefined) { + ajaxPatch(saveActionUrl, saveEmployeeRequest, (callbackResponse) => { + saveActionElement.disabled = false; + + if (isSuccessResponse(callbackResponse)) { + completeSaveAction(callbackResponse); + } + }); + } else { + ajaxPost(saveActionUrl, saveEmployeeRequest, (callbackResponse) => { + saveActionElement.disabled = false; + + if (isSuccessResponse(callbackResponse)) { + completeSaveAction(callbackResponse); + } + }); + } +} + +function validateSave() { + const firstNameEditElement = getEmployeeFirstNameEditElement(); + if (firstNameEditElement.value.trim() === "") { + displayError("Please provide a valid employee first name."); + firstNameEditElement.focus(); + firstNameEditElement.select(); + return false; + } + + const lastNameEditElement = getEmployeeLastNameEditElement(); + if (lastNameEditElement.value.trim() === "") { + displayError("Please provide a valid employee last name."); + lastNameEditElement.focus(); + lastNameEditElement.select(); + return false; + } + + const passwordEditElement = getEmployeePasswordEditElement(); + if (passwordEditElement.value.trim() === "") { + displayError("Please provide a valid employee password."); + passwordEditElement.focus(); + passwordEditElement.select(); + return false; + } + + if (passwordEditElement.value !== getEmployeeConfirmPassword()) { + displayError("Passwords do not match."); + passwordEditElement.focus() + passwordEditElement.select(); + return false; + } + + const employeeTypeSelectElement = getEmployeeTypeSelectElement(); + if (!employeeTypeSelectElement.closest("tr").classList.contains("hidden")) { + if (employeeTypeSelectElement.value <= 0) { + displayError("Please provide a valid employee Type."); + employeeTypeSelectElement.focus(); + return false; + } + } + + return true; +} + +function completeSaveAction(callbackResponse) { + if (callbackResponse.data == null) { + return; + } + + if ((callbackResponse.data.redirectUrl != null) + && (callbackResponse.data.redirectUrl !== "")) { + + window.location.replace(callbackResponse.data.redirectUrl); + return; + } + + displayEmployeeSavedAlertModal(); + + const employeeEmployeeIdElement = getEmployeeEmployeeIdElement(); + const employeeEmployeeIdRowElement = employeeEmployeeIdElement.closest("tr"); + if (employeeEmployeeIdRowElement.classList.contains("hidden")) { + setEmployeeId(callbackResponse.data.id); + employeeEmployeeIdElement.value = callbackResponse.data.employeeId; + employeeEmployeeIdRowElement.classList.remove("hidden"); + } +} + +function displayEmployeeSavedAlertModal() { + if (hideEmployeeSavedAlertTimer) { + clearTimeout(hideEmployeeSavedAlertTimer); + } + + const savedAlertModalElement = getSavedAlertModalElement(); + savedAlertModalElement.style.display = "none"; + savedAlertModalElement.style.display = "block"; + + hideEmployeeSavedAlertTimer = setTimeout(hideEmployeeSavedAlertModal, 1200); +} + +function hideEmployeeSavedAlertModal() { + if (hideEmployeeSavedAlertTimer) { + clearTimeout(hideEmployeeSavedAlertTimer); + } + + getSavedAlertModalElement().style.display = "none"; +} +// End save + +//Getters and setters +function getEmployeeId() { + return document.getElementById("employeeId").value; +} +function setEmployeeId(employeeId) { + document.getElementById("employeeId").value = employeeId; +} + +function getEmployeeManagerId() { + return document.getElementById("employeeManagerId").value; +} + +function getEmployeeEmployeeId() { + return getEmployeeEmployeeIdElement().value; +} +function getEmployeeEmployeeIdElement() { + return document.getElementById("employeeEmployeeId"); +} + +function getSavedAlertModalElement() { + return document.getElementById("employeeSavedAlertModal"); +} + +function getEmployeeFirstNameEditElement() { + return document.getElementById("employeeFirstName"); +} + +function getEmployeeLastNameEditElement() { + return document.getElementById("employeeLastName"); +} + +function getEmployeePasswordEditElement() { + return document.getElementById("employeePassword"); +} + +function getEmployeeConfirmPassword() { + return document.getElementById("employeeConfirmPassword").value; +} + +function getEmployeeTypeSelectElement() { + return document.getElementById("employeeType"); +} +//End getters and setters diff --git a/src/main/resources/static/scripts/mainMenu.js b/src/main/resources/static/scripts/mainMenu.js new file mode 100644 index 00000000..3b16c3f7 --- /dev/null +++ b/src/main/resources/static/scripts/mainMenu.js @@ -0,0 +1,43 @@ +document.addEventListener("DOMContentLoaded", function(event) { + getStartTransactionActionElement().addEventListener( + "click", + () => { displayError("Functionality has not yet been implemented."); }); + + getViewProductsActionElement().addEventListener( + "click", + () => { window.location.assign("/productListing"); }); + + getCreateEmployeeActionElement().addEventListener( + "click", + () => { window.location.assign("/employeeDetail"); }); + + getProductSalesReportActionElement().addEventListener( + "click", + () => { displayError("Functionality has not yet been implemented."); }); + + getCashierSalesReportActionElement().addEventListener( + "click", + () => { displayError("Functionality has not yet been implemented."); }); +}); + +// Getters and setters +function getViewProductsActionElement() { + return document.getElementById("viewProductsButton"); +} + +function getCreateEmployeeActionElement() { + return document.getElementById("createEmployeeButton"); +} + +function getStartTransactionActionElement() { + return document.getElementById("startTransactionButton"); +} + +function getProductSalesReportActionElement() { + return document.getElementById("productSalesReportButton"); +} + +function getCashierSalesReportActionElement() { + return document.getElementById("cashierSalesReportButton"); +} +// End getters and setters diff --git a/src/main/resources/static/scripts/master.js b/src/main/resources/static/scripts/master.js index 88b039d0..c61d3563 100644 --- a/src/main/resources/static/scripts/master.js +++ b/src/main/resources/static/scripts/master.js @@ -1,3 +1,10 @@ +document.addEventListener("DOMContentLoaded", () => { + const signOutActionElement = getSignOutActionElement(); + if (signOutActionElement != null) { + signOutActionElement.addEventListener("click", signOutActionClickHandler); + } +}); + // AJAX function ajaxGet(resourceRelativeUri, callback) { return ajax(resourceRelativeUri, "GET", null, callback); @@ -158,6 +165,10 @@ function displayError(errorMessage) { // End display error message //Getters and setters +function getSignOutActionElement() { + return document.getElementById("signOutImage"); +} + function getErrorMessageContainerElement() { return document.getElementById("error"); } @@ -166,3 +177,18 @@ function getErrorMessageDisplayElement() { return document.getElementById("errorMessage"); } // End getters and setters + +//Sign out +function signOutActionClickHandler() { + ajaxDelete("/api/signOut", (callbackResponse) => { + if ((callbackResponse.data != null) + && (callbackResponse.data.redirectUrl != null) + && (callbackResponse.data.redirectUrl !== "")) { + + window.location.replace(callbackResponse.data.redirectUrl); + } else { + window.location.replace("/"); + } + }); +} +//End sign out \ No newline at end of file diff --git a/src/main/resources/static/scripts/signIn.js b/src/main/resources/static/scripts/signIn.js new file mode 100644 index 00000000..e7b14144 --- /dev/null +++ b/src/main/resources/static/scripts/signIn.js @@ -0,0 +1,43 @@ +document.addEventListener("DOMContentLoaded", function(event) { + const employeeIdEditElement = getEmployeeIdEditElement(); + employeeIdEditElement.focus(); + employeeIdEditElement.select(); +}); + +function validateForm() { + const employeeIdEditElement = getEmployeeIdEditElement(); + if (isNaN(Number(employeeIdEditElement.value)) + || (Number(employeeIdEditElement.value) <= 0)) { + + displayError("Please provide a valid employee ID."); + + employeeIdEditElement.focus(); + employeeIdEditElement.select(); + + return false; + } + + const passwordEditElement = getPasswordEditElement(); + if ((passwordEditElement.value == null) + || (passwordEditElement.value.trim() === "")) { + + displayError("Please provide a valid password. It may not be blank."); + + passwordEditElement.focus(); + passwordEditElement.select(); + + return false; + } + + return true; +} + +//Getters and setters +function getPasswordEditElement() { + return document.getElementById("password"); +} + +function getEmployeeIdEditElement() { + return document.getElementById("employeeId"); +} +//End getters and setters diff --git a/src/main/resources/static/styles/master.css b/src/main/resources/static/styles/master.css index 2b060e6d..f35bb48a 100644 --- a/src/main/resources/static/styles/master.css +++ b/src/main/resources/static/styles/master.css @@ -30,6 +30,24 @@ td.hidden { display: none } +div.footer { + left: 0; + right: 0; + bottom: 0; + position: absolute; +} + +img.footer { + z-index: 1; + height: 50px; + text-decoration: none; + display: inline-block; + cursor: pointer; + position: fixed; + right: 20; + bottom: 20; +} + div.inputContent, form.inputContent { display: inline-block; diff --git a/src/main/resources/templates/employeeDetail.html b/src/main/resources/templates/employeeDetail.html new file mode 100644 index 00000000..85b4cb05 --- /dev/null +++ b/src/main/resources/templates/employeeDetail.html @@ -0,0 +1,104 @@ + + + + Register - Employee + + + + + + + + + + +
+

Employee Detail

+
+ +
+
+

+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Employee ID: + +
First Name: + +
Last Name: + +
Password: + +
Confirm Password: + +
Employee Type: + +
+
+ +
+
+ +
+ + +
+ + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/mainMenu.html b/src/main/resources/templates/mainMenu.html new file mode 100644 index 00000000..706fc531 --- /dev/null +++ b/src/main/resources/templates/mainMenu.html @@ -0,0 +1,62 @@ + + + + Register - Main Menu + + + + + + + + + + +
+

Main Menu

+
+ +
+
+

+
+ + +
+ + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/productDetail.html b/src/main/resources/templates/productDetail.html index ac183115..453d1041 100644 --- a/src/main/resources/templates/productDetail.html +++ b/src/main/resources/templates/productDetail.html @@ -34,20 +34,20 @@

Lookup Code: - + Count: - + -
+

@@ -68,6 +68,10 @@

+ + diff --git a/src/main/resources/templates/productListing.html b/src/main/resources/templates/productListing.html index c4b758ae..6c374f83 100644 --- a/src/main/resources/templates/productListing.html +++ b/src/main/resources/templates/productListing.html @@ -22,7 +22,7 @@

-
+ @@ -54,6 +54,10 @@

+ + diff --git a/src/main/resources/templates/signIn.html b/src/main/resources/templates/signIn.html new file mode 100644 index 00000000..6f4c3994 --- /dev/null +++ b/src/main/resources/templates/signIn.html @@ -0,0 +1,41 @@ + + + + Register - Sign In + + + + + + + + + + +
+

Sign In

+
+ +
+
+

+
+ +
+
+ +
+
+
+ +
+

+
+ +
+
+
+ + + + \ No newline at end of file From 88b86052456d68e1a727ada74a34ef497c7fa658 Mon Sep 17 00:00:00 2001 From: Jodicd Date: Fri, 19 Feb 2021 16:59:12 -0600 Subject: [PATCH 03/15] cleaning up the mess --- pom.xml | 12 ++ .../activeUsers/ActiveUserDeleteCommand.java | 38 ---- .../ValidateActiveUserCommand.java | 41 ---- .../employees/ActiveEmployeeExistsQuery.java | 21 -- .../employees/EmployeeCreateCommand.java | 89 --------- .../employees/EmployeeDeleteCommand.java | 42 ---- .../commands/employees/EmployeeQuery.java | 41 ---- .../employees/EmployeeSignInCommand.java | 108 ---------- .../employees/EmployeeUpdateCommand.java | 80 -------- .../employees/helpers/EmployeeHelper.java | 34 ---- .../controllers/BaseRestController.java | 89 --------- .../controllers/BaseRouteController.java | 117 ----------- .../EmployeeDetailRouteController.java | 145 -------------- .../controllers/EmployeeRestController.java | 100 ---------- .../controllers/MainMenuRouteController.java | 44 ----- .../ProductDetailRouteController.java | 62 +----- .../ProductListingRouteController.java | 31 +-- .../controllers/ProductRestController.java | 49 +---- .../controllers/SignInRestController.java | 34 ---- .../controllers/SignInRouteController.java | 89 --------- .../enums/QueryParameterMessages.java | 45 ----- .../enums/QueryParameterNames.java | 3 +- .../controllers/enums/ViewModelNames.java | 6 +- .../controllers/enums/ViewNames.java | 5 +- .../uark/registerapp/models/api/Employee.java | 145 -------------- .../models/api/EmployeeSignIn.java | 28 --- .../registerapp/models/api/EmployeeType.java | 55 ------ .../models/entities/ActiveUserEntity.java | 91 --------- .../models/entities/EmployeeEntity.java | 164 ---------------- .../models/enums/EmployeeClassification.java | 58 ------ .../repositories/ActiveUserRepository.java | 13 -- .../repositories/EmployeeRepository.java | 15 -- src/main/resources/static/images/Sign-Out.png | Bin 417 -> 0 bytes .../static/scripts/employeeDetail.js | 184 ------------------ src/main/resources/static/scripts/mainMenu.js | 43 ---- src/main/resources/static/scripts/master.js | 26 --- src/main/resources/static/scripts/signIn.js | 43 ---- src/main/resources/static/styles/master.css | 18 -- .../resources/templates/employeeDetail.html | 104 ---------- src/main/resources/templates/mainMenu.html | 62 ------ .../resources/templates/productDetail.html | 10 +- .../resources/templates/productListing.html | 6 +- src/main/resources/templates/signIn.html | 41 ---- 43 files changed, 35 insertions(+), 2396 deletions(-) delete mode 100644 src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/BaseRestController.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/SignInRestController.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java delete mode 100644 src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java delete mode 100644 src/main/java/edu/uark/registerapp/models/api/Employee.java delete mode 100644 src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java delete mode 100644 src/main/java/edu/uark/registerapp/models/api/EmployeeType.java delete mode 100644 src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java delete mode 100644 src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java delete mode 100644 src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java delete mode 100644 src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java delete mode 100644 src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java delete mode 100644 src/main/resources/static/images/Sign-Out.png delete mode 100644 src/main/resources/static/scripts/employeeDetail.js delete mode 100644 src/main/resources/static/scripts/mainMenu.js delete mode 100644 src/main/resources/static/scripts/signIn.js delete mode 100644 src/main/resources/templates/employeeDetail.html delete mode 100644 src/main/resources/templates/mainMenu.html delete mode 100644 src/main/resources/templates/signIn.html diff --git a/pom.xml b/pom.xml index 1f4ec523..330a0022 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,18 @@ commons-lang3
+ + + org.springframework.boot spring-boot-devtools diff --git a/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java b/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java deleted file mode 100644 index ccd22837..00000000 --- a/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java +++ /dev/null @@ -1,38 +0,0 @@ -package edu.uark.registerapp.commands.activeUsers; - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import edu.uark.registerapp.commands.VoidCommandInterface; -import edu.uark.registerapp.models.entities.ActiveUserEntity; -import edu.uark.registerapp.models.repositories.ActiveUserRepository; - -@Service -public class ActiveUserDeleteCommand implements VoidCommandInterface { - @Transactional - @Override - public void execute() { - final Optional activeUserEntity = - this.activeUserRepository.findBySessionKey(this.sessionKey); - - if (activeUserEntity.isPresent()) { - this.activeUserRepository.delete(activeUserEntity.get()); - } - } - - // Properties - private String sessionKey; - public String getSessionKey() { - return this.sessionKey; - } - public ActiveUserDeleteCommand setSessionKey(final String sessionKey) { - this.sessionKey = sessionKey; - return this; - } - - @Autowired - private ActiveUserRepository activeUserRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java b/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java deleted file mode 100644 index d04784cb..00000000 --- a/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java +++ /dev/null @@ -1,41 +0,0 @@ -package edu.uark.registerapp.commands.activeUsers; - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.exceptions.UnauthorizedException; -import edu.uark.registerapp.models.entities.ActiveUserEntity; -import edu.uark.registerapp.models.repositories.ActiveUserRepository; - -@Service -public class ValidateActiveUserCommand implements ResultCommandInterface { - @Override - public ActiveUserEntity execute() { - final Optional activeUserEntity = - this.activeUserRepository.findBySessionKey(this.sessionKey); - - if (!activeUserEntity.isPresent()) { - throw new UnauthorizedException(); - } - - return activeUserEntity.get(); - } - - // Properties - private String sessionKey; - - public String getSessionKey() { - return this.sessionKey; - } - - public ValidateActiveUserCommand setSessionKey(final String sessionKey) { - this.sessionKey = sessionKey; - return this; - } - - @Autowired - private ActiveUserRepository activeUserRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java deleted file mode 100644 index 9581f662..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java +++ /dev/null @@ -1,21 +0,0 @@ -package edu.uark.registerapp.commands.employees; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.VoidCommandInterface; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class ActiveEmployeeExistsQuery implements VoidCommandInterface { - @Override - public void execute() { - if (!this.employeeRepository.existsByIsActive(true)) { - throw new NotFoundException("Employee"); - } - } - - @Autowired - private EmployeeRepository employeeRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java deleted file mode 100644 index 0da298fb..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java +++ /dev/null @@ -1,89 +0,0 @@ -package edu.uark.registerapp.commands.employees; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; -import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.enums.EmployeeClassification; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class EmployeeCreateCommand implements ResultCommandInterface { - @Override - public Employee execute() { - this.validateProperties(); - - if (this.isInitialEmployee) { - this.apiEmployee.setClassification( - EmployeeClassification.GENERAL_MANAGER.getClassification()); - } - - // Create a new ENTITY object from the API object details. - final EmployeeEntity employeeEntity = - this.employeeRepository.save(new EmployeeEntity(this.apiEmployee)); - - // Synchronize information generated by the database upon INSERT. - this.apiEmployee.setId(employeeEntity.getId()); - // Only send the password over the network when modifying the database. - this.apiEmployee.setPassword(StringUtils.EMPTY); - this.apiEmployee.setCreatedOn(employeeEntity.getCreatedOn()); - this.apiEmployee.setEmployeeId( - EmployeeHelper.padEmployeeId( - employeeEntity.getEmployeeId())); - - return this.apiEmployee; - } - - // Helper methods - private void validateProperties() { - if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { - throw new UnprocessableEntityException("first name"); - } - if (StringUtils.isBlank(this.apiEmployee.getLastName())) { - throw new UnprocessableEntityException("last name"); - } - if (StringUtils.isBlank(this.apiEmployee.getPassword())) { - throw new UnprocessableEntityException("password"); - } - - if (!this.isInitialEmployee - && (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED)) { - - throw new UnprocessableEntityException("classification"); - } - } - - // Properties - private Employee apiEmployee; - public Employee getApiEmployee() { - return this.apiEmployee; - } - public EmployeeCreateCommand setApiEmployee(final Employee apiEmployee) { - this.apiEmployee = apiEmployee; - return this; - } - - private boolean isInitialEmployee; - public boolean getIsInitialEmployee() { - return this.isInitialEmployee; - } - public EmployeeCreateCommand setIsInitialEmployee( - final boolean isInitialEmployee - ) { - - this.isInitialEmployee = isInitialEmployee; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; - - public EmployeeCreateCommand() { - this.isInitialEmployee = false; - } -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java deleted file mode 100644 index da485479..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -package edu.uark.registerapp.commands.employees; - -import java.util.Optional; -import java.util.UUID; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import edu.uark.registerapp.commands.VoidCommandInterface; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class EmployeeDeleteCommand implements VoidCommandInterface { - @Transactional - @Override - public void execute() { - final Optional employeeEntity = - this.employeeRepository.findById(this.employeeId); - - if (!employeeEntity.isPresent()) { // No record with the associated record ID exists in the database. - throw new NotFoundException("Product"); - } - - this.employeeRepository.delete(employeeEntity.get()); - } - - // Properties - private UUID employeeId; - public UUID getEmployeeId() { - return this.employeeId; - } - public EmployeeDeleteCommand setEmployeeId(final UUID productId) { - this.employeeId = productId; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java deleted file mode 100644 index 0fcfb8e8..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java +++ /dev/null @@ -1,41 +0,0 @@ -package edu.uark.registerapp.commands.employees; - -import java.util.Optional; -import java.util.UUID; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class EmployeeQuery implements ResultCommandInterface { - @Override - public Employee execute() { - final Optional employeeEntity = - this.employeeRepository.findById(this.employeeId); - - if (employeeEntity.isPresent()) { - return new Employee(employeeEntity.get()); - } else { - throw new NotFoundException("Employee"); - } - } - - // Properties - private UUID employeeId; - public UUID getEmployeeId() { - return this.employeeId; - } - public EmployeeQuery setEmployeeId(final UUID employeeId) { - this.employeeId = employeeId; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java deleted file mode 100644 index ad757154..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java +++ /dev/null @@ -1,108 +0,0 @@ -package edu.uark.registerapp.commands.employees; - -import java.util.Arrays; -import java.util.Optional; - -import javax.transaction.Transactional; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; -import edu.uark.registerapp.commands.exceptions.UnauthorizedException; -import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.api.EmployeeSignIn; -import edu.uark.registerapp.models.entities.ActiveUserEntity; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.repositories.ActiveUserRepository; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class EmployeeSignInCommand implements ResultCommandInterface { - @Override - public Employee execute() { - this.validateProperties(); - - return new Employee(this.SignInEmployee()); - } - - // Helper methods - private void validateProperties() { - if (StringUtils.isBlank(this.employeeSignIn.getEmployeeId())) { - throw new UnprocessableEntityException("employee ID"); - } - try { - Integer.parseInt(this.employeeSignIn.getEmployeeId()); - } catch (final NumberFormatException e) { - throw new UnprocessableEntityException("employee ID"); - } - if (StringUtils.isBlank(this.employeeSignIn.getPassword())) { - throw new UnprocessableEntityException("password"); - } - } - - @Transactional - private EmployeeEntity SignInEmployee() { - final Optional employeeEntity = - this.employeeRepository.findByEmployeeId( - Integer.parseInt(this.employeeSignIn.getEmployeeId())); - - if (!employeeEntity.isPresent() - || !Arrays.equals( - employeeEntity.get().getPassword(), - EmployeeHelper.hashPassword(this.employeeSignIn.getPassword())) - ) { - - throw new UnauthorizedException(); - } - - final Optional activeUserEntity = - this.activeUserRepository - .findByEmployeeId(employeeEntity.get().getId()); - - if (!activeUserEntity.isPresent()) { - this.activeUserRepository.save( - (new ActiveUserEntity()) - .setSessionKey(this.sessionId) - .setEmployeeId(employeeEntity.get().getId()) - .setClassification( - employeeEntity.get().getClassification()) - .setName( - employeeEntity.get().getFirstName() - .concat(" ") - .concat(employeeEntity.get().getLastName()))); - } else { - this.activeUserRepository.save( - activeUserEntity.get().setSessionKey(this.sessionId)); - } - - return employeeEntity.get(); - } - - // Properties - private EmployeeSignIn employeeSignIn; - public EmployeeSignIn getEmployeeSignIn() { - return this.employeeSignIn; - } - public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn) { - this.employeeSignIn = employeeSignIn; - return this; - } - - private String sessionId; - public String getSessionId() { - return this.sessionId; - } - public EmployeeSignInCommand setSessionId(final String sessionId) { - this.sessionId = sessionId; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; - @Autowired - private ActiveUserRepository activeUserRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java deleted file mode 100644 index 0fd64d7a..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java +++ /dev/null @@ -1,80 +0,0 @@ -package edu.uark.registerapp.commands.employees; - -import java.util.Optional; -import java.util.UUID; - -import javax.transaction.Transactional; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.enums.EmployeeClassification; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class EmployeeUpdateCommand implements ResultCommandInterface { - @Override - public Employee execute() { - this.validateProperties(); - - this.updateEmployeeEntity(); - - return this.apiEmployee; - } - - // Helper methods - private void validateProperties() { - if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { - throw new UnprocessableEntityException("first name"); - } - if (StringUtils.isBlank(this.apiEmployee.getLastName())) { - throw new UnprocessableEntityException("last name"); - } - if (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED) { - throw new UnprocessableEntityException("classification"); - } - } - - @Transactional - private void updateEmployeeEntity() { - final Optional queriedEmployeeEntity = - this.employeeRepository.findById(this.employeeId); - - if (!queriedEmployeeEntity.isPresent()) { - throw new NotFoundException("Employee"); // No record with the associated record ID exists in the database. - } - - this.apiEmployee = queriedEmployeeEntity.get() - .synchronize(this.apiEmployee); // Synchronize any incoming changes for UPDATE to the database. - - this.employeeRepository.save(queriedEmployeeEntity.get()); // Write, via an UPDATE, any changes to the database. - } - - // Properties - private UUID employeeId; - public UUID getEmployeeId() { - return this.employeeId; - } - public EmployeeUpdateCommand setEmployeeId(final UUID employeeId) { - this.employeeId = employeeId; - return this; - } - - private Employee apiEmployee; - public Employee getApiEmployee() { - return this.apiEmployee; - } - public EmployeeUpdateCommand setApiEmployee(final Employee apiEmployee) { - this.apiEmployee = apiEmployee; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java deleted file mode 100644 index e6f54d73..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java +++ /dev/null @@ -1,34 +0,0 @@ -package edu.uark.registerapp.commands.employees.helpers; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -import org.apache.commons.lang3.StringUtils; - -public class EmployeeHelper { - public static String padEmployeeId(final int employeeId) { - final String employeeIdAsString = Integer.toString(employeeId); - - return ((employeeIdAsString.length() < EMPLOYEE_ID_MAXIMUM_LENGTH) - ? StringUtils.leftPad( - employeeIdAsString, - EMPLOYEE_ID_MAXIMUM_LENGTH, - "0") - : employeeIdAsString); - } - - public static byte[] hashPassword(final String password) { - try { - final MessageDigest messageDigest = - MessageDigest.getInstance("SHA-256"); - - messageDigest.update(password.getBytes()); - - return messageDigest.digest(); - } catch (final NoSuchAlgorithmException e) { - return new byte[0]; - } - } - - private static final int EMPLOYEE_ID_MAXIMUM_LENGTH = 5; -} diff --git a/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java b/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java deleted file mode 100644 index 0f39f55f..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java +++ /dev/null @@ -1,89 +0,0 @@ -package edu.uark.registerapp.controllers; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; - -import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand; -import edu.uark.registerapp.commands.exceptions.ConflictException; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.commands.exceptions.UnauthorizedException; -import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; -import edu.uark.registerapp.controllers.enums.QueryParameterMessages; -import edu.uark.registerapp.controllers.enums.QueryParameterNames; -import edu.uark.registerapp.controllers.enums.ViewNames; -import edu.uark.registerapp.models.api.ApiResponse; -import edu.uark.registerapp.models.entities.ActiveUserEntity; -import edu.uark.registerapp.models.enums.EmployeeClassification; - -public class BaseRestController extends BaseController { - protected ApiResponse redirectSessionNotActive( - final HttpServletResponse response - ) { - - response.setStatus(HttpStatus.FOUND.value()); - return (new ApiResponse()) - .setRedirectUrl( - ViewNames.SIGN_IN.getRoute().concat( - this.buildInitialQueryParameter( - QueryParameterNames.ERROR_CODE.getValue(), - QueryParameterMessages.SESSION_NOT_ACTIVE.getKeyAsString()))); - } - - protected ApiResponse redirectUserNotElevated( - final HttpServletRequest request, - final HttpServletResponse response - ) { - - return this.redirectUserNotElevated(request, response, ViewNames.MAIN_MENU.getRoute()); - } - - protected ApiResponse redirectUserNotElevated( - final HttpServletRequest request, - final HttpServletResponse response, - final String redirectRoute - ) { - - try { - final ActiveUserEntity activeUserEntity = - this.validateActiveUserCommand - .setSessionKey(request.getSession().getId()) - .execute(); - - if (activeUserEntity == null) { - return this.redirectSessionNotActive(response); - } else if (!EmployeeClassification.isElevatedUser(activeUserEntity.getClassification())) { - response.setStatus(HttpStatus.FOUND.value()); - - return (new ApiResponse()) - .setRedirectUrl( - redirectRoute.concat( - this.buildInitialQueryParameter( - QueryParameterNames.ERROR_CODE.getValue(), - QueryParameterMessages.NO_PERMISSIONS_FOR_ACTION.getKeyAsString()))); - } - } catch (final UnauthorizedException e) { - return this.redirectSessionNotActive(response); - } - - return new ApiResponse(); - } - - @ExceptionHandler({ - ConflictException.class, - NotFoundException.class, - UnauthorizedException.class, - UnprocessableEntityException.class - }) - public @ResponseBody ApiResponse handleError(final Exception e) { - return (new ApiResponse()).setErrorMessage(e.getMessage()); - } - - // Properties - @Autowired - private ValidateActiveUserCommand validateActiveUserCommand; -} diff --git a/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java b/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java deleted file mode 100644 index 58774f84..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java +++ /dev/null @@ -1,117 +0,0 @@ -package edu.uark.registerapp.controllers; - -import java.util.Map; -import java.util.Optional; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.servlet.ModelAndView; - -import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand; -import edu.uark.registerapp.commands.exceptions.UnauthorizedException; -import edu.uark.registerapp.controllers.enums.ViewModelNames; -import edu.uark.registerapp.controllers.enums.QueryParameterMessages; -import edu.uark.registerapp.controllers.enums.QueryParameterNames; -import edu.uark.registerapp.controllers.enums.ViewNames; -import edu.uark.registerapp.models.entities.ActiveUserEntity; -import edu.uark.registerapp.models.enums.EmployeeClassification; - -public abstract class BaseRouteController extends BaseController { - protected ModelAndView setErrorMessageFromQueryString( - ModelAndView modelAndView, - final Map queryParameters - ) { - - if (!queryParameters.containsKey(QueryParameterNames.ERROR_CODE.getValue())) { - return modelAndView; - } - - try { - modelAndView = - this.setErrorMessageFromQueryString( - modelAndView, - Integer.parseInt( - queryParameters.get( - QueryParameterNames.ERROR_CODE.getValue()))); - } catch (final NumberFormatException e) { } - - return modelAndView; - } - protected ModelAndView setErrorMessageFromQueryString( - final ModelAndView modelAndView, - final Optional errorCode - ) { - - if (!errorCode.isPresent()) { - return modelAndView; - } - - return this.setErrorMessageFromQueryString(modelAndView, errorCode.get()); - } - - protected Optional getCurrentUser( - final HttpServletRequest request - ) { - - try { - return Optional.of( - this.validateActiveUserCommand - .setSessionKey(request.getSession().getId()) - .execute()); - } catch (final UnauthorizedException e) { - return Optional.ofNullable(null); - } - } - - protected ModelAndView buildInvalidSessionResponse() { - return new ModelAndView( - REDIRECT_PREPEND.concat( - ViewNames.SIGN_IN.getRoute().concat( - this.buildInitialQueryParameter( - QueryParameterNames.ERROR_CODE.getValue(), - QueryParameterMessages.SESSION_NOT_ACTIVE.getKeyAsString())))); - } - - protected boolean isElevatedUser(final ActiveUserEntity activeUserEntity) { - return EmployeeClassification.isElevatedUser( - activeUserEntity.getClassification()); - } - - protected ModelAndView buildNoPermissionsResponse() { - return this.buildNoPermissionsResponse(ViewNames.MAIN_MENU.getRoute()); - } - - protected ModelAndView buildNoPermissionsResponse(final String redirectRoute) { - return new ModelAndView( - REDIRECT_PREPEND.concat( - redirectRoute.concat( - this.buildInitialQueryParameter( - QueryParameterNames.ERROR_CODE.getValue(), - QueryParameterMessages.NO_PERMISSIONS_TO_VIEW.getKeyAsString())))); - } - - protected static final String REDIRECT_PREPEND = "redirect:"; - - // Helper methods - private ModelAndView setErrorMessageFromQueryString( - final ModelAndView modelAndView, - final int errorCode - ) { - - final String errorMessage = QueryParameterMessages.mapMessage(errorCode); - - if (!StringUtils.isBlank(errorMessage)) { - modelAndView.addObject( - ViewModelNames.ERROR_MESSAGE.getValue(), - errorMessage); - } - - return modelAndView; - } - - // Properties - @Autowired - private ValidateActiveUserCommand validateActiveUserCommand; -} diff --git a/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java b/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java deleted file mode 100644 index 01397b4c..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java +++ /dev/null @@ -1,145 +0,0 @@ -package edu.uark.registerapp.controllers; - -import java.util.Map; -import java.util.Optional; -import java.util.UUID; - -import javax.servlet.http.HttpServletRequest; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.servlet.ModelAndView; - -import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; -import edu.uark.registerapp.commands.employees.EmployeeQuery; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.controllers.enums.ViewModelNames; -import edu.uark.registerapp.controllers.enums.ViewNames; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.api.EmployeeType; -import edu.uark.registerapp.models.entities.ActiveUserEntity; - -@Controller -@RequestMapping(value = "/employeeDetail") -public class EmployeeDetailRouteController extends BaseRouteController { - @RequestMapping(method = RequestMethod.GET) - public ModelAndView start( - @RequestParam final Map queryParameters, - final HttpServletRequest request - ) { - - final boolean activeUserExists = this.activeUserExists(); - - if (activeUserExists) { - final Optional activeUserEntity = - this.getCurrentUser(request); - - if (!activeUserEntity.isPresent()) { - return this.buildInvalidSessionResponse(); - } else if (!this.isElevatedUser(activeUserEntity.get())) { - return this.buildNoPermissionsResponse(); - } - } - - return this.buildStartResponse(!activeUserExists, queryParameters); - } - - @RequestMapping(value = "/{employeeId}", method = RequestMethod.GET) - public ModelAndView startWithEmployee( - @PathVariable final UUID employeeId, - @RequestParam final Map queryParameters, - final HttpServletRequest request - ) { - - final Optional activeUserEntity = - this.getCurrentUser(request); - - if (!activeUserEntity.isPresent()) { - return this.buildInvalidSessionResponse(); - } else if (!this.isElevatedUser(activeUserEntity.get())) { - return this.buildNoPermissionsResponse(); - } - - return this.buildStartResponse(employeeId, queryParameters); - } - - // Helper methods - private boolean activeUserExists() { - try { - this.activeEmployeeExistsQuery.execute(); - return true; - } catch (final NotFoundException e) { - return false; - } - } - - private ModelAndView buildStartResponse( - final boolean isInitialEmployee, - final Map queryParameters - ) { - - return this.buildStartResponse( - isInitialEmployee, - (new UUID(0, 0)), - queryParameters); - } - - private ModelAndView buildStartResponse( - final UUID employeeId, - final Map queryParameters - ) { - - return this.buildStartResponse(false, employeeId, queryParameters); - } - - private ModelAndView buildStartResponse( - final boolean isInitialEmployee, - final UUID employeeId, - final Map queryParameters - ) { - - ModelAndView modelAndView = - this.setErrorMessageFromQueryString( - new ModelAndView(ViewNames.EMPLOYEE_DETAIL.getViewName()), - queryParameters); - - if (employeeId.equals(new UUID(0, 0))) { - modelAndView.addObject( - ViewModelNames.EMPLOYEE.getValue(), - (new Employee()).setIsInitialEmployee(isInitialEmployee)); - } else { - try { - modelAndView.addObject( - ViewModelNames.EMPLOYEE.getValue(), - this.employeeQuery - .setEmployeeId(employeeId) - .execute() - .setIsInitialEmployee(isInitialEmployee)); - } catch (final Exception e) { - modelAndView.addObject( - ViewModelNames.ERROR_MESSAGE.getValue(), - e.getMessage()); - modelAndView.addObject( - ViewModelNames.EMPLOYEE.getValue(), - (new Employee()).setIsInitialEmployee(isInitialEmployee)); - } - } - - modelAndView.addObject( - ViewModelNames.EMPLOYEE_TYPES.getValue(), - EmployeeType.allEmployeeTypes()); - - return modelAndView; - } - - // Properties - @Autowired - private EmployeeQuery employeeQuery; - - @Autowired - private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; -} diff --git a/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java b/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java deleted file mode 100644 index f9569b14..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java +++ /dev/null @@ -1,100 +0,0 @@ -package edu.uark.registerapp.controllers; - -import java.util.UUID; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; -import edu.uark.registerapp.commands.employees.EmployeeCreateCommand; -import edu.uark.registerapp.commands.employees.EmployeeUpdateCommand; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.controllers.enums.QueryParameterNames; -import edu.uark.registerapp.controllers.enums.ViewNames; -import edu.uark.registerapp.models.api.ApiResponse; -import edu.uark.registerapp.models.api.Employee; - -@RestController -@RequestMapping(value = "/api/employee") -public class EmployeeRestController extends BaseRestController { - @RequestMapping(value = "/", method = RequestMethod.POST) - public @ResponseBody ApiResponse createEmployee( - @RequestBody final Employee employee, - final HttpServletRequest request, - final HttpServletResponse response - ) { - - boolean isInitialEmployee = false; - ApiResponse canCreateEmployeeResponse; - - try { - this.activeEmployeeExistsQuery.execute(); - - canCreateEmployeeResponse = - this.redirectUserNotElevated(request, response); - } catch (final NotFoundException e) { - isInitialEmployee = true; - canCreateEmployeeResponse = new ApiResponse(); - } - - if (!canCreateEmployeeResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { - return canCreateEmployeeResponse; - } - - final Employee createdEmployee = - this.employeeCreateCommand - .setApiEmployee(employee) - .setIsInitialEmployee(isInitialEmployee) - .execute(); - - if (isInitialEmployee) { - createdEmployee - .setRedirectUrl( - ViewNames.SIGN_IN.getRoute().concat( - this.buildInitialQueryParameter( - QueryParameterNames.EMPLOYEE_ID.getValue(), - createdEmployee.getEmployeeId()))); - } - - return createdEmployee.setIsInitialEmployee(isInitialEmployee); - } - - @RequestMapping(value = "/{employeeId}", method = RequestMethod.PATCH) - public @ResponseBody ApiResponse updateEmployee( - @PathVariable final UUID employeeId, - @RequestBody final Employee employee, - final HttpServletRequest request, - final HttpServletResponse response - ) { - - final ApiResponse elevatedUserResponse = - this.redirectUserNotElevated(request, response); - if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { - return elevatedUserResponse; - } - - return this.employeeUpdateCommand - .setEmployeeId(employeeId) - .setApiEmployee(employee) - .execute(); - } - - // Properties - @Autowired - private EmployeeCreateCommand employeeCreateCommand; - - @Autowired - private EmployeeUpdateCommand employeeUpdateCommand; - - @Autowired - private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; -} diff --git a/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java b/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java deleted file mode 100644 index 7f28f3be..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java +++ /dev/null @@ -1,44 +0,0 @@ -package edu.uark.registerapp.controllers; - -import java.util.Map; -import java.util.Optional; - -import javax.servlet.http.HttpServletRequest; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.servlet.ModelAndView; - -import edu.uark.registerapp.controllers.enums.ViewModelNames; -import edu.uark.registerapp.controllers.enums.ViewNames; -import edu.uark.registerapp.models.entities.ActiveUserEntity; - -@Controller -@RequestMapping(value = "/mainMenu") -public class MainMenuRouteController extends BaseRouteController { - @RequestMapping(method = RequestMethod.GET) - public ModelAndView start( - @RequestParam final Map queryParameters, - final HttpServletRequest request - ) { - - final Optional activeUserEntity = - this.getCurrentUser(request); - if (!activeUserEntity.isPresent()) { - return this.buildInvalidSessionResponse(); - } - - ModelAndView modelAndView = - this.setErrorMessageFromQueryString( - new ModelAndView(ViewNames.MAIN_MENU.getViewName()), - queryParameters); - - modelAndView.addObject( - ViewModelNames.IS_ELEVATED_USER.getValue(), - this.isElevatedUser(activeUserEntity.get())); - - return modelAndView; - } -} diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java b/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java index f6c4edc2..f6f35713 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java @@ -1,81 +1,35 @@ package edu.uark.registerapp.controllers; -import java.util.Map; -import java.util.Optional; import java.util.UUID; -import javax.servlet.http.HttpServletRequest; - import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import edu.uark.registerapp.commands.products.ProductQuery; import edu.uark.registerapp.controllers.enums.ViewModelNames; import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.Product; -import edu.uark.registerapp.models.entities.ActiveUserEntity; -import edu.uark.registerapp.models.enums.EmployeeClassification; @Controller @RequestMapping(value = "/productDetail") -public class ProductDetailRouteController extends BaseRouteController { +public class ProductDetailRouteController { @RequestMapping(method = RequestMethod.GET) - public ModelAndView start( - @RequestParam final Map queryParameters, - final HttpServletRequest request - ) { - final Optional activeUserEntity = - this.getCurrentUser(request); - if (!activeUserEntity.isPresent()) { - return this.buildInvalidSessionResponse(); - } else if (!this.isElevatedUser(activeUserEntity.get())) { - return this.buildNoPermissionsResponse( - ViewNames.PRODUCT_LISTING.getRoute()); - } - - final ModelAndView modelAndView = - this.setErrorMessageFromQueryString( - new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()), - queryParameters); - - modelAndView.addObject( - ViewModelNames.IS_ELEVATED_USER.getValue(), - true); - modelAndView.addObject( - ViewModelNames.PRODUCT.getValue(), - (new Product()).setLookupCode(StringUtils.EMPTY).setCount(0)); - - return modelAndView; + public ModelAndView start() { + return (new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName())) + .addObject( + ViewModelNames.PRODUCT.getValue(), + (new Product()).setLookupCode(StringUtils.EMPTY).setCount(0)); } @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ModelAndView startWithProduct( - @PathVariable final UUID productId, - @RequestParam final Map queryParameters, - final HttpServletRequest request - ) { - - final Optional activeUserEntity = - this.getCurrentUser(request); - if (!activeUserEntity.isPresent()) { - return this.buildInvalidSessionResponse(); - } - + public ModelAndView startWithProduct(@PathVariable final UUID productId) { final ModelAndView modelAndView = - this.setErrorMessageFromQueryString( - new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()), - queryParameters); - - modelAndView.addObject( - ViewModelNames.IS_ELEVATED_USER.getValue(), - EmployeeClassification.isElevatedUser( - activeUserEntity.get().getClassification())); + new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()); try { modelAndView.addObject( diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java b/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java index ed9a3414..3c2a1178 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java @@ -1,46 +1,23 @@ package edu.uark.registerapp.controllers; -import java.util.Map; -import java.util.Optional; - -import javax.servlet.http.HttpServletRequest; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import edu.uark.registerapp.commands.products.ProductsQuery; import edu.uark.registerapp.controllers.enums.ViewModelNames; import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.Product; -import edu.uark.registerapp.models.entities.ActiveUserEntity; @Controller -@RequestMapping(value = "/productListing") -public class ProductListingRouteController extends BaseRouteController { +@RequestMapping(value = "/") +public class ProductListingRouteController { @RequestMapping(method = RequestMethod.GET) - public ModelAndView showProductListing( - @RequestParam final Map queryParameters, - final HttpServletRequest request - ) { - - final Optional activeUserEntity = - this.getCurrentUser(request); - if (!activeUserEntity.isPresent()) { - return buildInvalidSessionResponse(); - } - + public ModelAndView showProductListing() { ModelAndView modelAndView = - this.setErrorMessageFromQueryString( - new ModelAndView(ViewNames.PRODUCT_LISTING.getViewName()), - queryParameters); - - modelAndView.addObject( - ViewModelNames.IS_ELEVATED_USER.getValue(), - this.isElevatedUser(activeUserEntity.get())); + new ModelAndView(ViewNames.PRODUCT_LISTING.getViewName()); try { modelAndView.addObject( diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java b/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java index 9f1be30f..0c6c5f62 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductRestController.java @@ -2,10 +2,6 @@ import java.util.UUID; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -17,30 +13,17 @@ import edu.uark.registerapp.commands.products.ProductCreateCommand; import edu.uark.registerapp.commands.products.ProductDeleteCommand; import edu.uark.registerapp.commands.products.ProductUpdateCommand; -import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.ApiResponse; import edu.uark.registerapp.models.api.Product; @RestController @RequestMapping(value = "/api/product") -public class ProductRestController extends BaseRestController { +public class ProductRestController { @RequestMapping(value = "/", method = RequestMethod.POST) public @ResponseBody ApiResponse createProduct( - @RequestBody final Product product, - final HttpServletRequest request, - final HttpServletResponse response + @RequestBody final Product product ) { - final ApiResponse elevatedUserResponse = - this.redirectUserNotElevated( - request, - response, - ViewNames.PRODUCT_LISTING.getRoute()); - - if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { - return elevatedUserResponse; - } - return this.productCreateCommand .setApiProduct(product) .execute(); @@ -49,21 +32,9 @@ public class ProductRestController extends BaseRestController { @RequestMapping(value = "/{productId}", method = RequestMethod.PUT) public @ResponseBody ApiResponse updateProduct( @PathVariable final UUID productId, - @RequestBody final Product product, - final HttpServletRequest request, - final HttpServletResponse response + @RequestBody final Product product ) { - final ApiResponse elevatedUserResponse = - this.redirectUserNotElevated( - request, - response, - ViewNames.PRODUCT_LISTING.getRoute()); - - if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { - return elevatedUserResponse; - } - return this.productUpdateCommand .setProductId(productId) .setApiProduct(product) @@ -72,21 +43,9 @@ public class ProductRestController extends BaseRestController { @RequestMapping(value = "/{productId}", method = RequestMethod.DELETE) public @ResponseBody ApiResponse deleteProduct( - @PathVariable final UUID productId, - final HttpServletRequest request, - final HttpServletResponse response + @PathVariable final UUID productId ) { - final ApiResponse elevatedUserResponse = - this.redirectUserNotElevated( - request, - response, - ViewNames.PRODUCT_LISTING.getRoute()); - - if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { - return elevatedUserResponse; - } - this.productDeleteCommand .setProductId(productId) .execute(); diff --git a/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java b/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java deleted file mode 100644 index 8ec7ef25..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java +++ /dev/null @@ -1,34 +0,0 @@ -package edu.uark.registerapp.controllers; - -import javax.servlet.http.HttpServletRequest; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -import edu.uark.registerapp.commands.activeUsers.ActiveUserDeleteCommand; -import edu.uark.registerapp.controllers.enums.ViewNames; -import edu.uark.registerapp.models.api.ApiResponse; - -@RestController -@RequestMapping(value = "/api") -public class SignInRestController extends BaseRestController { - @RequestMapping(value="/signOut", method = RequestMethod.DELETE) - public @ResponseBody ApiResponse removeActiveUser( - final HttpServletRequest request - ) { - - this.activeUserDeleteCommand - .setSessionKey(request.getSession().getId()) - .execute(); - - return (new ApiResponse()) - .setRedirectUrl(ViewNames.SIGN_IN.getRoute()); - } - - // Properties - @Autowired - private ActiveUserDeleteCommand activeUserDeleteCommand; -} diff --git a/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java b/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java deleted file mode 100644 index 749ace20..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java +++ /dev/null @@ -1,89 +0,0 @@ -package edu.uark.registerapp.controllers; - -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.servlet.ModelAndView; - -import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; -import edu.uark.registerapp.commands.employees.EmployeeSignInCommand; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.controllers.enums.QueryParameterNames; -import edu.uark.registerapp.controllers.enums.ViewModelNames; -import edu.uark.registerapp.controllers.enums.ViewNames; -import edu.uark.registerapp.models.api.EmployeeSignIn; - -@Controller -@RequestMapping(value = "/") -public class SignInRouteController extends BaseRouteController { - @RequestMapping(method = RequestMethod.GET) - public ModelAndView showSignIn( - @RequestParam final Map queryParameters - ) { - - try { - this.activeEmployeeExistsQuery.execute(); - } catch (NotFoundException e) { - return new ModelAndView( - REDIRECT_PREPEND.concat( - ViewNames.EMPLOYEE_DETAIL.getRoute())); - } - - ModelAndView modelAndView = - this.setErrorMessageFromQueryString( - new ModelAndView(ViewNames.SIGN_IN.getViewName()), - queryParameters); - - if (queryParameters.containsKey(QueryParameterNames.EMPLOYEE_ID.getValue())) { - modelAndView.addObject( - ViewModelNames.EMPLOYEE_ID.getValue(), - queryParameters.get(QueryParameterNames.EMPLOYEE_ID.getValue())); - } - - return modelAndView; - } - - @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public ModelAndView performSignIn( - EmployeeSignIn employeeSignIn, - HttpServletRequest request - ) { - - try { - this.employeeSignInCommand - .setSessionId(request.getSession().getId()) - .setEmployeeSignIn(employeeSignIn) - .execute(); - } catch (Exception e) { - ModelAndView modelAndView = - new ModelAndView(ViewNames.SIGN_IN.getViewName()); - - modelAndView.addObject( - ViewModelNames.ERROR_MESSAGE.getValue(), - e.getMessage()); - modelAndView.addObject( - ViewModelNames.EMPLOYEE_ID.getValue(), - employeeSignIn.getEmployeeId()); - - return modelAndView; - } - - return new ModelAndView( - REDIRECT_PREPEND.concat( - ViewNames.MAIN_MENU.getRoute())); - } - - // Properties - @Autowired - private EmployeeSignInCommand employeeSignInCommand; - - @Autowired - private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; -} diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java deleted file mode 100644 index 3f1bed87..00000000 --- a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java +++ /dev/null @@ -1,45 +0,0 @@ -package edu.uark.registerapp.controllers.enums; - -import java.util.HashMap; -import java.util.Map; - -public enum QueryParameterMessages { - NOT_DEFINED(-1, ""), - SESSION_NOT_ACTIVE(1001, "The current user's session is no longer active."), - NO_PERMISSIONS_TO_VIEW(1101, "You do not have permission to view this resource."), - NO_PERMISSIONS_FOR_ACTION(1102, "You do not have permission to perform this action."); - - public int getKey() { - return this.key; - } - public String getKeyAsString() { - return Integer.toString(this.key); - } - public String getMessage() { - return this.message; - } - - public static String mapMessage(final int key) { - if (valueMap == null) { - valueMap = new HashMap(); - - for (final QueryParameterMessages status : QueryParameterMessages.values()) { - valueMap.put(status.getKey(), status.getMessage()); - } - } - - return (valueMap.containsKey(key) - ? valueMap.get(key) - : QueryParameterMessages.NOT_DEFINED.getMessage()); - } - - private int key; - private String message; - - private static Map valueMap = null; - - private QueryParameterMessages(final int key, final String message) { - this.key = key; - this.message = message; - } -} diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java index d20b0100..988a9c79 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java @@ -2,8 +2,7 @@ public enum QueryParameterNames { NOT_DEFINED(""), - ERROR_CODE("errorCode"), - EMPLOYEE_ID("employeeId"); + ERROR_CODE("errorCode"); public String getValue() { return value; diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java index fefde2fd..7c39b102 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java @@ -3,12 +3,8 @@ public enum ViewModelNames { NOT_DEFINED(""), ERROR_MESSAGE("errorMessage"), - IS_ELEVATED_USER("isElevatedUser"), PRODUCTS("products"), // Product listing - PRODUCT("product"), // Product detail - EMPLOYEE_ID("employeeId"), // Sign in - EMPLOYEE("employee"), // Employee detail - EMPLOYEE_TYPES("employeeTypes"); + PRODUCT("product"); // Product detail public String getValue() { return value; diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java index 3e3e2ca2..cd1a1246 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java @@ -1,11 +1,8 @@ package edu.uark.registerapp.controllers.enums; public enum ViewNames { - SIGN_IN("signIn", "/"), - MAIN_MENU("mainMenu"), PRODUCT_DETAIL("productDetail"), - EMPLOYEE_DETAIL("employeeDetail"), - PRODUCT_LISTING("productListing"); + PRODUCT_LISTING("productListing", "/"); public String getRoute() { return this.route; diff --git a/src/main/java/edu/uark/registerapp/models/api/Employee.java b/src/main/java/edu/uark/registerapp/models/api/Employee.java deleted file mode 100644 index 6e313bfc..00000000 --- a/src/main/java/edu/uark/registerapp/models/api/Employee.java +++ /dev/null @@ -1,145 +0,0 @@ -package edu.uark.registerapp.models.api; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.UUID; - -import org.apache.commons.lang3.StringUtils; - -import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; -import edu.uark.registerapp.models.entities.EmployeeEntity; - -public class Employee extends ApiResponse { - private UUID id; - public UUID getId() { - return this.id; - } - public Employee setId(final UUID id) { - this.id = id; - return this; - } - - private String employeeId; - public String getEmployeeId() { - return this.employeeId; - } - public Employee setEmployeeId(final int employeeId) { - this.employeeId = EmployeeHelper.padEmployeeId(employeeId); - return this; - } - public Employee setEmployeeId(final String employeeId) { - this.employeeId = employeeId; - return this; - } - - private String firstName; - public String getFirstName() { - return this.firstName; - } - public Employee setFirstName(final String firstName) { - this.firstName = firstName; - return this; - } - - private String lastName; - public String getLastName() { - return this.lastName; - } - public Employee setLastName(final String lastName) { - this.lastName = lastName; - return this; - } - - private String password; - public String getPassword() { - return this.password; - } - public Employee setPassword(final String password) { - this.password = password; - return this; - } - - private boolean isActive; - public boolean getIsActive() { - return this.isActive; - } - public Employee setIsActive(final boolean isActive) { - this.isActive = isActive; - return this; - } - - private int classification; - public int getClassification() { - return this.classification; - } - public Employee setClassification(final int classification) { - this.classification = classification; - return this; - } - - private UUID managerId; - public UUID getManagerId() { - return this.managerId; - } - public Employee setManagerId(final UUID managerId) { - this.managerId = managerId; - return this; - } - - private String createdOn; - public String getCreatedOn() { - return this.createdOn; - } - public Employee setCreatedOn(final String createdOn) { - this.createdOn = createdOn; - return this; - } - public Employee setCreatedOn(final LocalDateTime createdOn) { - this.createdOn = - createdOn.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); - - return this; - } - - private boolean isInitialEmployee; - public boolean getIsInitialEmployee() { - return this.isInitialEmployee; - } - public Employee setIsInitialEmployee(final boolean isInitialEmployee) { - this.isInitialEmployee = isInitialEmployee; - return this; - } - - public Employee() { - super(); - - this.isActive = true; - this.id = new UUID(0, 0); - this.classification = -1; - this.isInitialEmployee = false; - this.managerId = new UUID(0, 0); - this.lastName = StringUtils.EMPTY; - this.password = StringUtils.EMPTY; - this.firstName = StringUtils.EMPTY; - this.employeeId = StringUtils.EMPTY; - - this.setCreatedOn(LocalDateTime.now()); - } - - public Employee(final EmployeeEntity employeeEntity) { - super(false); - - this.isInitialEmployee = false; - this.id = employeeEntity.getId(); - this.password = StringUtils.EMPTY; - this.isActive = employeeEntity.getIsActive(); - this.lastName = employeeEntity.getLastName(); - this.firstName = employeeEntity.getFirstName(); - this.managerId = employeeEntity.getManagerId(); - this.classification = employeeEntity.getClassification(); - this.employeeId = - EmployeeHelper.padEmployeeId(employeeEntity.getEmployeeId()); - - this.setCreatedOn(employeeEntity.getCreatedOn()); - } -} diff --git a/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java b/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java deleted file mode 100644 index e01e4f6c..00000000 --- a/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java +++ /dev/null @@ -1,28 +0,0 @@ -package edu.uark.registerapp.models.api; - -import org.apache.commons.lang3.StringUtils; - -public class EmployeeSignIn { - private String employeeId; - public String getEmployeeId() { - return this.employeeId; - } - public EmployeeSignIn setEmployeeId(final String employeeId) { - this.employeeId = employeeId; - return this; - } - - private String password; - public String getPassword() { - return this.password; - } - public EmployeeSignIn setPassword(final String password) { - this.password = password; - return this; - } - - public EmployeeSignIn() { - this.password = StringUtils.EMPTY; - this.employeeId = StringUtils.EMPTY; - } -} diff --git a/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java b/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java deleted file mode 100644 index fa9e8421..00000000 --- a/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java +++ /dev/null @@ -1,55 +0,0 @@ -package edu.uark.registerapp.models.api; - -import org.apache.commons.lang3.StringUtils; - -import edu.uark.registerapp.models.enums.EmployeeClassification; - -public class EmployeeType { - private int classification; - public int getClassification() { - return this.classification; - } - public EmployeeType setClassification(final int classification) { - this.classification = classification; - return this; - } - - private String displayLabel; - - public String getDisplayLabel() { - return this.displayLabel; - } - - public EmployeeType setDisplayLabel(final String displayLabel) { - this.displayLabel = displayLabel; - return this; - } - - public static EmployeeType[] allEmployeeTypes() { - final EmployeeClassification[] employeeClassifications = - EmployeeClassification.values(); - final EmployeeType[] employeeTypes = - new EmployeeType[employeeClassifications.length]; - - for (int i = 0; i < employeeClassifications.length; i++) { - employeeTypes[i] = new EmployeeType(employeeClassifications[i]); - } - - return employeeTypes; - } - - public EmployeeType() { - this(-1, StringUtils.EMPTY); - } - - public EmployeeType(final EmployeeClassification employeeClassification) { - this( - employeeClassification.getClassification(), - employeeClassification.getDisplayLabel()); - } - - public EmployeeType(final int classification, final String displayLabel) { - this.displayLabel = displayLabel; - this.classification = classification; - } -} diff --git a/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java deleted file mode 100644 index 6eba3528..00000000 --- a/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java +++ /dev/null @@ -1,91 +0,0 @@ -package edu.uark.registerapp.models.entities; - -import java.time.LocalDateTime; -import java.util.UUID; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.apache.commons.lang3.StringUtils; -import org.hibernate.annotations.Generated; -import org.hibernate.annotations.GenerationTime; - -@Entity -@Table(name="activeuser") -public class ActiveUserEntity { - @Id - @Column(name="id", updatable = false) - @GeneratedValue(strategy=GenerationType.AUTO) - private final UUID id; - - public UUID getId() { - return this.id; - } - - @Column(name = "employeeid") - private UUID employeeId; - - public UUID getEmployeeId() { - return this.employeeId; - } - - public ActiveUserEntity setEmployeeId(final UUID employeeId) { - this.employeeId = employeeId; - return this; - } - - @Column(name = "name") - private String name; - - public String getName() { - return this.name; - } - - public ActiveUserEntity setName(final String name) { - this.name = name; - return this; - } - - @Column(name = "classification") - private int classification; - - public int getClassification() { - return this.classification; - } - - public ActiveUserEntity setClassification(final int classification) { - this.classification = classification; - return this; - } - - @Column(name = "sessionkey") - private String sessionKey; - - public String getSessionKey() { - return this.sessionKey; - } - - public ActiveUserEntity setSessionKey(final String sessionKey) { - this.sessionKey = sessionKey; - return this; - } - - @Column(name="createdon", insertable=false, updatable = false) - @Generated(GenerationTime.INSERT) - private LocalDateTime createdOn; - public LocalDateTime getCreatedOn() { - return this.createdOn; - } - - public ActiveUserEntity() { - this.id = new UUID(0, 0); - this.classification = -1; - this.name = StringUtils.EMPTY; - this.employeeId = new UUID(0, 0); - this.sessionKey = StringUtils.EMPTY; - } -} diff --git a/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java b/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java deleted file mode 100644 index 324e5b31..00000000 --- a/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java +++ /dev/null @@ -1,164 +0,0 @@ -package edu.uark.registerapp.models.entities; - -import java.time.LocalDateTime; -import java.util.UUID; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.apache.commons.lang3.StringUtils; -import org.hibernate.annotations.Generated; -import org.hibernate.annotations.GenerationTime; - -import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; -import edu.uark.registerapp.models.api.Employee; - -@Entity -@Table(name="employee") -public class EmployeeEntity { - @Id - @Column(name="id", updatable = false) - @GeneratedValue(strategy=GenerationType.AUTO) - private final UUID id; - - public UUID getId() { - return this.id; - } - - @Column(name = "employeeid", insertable = false, updatable = false) - @Generated(GenerationTime.INSERT) - private int employeeId; - - public int getEmployeeId() { - return this.employeeId; - } - - @Column(name = "firstname") - private String firstName; - - public String getFirstName() { - return this.firstName; - } - - public EmployeeEntity setFirstName(final String firstName) { - this.firstName = firstName; - return this; - } - - @Column(name = "lastname") - private String lastName; - - public String getLastName() { - return this.lastName; - } - - public EmployeeEntity setLastName(final String lastName) { - this.lastName = lastName; - return this; - } - - @Column(name = "password") - private byte[] password; - - public byte[] getPassword() { - return this.password; - } - - public EmployeeEntity setPassword(final byte[] password) { - this.password = password; - return this; - } - - @Column(name = "active") - private boolean isActive; - - public boolean getIsActive() { - return this.isActive; - } - - public EmployeeEntity setIsActive(final boolean isActive) { - this.isActive = isActive; - return this; - } - - @Column(name = "classification") - private int classification; - - public int getClassification() { - return this.classification; - } - - public EmployeeEntity setClassification(final int classification) { - this.classification = classification; - return this; - } - - @Column(name = "managerid") - private UUID managerId; - - public UUID getManagerId() { - return this.managerId; - } - - public EmployeeEntity setManagerId(final UUID managerId) { - this.managerId = managerId; - return this; - } - - @Column(name = "createdon", insertable = false, updatable = false) - @Generated(GenerationTime.INSERT) - private LocalDateTime createdOn; - - public LocalDateTime getCreatedOn() { - return this.createdOn; - } - - public Employee synchronize(final Employee apiEmployee) { - this.setIsActive(apiEmployee.getIsActive()); - this.setLastName(apiEmployee.getLastName()); - this.setFirstName(apiEmployee.getFirstName()); - this.setClassification(apiEmployee.getClassification()); - if (apiEmployee.getManagerId() != null) { - this.setManagerId(apiEmployee.getManagerId()); - } - if (!StringUtils.isBlank(apiEmployee.getPassword())) { - this.setPassword( - EmployeeHelper.hashPassword( - apiEmployee.getPassword())); - } - - apiEmployee.setId(this.getId()); - apiEmployee.setCreatedOn(this.getCreatedOn()); - apiEmployee.setEmployeeId(this.getEmployeeId()); - - return apiEmployee; - } - - public EmployeeEntity() { - this.employeeId = -1; - this.isActive = false; - this.id = new UUID(0, 0); - this.classification = -1; - this.password = new byte[0]; - this.managerId = new UUID(0, 0); - this.lastName = StringUtils.EMPTY; - this.firstName = StringUtils.EMPTY; - } - - public EmployeeEntity(final Employee apiEmployee) { - this.id = new UUID(0, 0); - this.isActive = apiEmployee.getIsActive(); - this.lastName = apiEmployee.getLastName(); - this.firstName = apiEmployee.getFirstName(); - this.classification = apiEmployee.getClassification(); - this.password = EmployeeHelper.hashPassword(apiEmployee.getPassword()); - this.managerId = ( - (apiEmployee.getManagerId() != null) - ? apiEmployee.getManagerId() - : new UUID(0, 0)); - } -} diff --git a/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java b/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java deleted file mode 100644 index 751c80ef..00000000 --- a/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java +++ /dev/null @@ -1,58 +0,0 @@ -package edu.uark.registerapp.models.enums; - -import java.util.HashMap; -import java.util.Map; - -public enum EmployeeClassification { - NOT_DEFINED(-1, "Not Selected"), - CASHIER(101, "Cashier"), - SHIFT_MANAGER(501, "Shift Manager"), - GENERAL_MANAGER(701, "General Manager"); - - public int getClassification() { - return this.classification; - } - - public String getDisplayLabel() { - return this.displayLabel; - } - - public static EmployeeClassification map(final int key) { - if (valueMap == null) { - valueMap = new HashMap(); - - for (final EmployeeClassification employeeClassification : EmployeeClassification.values()) { - valueMap.put( - employeeClassification.getClassification(), - employeeClassification); - } - } - - return ((valueMap.containsKey(key) - ? valueMap.get(key) - : EmployeeClassification.NOT_DEFINED)); - } - - public static boolean isElevatedUser(final int classification) { - final EmployeeClassification employeeClassification = - EmployeeClassification.map(classification); - - return ( - (employeeClassification == EmployeeClassification.GENERAL_MANAGER) - || (employeeClassification == EmployeeClassification.SHIFT_MANAGER)); - } - - private int classification; - private String displayLabel; - - private static Map valueMap = null; - - private EmployeeClassification( - final int classification, - final String displayLabel - ) { - - this.displayLabel = displayLabel; - this.classification = classification; - } -} diff --git a/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java deleted file mode 100644 index 53746cee..00000000 --- a/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package edu.uark.registerapp.models.repositories; - -import java.util.Optional; -import java.util.UUID; - -import org.springframework.data.repository.CrudRepository; - -import edu.uark.registerapp.models.entities.ActiveUserEntity; - -public interface ActiveUserRepository extends CrudRepository { - Optional findByEmployeeId(UUID employeeId); - Optional findBySessionKey(String sessionKey); -} diff --git a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java deleted file mode 100644 index 8ba4b81f..00000000 --- a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java +++ /dev/null @@ -1,15 +0,0 @@ -package edu.uark.registerapp.models.repositories; - -import java.util.Optional; -import java.util.UUID; - -import org.springframework.data.repository.CrudRepository; - -import edu.uark.registerapp.models.entities.EmployeeEntity; - -public interface EmployeeRepository extends CrudRepository { - boolean existsByIsActive(boolean isActive); - boolean existsByEmployeeId(int employeeId); - Optional findById(UUID id); - Optional findByEmployeeId(int employeeId); -} diff --git a/src/main/resources/static/images/Sign-Out.png b/src/main/resources/static/images/Sign-Out.png deleted file mode 100644 index 82fe8695ab00a4cdbd2b3f95f065fd7acbd108ec..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 417 zcmV;S0bc%zP)7GIp>_a(3m+}_QaKE&z3o3q#8Dyh{QRup`m9ZI--K<7}2X0QOT_6(UfB~ zfFo0iTM!k^g5q{n0XmBNtqSl%ad9?U37>C(0R|XgKr-NscF2HNf}kBL;Dz{~wmv|? zI=>#2+PS61|;Y%W<1K>^~fyX^@&QOFD-2!R*p^-#O=;bI$z%Lh{J>>1jl;00000 LNkvXXu0mjf0)eri diff --git a/src/main/resources/static/scripts/employeeDetail.js b/src/main/resources/static/scripts/employeeDetail.js deleted file mode 100644 index b5d7d2f8..00000000 --- a/src/main/resources/static/scripts/employeeDetail.js +++ /dev/null @@ -1,184 +0,0 @@ -let hideEmployeeSavedAlertTimer = undefined; - -document.addEventListener("DOMContentLoaded", () => { - document.getElementById("saveButton") - .addEventListener("click", saveActionClick); - - const employeeFirstNameEditElement = - getEmployeeFirstNameEditElement(); - employeeFirstNameEditElement.focus(); - employeeFirstNameEditElement.select(); -}); - -// Save -function saveActionClick(event) { - if (!validateSave()) { - return; - } - - const saveActionElement = event.target; - saveActionElement.disabled = true; - - const employeeId = getEmployeeId(); - const employeeIdIsDefined = (employeeId.trim() !== ""); - const saveActionUrl = ("/api/employee/" - + (employeeIdIsDefined ? employeeId : "")); - const saveEmployeeRequest = { - id: employeeId, - managerId: getEmployeeManagerId(), - lastName: getEmployeeLastNameEditElement().value, - password: getEmployeePasswordEditElement().value, - firstName: getEmployeeFirstNameEditElement().value, - classification: getEmployeeTypeSelectElement().value - }; - - if (employeeIdIsDefined) { - ajaxPatch(saveActionUrl, saveEmployeeRequest, (callbackResponse) => { - saveActionElement.disabled = false; - - if (isSuccessResponse(callbackResponse)) { - completeSaveAction(callbackResponse); - } - }); - } else { - ajaxPost(saveActionUrl, saveEmployeeRequest, (callbackResponse) => { - saveActionElement.disabled = false; - - if (isSuccessResponse(callbackResponse)) { - completeSaveAction(callbackResponse); - } - }); - } -} - -function validateSave() { - const firstNameEditElement = getEmployeeFirstNameEditElement(); - if (firstNameEditElement.value.trim() === "") { - displayError("Please provide a valid employee first name."); - firstNameEditElement.focus(); - firstNameEditElement.select(); - return false; - } - - const lastNameEditElement = getEmployeeLastNameEditElement(); - if (lastNameEditElement.value.trim() === "") { - displayError("Please provide a valid employee last name."); - lastNameEditElement.focus(); - lastNameEditElement.select(); - return false; - } - - const passwordEditElement = getEmployeePasswordEditElement(); - if (passwordEditElement.value.trim() === "") { - displayError("Please provide a valid employee password."); - passwordEditElement.focus(); - passwordEditElement.select(); - return false; - } - - if (passwordEditElement.value !== getEmployeeConfirmPassword()) { - displayError("Passwords do not match."); - passwordEditElement.focus() - passwordEditElement.select(); - return false; - } - - const employeeTypeSelectElement = getEmployeeTypeSelectElement(); - if (!employeeTypeSelectElement.closest("tr").classList.contains("hidden")) { - if (employeeTypeSelectElement.value <= 0) { - displayError("Please provide a valid employee Type."); - employeeTypeSelectElement.focus(); - return false; - } - } - - return true; -} - -function completeSaveAction(callbackResponse) { - if (callbackResponse.data == null) { - return; - } - - if ((callbackResponse.data.redirectUrl != null) - && (callbackResponse.data.redirectUrl !== "")) { - - window.location.replace(callbackResponse.data.redirectUrl); - return; - } - - displayEmployeeSavedAlertModal(); - - const employeeEmployeeIdElement = getEmployeeEmployeeIdElement(); - const employeeEmployeeIdRowElement = employeeEmployeeIdElement.closest("tr"); - if (employeeEmployeeIdRowElement.classList.contains("hidden")) { - setEmployeeId(callbackResponse.data.id); - employeeEmployeeIdElement.value = callbackResponse.data.employeeId; - employeeEmployeeIdRowElement.classList.remove("hidden"); - } -} - -function displayEmployeeSavedAlertModal() { - if (hideEmployeeSavedAlertTimer) { - clearTimeout(hideEmployeeSavedAlertTimer); - } - - const savedAlertModalElement = getSavedAlertModalElement(); - savedAlertModalElement.style.display = "none"; - savedAlertModalElement.style.display = "block"; - - hideEmployeeSavedAlertTimer = setTimeout(hideEmployeeSavedAlertModal, 1200); -} - -function hideEmployeeSavedAlertModal() { - if (hideEmployeeSavedAlertTimer) { - clearTimeout(hideEmployeeSavedAlertTimer); - } - - getSavedAlertModalElement().style.display = "none"; -} -// End save - -//Getters and setters -function getEmployeeId() { - return document.getElementById("employeeId").value; -} -function setEmployeeId(employeeId) { - document.getElementById("employeeId").value = employeeId; -} - -function getEmployeeManagerId() { - return document.getElementById("employeeManagerId").value; -} - -function getEmployeeEmployeeId() { - return getEmployeeEmployeeIdElement().value; -} -function getEmployeeEmployeeIdElement() { - return document.getElementById("employeeEmployeeId"); -} - -function getSavedAlertModalElement() { - return document.getElementById("employeeSavedAlertModal"); -} - -function getEmployeeFirstNameEditElement() { - return document.getElementById("employeeFirstName"); -} - -function getEmployeeLastNameEditElement() { - return document.getElementById("employeeLastName"); -} - -function getEmployeePasswordEditElement() { - return document.getElementById("employeePassword"); -} - -function getEmployeeConfirmPassword() { - return document.getElementById("employeeConfirmPassword").value; -} - -function getEmployeeTypeSelectElement() { - return document.getElementById("employeeType"); -} -//End getters and setters diff --git a/src/main/resources/static/scripts/mainMenu.js b/src/main/resources/static/scripts/mainMenu.js deleted file mode 100644 index 3b16c3f7..00000000 --- a/src/main/resources/static/scripts/mainMenu.js +++ /dev/null @@ -1,43 +0,0 @@ -document.addEventListener("DOMContentLoaded", function(event) { - getStartTransactionActionElement().addEventListener( - "click", - () => { displayError("Functionality has not yet been implemented."); }); - - getViewProductsActionElement().addEventListener( - "click", - () => { window.location.assign("/productListing"); }); - - getCreateEmployeeActionElement().addEventListener( - "click", - () => { window.location.assign("/employeeDetail"); }); - - getProductSalesReportActionElement().addEventListener( - "click", - () => { displayError("Functionality has not yet been implemented."); }); - - getCashierSalesReportActionElement().addEventListener( - "click", - () => { displayError("Functionality has not yet been implemented."); }); -}); - -// Getters and setters -function getViewProductsActionElement() { - return document.getElementById("viewProductsButton"); -} - -function getCreateEmployeeActionElement() { - return document.getElementById("createEmployeeButton"); -} - -function getStartTransactionActionElement() { - return document.getElementById("startTransactionButton"); -} - -function getProductSalesReportActionElement() { - return document.getElementById("productSalesReportButton"); -} - -function getCashierSalesReportActionElement() { - return document.getElementById("cashierSalesReportButton"); -} -// End getters and setters diff --git a/src/main/resources/static/scripts/master.js b/src/main/resources/static/scripts/master.js index c61d3563..88b039d0 100644 --- a/src/main/resources/static/scripts/master.js +++ b/src/main/resources/static/scripts/master.js @@ -1,10 +1,3 @@ -document.addEventListener("DOMContentLoaded", () => { - const signOutActionElement = getSignOutActionElement(); - if (signOutActionElement != null) { - signOutActionElement.addEventListener("click", signOutActionClickHandler); - } -}); - // AJAX function ajaxGet(resourceRelativeUri, callback) { return ajax(resourceRelativeUri, "GET", null, callback); @@ -165,10 +158,6 @@ function displayError(errorMessage) { // End display error message //Getters and setters -function getSignOutActionElement() { - return document.getElementById("signOutImage"); -} - function getErrorMessageContainerElement() { return document.getElementById("error"); } @@ -177,18 +166,3 @@ function getErrorMessageDisplayElement() { return document.getElementById("errorMessage"); } // End getters and setters - -//Sign out -function signOutActionClickHandler() { - ajaxDelete("/api/signOut", (callbackResponse) => { - if ((callbackResponse.data != null) - && (callbackResponse.data.redirectUrl != null) - && (callbackResponse.data.redirectUrl !== "")) { - - window.location.replace(callbackResponse.data.redirectUrl); - } else { - window.location.replace("/"); - } - }); -} -//End sign out \ No newline at end of file diff --git a/src/main/resources/static/scripts/signIn.js b/src/main/resources/static/scripts/signIn.js deleted file mode 100644 index e7b14144..00000000 --- a/src/main/resources/static/scripts/signIn.js +++ /dev/null @@ -1,43 +0,0 @@ -document.addEventListener("DOMContentLoaded", function(event) { - const employeeIdEditElement = getEmployeeIdEditElement(); - employeeIdEditElement.focus(); - employeeIdEditElement.select(); -}); - -function validateForm() { - const employeeIdEditElement = getEmployeeIdEditElement(); - if (isNaN(Number(employeeIdEditElement.value)) - || (Number(employeeIdEditElement.value) <= 0)) { - - displayError("Please provide a valid employee ID."); - - employeeIdEditElement.focus(); - employeeIdEditElement.select(); - - return false; - } - - const passwordEditElement = getPasswordEditElement(); - if ((passwordEditElement.value == null) - || (passwordEditElement.value.trim() === "")) { - - displayError("Please provide a valid password. It may not be blank."); - - passwordEditElement.focus(); - passwordEditElement.select(); - - return false; - } - - return true; -} - -//Getters and setters -function getPasswordEditElement() { - return document.getElementById("password"); -} - -function getEmployeeIdEditElement() { - return document.getElementById("employeeId"); -} -//End getters and setters diff --git a/src/main/resources/static/styles/master.css b/src/main/resources/static/styles/master.css index f35bb48a..2b060e6d 100644 --- a/src/main/resources/static/styles/master.css +++ b/src/main/resources/static/styles/master.css @@ -30,24 +30,6 @@ td.hidden { display: none } -div.footer { - left: 0; - right: 0; - bottom: 0; - position: absolute; -} - -img.footer { - z-index: 1; - height: 50px; - text-decoration: none; - display: inline-block; - cursor: pointer; - position: fixed; - right: 20; - bottom: 20; -} - div.inputContent, form.inputContent { display: inline-block; diff --git a/src/main/resources/templates/employeeDetail.html b/src/main/resources/templates/employeeDetail.html deleted file mode 100644 index 85b4cb05..00000000 --- a/src/main/resources/templates/employeeDetail.html +++ /dev/null @@ -1,104 +0,0 @@ - - - - Register - Employee - - - - - - - - - - -
-

Employee Detail

-
- -
-
-

-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Employee ID: - -
First Name: - -
Last Name: - -
Password: - -
Confirm Password: - -
Employee Type: - -
-
- -
-
- -
- - -
- - - - - - \ No newline at end of file diff --git a/src/main/resources/templates/mainMenu.html b/src/main/resources/templates/mainMenu.html deleted file mode 100644 index 706fc531..00000000 --- a/src/main/resources/templates/mainMenu.html +++ /dev/null @@ -1,62 +0,0 @@ - - - - Register - Main Menu - - - - - - - - - - -
-

Main Menu

-
- -
-
-

-
- - -
- - - - - - - - \ No newline at end of file diff --git a/src/main/resources/templates/productDetail.html b/src/main/resources/templates/productDetail.html index 453d1041..ac183115 100644 --- a/src/main/resources/templates/productDetail.html +++ b/src/main/resources/templates/productDetail.html @@ -34,20 +34,20 @@

Lookup Code: - + Count: - + -
+

@@ -68,10 +68,6 @@

- - diff --git a/src/main/resources/templates/productListing.html b/src/main/resources/templates/productListing.html index 6c374f83..c4b758ae 100644 --- a/src/main/resources/templates/productListing.html +++ b/src/main/resources/templates/productListing.html @@ -22,7 +22,7 @@

-
+ @@ -54,10 +54,6 @@

- - diff --git a/src/main/resources/templates/signIn.html b/src/main/resources/templates/signIn.html deleted file mode 100644 index 6f4c3994..00000000 --- a/src/main/resources/templates/signIn.html +++ /dev/null @@ -1,41 +0,0 @@ - - - - Register - Sign In - - - - - - - - - - -
-

Sign In

-
- -
-
-

-
- -
-
- -
-
-
- -
-

-
- -
-
-
- - - - \ No newline at end of file From 4d0835277f5d31ae2ce4295cfe89af09bb0f74c6 Mon Sep 17 00:00:00 2001 From: bthiele88 <77599601+bthiele88@users.noreply.github.com> Date: Sun, 28 Feb 2021 19:29:31 -0600 Subject: [PATCH 04/15] Create EmployeeSignIn.java --- .../models/api/EmployeeSignIn.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java diff --git a/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java b/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java new file mode 100644 index 00000000..fa2a9279 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java @@ -0,0 +1,35 @@ +// Sprint 2 - Task 4. Assigned to Ben Thiele. Taken from sprint 2 example with slight reorganization. +package edu.uark.registerapp.models.api; + +import org.apache.commons.lang3.StringUtils; + +public class EmployeeSignIn { + // Instance Variables + private String employeeId; + private String password; + + // Constructor - Sets strings to empty. + public EmployeeSignIn() { + this.password = StringUtils.EMPTY; + this.employeeId = StringUtils.EMPTY; + } + + // Setters and Getters + public String getEmployeeId() { + return this.employeeId; + } + + public EmployeeSignIn setEmployeeId(final String employeeId) { + this.employeeId = employeeId; + return this; + } + + public String getPassword() { + return this.password; + } + + public EmployeeSignIn setPassword(final String password) { + this.password = password; + return this; + } +} From fffd8ddf8147f1b9bbe71ef0cee8e2a40acdda0d Mon Sep 17 00:00:00 2001 From: Jodicd Date: Mon, 1 Mar 2021 16:00:33 -0600 Subject: [PATCH 05/15] JM task 7 --- .../commands/employees/CreateEmployee.java | 96 ++++++++++ .../commands/employees/DeleteEmployee.java | 43 +++++ .../commands/employees/EmployeeByIdQuery.java | 45 +++++ .../commands/employees/UpdateEmployee.java | 81 +++++++++ .../employees/helpers/EmployeeHelper.java | 24 +++ .../uark/registerapp/models/api/Employee.java | 146 ++++++++++++++++ .../models/entities/ActiveUserEntity.java | 92 ++++++++++ .../models/entities/EmployeeEntity.java | 164 ++++++++++++++++++ .../models/enums/EmployeeClassification.java | 58 +++++++ .../repositories/ActiveUserRepository.java | 15 ++ .../repositories/EmployeeRepository.java | 15 ++ .../resources/templates/employeeDetail.html | 104 +++++++++++ 12 files changed, 883 insertions(+) create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/CreateEmployee.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/DeleteEmployee.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/UpdateEmployee.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java create mode 100644 src/main/java/edu/uark/registerapp/models/api/Employee.java create mode 100644 src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java create mode 100644 src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java create mode 100644 src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java create mode 100644 src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java create mode 100644 src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java create mode 100644 src/main/resources/templates/employeeDetail.html diff --git a/src/main/java/edu/uark/registerapp/commands/employees/CreateEmployee.java b/src/main/java/edu/uark/registerapp/commands/employees/CreateEmployee.java new file mode 100644 index 00000000..a63ea746 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/CreateEmployee.java @@ -0,0 +1,96 @@ +// This will create a new employee + +package edu.uark.registerapp.commands.employees; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class CreateEmployee implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + // If this is the first employee to be added and make them the general manager + if (this.isInitialEmployee) { + this.apiEmployee.setClassification( + EmployeeClassification.GENERAL_MANAGER.getClassification()); + } + + // Create a new ENTITY object from the API object details. + final EmployeeEntity employeeEntity = + this.employeeRepository.save(new EmployeeEntity(this.apiEmployee)); + + // Synchronize information generated by the database upon INSERT. + this.apiEmployee.setId(employeeEntity.getId()); + // Only send the password over the network when modifying the database. + this.apiEmployee.setPassword(StringUtils.EMPTY); + this.apiEmployee.setCreatedOn(employeeEntity.getCreatedOn()); + this.apiEmployee.setEmployeeId( + EmployeeHelper.padEmployeeId( + employeeEntity.getEmployeeId())); + + return this.apiEmployee; + } + + // Helper methods + // This checks the fields on the view. + // Will not accept if any of the fields are blank. + private void validateProperties() { + if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { + throw new UnprocessableEntityException("first name"); + } + if (StringUtils.isBlank(this.apiEmployee.getLastName())) { + throw new UnprocessableEntityException("last name"); + } + if (StringUtils.isBlank(this.apiEmployee.getPassword())) { + throw new UnprocessableEntityException("password"); + } + + // If there is a single employee in the DB then select a position for the new addition + if (!this.isInitialEmployee + && (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED)) { + + throw new UnprocessableEntityException("classification"); + } + } + + // Getters and Setters for the new employee + private Employee apiEmployee; + public Employee getApiEmployee() { + return this.apiEmployee; + } + public CreateEmployee setApiEmployee(final Employee apiEmployee) { + this.apiEmployee = apiEmployee; + return this; + } + + // Getters and Setters for the first employee, the general manager. + private boolean isInitialEmployee; + public boolean getIsInitialEmployee() { + return this.isInitialEmployee; + } + public CreateEmployee setIsInitialEmployee( + final boolean isInitialEmployee + ) { + + this.isInitialEmployee = isInitialEmployee; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; + + public CreateEmployee() { + this.isInitialEmployee = false; + } +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/commands/employees/DeleteEmployee.java b/src/main/java/edu/uark/registerapp/commands/employees/DeleteEmployee.java new file mode 100644 index 00000000..469ee2f8 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/DeleteEmployee.java @@ -0,0 +1,43 @@ +// This will delete an employee by their employee ID + +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import edu.uark.registerapp.commands.VoidCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class DeleteEmployee implements VoidCommandInterface { + @Transactional + @Override + public void execute() { + final Optional employeeEntity = + this.employeeRepository.findById(this.employeeId); // Find the employee first + if (!employeeEntity.isPresent()) { // No record with the associated record ID exists in the database. + throw new NotFoundException("Employee"); + } + + this.employeeRepository.delete(employeeEntity.get()); // Delete the employee if found + } + + // Getters and Setters + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public DeleteEmployee setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java new file mode 100644 index 00000000..8aafdc22 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java @@ -0,0 +1,45 @@ +// Last updated 02/20/2021 by Jodi Mitchell +// This file grabs a specific employee by their ID +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeByIdQuery implements ResultCommandInterface +{ + @Override + public Employee execute() + { + + final Optional employeeEntity = + this.employeeRepository.findByEmployeeId(this.employeeId); // This is throwing errors because the + Checking to see if the employee exist. // operations in EmployeeRepository.java are + if (employeeEntity.isPresent()){ // commented out. I had to do this so the app + return new Employee(employeeEntity.get()); // will run as expected. + } else { // I think Once we get everything put together + throw new NotFoundException("Employee"); // with the routers we can uncomment those // functions. + } + } + + // Getters and Setters + private int employeeId; + public int getEmployeeId() { + return this.employeeId; + } + public EmployeeByIdQuery setemployeeId(final int employeeId) { + this.employeeId = employeeId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/UpdateEmployee.java b/src/main/java/edu/uark/registerapp/commands/employees/UpdateEmployee.java new file mode 100644 index 00000000..c0d044fa --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/UpdateEmployee.java @@ -0,0 +1,81 @@ +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; + +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class UpdateEmployee implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + this.updateEmployeeEntity(); + + return this.apiEmployee; + } + + // Exceptions for empty fields + private void validateProperties() { + if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { + throw new UnprocessableEntityException("first name"); + } + if (StringUtils.isBlank(this.apiEmployee.getLastName())) { + throw new UnprocessableEntityException("last name"); + } + if (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED) { + throw new UnprocessableEntityException("classification"); + } + } + + @Transactional + private void updateEmployeeEntity() { + final Optional queriedEmployeeEntity = + this.employeeRepository.findById(this.employeeId); + + if (!queriedEmployeeEntity.isPresent()) { + throw new NotFoundException("Employee"); // No record with the associated record ID exists in the database. + } + + this.apiEmployee = queriedEmployeeEntity.get() + .synchronize(this.apiEmployee); // Synchronize any incoming changes for UPDATE to the database. + + this.employeeRepository.save(queriedEmployeeEntity.get()); // Write, via an UPDATE, any changes to the database. + } + + // Getter and Setters for the universally unique Identifies that tuple + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public UpdateEmployee setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + // Getters and Setters for the API + private Employee apiEmployee; + public Employee getApiEmployee() { + return this.apiEmployee; + } + public UpdateEmployee setApiEmployee(final Employee apiEmployee) { + this.apiEmployee = apiEmployee; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java new file mode 100644 index 00000000..1f1d3821 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java @@ -0,0 +1,24 @@ +package edu.uark.registerapp.commands.employees.helpers; + +import org.apache.commons.lang3.StringUtils; + +public class EmployeeHelper { + public static String padEmployeeId(final int employeeId) { + final String employeeIdAsString = Integer.toString(employeeId); + + return ((employeeIdAsString.length() < EMPLOYEE_ID_MAXIMUM_LENGTH) + ? StringUtils.leftPad( + employeeIdAsString, + EMPLOYEE_ID_MAXIMUM_LENGTH, + "0") + : employeeIdAsString); + } + + public static byte[] hashPassword(final String password) { + // TODO: Hash the password using a MessageDigest. An example can be found at http://tutorials.jenkov.com/java-cryptography/messagedigest.html + + return new byte[0]; + } + + private static final int EMPLOYEE_ID_MAXIMUM_LENGTH = 5; +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/models/api/Employee.java b/src/main/java/edu/uark/registerapp/models/api/Employee.java new file mode 100644 index 00000000..2f3726e1 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/api/Employee.java @@ -0,0 +1,146 @@ +// The API that talks to DB about the entity employee and gives back the data to the user. +package edu.uark.registerapp.models.api; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.UUID; + +import org.apache.commons.lang3.StringUtils; + +// import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +// import edu.uark.registerapp.models.entities.EmployeeEntity; + +public class Employee extends ApiResponse { + private UUID id; + public UUID getId() { + return this.id; + } + public Employee setId(final UUID id) { + this.id = id; + return this; + } + + private String employeeId; + public String getEmployeeId() { + return this.employeeId; + } + // public Employee setEmployeeId(final int employeeId) { + // this.employeeId = EmployeeHelper.padEmployeeId(employeeId); + // return this; + // } + public Employee setEmployeeId(final String employeeId) { + this.employeeId = employeeId; + return this; + } + + private String firstName; + public String getFirstName() { + return this.firstName; + } + public Employee setFirstName(final String firstName) { + this.firstName = firstName; + return this; + } + + private String lastName; + public String getLastName() { + return this.lastName; + } + public Employee setLastName(final String lastName) { + this.lastName = lastName; + return this; + } + + private String password; + public String getPassword() { + return this.password; + } + public Employee setPassword(final String password) { + this.password = password; + return this; + } + + private boolean isActive; + public boolean getIsActive() { + return this.isActive; + } + public Employee setIsActive(final boolean isActive) { + this.isActive = isActive; + return this; + } + + private int classification; + public int getClassification() { + return this.classification; + } + public Employee setClassification(final int classification) { + this.classification = classification; + return this; + } + + private UUID managerId; + public UUID getManagerId() { + return this.managerId; + } + public Employee setManagerId(final UUID managerId) { + this.managerId = managerId; + return this; + } + + private String createdOn; + public String getCreatedOn() { + return this.createdOn; + } + public Employee setCreatedOn(final String createdOn) { + this.createdOn = createdOn; + return this; + } + public Employee setCreatedOn(final LocalDateTime createdOn) { + this.createdOn = + createdOn.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); + + return this; + } + + private boolean isInitialEmployee; + public boolean getIsInitialEmployee() { + return this.isInitialEmployee; + } + public Employee setIsInitialEmployee(final boolean isInitialEmployee) { + this.isInitialEmployee = isInitialEmployee; + return this; + } + + public Employee() { + super(); + + this.isActive = true; + this.id = new UUID(0, 0); + this.classification = -1; + this.isInitialEmployee = false; + this.managerId = new UUID(0, 0); + this.lastName = StringUtils.EMPTY; + this.password = StringUtils.EMPTY; + this.firstName = StringUtils.EMPTY; + this.employeeId = StringUtils.EMPTY; + + this.setCreatedOn(LocalDateTime.now()); + } + + public Employee(final edu.uark.registerapp.models.entities.EmployeeEntity employeeEntity) { + super(false); + + this.isInitialEmployee = false; + this.id = employeeEntity.getId(); + this.password = StringUtils.EMPTY; + this.isActive = employeeEntity.getIsActive(); + this.lastName = employeeEntity.getLastName(); + this.firstName = employeeEntity.getFirstName(); + this.managerId = employeeEntity.getManagerId(); + this.classification = employeeEntity.getClassification(); + // this.employeeId = + // EmployeeHelper.padEmployeeId(employeeEntity.getEmployeeId()); + + this.setCreatedOn(employeeEntity.getCreatedOn()); + } +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java new file mode 100644 index 00000000..00b509ac --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java @@ -0,0 +1,92 @@ +// This is how we talk to the DB in the relation useractive +package edu.uark.registerapp.models.entities; + +import java.time.LocalDateTime; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.annotations.Generated; +import org.hibernate.annotations.GenerationTime; + +@Entity +@Table(name="activeuser") // In the acitveuser relation +public class ActiveUserEntity { + @Id + @Column(name="id", updatable = false) // The id can not be changed + @GeneratedValue(strategy=GenerationType.AUTO) + private final UUID id; + + public UUID getId() { + return this.id; + } + + @Column(name = "employeeid") + private UUID employeeId; + + public UUID getEmployeeId() { + return this.employeeId; + } + + public ActiveUserEntity setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + @Column(name = "name") + private String name; + + public String getName() { + return this.name; + } + + public ActiveUserEntity setName(final String name) { + this.name = name; + return this; + } + + @Column(name = "classification") + private int classification; + + public int getClassification() { + return this.classification; + } + + public ActiveUserEntity setClassification(final int classification) { + this.classification = classification; + return this; + } + + @Column(name = "sessionkey") + private String sessionKey; + + public String getSessionKey() { + return this.sessionKey; + } + + public ActiveUserEntity setSessionKey(final String sessionKey) { + this.sessionKey = sessionKey; + return this; + } + + @Column(name="createdon", insertable=false, updatable = false) + @Generated(GenerationTime.INSERT) + private LocalDateTime createdOn; + public LocalDateTime getCreatedOn() { + return this.createdOn; + } + + public ActiveUserEntity() { + this.id = new UUID(0, 0); + this.classification = -1; + this.name = StringUtils.EMPTY; + this.employeeId = new UUID(0, 0); + this.sessionKey = StringUtils.EMPTY; + } +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java b/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java new file mode 100644 index 00000000..0e2c83c9 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java @@ -0,0 +1,164 @@ +package edu.uark.registerapp.models.entities; + +import java.time.LocalDateTime; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.annotations.Generated; +import org.hibernate.annotations.GenerationTime; + +// import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.models.api.Employee; + +@Entity +@Table(name="employee") +public class EmployeeEntity { + @Id + @Column(name="id", updatable = false) + @GeneratedValue(strategy=GenerationType.AUTO) + private final UUID id; + + public UUID getId() { + return this.id; + } + + @Column(name = "employeeid", insertable = false, updatable = false) + @Generated(GenerationTime.INSERT) + private int employeeId; + + public int getEmployeeId() { + return this.employeeId; + } + + @Column(name = "firstname") + private String firstName; + + public String getFirstName() { + return this.firstName; + } + + public EmployeeEntity setFirstName(final String firstName) { + this.firstName = firstName; + return this; + } + + @Column(name = "lastname") + private String lastName; + + public String getLastName() { + return this.lastName; + } + + public EmployeeEntity setLastName(final String lastName) { + this.lastName = lastName; + return this; + } + + @Column(name = "password") + private byte[] password; + + public byte[] getPassword() { + return this.password; + } + + public EmployeeEntity setPassword(final byte[] password) { + this.password = password; + return this; + } + + @Column(name = "active") + private boolean isActive; + + public boolean getIsActive() { + return this.isActive; + } + + public EmployeeEntity setIsActive(final boolean isActive) { + this.isActive = isActive; + return this; + } + + @Column(name = "classification") + private int classification; + + public int getClassification() { + return this.classification; + } + + public EmployeeEntity setClassification(final int classification) { + this.classification = classification; + return this; + } + + @Column(name = "managerid") + private UUID managerId; + + public UUID getManagerId() { + return this.managerId; + } + + public EmployeeEntity setManagerId(final UUID managerId) { + this.managerId = managerId; + return this; + } + + @Column(name = "createdon", insertable = false, updatable = false) + @Generated(GenerationTime.INSERT) + private LocalDateTime createdOn; + + public LocalDateTime getCreatedOn() { + return this.createdOn; + } + + public Employee synchronize(final Employee apiEmployee) { + this.setIsActive(apiEmployee.getIsActive()); + this.setLastName(apiEmployee.getLastName()); + this.setFirstName(apiEmployee.getFirstName()); + this.setClassification(apiEmployee.getClassification()); + if (apiEmployee.getManagerId() != null) { + this.setManagerId(apiEmployee.getManagerId()); + } + // if (!StringUtils.isBlank(apiEmployee.getPassword())) { + // this.setPassword( + // EmployeeHelper.hashPassword( + // apiEmployee.getPassword())); + // } + + apiEmployee.setId(this.getId()); + apiEmployee.setCreatedOn(this.getCreatedOn()); + // apiEmployee.setEmployeeId(this.getEmployeeId()); + + return apiEmployee; + } + + public EmployeeEntity() { + this.employeeId = -1; + this.isActive = false; + this.id = new UUID(0, 0); + this.classification = -1; + this.password = new byte[0]; + this.managerId = new UUID(0, 0); + this.lastName = StringUtils.EMPTY; + this.firstName = StringUtils.EMPTY; + } + + public EmployeeEntity(final Employee apiEmployee) { + this.id = new UUID(0, 0); + this.isActive = apiEmployee.getIsActive(); + this.lastName = apiEmployee.getLastName(); + this.firstName = apiEmployee.getFirstName(); + this.classification = apiEmployee.getClassification(); + // this.password = EmployeeHelper.hashPassword(apiEmployee.getPassword()); + this.managerId = ( + (apiEmployee.getManagerId() != null) + ? apiEmployee.getManagerId() + : new UUID(0, 0)); + } +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java b/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java new file mode 100644 index 00000000..7ff08f1c --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/enums/EmployeeClassification.java @@ -0,0 +1,58 @@ +package edu.uark.registerapp.models.enums; + +import java.util.HashMap; +import java.util.Map; + +public enum EmployeeClassification { + NOT_DEFINED(-1, "Not Selected"), + CASHIER(101, "Cashier"), + SHIFT_MANAGER(501, "Shift Manager"), + GENERAL_MANAGER(701, "General Manager"); + + public int getClassification() { + return this.classification; + } + + public String getDisplayLabel() { + return this.displayLabel; + } + + public static EmployeeClassification map(final int key) { + if (valueMap == null) { + valueMap = new HashMap(); + + for (final EmployeeClassification employeeClassification : EmployeeClassification.values()) { + valueMap.put( + employeeClassification.getClassification(), + employeeClassification); + } + } + + return ((valueMap.containsKey(key) + ? valueMap.get(key) + : EmployeeClassification.NOT_DEFINED)); + } + + public static boolean isElevatedUser(final int classification) { + final EmployeeClassification employeeClassification = + EmployeeClassification.map(classification); + + return ( + (employeeClassification == EmployeeClassification.GENERAL_MANAGER) + || (employeeClassification == EmployeeClassification.SHIFT_MANAGER)); + } + + private int classification; + private String displayLabel; + + private static Map valueMap = null; + + private EmployeeClassification( + final int classification, + final String displayLabel + ) { + + this.displayLabel = displayLabel; + this.classification = classification; + } +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java new file mode 100644 index 00000000..baece09b --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/repositories/ActiveUserRepository.java @@ -0,0 +1,15 @@ +// We will need this for the CRUD operations to find the employee Id and the session key of the acitve user + +package edu.uark.registerapp.models.repositories; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +import edu.uark.registerapp.models.entities.ActiveUserEntity; + +public interface ActiveUserRepository extends CrudRepository { + Optional findByEmployeeId(UUID employeeId); + Optional findBySessionKey(String sessionKey); +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java new file mode 100644 index 00000000..6b3ea9e2 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java @@ -0,0 +1,15 @@ +package edu.uark.registerapp.models.repositories; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +import edu.uark.registerapp.models.entities.EmployeeEntity; + +public interface EmployeeRepository extends CrudRepository { + // boolean Active(boolean isActive); + // boolean employeeIdExists(int employeeId); + // Optional findById(UUID id); + // Optional findByEmployeeId(int employeeId); +} \ No newline at end of file diff --git a/src/main/resources/templates/employeeDetail.html b/src/main/resources/templates/employeeDetail.html new file mode 100644 index 00000000..85b4cb05 --- /dev/null +++ b/src/main/resources/templates/employeeDetail.html @@ -0,0 +1,104 @@ + + + + Register - Employee + + + + + + + + + + +
+

Employee Detail

+
+ +
+
+

+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Employee ID: + +
First Name: + +
Last Name: + +
Password: + +
Confirm Password: + +
Employee Type: + +
+
+ +
+
+ +
+ + +
+ + + + + + \ No newline at end of file From f3e6b46851c1f04a54f319acaf3421fca24125fd Mon Sep 17 00:00:00 2001 From: Emily Lea <77511228+emilyglea@users.noreply.github.com> Date: Tue, 2 Mar 2021 21:23:41 -0600 Subject: [PATCH 06/15] Task 8 BaseRouteController base class --- .../controllers/BaseRouteController.java | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java diff --git a/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java b/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java new file mode 100644 index 00000000..58774f84 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/BaseRouteController.java @@ -0,0 +1,117 @@ +package edu.uark.registerapp.controllers; + +import java.util.Map; +import java.util.Optional; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.QueryParameterMessages; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; + +public abstract class BaseRouteController extends BaseController { + protected ModelAndView setErrorMessageFromQueryString( + ModelAndView modelAndView, + final Map queryParameters + ) { + + if (!queryParameters.containsKey(QueryParameterNames.ERROR_CODE.getValue())) { + return modelAndView; + } + + try { + modelAndView = + this.setErrorMessageFromQueryString( + modelAndView, + Integer.parseInt( + queryParameters.get( + QueryParameterNames.ERROR_CODE.getValue()))); + } catch (final NumberFormatException e) { } + + return modelAndView; + } + protected ModelAndView setErrorMessageFromQueryString( + final ModelAndView modelAndView, + final Optional errorCode + ) { + + if (!errorCode.isPresent()) { + return modelAndView; + } + + return this.setErrorMessageFromQueryString(modelAndView, errorCode.get()); + } + + protected Optional getCurrentUser( + final HttpServletRequest request + ) { + + try { + return Optional.of( + this.validateActiveUserCommand + .setSessionKey(request.getSession().getId()) + .execute()); + } catch (final UnauthorizedException e) { + return Optional.ofNullable(null); + } + } + + protected ModelAndView buildInvalidSessionResponse() { + return new ModelAndView( + REDIRECT_PREPEND.concat( + ViewNames.SIGN_IN.getRoute().concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.SESSION_NOT_ACTIVE.getKeyAsString())))); + } + + protected boolean isElevatedUser(final ActiveUserEntity activeUserEntity) { + return EmployeeClassification.isElevatedUser( + activeUserEntity.getClassification()); + } + + protected ModelAndView buildNoPermissionsResponse() { + return this.buildNoPermissionsResponse(ViewNames.MAIN_MENU.getRoute()); + } + + protected ModelAndView buildNoPermissionsResponse(final String redirectRoute) { + return new ModelAndView( + REDIRECT_PREPEND.concat( + redirectRoute.concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.NO_PERMISSIONS_TO_VIEW.getKeyAsString())))); + } + + protected static final String REDIRECT_PREPEND = "redirect:"; + + // Helper methods + private ModelAndView setErrorMessageFromQueryString( + final ModelAndView modelAndView, + final int errorCode + ) { + + final String errorMessage = QueryParameterMessages.mapMessage(errorCode); + + if (!StringUtils.isBlank(errorMessage)) { + modelAndView.addObject( + ViewModelNames.ERROR_MESSAGE.getValue(), + errorMessage); + } + + return modelAndView; + } + + // Properties + @Autowired + private ValidateActiveUserCommand validateActiveUserCommand; +} From fcd230e1a90f1aec614595b70706d6659ad94c6f Mon Sep 17 00:00:00 2001 From: Emily Lea <77511228+emilyglea@users.noreply.github.com> Date: Tue, 2 Mar 2021 21:47:58 -0600 Subject: [PATCH 07/15] Task 8 MainMenuRouteController --- .../controllers/MainMenuRouteController.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java diff --git a/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java b/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java new file mode 100644 index 00000000..94470fbf --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java @@ -0,0 +1,46 @@ +package edu.uark.registerapp.controllers; + +//HttpServletRequest allows a servlet to get info about a client request +import javax.servlet.http.HttpServletRequest; +import java.util.Map; +import java.util.Optional; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.entities.ActiveUserEntity; + +@Controller +@RequestMapping(value = "/mainMenu") +public class MainMenuRouteController extends BaseRouteController{ + //GET request with /mainMenu route/endpoint path + @RequestMapping(method = RequestMethod.GET) + //@RequestParam reads form data and binds it to the specified parameters; maps the request parameter to query parameter + //parameters for get request: Map and HttpServletRequest + //Map object is a collection of keyed-in elements using any type of key + public ModelAndView start(@RequestParam final Map queryParameters, final HttpServletRequest request) { + //if there's an active user in the current session, add any error messages received in requestParam to the view + //serve up the main menu view/doc + //ELSE: immediately redirect to the Sign-In view document with an appropriate error message + final Optional activeUserEntity = + this.getCurrentUser(request); + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } + + ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.MAIN_MENU.getViewName()), + queryParameters); + + modelAndView.addObject( + ViewModelNames.IS_ELEVATED_USER.getValue(), + this.isElevatedUser(activeUserEntity.get())); + } +} + From 9bee13d9189e1a6fea2eca855394d05d83bb005c Mon Sep 17 00:00:00 2001 From: Emily Lea <77511228+emilyglea@users.noreply.github.com> Date: Tue, 2 Mar 2021 22:03:01 -0600 Subject: [PATCH 08/15] Task 8: ActiveUserEntity.java base class --- .../models/entities/ActiveUserEntity.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java diff --git a/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java new file mode 100644 index 00000000..6eba3528 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java @@ -0,0 +1,91 @@ +package edu.uark.registerapp.models.entities; + +import java.time.LocalDateTime; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.annotations.Generated; +import org.hibernate.annotations.GenerationTime; + +@Entity +@Table(name="activeuser") +public class ActiveUserEntity { + @Id + @Column(name="id", updatable = false) + @GeneratedValue(strategy=GenerationType.AUTO) + private final UUID id; + + public UUID getId() { + return this.id; + } + + @Column(name = "employeeid") + private UUID employeeId; + + public UUID getEmployeeId() { + return this.employeeId; + } + + public ActiveUserEntity setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + @Column(name = "name") + private String name; + + public String getName() { + return this.name; + } + + public ActiveUserEntity setName(final String name) { + this.name = name; + return this; + } + + @Column(name = "classification") + private int classification; + + public int getClassification() { + return this.classification; + } + + public ActiveUserEntity setClassification(final int classification) { + this.classification = classification; + return this; + } + + @Column(name = "sessionkey") + private String sessionKey; + + public String getSessionKey() { + return this.sessionKey; + } + + public ActiveUserEntity setSessionKey(final String sessionKey) { + this.sessionKey = sessionKey; + return this; + } + + @Column(name="createdon", insertable=false, updatable = false) + @Generated(GenerationTime.INSERT) + private LocalDateTime createdOn; + public LocalDateTime getCreatedOn() { + return this.createdOn; + } + + public ActiveUserEntity() { + this.id = new UUID(0, 0); + this.classification = -1; + this.name = StringUtils.EMPTY; + this.employeeId = new UUID(0, 0); + this.sessionKey = StringUtils.EMPTY; + } +} From 809b326b5e98c42b0791efaf7853176d472c12ae Mon Sep 17 00:00:00 2001 From: Emily Lea <77511228+emilyglea@users.noreply.github.com> Date: Wed, 3 Mar 2021 11:29:40 -0600 Subject: [PATCH 09/15] Task 2: signIn.html --- src/main/resources/templates/signIn.html | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/resources/templates/signIn.html diff --git a/src/main/resources/templates/signIn.html b/src/main/resources/templates/signIn.html new file mode 100644 index 00000000..27d9be30 --- /dev/null +++ b/src/main/resources/templates/signIn.html @@ -0,0 +1,42 @@ + + + + + Register - Sign In + + + + + + + + + + +
+

Sign In

+
+ +
+
+

+
+ +
+
+ +
+
+
+ +
+

+
+ +
+
+
+ + + + \ No newline at end of file From 1191a28924cabd0f924ed8406c4a25a356d7598e Mon Sep 17 00:00:00 2001 From: Emily Lea <77511228+emilyglea@users.noreply.github.com> Date: Wed, 3 Mar 2021 11:38:46 -0600 Subject: [PATCH 10/15] Revert; ActiveUserEntity is already there. This reverts commit 9bee13d9189e1a6fea2eca855394d05d83bb005c. --- .../models/entities/ActiveUserEntity.java | 91 ------------------- 1 file changed, 91 deletions(-) delete mode 100644 src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java diff --git a/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java b/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java deleted file mode 100644 index 6eba3528..00000000 --- a/src/main/java/edu/uark/registerapp/models/entities/ActiveUserEntity.java +++ /dev/null @@ -1,91 +0,0 @@ -package edu.uark.registerapp.models.entities; - -import java.time.LocalDateTime; -import java.util.UUID; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.apache.commons.lang3.StringUtils; -import org.hibernate.annotations.Generated; -import org.hibernate.annotations.GenerationTime; - -@Entity -@Table(name="activeuser") -public class ActiveUserEntity { - @Id - @Column(name="id", updatable = false) - @GeneratedValue(strategy=GenerationType.AUTO) - private final UUID id; - - public UUID getId() { - return this.id; - } - - @Column(name = "employeeid") - private UUID employeeId; - - public UUID getEmployeeId() { - return this.employeeId; - } - - public ActiveUserEntity setEmployeeId(final UUID employeeId) { - this.employeeId = employeeId; - return this; - } - - @Column(name = "name") - private String name; - - public String getName() { - return this.name; - } - - public ActiveUserEntity setName(final String name) { - this.name = name; - return this; - } - - @Column(name = "classification") - private int classification; - - public int getClassification() { - return this.classification; - } - - public ActiveUserEntity setClassification(final int classification) { - this.classification = classification; - return this; - } - - @Column(name = "sessionkey") - private String sessionKey; - - public String getSessionKey() { - return this.sessionKey; - } - - public ActiveUserEntity setSessionKey(final String sessionKey) { - this.sessionKey = sessionKey; - return this; - } - - @Column(name="createdon", insertable=false, updatable = false) - @Generated(GenerationTime.INSERT) - private LocalDateTime createdOn; - public LocalDateTime getCreatedOn() { - return this.createdOn; - } - - public ActiveUserEntity() { - this.id = new UUID(0, 0); - this.classification = -1; - this.name = StringUtils.EMPTY; - this.employeeId = new UUID(0, 0); - this.sessionKey = StringUtils.EMPTY; - } -} From 36195b55c871f763447249490f14eb5f9e513eda Mon Sep 17 00:00:00 2001 From: Jodicd Date: Wed, 3 Mar 2021 15:48:31 -0600 Subject: [PATCH 11/15] JM - The real task 7 --- src/main/resources/static/scripts/mainMenu.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/resources/static/scripts/mainMenu.js diff --git a/src/main/resources/static/scripts/mainMenu.js b/src/main/resources/static/scripts/mainMenu.js new file mode 100644 index 00000000..d2467edc --- /dev/null +++ b/src/main/resources/static/scripts/mainMenu.js @@ -0,0 +1,45 @@ +// This is the functionality for the main menu. +document.addEventListener("DOMContentLoaded", function(event) +{ + startTransaction.addEventListener("click", transactionActionElement); + + viewProducts.addEventListener("click", productsActionElement); + + createEmployee.addEventListener("click", employeeActionElement); + + salesReport.addEventListener("click", productSalesReportActionElement); + + cashiersReport.addEventListener("click", cashierSalesReportActionElement); + + signOut.addEventListener("click", signOutActionElement); // signOut is the id of the sign out button + +}); + + +function transactionActionElement() { + document.getElementById("errorMessage").innerHTML = "Functionality has not yet been implemented"; +} + +function productsActionElement() { + window.location.href = "\productListing.html"; // go to the product listing page +} + +function employeeActionElement() { + window.location.href = "\employeeDetails.html"; // go to the employee detail page +} + +function productSalesReportActionElement() { + document.getElementById("errorMessage").innerHTML = "Functionality has not yet been implemented"; + +} + +function cashierSalesReportActionElement() { + document.getElementById("errorMessage").innerHTML = "Functionality has not yet been implemented"; + +} + +function signOutActionElement() { + + window.location.href = "\signIn.html"; // Go to the sign in page + +} \ No newline at end of file From 94411813db08d1fc7d7a6dd524b1c5e4f51ba478 Mon Sep 17 00:00:00 2001 From: Alexis Jamilee Carter Date: Thu, 4 Mar 2021 19:48:44 -0600 Subject: [PATCH 12/15] Task 3 - sign in checks --- .DS_Store | Bin 0 -> 6148 bytes src/.DS_Store | Bin 0 -> 6148 bytes src/main/.DS_Store | Bin 0 -> 6148 bytes src/main/java/.DS_Store | Bin 0 -> 6148 bytes src/main/java/edu/.DS_Store | Bin 0 -> 6148 bytes src/main/java/edu/uark/.DS_Store | Bin 0 -> 6148 bytes src/main/java/edu/uark/registerapp/.DS_Store | Bin 0 -> 6148 bytes .../edu/uark/registerapp/commands/.DS_Store | Bin 0 -> 6148 bytes .../registerapp/commands/employees/.DS_Store | Bin 0 -> 6148 bytes .../employees/EmployeeSignInCommand.java | 114 ++++++++++++++++++ .../RegisterappApplicationTests.java | 0 11 files changed, 114 insertions(+) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/main/.DS_Store create mode 100644 src/main/java/.DS_Store create mode 100644 src/main/java/edu/.DS_Store create mode 100644 src/main/java/edu/uark/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/commands/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java rename src/{ => main}/test/java/edu/uark/registerapp/RegisterappApplicationTests.java (100%) diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9a874b5768f336915163bb88cd434575b859f936 GIT binary patch literal 6148 zcmeH~Jr2S!425ml0g0s}V-^m;4I%_5-~tF3k&vj^b9A16778<}(6eNJu~Vz<8=6`~ zboab&MFtUB!i}=AFfm2m$tVxGT*u4pe81nUlA49C} z?O@64YO)2RT{MRe%{!}2F))pG(Sih~)xkgosK7*lF7m<7{{#Hn{6A@7N(HFEpDCdI z{V4u3`M`g7D#EfOgRk)$PGpaPQV2S&>%r5BKkQx-ySzvsH0W%E!l7ES!;KH zv9ktX>-*^w7y&HlPOLmk%$N_j;tOYdpMH*)!|itQBJHgMp3+B5_H$d10#ZNMM4T9irw zDe%t}uwk>?toc%Twm!X{*Y{cVb)%DUIm54?049DEf6&9YUwlE)m< zk%er^j=Knh3!DIf); zz>E~gV|@8Jqi52iNC7D@4+Z@DQ0UH@Y@PAxV2BZb99RzHI%Wy7c!8|R*2xOZa(b|A zwHQOZ9_?hw>uR!f_I6kfAC`AEpJHg%+hK(X&1yhF3P^#80*juHe*W+1ujcQ^n8@Auu4PPqH)~DC=`aY|^Zges(XZZ6Iz{HQ@6+Mjm#TR5vwoX=P`Vj~j6r{kv GD)0dNyb`AX literal 0 HcmV?d00001 diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8ee47d97bdd6fdb00cdb10572569902931b99818 GIT binary patch literal 6148 zcmeH~Jr2S!425mVfW*>~F$)La1`&c2Z~+8mL+XIob9A16778<}(6eNJu~Vz<8=6`~ zbpIT7BE5*L;6~Y6n3y8(<_Lbudr?Dlk!?i@dk<{|Ns!|4&+&QUNOPXA0=@ za60VqQhBz1yq?w1S+#Y8gMK-}+fM)zJBl}OH|!T%fHm2IsKEFm;4&~!fuAby0>G&e AYXATM literal 0 HcmV?d00001 diff --git a/src/main/java/edu/.DS_Store b/src/main/java/edu/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..293351a57c0a86dae404af24ac92deb45ceaeb62 GIT binary patch literal 6148 zcmeH~J&wXa427SU6iC~oq@0EW>lO)VSM+oAefGE^Y>guHEZN`K6D!Fp zGBE(#+%I3i2w+KfV(npK#(cmXNBrRX@_W9XZnvu!X&)W%ls;mzpWA{IkOERb3P=Gd zFe3%>7+-$Q=$Z5=Qa}pKLjnIj6uPq}TW5Sa7-9q<2bROQj#+{%ULb3-b+SUUoE|J& zEyfV9M>|>ax|(dAy&aarhvl8krx=>`c35FTvl>v40#ab2z@q1)pZ`1hfAjyOMX3~! z0&k{(4cq;8!)6@?RU0TL1+q)3Sf^smafI2yB`=IKEfnrK#9kG;=|N@-+f|t6)_^6XPohgJwC9(X_EbWz_|-D_IShlh=1Ch zUk}^k@!%;kDIf);fE17dQs7DoRAHSju5_M`kpfcSb`Px&@8FMq*}EYmUPBj<@LfLG3l^sKCEuG>QF3h=lLztVLef!6p#XA z1@3dX^!ooq|D*pOleCfoQsAZ(u-W=~z2cLqwoV@BwYJe;=$`XUcjG)L9HJZ(qa1VL f<@h0zGOziZ`@L{T3_9aMC+cUwb&*Md|5o4!^FS6M literal 0 HcmV?d00001 diff --git a/src/main/java/edu/uark/registerapp/.DS_Store b/src/main/java/edu/uark/registerapp/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..534c0929912ac332544f9d7c58ced2a84156069e GIT binary patch literal 6148 zcmeHKyKciU3_PY83}ngJQNPeX5Q6jz@&jq(F|CK7yQh9vKdr~d(7^4MF2T{DPNH}+ ze1m8Lu-(V_1Z)8ebVq#pur%LyU)ZW5Mx^tM7rbM{WxSlui|pS6&K+>Vgf~7v_^;D+ z_Dz`-kOERb3P=Gda8m`Uybhb2o~py7fE2g~1^j+!bjMyeC&tymAzA?9n&B|cqn98y z4-k9doX7~xl1fafRf}OsXS`KjFPsyT4y)$F>djUiipAS`ev5QiPt+&{q`LzpI^#hn>Sw@pkx7C7R^S^YG!=&c literal 0 HcmV?d00001 diff --git a/src/main/java/edu/uark/registerapp/commands/.DS_Store b/src/main/java/edu/uark/registerapp/commands/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fb63e898f96c58ec85b7934c294cbf1af3542888 GIT binary patch literal 6148 zcmeHKJ8r`;3?-WkF1lpws4L_Kg5jQ^7YNd1bKn9-hi;yFt{kn8pCN(TEm?v`gL)Fh zC&O#!Bsh)4%Flph;fvwic%o-(39IL?^lTaI|Tk#S#nVM=AV(@d1+EoX z#d2fy{|)|6|9?&5iV9GHhf+Y-!~4+Vld`rpA7{0;z;|%VIm68`cM1kC$3QQ~SXeoJ d^`yuvHphNVTmqerxYL3B88BUFRN&qU`~sa36}bQa literal 0 HcmV?d00001 diff --git a/src/main/java/edu/uark/registerapp/commands/employees/.DS_Store b/src/main/java/edu/uark/registerapp/commands/employees/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6d866df1d813c77b62420ea064566e4a075a76da GIT binary patch literal 6148 zcmeHKJ8r`;3?K z{Asi;8Wo@dRDcRl0V;4~1+u`dpKp9FkD~%q;QuRN--iM>tcgpYe>yOD3jkap?1s7b z62M{sU`<>C5rJt?fkD-5F*N9im&~h)OJLANv-!}xS+he?za8fnPZzC$9H{^mxK!XV zmYvoASNN6w|B}QN6`%rlrGT!6x1qDV cDe{WVv0oFHK&K<_bRd5QOcxpz_-h4z05wAuoB#j- literal 0 HcmV?d00001 diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java new file mode 100644 index 00000000..d31750e1 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java @@ -0,0 +1,114 @@ +package edu.uark.registerapp.commands.employees; + +import java.util.Arrays; +import java.util.Optional; + +import javax.transaction.Transactional; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.api.EmployeeSignIn; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.ActiveUserRepository; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeSignInCommand implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + return new Employee(this.SignInEmployee()); + } + + // Helper methods + + //this is the method for task 3 that checks for numeric and nonblank entries + private void validateProperties() { + + //test for blanks + if (StringUtils.isBlank(this.employeeSignIn.getEmployeeId())) { + throw new UnprocessableEntityException("employee ID"); + } + try { + //test for numerics + +Integer.parseInt(this.employeeSignIn.getEmployeeId()); + } catch (final NumberFormatException e) { + throw new UnprocessableEntityException("employee ID"); + } + if (StringUtils.isBlank(this.employeeSignIn.getPassword())) { + throw new UnprocessableEntityException("password"); + } + } + + @Transactional + private EmployeeEntity SignInEmployee() { + final Optional employeeEntity = + this.employeeRepository.findByEmployeeId( + Integer.parseInt(this.employeeSignIn.getEmployeeId())); + + if (!employeeEntity.isPresent() + || !Arrays.equals( + employeeEntity.get().getPassword(), + EmployeeHelper.hashPassword(this.employeeSignIn.getPassword())) + ) { + + throw new UnauthorizedException(); + } + + final Optional activeUserEntity = + this.activeUserRepository + .findByEmployeeId(employeeEntity.get().getId()); + + if (!activeUserEntity.isPresent()) { + this.activeUserRepository.save( + (new ActiveUserEntity()) + .setSessionKey(this.sessionId) + .setEmployeeId(employeeEntity.get().getId()) + .setClassification( + employeeEntity.get().getClassification()) + .setName( + employeeEntity.get().getFirstName() + .concat(" ") + .concat(employeeEntity.get().getLastName()))); + } else { + this.activeUserRepository.save( + activeUserEntity.get().setSessionKey(this.sessionId)); + } + + return employeeEntity.get(); + } + + // Properties + private EmployeeSignIn employeeSignIn; + public EmployeeSignIn getEmployeeSignIn() { + return this.employeeSignIn; + } + public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn) { + this.employeeSignIn = employeeSignIn; + return this; + } + + private String sessionId; + public String getSessionId() { + return this.sessionId; + } + public EmployeeSignInCommand setSessionId(final String sessionId) { + this.sessionId = sessionId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; + @Autowired + private ActiveUserRepository activeUserRepository; +} diff --git a/src/test/java/edu/uark/registerapp/RegisterappApplicationTests.java b/src/main/test/java/edu/uark/registerapp/RegisterappApplicationTests.java similarity index 100% rename from src/test/java/edu/uark/registerapp/RegisterappApplicationTests.java rename to src/main/test/java/edu/uark/registerapp/RegisterappApplicationTests.java From 3622212cdd4bb65b698c3c886cc9489adaf471f0 Mon Sep 17 00:00:00 2001 From: Alexis Jamilee Carter Date: Thu, 4 Mar 2021 21:22:00 -0600 Subject: [PATCH 13/15] Task 3 - updates --- src/main/java/edu/uark/registerapp/.DS_Store | Bin 6148 -> 6148 bytes .../edu/uark/registerapp/commands/.DS_Store | Bin 6148 -> 6148 bytes .../registerapp/commands/employees/.DS_Store | Bin 6148 -> 6148 bytes .../employees/helpers/EmployeeHelper.java | 16 +++++++++++---- .../uark/registerapp/controllers/.DS_Store | Bin 0 -> 6148 bytes .../edu/uark/registerapp/models/.DS_Store | Bin 0 -> 6148 bytes .../uark/registerapp/models/api/Employee.java | 19 ++++++++++-------- .../models/entities/EmployeeEntity.java | 19 +++++++++--------- .../repositories/EmployeeRepository.java | 8 ++++---- 9 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 src/main/java/edu/uark/registerapp/controllers/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/models/.DS_Store diff --git a/src/main/java/edu/uark/registerapp/.DS_Store b/src/main/java/edu/uark/registerapp/.DS_Store index 534c0929912ac332544f9d7c58ced2a84156069e..9ac7ddccb405814d41509e01bae978c5829e1e51 100644 GIT binary patch delta 114 zcmZoMXfc=|&e%S&P;8=}A|vy}0Ba!8BV)q=1l$bC4EYRs3?&RjKspBqQ-Q4Fl;Y%^ zr2PCGkP>L6djBOh~erKM{FQUuJ O$iTpGV6&~rVP*gZF%a|s diff --git a/src/main/java/edu/uark/registerapp/commands/.DS_Store b/src/main/java/edu/uark/registerapp/commands/.DS_Store index fb63e898f96c58ec85b7934c294cbf1af3542888..5f5b9bac45d3228fe7f8667e00cb335e880b64b0 100644 GIT binary patch delta 116 zcmZoMXfc=|&e%S&P;8=}A|vy}0Ba!8BWK3|1Y8WM3>6H?K(c_Lgdvk5pCON-IHfo_ zCn-Na2c&=lC{Y9yPGKkoDl0)qZrmu(zL}jvfP=Ap>L6djBOh~erKM{FQUuJ O$iTpGV6&~rVP*gZLlE=; diff --git a/src/main/java/edu/uark/registerapp/commands/employees/.DS_Store b/src/main/java/edu/uark/registerapp/commands/employees/.DS_Store index 6d866df1d813c77b62420ea064566e4a075a76da..90ee518affea0aaa9fbbc016a2f055ca9b5a1855 100644 GIT binary patch delta 18 acmZoMXfc?uY~#i-_K6Mro4GlD@&f=#kOxcv delta 20 ccmZoMXfc?ujFEBU#xVAY4P2YqIsWnk08YaPNdN!< diff --git a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java index 1f1d3821..d0e0c210 100644 --- a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java +++ b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java @@ -14,11 +14,19 @@ public static String padEmployeeId(final int employeeId) { : employeeIdAsString); } + // Hash password using message digest public static byte[] hashPassword(final String password) { - // TODO: Hash the password using a MessageDigest. An example can be found at http://tutorials.jenkov.com/java-cryptography/messagedigest.html - - return new byte[0]; - } + try { + final MessageDigest messageDigest = + MessageDigest.getInstance("SHA-256"); + + messageDigest.update(password.getBytes()); + return messageDigest.digest(); + } catch (final NoSuchAlgorithmException e) { + return new byte[0]; + } + } + private static final int EMPLOYEE_ID_MAXIMUM_LENGTH = 5; } \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/controllers/.DS_Store b/src/main/java/edu/uark/registerapp/controllers/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..741eaa87dcd9592f51cfb3c4991c5d2bcdd23fe0 GIT binary patch literal 6148 zcmeH~I|>3p42BaQAlO)1PU8W*!654iyny)Vd=z3oNB7SI!PQzs{y_52WD;e+VrL^F zy1E~hA~O-0z)fXgp=XNySdMa$yPV~Axb087>-FSSgRHe0IDX^xJdY_PKmsH{0wh2J zKSaRpZP;WQ%18nvKmtz!_I@aE)0)~s{ndfsBLH-Sv>VnwOF)Yypf$CHA_LQCg+{CT z7-D&EhnB3XsVy|xMRWMj{AaZ(2By(2TCjm>bzvX@66g^a#(rk^{~G>j{_nMLO9CYD zX9RTAZMqd6D(}{}$Fux4v$k$?!URf0p09QPOagH=_&fFIz|9wHS|k@lV&zs>jo$|`W>0Jh|vtnJAv zbBNafWOLr#0&4&(x+30nnVaU-XLb>hB~kPp&xfbJKWOyZ`Y3NexeGkvh;BDb`Fp(L zgwA}AVVb;zCeL_j!`FCY;)ouD@p|q%MjCE&cy3aGR3H^d1yX^3r~rGm+Um+N*Hj=C zNCmzW(EmfBE7ri#(S99VYy=?cHQtQRHcJqT28cCqbYz4kP9-{3Vq_4fGhU*u29AzS zhs5w9ak9k3A$B_7U#uKb9dk_uQh`>1g>To||L^EO%>OMZcd0-s@TU}z<@SEN;U`6J zoqe44+CqPze;RA8oQt($qP1dfv=!e>>WV%yUjs)+qcd)FVqOGPm$X#iHx&2)8%!pv literal 0 HcmV?d00001 diff --git a/src/main/java/edu/uark/registerapp/models/api/Employee.java b/src/main/java/edu/uark/registerapp/models/api/Employee.java index 2f3726e1..e05e76ca 100644 --- a/src/main/java/edu/uark/registerapp/models/api/Employee.java +++ b/src/main/java/edu/uark/registerapp/models/api/Employee.java @@ -7,14 +7,15 @@ import org.apache.commons.lang3.StringUtils; -// import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; -// import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.models.entities.EmployeeEntity; public class Employee extends ApiResponse { private UUID id; public UUID getId() { return this.id; } + public Employee setId(final UUID id) { this.id = id; return this; @@ -24,10 +25,12 @@ public Employee setId(final UUID id) { public String getEmployeeId() { return this.employeeId; } - // public Employee setEmployeeId(final int employeeId) { - // this.employeeId = EmployeeHelper.padEmployeeId(employeeId); - // return this; - // } + + //Part of task 3 + public Employee setEmployeeId(final int employeeId) { + this.employeeId = EmployeeHelper.padEmployeeId(employeeId); + return this; + } public Employee setEmployeeId(final String employeeId) { this.employeeId = employeeId; return this; @@ -138,8 +141,8 @@ public Employee(final edu.uark.registerapp.models.entities.EmployeeEntity employ this.firstName = employeeEntity.getFirstName(); this.managerId = employeeEntity.getManagerId(); this.classification = employeeEntity.getClassification(); - // this.employeeId = - // EmployeeHelper.padEmployeeId(employeeEntity.getEmployeeId()); + this.employeeId = + EmployeeHelper.padEmployeeId(employeeEntity.getEmployeeId()); this.setCreatedOn(employeeEntity.getCreatedOn()); } diff --git a/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java b/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java index 0e2c83c9..7b8194ce 100644 --- a/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java +++ b/src/main/java/edu/uark/registerapp/models/entities/EmployeeEntity.java @@ -13,8 +13,7 @@ import org.apache.commons.lang3.StringUtils; import org.hibernate.annotations.Generated; import org.hibernate.annotations.GenerationTime; - -// import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; import edu.uark.registerapp.models.api.Employee; @Entity @@ -125,15 +124,17 @@ public Employee synchronize(final Employee apiEmployee) { if (apiEmployee.getManagerId() != null) { this.setManagerId(apiEmployee.getManagerId()); } - // if (!StringUtils.isBlank(apiEmployee.getPassword())) { - // this.setPassword( - // EmployeeHelper.hashPassword( - // apiEmployee.getPassword())); - // } + + //part of task 3 + if (!StringUtils.isBlank(apiEmployee.getPassword())) { + this.setPassword( + EmployeeHelper.hashPassword( + apiEmployee.getPassword())); + } apiEmployee.setId(this.getId()); apiEmployee.setCreatedOn(this.getCreatedOn()); - // apiEmployee.setEmployeeId(this.getEmployeeId()); + apiEmployee.setEmployeeId(this.getEmployeeId()); return apiEmployee; } @@ -155,7 +156,7 @@ public EmployeeEntity(final Employee apiEmployee) { this.lastName = apiEmployee.getLastName(); this.firstName = apiEmployee.getFirstName(); this.classification = apiEmployee.getClassification(); - // this.password = EmployeeHelper.hashPassword(apiEmployee.getPassword()); + this.password = EmployeeHelper.hashPassword(apiEmployee.getPassword()); this.managerId = ( (apiEmployee.getManagerId() != null) ? apiEmployee.getManagerId() diff --git a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java index 6b3ea9e2..12ce2419 100644 --- a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java +++ b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java @@ -8,8 +8,8 @@ import edu.uark.registerapp.models.entities.EmployeeEntity; public interface EmployeeRepository extends CrudRepository { - // boolean Active(boolean isActive); - // boolean employeeIdExists(int employeeId); - // Optional findById(UUID id); - // Optional findByEmployeeId(int employeeId); + boolean Active(boolean isActive); + boolean employeeIdExists(int employeeId); + Optional findById(UUID id); + Optional findByEmployeeId(int employeeId); } \ No newline at end of file From 72dbacb11f3e4d664f8da3ce6b6324a407185597 Mon Sep 17 00:00:00 2001 From: Alexis Jamilee Carter Date: Thu, 4 Mar 2021 22:14:07 -0600 Subject: [PATCH 14/15] Task 3 - final updates --- src/main/.DS_Store | Bin 6148 -> 6148 bytes .../edu/uark/registerapp/commands/.DS_Store | Bin 6148 -> 6148 bytes .../employees/EmployeeSignInCommand.java | 114 ------------------ src/main/resources/.DS_Store | Bin 0 -> 6148 bytes src/main/resources/static/.DS_Store | Bin 0 -> 6148 bytes src/main/resources/static/scripts/signin.js | 90 ++++++++++++++ 6 files changed, 90 insertions(+), 114 deletions(-) delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java create mode 100644 src/main/resources/.DS_Store create mode 100644 src/main/resources/static/.DS_Store create mode 100644 src/main/resources/static/scripts/signin.js diff --git a/src/main/.DS_Store b/src/main/.DS_Store index 55c1fcbeb4ba2bef02771ea2748f3e4b0ea5c07d..9aaf253d36b255d6c35926aaf89a35d8cbee0eb7 100644 GIT binary patch delta 144 zcmZoMXfc=|&e%3FQH+&?fq{WzVxov76OaJ{AexbZL4YBpI5{UNKR;(;qP7l5kcAK2Y{Giv!lpo0OXz*$N&HU diff --git a/src/main/java/edu/uark/registerapp/commands/.DS_Store b/src/main/java/edu/uark/registerapp/commands/.DS_Store index 5f5b9bac45d3228fe7f8667e00cb335e880b64b0..1cdd9974799e59673b4e94d43a0ae9f5c6e2db80 100644 GIT binary patch delta 12 TcmZoMXfc?uiji^S>PT?_9peOX delta 12 TcmZoMXfc?uijiUC>PT?_9o__R diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java deleted file mode 100644 index d31750e1..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java +++ /dev/null @@ -1,114 +0,0 @@ -package edu.uark.registerapp.commands.employees; - -import java.util.Arrays; -import java.util.Optional; - -import javax.transaction.Transactional; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; -import edu.uark.registerapp.commands.exceptions.UnauthorizedException; -import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.api.EmployeeSignIn; -import edu.uark.registerapp.models.entities.ActiveUserEntity; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.repositories.ActiveUserRepository; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class EmployeeSignInCommand implements ResultCommandInterface { - @Override - public Employee execute() { - this.validateProperties(); - - return new Employee(this.SignInEmployee()); - } - - // Helper methods - - //this is the method for task 3 that checks for numeric and nonblank entries - private void validateProperties() { - - //test for blanks - if (StringUtils.isBlank(this.employeeSignIn.getEmployeeId())) { - throw new UnprocessableEntityException("employee ID"); - } - try { - //test for numerics - -Integer.parseInt(this.employeeSignIn.getEmployeeId()); - } catch (final NumberFormatException e) { - throw new UnprocessableEntityException("employee ID"); - } - if (StringUtils.isBlank(this.employeeSignIn.getPassword())) { - throw new UnprocessableEntityException("password"); - } - } - - @Transactional - private EmployeeEntity SignInEmployee() { - final Optional employeeEntity = - this.employeeRepository.findByEmployeeId( - Integer.parseInt(this.employeeSignIn.getEmployeeId())); - - if (!employeeEntity.isPresent() - || !Arrays.equals( - employeeEntity.get().getPassword(), - EmployeeHelper.hashPassword(this.employeeSignIn.getPassword())) - ) { - - throw new UnauthorizedException(); - } - - final Optional activeUserEntity = - this.activeUserRepository - .findByEmployeeId(employeeEntity.get().getId()); - - if (!activeUserEntity.isPresent()) { - this.activeUserRepository.save( - (new ActiveUserEntity()) - .setSessionKey(this.sessionId) - .setEmployeeId(employeeEntity.get().getId()) - .setClassification( - employeeEntity.get().getClassification()) - .setName( - employeeEntity.get().getFirstName() - .concat(" ") - .concat(employeeEntity.get().getLastName()))); - } else { - this.activeUserRepository.save( - activeUserEntity.get().setSessionKey(this.sessionId)); - } - - return employeeEntity.get(); - } - - // Properties - private EmployeeSignIn employeeSignIn; - public EmployeeSignIn getEmployeeSignIn() { - return this.employeeSignIn; - } - public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn) { - this.employeeSignIn = employeeSignIn; - return this; - } - - private String sessionId; - public String getSessionId() { - return this.sessionId; - } - public EmployeeSignInCommand setSessionId(final String sessionId) { - this.sessionId = sessionId; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; - @Autowired - private ActiveUserRepository activeUserRepository; -} diff --git a/src/main/resources/.DS_Store b/src/main/resources/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..2a43b86b965c6735a338394c4d17e6a7a5a71078 GIT binary patch literal 6148 zcmeHKISv9b4733uB$_QL^96noA$S2F;A)VND1d$|-o?`x9|dTkg90>`oJkx{qD-+~ zi-^uHyQxScA|tq=Ty5x@?VES3mk|ZRamHS@yVLftIUWz4?AHO~4&@|gS;HUrwn3u; zRDcRl0V+TRKCVEP*wOgoC-XomKm~qX0sB4_xM59f1O3y1!CL^}0AV-Gy_WzM3jk|k z8;A%@g9;3)W{aUgN4#WSO>6^$E}G4U=FOTNiu&z1zj(T64dh4#sKB!V{a7|u{}=E# z{r@wGD=I(*{z?HI%$M^So|Lt<^Ej)u1-^n?&JAvcxl=HBIR<(;#=^?+#FHYg*c|&c Vu?=)O;!X$hXTWr!QGstOa0m456^#G@ literal 0 HcmV?d00001 diff --git a/src/main/resources/static/.DS_Store b/src/main/resources/static/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..eba13223f6781b153af5c8a6f35630fbb0dc1cc4 GIT binary patch literal 6148 zcmeHKJ5EC}5S)cbM50NV(pTUHR+O9|7eFLUq)0(L=wHRTI2yB`g6NShDL}K*dhGR% zEl=_GEdX16?4N)IfH~a}pFT{@_uVIUR}mx9dB!W=G3EIA80CC1}80XPT z5Ss^xy>Luqgl0)4Ce^CNu%t8IDz6ugiAjf5^I>(fRfl46JI`;C4(o{;rGOMTSKvOE z3$Onh`i1`goTQZ$kOKco0h_J2>lL3=wRQ41ueFW-O!u5Gx*O*~;SlAR80DA?FUQMB b%Dm=t?)SnmG3bm3ov5Dy*F`1;ZmqyKRWud8 literal 0 HcmV?d00001 diff --git a/src/main/resources/static/scripts/signin.js b/src/main/resources/static/scripts/signin.js new file mode 100644 index 00000000..88b2a2ee --- /dev/null +++ b/src/main/resources/static/scripts/signin.js @@ -0,0 +1,90 @@ +//Task 3 - Sign in verification + +document.addEventListener("DOMContentLoaded", function(event) { + + const employeeIdEditElement = getEmployeeIdEditElement(); + + employeeIdEditElement.focus(); + + employeeIdEditElement.select(); + +}); + + + +function validateForm() { + + const employeeIdEditElement = getEmployeeIdEditElement(); + + //Check the id + if (isNaN(Number(employeeIdEditElement.value)) + + || (Number(employeeIdEditElement.value) <= 0)) { + + + + displayError("Please provide a valid employee ID. Please try again."); + + + + employeeIdEditElement.focus(); + + employeeIdEditElement.select(); + + + + return false; + + } + + + + const passwordEditElement = getPasswordEditElement(); + + //check to see if password is equal to null + + if ((passwordEditElement.value == null) + + || (passwordEditElement.value.trim() === "")) { + + + + displayError("Please provide a valid password. It may not be blank. Please try again."); + + + + passwordEditElement.focus(); + + passwordEditElement.select(); + + + + return false; + + } + + + + return true; + +} + + + +//Getters and setters + +function getPasswordEditElement() { + + return document.getElementById("password"); + +} + + + +function getEmployeeIdEditElement() { + + return document.getElementById("employeeId"); + +} + +//End getters and setters From e85c8d75cf0d59c2e30b3c9b7c007a241e58a13d Mon Sep 17 00:00:00 2001 From: Alexis Jamilee Carter Date: Fri, 9 Apr 2021 20:53:43 -0500 Subject: [PATCH 15/15] Task 10. EmployeeDetail Client Side Functionality. --- .DS_Store | Bin 6148 -> 6148 bytes src/main/.DS_Store | Bin 6148 -> 6148 bytes src/main/java/edu/uark/registerapp/.DS_Store | Bin 6148 -> 6148 bytes .../edu/uark/registerapp/commands/.DS_Store | Bin 6148 -> 0 bytes .../activeUsers/ActiveUserDeleteCommand.java | 38 ++++ .../ValidateActiveUserCommand.java | 41 ++++ .../registerapp/commands/employees/.DS_Store | Bin 6148 -> 0 bytes .../employees/ActiveEmployeeExistsQuery.java | 21 ++ .../commands/employees/EmployeeByIdQuery.java | 45 ---- ...ployee.java => EmployeeCreateCommand.java} | 193 ++++++++-------- ...ployee.java => EmployeeDeleteCommand.java} | 87 ++++---- .../commands/employees/EmployeeQuery.java | 43 ++++ .../employees/EmployeeSignInCommand.java | 108 +++++++++ ...ployee.java => EmployeeUpdateCommand.java} | 164 +++++++------- .../employees/helpers/EmployeeHelper.java | 3 + .../controllers/BaseRestController.java | 89 ++++++++ .../EmployeeDetailRouteController.java | 148 +++++++++++++ .../controllers/EmployeeRestController.java | 100 +++++++++ .../controllers/MainMenuRouteController.java | 12 +- .../ProductDetailRouteController.java | 92 ++++++-- .../ProductListingRouteController.java | 33 ++- .../controllers/SignInRestController.java | 35 +++ .../controllers/SignInRouteController.java | 95 ++++++++ .../enums/QueryParameterMessages.java | 45 ++++ .../enums/QueryParameterNames.java | 3 +- .../controllers/enums/ViewModelNames.java | 6 +- .../controllers/enums/ViewNames.java | 5 +- .../edu/uark/registerapp/models/.DS_Store | Bin 6148 -> 0 bytes .../registerapp/models/api/EmployeeType.java | 55 +++++ .../repositories/EmployeeRepository.java | 4 +- src/main/resources/.DS_Store | Bin 6148 -> 6148 bytes src/main/resources/static/.DS_Store | Bin 6148 -> 6148 bytes src/main/resources/static/images/Sign-Out.png | Bin 0 -> 417 bytes .../resources/static/images/signout-img.svg | 1 + .../static/scripts/employeeDetail.js | 207 ++++++++++++++++++ src/main/resources/static/scripts/mainMenu.js | 56 +++-- src/main/resources/static/scripts/master.js | 42 +++- src/main/resources/static/styles/master.css | 33 +++ .../resources/templates/employeeDetail.html | 23 +- src/main/resources/templates/mainMenu.html | 62 ++++++ .../resources/templates/productDetail.html | 19 +- .../resources/templates/productListing.html | 12 +- src/main/resources/templates/signIn.html | 1 - 43 files changed, 1579 insertions(+), 342 deletions(-) delete mode 100644 src/main/java/edu/uark/registerapp/commands/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java create mode 100644 src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java delete mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java rename src/main/java/edu/uark/registerapp/commands/employees/{CreateEmployee.java => EmployeeCreateCommand.java} (85%) rename src/main/java/edu/uark/registerapp/commands/employees/{DeleteEmployee.java => EmployeeDeleteCommand.java} (76%) create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java create mode 100644 src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java rename src/main/java/edu/uark/registerapp/commands/employees/{UpdateEmployee.java => EmployeeUpdateCommand.java} (86%) create mode 100644 src/main/java/edu/uark/registerapp/controllers/BaseRestController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/SignInRestController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java create mode 100644 src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java delete mode 100644 src/main/java/edu/uark/registerapp/models/.DS_Store create mode 100644 src/main/java/edu/uark/registerapp/models/api/EmployeeType.java create mode 100644 src/main/resources/static/images/Sign-Out.png create mode 100644 src/main/resources/static/images/signout-img.svg create mode 100644 src/main/resources/static/scripts/employeeDetail.js create mode 100644 src/main/resources/templates/mainMenu.html diff --git a/.DS_Store b/.DS_Store index 9a874b5768f336915163bb88cd434575b859f936..feca8613504bcd7430524202412f605a970dd548 100644 GIT binary patch delta 24 gcmZoMXfc>z$H*|zUVdZZGWN;3BJ7*FIezj30AOtgj{pDw delta 26 icmZoMXfc>z$H+L*UY?P0W8yOQ$$BDeo4GlD@&f>7?FW(o diff --git a/src/main/.DS_Store b/src/main/.DS_Store index 9aaf253d36b255d6c35926aaf89a35d8cbee0eb7..508c44cb3887c56308b5dcf9f69527b4bf5b4fec 100644 GIT binary patch delta 25 fcmZoMXfc?Oz{of;(H=-Fl-qdFjeRpK$A5kRYg-6w delta 32 ocmZoMXfc?OF!7-FWCIbFiT#3%3=#jKq8$xemk=d5XecJBHFcf$KIR0 zlc(@{0m$OeJ^^z84P6l@4P(=F^^x60=AJ0FM~^RTcJaB!&-(lO-k;vD3MKF*>9=W=sWAfm;Q1_NL9X|3A}zng5lOmI|Z-|5O2)EMJxj zzFhRy(Q9h2E%X)rY~&i9!CEoVS}`BA6+d3_ir#a*1`du^&b*Zq^G85+NlOL(LV<58 C3L|O& diff --git a/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java b/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java new file mode 100644 index 00000000..ccd22837 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/activeUsers/ActiveUserDeleteCommand.java @@ -0,0 +1,38 @@ +package edu.uark.registerapp.commands.activeUsers; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import edu.uark.registerapp.commands.VoidCommandInterface; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.repositories.ActiveUserRepository; + +@Service +public class ActiveUserDeleteCommand implements VoidCommandInterface { + @Transactional + @Override + public void execute() { + final Optional activeUserEntity = + this.activeUserRepository.findBySessionKey(this.sessionKey); + + if (activeUserEntity.isPresent()) { + this.activeUserRepository.delete(activeUserEntity.get()); + } + } + + // Properties + private String sessionKey; + public String getSessionKey() { + return this.sessionKey; + } + public ActiveUserDeleteCommand setSessionKey(final String sessionKey) { + this.sessionKey = sessionKey; + return this; + } + + @Autowired + private ActiveUserRepository activeUserRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java b/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java new file mode 100644 index 00000000..d04784cb --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/activeUsers/ValidateActiveUserCommand.java @@ -0,0 +1,41 @@ +package edu.uark.registerapp.commands.activeUsers; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.repositories.ActiveUserRepository; + +@Service +public class ValidateActiveUserCommand implements ResultCommandInterface { + @Override + public ActiveUserEntity execute() { + final Optional activeUserEntity = + this.activeUserRepository.findBySessionKey(this.sessionKey); + + if (!activeUserEntity.isPresent()) { + throw new UnauthorizedException(); + } + + return activeUserEntity.get(); + } + + // Properties + private String sessionKey; + + public String getSessionKey() { + return this.sessionKey; + } + + public ValidateActiveUserCommand setSessionKey(final String sessionKey) { + this.sessionKey = sessionKey; + return this; + } + + @Autowired + private ActiveUserRepository activeUserRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/.DS_Store b/src/main/java/edu/uark/registerapp/commands/employees/.DS_Store deleted file mode 100644 index 90ee518affea0aaa9fbbc016a2f055ca9b5a1855..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKONs(P3{6x}aN}~8bp>xwH0=qzz~Cs%qQPOreXl&1NAu;wFjCG++zc;}ysC6w z7xfmMj)-V?-mgVgBC>!R%Edx!_T0R&NoEuX$2$)4EvNnIW7r*=>cLxYZd$-0_21_oU;hY!t@H76AH)A9b|<)Sr^kqS_Odj+Pk zU0MI%!2g*4?@8QI0V?pX6wq0}?K@m4duww!>$L^`4!4>s+ze}{Ab2|ldOOC(+VPtg bMP0Eqp4Y@N(CNrK9mt;n(}hL_Uai0l9px39 diff --git a/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java new file mode 100644 index 00000000..9581f662 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/ActiveEmployeeExistsQuery.java @@ -0,0 +1,21 @@ +package edu.uark.registerapp.commands.employees; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.VoidCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class ActiveEmployeeExistsQuery implements VoidCommandInterface { + @Override + public void execute() { + if (!this.employeeRepository.existsByIsActive(true)) { + throw new NotFoundException("Employee"); + } + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java deleted file mode 100644 index 8aafdc22..00000000 --- a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeByIdQuery.java +++ /dev/null @@ -1,45 +0,0 @@ -// Last updated 02/20/2021 by Jodi Mitchell -// This file grabs a specific employee by their ID -package edu.uark.registerapp.commands.employees; - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class EmployeeByIdQuery implements ResultCommandInterface -{ - @Override - public Employee execute() - { - - final Optional employeeEntity = - this.employeeRepository.findByEmployeeId(this.employeeId); // This is throwing errors because the - Checking to see if the employee exist. // operations in EmployeeRepository.java are - if (employeeEntity.isPresent()){ // commented out. I had to do this so the app - return new Employee(employeeEntity.get()); // will run as expected. - } else { // I think Once we get everything put together - throw new NotFoundException("Employee"); // with the routers we can uncomment those // functions. - } - } - - // Getters and Setters - private int employeeId; - public int getEmployeeId() { - return this.employeeId; - } - public EmployeeByIdQuery setemployeeId(final int employeeId) { - this.employeeId = employeeId; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; -} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/CreateEmployee.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java similarity index 85% rename from src/main/java/edu/uark/registerapp/commands/employees/CreateEmployee.java rename to src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java index a63ea746..5471dfa5 100644 --- a/src/main/java/edu/uark/registerapp/commands/employees/CreateEmployee.java +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeCreateCommand.java @@ -1,96 +1,97 @@ -// This will create a new employee - -package edu.uark.registerapp.commands.employees; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; -import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.enums.EmployeeClassification; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class CreateEmployee implements ResultCommandInterface { - @Override - public Employee execute() { - this.validateProperties(); - - // If this is the first employee to be added and make them the general manager - if (this.isInitialEmployee) { - this.apiEmployee.setClassification( - EmployeeClassification.GENERAL_MANAGER.getClassification()); - } - - // Create a new ENTITY object from the API object details. - final EmployeeEntity employeeEntity = - this.employeeRepository.save(new EmployeeEntity(this.apiEmployee)); - - // Synchronize information generated by the database upon INSERT. - this.apiEmployee.setId(employeeEntity.getId()); - // Only send the password over the network when modifying the database. - this.apiEmployee.setPassword(StringUtils.EMPTY); - this.apiEmployee.setCreatedOn(employeeEntity.getCreatedOn()); - this.apiEmployee.setEmployeeId( - EmployeeHelper.padEmployeeId( - employeeEntity.getEmployeeId())); - - return this.apiEmployee; - } - - // Helper methods - // This checks the fields on the view. - // Will not accept if any of the fields are blank. - private void validateProperties() { - if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { - throw new UnprocessableEntityException("first name"); - } - if (StringUtils.isBlank(this.apiEmployee.getLastName())) { - throw new UnprocessableEntityException("last name"); - } - if (StringUtils.isBlank(this.apiEmployee.getPassword())) { - throw new UnprocessableEntityException("password"); - } - - // If there is a single employee in the DB then select a position for the new addition - if (!this.isInitialEmployee - && (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED)) { - - throw new UnprocessableEntityException("classification"); - } - } - - // Getters and Setters for the new employee - private Employee apiEmployee; - public Employee getApiEmployee() { - return this.apiEmployee; - } - public CreateEmployee setApiEmployee(final Employee apiEmployee) { - this.apiEmployee = apiEmployee; - return this; - } - - // Getters and Setters for the first employee, the general manager. - private boolean isInitialEmployee; - public boolean getIsInitialEmployee() { - return this.isInitialEmployee; - } - public CreateEmployee setIsInitialEmployee( - final boolean isInitialEmployee - ) { - - this.isInitialEmployee = isInitialEmployee; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; - - public CreateEmployee() { - this.isInitialEmployee = false; - } -} \ No newline at end of file +// This will create a new employee + +package edu.uark.registerapp.commands.employees; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeCreateCommand implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + // If this is the first employee to be added and make them the general manager + if (this.isInitialEmployee) { + this.apiEmployee.setClassification( + EmployeeClassification.GENERAL_MANAGER.getClassification()); + } + + // Create a new ENTITY object from the API object details. + final EmployeeEntity employeeEntity = + this.employeeRepository.save(new EmployeeEntity(this.apiEmployee)); + + // Synchronize information generated by the database upon INSERT. + this.apiEmployee.setId(employeeEntity.getId()); + // Only send the password over the network when modifying the database. + this.apiEmployee.setPassword(StringUtils.EMPTY); + // Only send the password over the network when modifying the database. + this.apiEmployee.setCreatedOn(employeeEntity.getCreatedOn()); + this.apiEmployee.setEmployeeId( + EmployeeHelper.padEmployeeId( + employeeEntity.getEmployeeId())); + + return this.apiEmployee; + } + + // Helper methods + // This checks the fields on the view. + // Will not accept if any of the fields are blank. + private void validateProperties() { + if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { + throw new UnprocessableEntityException("first name"); + } + if (StringUtils.isBlank(this.apiEmployee.getLastName())) { + throw new UnprocessableEntityException("last name"); + } + if (StringUtils.isBlank(this.apiEmployee.getPassword())) { + throw new UnprocessableEntityException("password"); + } + + // If there is a single employee in the DB then select a position for the new addition + if (!this.isInitialEmployee + && (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED)) { + + throw new UnprocessableEntityException("classification"); + } + } + + // Getters and Setters for the new employee + private Employee apiEmployee; + public Employee getApiEmployee() { + return this.apiEmployee; + } + public EmployeeCreateCommand setApiEmployee(final Employee apiEmployee) { + this.apiEmployee = apiEmployee; + return this; + } + + // Getters and Setters for the first employee, the general manager. + private boolean isInitialEmployee; + public boolean getIsInitialEmployee() { + return this.isInitialEmployee; + } + public EmployeeCreateCommand setIsInitialEmployee( + final boolean isInitialEmployee + ) { + + this.isInitialEmployee = isInitialEmployee; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; + + public EmployeeCreateCommand() { + this.isInitialEmployee = false; + } +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/DeleteEmployee.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java similarity index 76% rename from src/main/java/edu/uark/registerapp/commands/employees/DeleteEmployee.java rename to src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java index 469ee2f8..0e750082 100644 --- a/src/main/java/edu/uark/registerapp/commands/employees/DeleteEmployee.java +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeDeleteCommand.java @@ -1,43 +1,44 @@ -// This will delete an employee by their employee ID - -package edu.uark.registerapp.commands.employees; - -import java.util.Optional; -import java.util.UUID; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import edu.uark.registerapp.commands.VoidCommandInterface; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class DeleteEmployee implements VoidCommandInterface { - @Transactional - @Override - public void execute() { - final Optional employeeEntity = - this.employeeRepository.findById(this.employeeId); // Find the employee first - if (!employeeEntity.isPresent()) { // No record with the associated record ID exists in the database. - throw new NotFoundException("Employee"); - } - - this.employeeRepository.delete(employeeEntity.get()); // Delete the employee if found - } - - // Getters and Setters - private UUID employeeId; - public UUID getEmployeeId() { - return this.employeeId; - } - public DeleteEmployee setEmployeeId(final UUID employeeId) { - this.employeeId = employeeId; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; -} +// This will delete an employee by their employee ID + +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import edu.uark.registerapp.commands.VoidCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeDeleteCommand implements VoidCommandInterface { + @Transactional + @Override + public void execute() { + final Optional employeeEntity = + this.employeeRepository.findById(this.employeeId);// Find the employee first + + if (!employeeEntity.isPresent()) { // No record with the associated record ID exists in the database. + throw new NotFoundException("Product"); + } + + this.employeeRepository.delete(employeeEntity.get()); // Delete the employee if found + } + + // Getters and Setters + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public EmployeeDeleteCommand setEmployeeId(final UUID productId) { + this.employeeId = productId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java new file mode 100644 index 00000000..335f375b --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeQuery.java @@ -0,0 +1,43 @@ +// Find employee by Id + +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeQuery implements ResultCommandInterface { + @Override + public Employee execute() { + final Optional employeeEntity = + this.employeeRepository.findById(this.employeeId); + + if (employeeEntity.isPresent()) { + return new Employee(employeeEntity.get()); + } else { + throw new NotFoundException("Employee"); + } + } + + // Getters and setters + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public EmployeeQuery setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java new file mode 100644 index 00000000..ad757154 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeSignInCommand.java @@ -0,0 +1,108 @@ +package edu.uark.registerapp.commands.employees; + +import java.util.Arrays; +import java.util.Optional; + +import javax.transaction.Transactional; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.api.EmployeeSignIn; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.repositories.ActiveUserRepository; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeSignInCommand implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + return new Employee(this.SignInEmployee()); + } + + // Helper methods + private void validateProperties() { + if (StringUtils.isBlank(this.employeeSignIn.getEmployeeId())) { + throw new UnprocessableEntityException("employee ID"); + } + try { + Integer.parseInt(this.employeeSignIn.getEmployeeId()); + } catch (final NumberFormatException e) { + throw new UnprocessableEntityException("employee ID"); + } + if (StringUtils.isBlank(this.employeeSignIn.getPassword())) { + throw new UnprocessableEntityException("password"); + } + } + + @Transactional + private EmployeeEntity SignInEmployee() { + final Optional employeeEntity = + this.employeeRepository.findByEmployeeId( + Integer.parseInt(this.employeeSignIn.getEmployeeId())); + + if (!employeeEntity.isPresent() + || !Arrays.equals( + employeeEntity.get().getPassword(), + EmployeeHelper.hashPassword(this.employeeSignIn.getPassword())) + ) { + + throw new UnauthorizedException(); + } + + final Optional activeUserEntity = + this.activeUserRepository + .findByEmployeeId(employeeEntity.get().getId()); + + if (!activeUserEntity.isPresent()) { + this.activeUserRepository.save( + (new ActiveUserEntity()) + .setSessionKey(this.sessionId) + .setEmployeeId(employeeEntity.get().getId()) + .setClassification( + employeeEntity.get().getClassification()) + .setName( + employeeEntity.get().getFirstName() + .concat(" ") + .concat(employeeEntity.get().getLastName()))); + } else { + this.activeUserRepository.save( + activeUserEntity.get().setSessionKey(this.sessionId)); + } + + return employeeEntity.get(); + } + + // Properties + private EmployeeSignIn employeeSignIn; + public EmployeeSignIn getEmployeeSignIn() { + return this.employeeSignIn; + } + public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn) { + this.employeeSignIn = employeeSignIn; + return this; + } + + private String sessionId; + public String getSessionId() { + return this.sessionId; + } + public EmployeeSignInCommand setSessionId(final String sessionId) { + this.sessionId = sessionId; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; + @Autowired + private ActiveUserRepository activeUserRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/UpdateEmployee.java b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java similarity index 86% rename from src/main/java/edu/uark/registerapp/commands/employees/UpdateEmployee.java rename to src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java index c0d044fa..9919dc2c 100644 --- a/src/main/java/edu/uark/registerapp/commands/employees/UpdateEmployee.java +++ b/src/main/java/edu/uark/registerapp/commands/employees/EmployeeUpdateCommand.java @@ -1,81 +1,83 @@ -package edu.uark.registerapp.commands.employees; - -import java.util.Optional; -import java.util.UUID; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import edu.uark.registerapp.commands.ResultCommandInterface; -import edu.uark.registerapp.commands.exceptions.NotFoundException; -import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; - -import edu.uark.registerapp.models.api.Employee; -import edu.uark.registerapp.models.entities.EmployeeEntity; -import edu.uark.registerapp.models.enums.EmployeeClassification; -import edu.uark.registerapp.models.repositories.EmployeeRepository; - -@Service -public class UpdateEmployee implements ResultCommandInterface { - @Override - public Employee execute() { - this.validateProperties(); - - this.updateEmployeeEntity(); - - return this.apiEmployee; - } - - // Exceptions for empty fields - private void validateProperties() { - if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { - throw new UnprocessableEntityException("first name"); - } - if (StringUtils.isBlank(this.apiEmployee.getLastName())) { - throw new UnprocessableEntityException("last name"); - } - if (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED) { - throw new UnprocessableEntityException("classification"); - } - } - - @Transactional - private void updateEmployeeEntity() { - final Optional queriedEmployeeEntity = - this.employeeRepository.findById(this.employeeId); - - if (!queriedEmployeeEntity.isPresent()) { - throw new NotFoundException("Employee"); // No record with the associated record ID exists in the database. - } - - this.apiEmployee = queriedEmployeeEntity.get() - .synchronize(this.apiEmployee); // Synchronize any incoming changes for UPDATE to the database. - - this.employeeRepository.save(queriedEmployeeEntity.get()); // Write, via an UPDATE, any changes to the database. - } - - // Getter and Setters for the universally unique Identifies that tuple - private UUID employeeId; - public UUID getEmployeeId() { - return this.employeeId; - } - public UpdateEmployee setEmployeeId(final UUID employeeId) { - this.employeeId = employeeId; - return this; - } - - // Getters and Setters for the API - private Employee apiEmployee; - public Employee getApiEmployee() { - return this.apiEmployee; - } - public UpdateEmployee setApiEmployee(final Employee apiEmployee) { - this.apiEmployee = apiEmployee; - return this; - } - - @Autowired - private EmployeeRepository employeeRepository; -} +// This will update an employee + +package edu.uark.registerapp.commands.employees; + +import java.util.Optional; +import java.util.UUID; + +import javax.transaction.Transactional; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.uark.registerapp.commands.ResultCommandInterface; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.entities.EmployeeEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; +import edu.uark.registerapp.models.repositories.EmployeeRepository; + +@Service +public class EmployeeUpdateCommand implements ResultCommandInterface { + @Override + public Employee execute() { + this.validateProperties(); + + this.updateEmployeeEntity(); + + return this.apiEmployee; + } + + // Exceptions for empty fields + private void validateProperties() { + if (StringUtils.isBlank(this.apiEmployee.getFirstName())) { + throw new UnprocessableEntityException("first name"); + } + if (StringUtils.isBlank(this.apiEmployee.getLastName())) { + throw new UnprocessableEntityException("last name"); + } + if (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED) { + throw new UnprocessableEntityException("classification"); + } + } + + @Transactional + private void updateEmployeeEntity() { + final Optional queriedEmployeeEntity = + this.employeeRepository.findById(this.employeeId); + + if (!queriedEmployeeEntity.isPresent()) { + throw new NotFoundException("Employee"); // No record with the associated record ID exists in the database. + } + + this.apiEmployee = queriedEmployeeEntity.get() + .synchronize(this.apiEmployee); // Synchronize any incoming changes for UPDATE to the database. + + this.employeeRepository.save(queriedEmployeeEntity.get()); // Write, via an UPDATE, any changes to the database. + } + + // Getter and Setters for the universally unique Identifies that tuple + private UUID employeeId; + public UUID getEmployeeId() { + return this.employeeId; + } + public EmployeeUpdateCommand setEmployeeId(final UUID employeeId) { + this.employeeId = employeeId; + return this; + } + + // Getters and Setters for the API + private Employee apiEmployee; + public Employee getApiEmployee() { + return this.apiEmployee; + } + public EmployeeUpdateCommand setApiEmployee(final Employee apiEmployee) { + this.apiEmployee = apiEmployee; + return this; + } + + @Autowired + private EmployeeRepository employeeRepository; +} diff --git a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java index d0e0c210..09a42fd1 100644 --- a/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java +++ b/src/main/java/edu/uark/registerapp/commands/employees/helpers/EmployeeHelper.java @@ -1,5 +1,8 @@ package edu.uark.registerapp.commands.employees.helpers; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + import org.apache.commons.lang3.StringUtils; public class EmployeeHelper { diff --git a/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java b/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java new file mode 100644 index 00000000..2dee66e1 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/BaseRestController.java @@ -0,0 +1,89 @@ +package edu.uark.registerapp.controllers; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand; +import edu.uark.registerapp.commands.exceptions.ConflictException; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; +import edu.uark.registerapp.controllers.enums.QueryParameterMessages; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.ApiResponse; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; + +public class BaseRestController extends BaseController { + protected ApiResponse redirectSessionNotActive( + final HttpServletResponse response + ) { + + response.setStatus(HttpStatus.FOUND.value()); + return (new ApiResponse()) + .setRedirectUrl( + ViewNames.SIGN_IN.getRoute().concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.SESSION_NOT_ACTIVE.getKeyAsString()))); + } + + protected ApiResponse redirectUserNotElevated( + final HttpServletRequest request, + final HttpServletResponse response + ) { + + return this.redirectUserNotElevated(request, response, ViewNames.MAIN_MENU.getRoute()); + } + + protected ApiResponse redirectUserNotElevated( + final HttpServletRequest request, + final HttpServletResponse response, + final String redirectRoute + ) { + + try { + final ActiveUserEntity activeUserEntity = + this.validateActiveUserCommand + .setSessionKey(request.getSession().getId()) + .execute(); + + if (activeUserEntity == null) { + return this.redirectSessionNotActive(response); + } else if (!EmployeeClassification.isElevatedUser(activeUserEntity.getClassification())) { + response.setStatus(HttpStatus.FOUND.value()); + + return (new ApiResponse()) + .setRedirectUrl( + redirectRoute.concat( + this.buildInitialQueryParameter( + QueryParameterNames.ERROR_CODE.getValue(), + QueryParameterMessages.NO_PERMISSIONS_FOR_ACTION.getKeyAsString()))); + } + } catch (final UnauthorizedException e) { + return this.redirectSessionNotActive(response); + } + + return new ApiResponse(); + } + + @ExceptionHandler({ + ConflictException.class, + NotFoundException.class, + UnauthorizedException.class, + UnprocessableEntityException.class + }) + public @ResponseBody ApiResponse handleError(final Exception e) { + return (new ApiResponse()).setErrorMessage(e.getMessage()); + } + + // Properties + @Autowired + private ValidateActiveUserCommand validateActiveUserCommand; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java b/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java new file mode 100644 index 00000000..90ba211d --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/EmployeeDetailRouteController.java @@ -0,0 +1,148 @@ +package edu.uark.registerapp.controllers; + +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; +import edu.uark.registerapp.commands.employees.EmployeeQuery; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.Employee; +import edu.uark.registerapp.models.api.EmployeeType; +import edu.uark.registerapp.models.entities.ActiveUserEntity; + +@Controller +@RequestMapping(value = "/employeeDetail") +public class EmployeeDetailRouteController extends BaseRouteController { + @RequestMapping(method = RequestMethod.GET) + public ModelAndView start( + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final boolean activeUserExists = this.activeUserExists(); + + //see if active user exists + if (activeUserExists) { + final Optional activeUserEntity = + this.getCurrentUser(request); + + if (!activeUserEntity.isPresent()) + { + return this.buildInvalidSessionResponse(); + } else if (!this.isElevatedUser(activeUserEntity.get())) + { + return this.buildNoPermissionsResponse(); + } + } + + return this.buildStartResponse(!activeUserExists, queryParameters); + } + + @RequestMapping(value = "/{employeeId}", method = RequestMethod.GET) + public ModelAndView startWithEmployee( + @PathVariable final UUID employeeId, + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final Optional activeUserEntity = + this.getCurrentUser(request); + + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } else if (!this.isElevatedUser(activeUserEntity.get())) { + return this.buildNoPermissionsResponse(); + } + + return this.buildStartResponse(employeeId, queryParameters); + } + + // Helper methods + private boolean activeUserExists() { + try { + this.activeEmployeeExistsQuery.execute(); + return true; + } catch (final NotFoundException e) { + return false; + } + } + + private ModelAndView buildStartResponse( + final boolean isInitialEmployee, + final Map queryParameters + ) { + + return this.buildStartResponse( + isInitialEmployee, + (new UUID(0, 0)), + queryParameters); + } + + private ModelAndView buildStartResponse( + final UUID employeeId, + final Map queryParameters + ) { + + return this.buildStartResponse(false, employeeId, queryParameters); + } + + private ModelAndView buildStartResponse( + final boolean isInitialEmployee, + final UUID employeeId, + final Map queryParameters + ) { + + ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.EMPLOYEE_DETAIL.getViewName()), + queryParameters); + + if (employeeId.equals(new UUID(0, 0))) { + modelAndView.addObject( + ViewModelNames.EMPLOYEE.getValue(), + (new Employee()).setIsInitialEmployee(isInitialEmployee)); + } else { + try { + modelAndView.addObject( + ViewModelNames.EMPLOYEE.getValue(), + this.employeeQuery + .setEmployeeId(employeeId) + .execute() + .setIsInitialEmployee(isInitialEmployee)); + } catch (final Exception e) { + modelAndView.addObject( + ViewModelNames.ERROR_MESSAGE.getValue(), + e.getMessage()); + modelAndView.addObject( + ViewModelNames.EMPLOYEE.getValue(), + (new Employee()).setIsInitialEmployee(isInitialEmployee)); + } + } + + modelAndView.addObject( + ViewModelNames.EMPLOYEE_TYPES.getValue(), + EmployeeType.allEmployeeTypes()); + + return modelAndView; + } + + // Properties + @Autowired + private EmployeeQuery employeeQuery; + + @Autowired + private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java b/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java new file mode 100644 index 00000000..236a0291 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/EmployeeRestController.java @@ -0,0 +1,100 @@ +package edu.uark.registerapp.controllers; + +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; +import edu.uark.registerapp.commands.employees.EmployeeCreateCommand; +import edu.uark.registerapp.commands.employees.EmployeeUpdateCommand; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.ApiResponse; +import edu.uark.registerapp.models.api.Employee; + +@RestController +@RequestMapping(value = "/api/employee") +public class EmployeeRestController extends BaseRestController { + @RequestMapping(value = "/", method = RequestMethod.POST) + public @ResponseBody ApiResponse createEmployee( + @RequestBody final Employee employee, + final HttpServletRequest request, + final HttpServletResponse response + ) { + + boolean isInitialEmployee = false; + ApiResponse canCreateEmployeeResponse; + + try { + this.activeEmployeeExistsQuery.execute(); + + canCreateEmployeeResponse = + this.redirectUserNotElevated(request, response); + } catch (final NotFoundException e) { + isInitialEmployee = true; + canCreateEmployeeResponse = new ApiResponse(); + } + + if (!canCreateEmployeeResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { + return canCreateEmployeeResponse; + } + + final Employee createdEmployee = + this.employeeCreateCommand + .setApiEmployee(employee) + .setIsInitialEmployee(isInitialEmployee) + .execute(); + + if (isInitialEmployee) { + createdEmployee + .setRedirectUrl( + ViewNames.SIGN_IN.getRoute().concat( + this.buildInitialQueryParameter( + QueryParameterNames.EMPLOYEE_ID.getValue(), + createdEmployee.getEmployeeId()))); + } + + return createdEmployee.setIsInitialEmployee(isInitialEmployee); + } + + @RequestMapping(value = "/{employeeId}", method = RequestMethod.PATCH) + public @ResponseBody ApiResponse updateEmployee( + @PathVariable final UUID employeeId, + @RequestBody final Employee employee, + final HttpServletRequest request, + final HttpServletResponse response + ) { + + final ApiResponse elevatedUserResponse = + this.redirectUserNotElevated(request, response); + if (!elevatedUserResponse.getRedirectUrl().equals(StringUtils.EMPTY)) { + return elevatedUserResponse; + } + + return this.employeeUpdateCommand + .setEmployeeId(employeeId) + .setApiEmployee(employee) + .execute(); + } + + // Properties + @Autowired + private EmployeeCreateCommand employeeCreateCommand; + + @Autowired + private EmployeeUpdateCommand employeeUpdateCommand; + + @Autowired + private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java b/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java index 94470fbf..90cd6724 100644 --- a/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java +++ b/src/main/java/edu/uark/registerapp/controllers/MainMenuRouteController.java @@ -23,13 +23,17 @@ public class MainMenuRouteController extends BaseRouteController{ //@RequestParam reads form data and binds it to the specified parameters; maps the request parameter to query parameter //parameters for get request: Map and HttpServletRequest //Map object is a collection of keyed-in elements using any type of key - public ModelAndView start(@RequestParam final Map queryParameters, final HttpServletRequest request) { + public ModelAndView start( + @RequestParam final Map queryParameters, + final HttpServletRequest request) { //if there's an active user in the current session, add any error messages received in requestParam to the view //serve up the main menu view/doc //ELSE: immediately redirect to the Sign-In view document with an appropriate error message - final Optional activeUserEntity = + final Optional + activeUserEntity = this.getCurrentUser(request); - if (!activeUserEntity.isPresent()) { + if (!activeUserEntity.isPresent() + ) { return this.buildInvalidSessionResponse(); } @@ -41,6 +45,8 @@ public ModelAndView start(@RequestParam final Map queryParameters modelAndView.addObject( ViewModelNames.IS_ELEVATED_USER.getValue(), this.isElevatedUser(activeUserEntity.get())); + + return modelAndView; } } diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java b/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java index f6f35713..96b28f7a 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductDetailRouteController.java @@ -1,50 +1,94 @@ package edu.uark.registerapp.controllers; +import java.util.Map; // added +import java.util.Optional; // added import java.util.UUID; +import javax.servlet.http.HttpServletRequest; // added + import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; //added import org.springframework.web.servlet.ModelAndView; import edu.uark.registerapp.commands.products.ProductQuery; import edu.uark.registerapp.controllers.enums.ViewModelNames; import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.Product; +import edu.uark.registerapp.models.entities.ActiveUserEntity; // added +import edu.uark.registerapp.models.enums.EmployeeClassification; // added @Controller @RequestMapping(value = "/productDetail") -public class ProductDetailRouteController { +public class ProductDetailRouteController extends BaseRouteController { + // added extend BaseRouteController @RequestMapping(method = RequestMethod.GET) - public ModelAndView start() { - return (new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName())) - .addObject( - ViewModelNames.PRODUCT.getValue(), - (new Product()).setLookupCode(StringUtils.EMPTY).setCount(0)); + // added parameters to start + public ModelAndView start( + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + final Optional activeUserEntity = + this.getCurrentUser(request); + if (!activeUserEntity.isPresent()) { + return this.buildInvalidSessionResponse(); + } else if (!this.isElevatedUser(activeUserEntity.get())) { + return this.buildNoPermissionsResponse( + ViewNames.PRODUCT_LISTING.getRoute()); + } + + final ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()), + queryParameters); + + modelAndView.addObject( + ViewModelNames.IS_ELEVATED_USER.getValue(), + true); + modelAndView.addObject( + ViewModelNames.PRODUCT.getValue(), + (new Product()).setLookupCode(StringUtils.EMPTY).setCount(0)); + + return modelAndView; } @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ModelAndView startWithProduct(@PathVariable final UUID productId) { - final ModelAndView modelAndView = - new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()); - - try { - modelAndView.addObject( - ViewModelNames.PRODUCT.getValue(), - this.productQuery.setProductId(productId).execute()); - } catch (final Exception e) { - modelAndView.addObject( - ViewModelNames.ERROR_MESSAGE.getValue(), - e.getMessage()); - modelAndView.addObject( - ViewModelNames.PRODUCT.getValue(), - (new Product()) - .setCount(0) - .setLookupCode(StringUtils.EMPTY)); - } + public ModelAndView startWithProduct( + @PathVariable final UUID productId, + @RequestParam final Map queryParameters, + final HttpServletRequest request + ){ + final Optional activeUserEntity = this.getCurrentUser(request); + if(!activeUserEntity.isPresent()) + { + return this.buildInvalidSessionResponse(); + } + + final ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.PRODUCT_DETAIL.getViewName()), queryParameters); + + modelAndView.addObject(ViewModelNames.IS_ELEVATED_USER.getValue(), + EmployeeClassification.isElevatedUser(activeUserEntity.get().getClassification())); + + try { + modelAndView.addObject( + ViewModelNames.PRODUCT.getValue(), + this.productQuery.setProductId(productId).execute()); + } catch (final Exception e) { + modelAndView.addObject( + ViewModelNames.ERROR_MESSAGE.getValue(), + e.getMessage()); + modelAndView.addObject( + ViewModelNames.PRODUCT.getValue(), + (new Product()) + .setCount(0) + .setLookupCode(StringUtils.EMPTY)); + } return modelAndView; } diff --git a/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java b/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java index 3c2a1178..e4e8164a 100644 --- a/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java +++ b/src/main/java/edu/uark/registerapp/controllers/ProductListingRouteController.java @@ -1,23 +1,46 @@ package edu.uark.registerapp.controllers; +import java.util.Map; +import java.util.Optional; + +import javax.servlet.http.HttpServletRequest; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import edu.uark.registerapp.commands.products.ProductsQuery; import edu.uark.registerapp.controllers.enums.ViewModelNames; import edu.uark.registerapp.controllers.enums.ViewNames; import edu.uark.registerapp.models.api.Product; +import edu.uark.registerapp.models.entities.ActiveUserEntity; @Controller -@RequestMapping(value = "/") -public class ProductListingRouteController { +@RequestMapping(value = "/productListing") +public class ProductListingRouteController extends BaseRouteController { @RequestMapping(method = RequestMethod.GET) - public ModelAndView showProductListing() { + public ModelAndView showProductListing( + @RequestParam final Map queryParameters, + final HttpServletRequest request + ) { + + final Optional activeUserEntity = + this.getCurrentUser(request); + if (!activeUserEntity.isPresent()) { + return buildInvalidSessionResponse(); + } + ModelAndView modelAndView = - new ModelAndView(ViewNames.PRODUCT_LISTING.getViewName()); + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.PRODUCT_LISTING.getViewName()), + queryParameters); + + modelAndView.addObject( + ViewModelNames.IS_ELEVATED_USER.getValue(), + this.isElevatedUser(activeUserEntity.get())); try { modelAndView.addObject( @@ -38,4 +61,4 @@ public ModelAndView showProductListing() { // Properties @Autowired private ProductsQuery productsQuery; -} +} \ No newline at end of file diff --git a/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java b/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java new file mode 100644 index 00000000..96741f18 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/SignInRestController.java @@ -0,0 +1,35 @@ +// Task 4 - Assigned to Ben Thiele +package edu.uark.registerapp.controllers; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import edu.uark.registerapp.commands.activeUsers.ActiveUserDeleteCommand; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.ApiResponse; + +@RestController +@RequestMapping(value = "/api") +public class SignInRestController extends BaseRestController { + @RequestMapping(value="/signOut", method = RequestMethod.DELETE) + public @ResponseBody ApiResponse removeActiveUser( + final HttpServletRequest request + ) { + + this.activeUserDeleteCommand + .setSessionKey(request.getSession().getId()) + .execute(); + + return (new ApiResponse()) + .setRedirectUrl(ViewNames.SIGN_IN.getRoute()); + } + + // Properties + @Autowired + private ActiveUserDeleteCommand activeUserDeleteCommand; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java b/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java new file mode 100644 index 00000000..ec374a9b --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/SignInRouteController.java @@ -0,0 +1,95 @@ +// Task 4 - Assigned to Ben Thiele +package edu.uark.registerapp.controllers; + +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; +import edu.uark.registerapp.commands.employees.EmployeeSignInCommand; +import edu.uark.registerapp.commands.exceptions.NotFoundException; +import edu.uark.registerapp.controllers.enums.QueryParameterNames; +import edu.uark.registerapp.controllers.enums.ViewModelNames; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.EmployeeSignIn; + +@Controller // Defines Class as route handler +@RequestMapping(value = "/") // Maps class to url address +public class SignInRouteController extends BaseRouteController { + @RequestMapping(method = RequestMethod.GET) // Method responds to GET request + // Show sign in screen + public ModelAndView showSignIn( + @RequestParam final Map queryParameters // Extract query string + ) { + // Check if active employees exist, redirect to employee detail page if not + try { + this.activeEmployeeExistsQuery.execute(); + } catch (NotFoundException e) { + return new ModelAndView( + REDIRECT_PREPEND.concat( + ViewNames.EMPLOYEE_DETAIL.getRoute())); + } + //If active employees exist go to sign in + ModelAndView modelAndView = + this.setErrorMessageFromQueryString( + new ModelAndView(ViewNames.SIGN_IN.getViewName()), //Set view + queryParameters); + + if (queryParameters.containsKey(QueryParameterNames.EMPLOYEE_ID.getValue())) { + modelAndView.addObject( + ViewModelNames.EMPLOYEE_ID.getValue(), // Set model + queryParameters.get(QueryParameterNames.EMPLOYEE_ID.getValue())); + } + + return modelAndView; + } + + // Respond to POST request + @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + public ModelAndView performSignIn( + EmployeeSignIn employeeSignIn, // Object to hold employee credentials + HttpServletRequest request + ) { + + try { + // Object validates employee information and session + // If fails, error message is displayed + this.employeeSignInCommand + .setSessionId(request.getSession().getId()) + .setEmployeeSignIn(employeeSignIn) + .execute(); + } catch (Exception e) { + ModelAndView modelAndView = + new ModelAndView(ViewNames.SIGN_IN.getViewName()); + + modelAndView.addObject( + ViewModelNames.ERROR_MESSAGE.getValue(), + e.getMessage()); + modelAndView.addObject( + ViewModelNames.EMPLOYEE_ID.getValue(), + employeeSignIn.getEmployeeId()); + + return modelAndView; + } + + // Successful login moves user to main menu + return new ModelAndView( + REDIRECT_PREPEND.concat( + ViewNames.MAIN_MENU.getRoute())); + } + + // Properties + @Autowired + private EmployeeSignInCommand employeeSignInCommand; + + @Autowired + private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; +} diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java new file mode 100644 index 00000000..3f1bed87 --- /dev/null +++ b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterMessages.java @@ -0,0 +1,45 @@ +package edu.uark.registerapp.controllers.enums; + +import java.util.HashMap; +import java.util.Map; + +public enum QueryParameterMessages { + NOT_DEFINED(-1, ""), + SESSION_NOT_ACTIVE(1001, "The current user's session is no longer active."), + NO_PERMISSIONS_TO_VIEW(1101, "You do not have permission to view this resource."), + NO_PERMISSIONS_FOR_ACTION(1102, "You do not have permission to perform this action."); + + public int getKey() { + return this.key; + } + public String getKeyAsString() { + return Integer.toString(this.key); + } + public String getMessage() { + return this.message; + } + + public static String mapMessage(final int key) { + if (valueMap == null) { + valueMap = new HashMap(); + + for (final QueryParameterMessages status : QueryParameterMessages.values()) { + valueMap.put(status.getKey(), status.getMessage()); + } + } + + return (valueMap.containsKey(key) + ? valueMap.get(key) + : QueryParameterMessages.NOT_DEFINED.getMessage()); + } + + private int key; + private String message; + + private static Map valueMap = null; + + private QueryParameterMessages(final int key, final String message) { + this.key = key; + this.message = message; + } +} diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java index 988a9c79..d20b0100 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/QueryParameterNames.java @@ -2,7 +2,8 @@ public enum QueryParameterNames { NOT_DEFINED(""), - ERROR_CODE("errorCode"); + ERROR_CODE("errorCode"), + EMPLOYEE_ID("employeeId"); public String getValue() { return value; diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java index 7c39b102..fefde2fd 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/ViewModelNames.java @@ -3,8 +3,12 @@ public enum ViewModelNames { NOT_DEFINED(""), ERROR_MESSAGE("errorMessage"), + IS_ELEVATED_USER("isElevatedUser"), PRODUCTS("products"), // Product listing - PRODUCT("product"); // Product detail + PRODUCT("product"), // Product detail + EMPLOYEE_ID("employeeId"), // Sign in + EMPLOYEE("employee"), // Employee detail + EMPLOYEE_TYPES("employeeTypes"); public String getValue() { return value; diff --git a/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java b/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java index cd1a1246..3e3e2ca2 100644 --- a/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java +++ b/src/main/java/edu/uark/registerapp/controllers/enums/ViewNames.java @@ -1,8 +1,11 @@ package edu.uark.registerapp.controllers.enums; public enum ViewNames { + SIGN_IN("signIn", "/"), + MAIN_MENU("mainMenu"), PRODUCT_DETAIL("productDetail"), - PRODUCT_LISTING("productListing", "/"); + EMPLOYEE_DETAIL("employeeDetail"), + PRODUCT_LISTING("productListing"); public String getRoute() { return this.route; diff --git a/src/main/java/edu/uark/registerapp/models/.DS_Store b/src/main/java/edu/uark/registerapp/models/.DS_Store deleted file mode 100644 index fdf856ed6e2deef61ab5f13996e688143a4f5d4a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%TB{U3>?!URf0p09QPOagH=_&fFIz|9wHS|k@lV&zs>jo$|`W>0Jh|vtnJAv zbBNafWOLr#0&4&(x+30nnVaU-XLb>hB~kPp&xfbJKWOyZ`Y3NexeGkvh;BDb`Fp(L zgwA}AVVb;zCeL_j!`FCY;)ouD@p|q%MjCE&cy3aGR3H^d1yX^3r~rGm+Um+N*Hj=C zNCmzW(EmfBE7ri#(S99VYy=?cHQtQRHcJqT28cCqbYz4kP9-{3Vq_4fGhU*u29AzS zhs5w9ak9k3A$B_7U#uKb9dk_uQh`>1g>To||L^EO%>OMZcd0-s@TU}z<@SEN;U`6J zoqe44+CqPze;RA8oQt($qP1dfv=!e>>WV%yUjs)+qcd)FVqOGPm$X#iHx&2)8%!pv diff --git a/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java b/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java new file mode 100644 index 00000000..f26c5f0e --- /dev/null +++ b/src/main/java/edu/uark/registerapp/models/api/EmployeeType.java @@ -0,0 +1,55 @@ +package edu.uark.registerapp.models.api; + +import org.apache.commons.lang3.StringUtils; + +import edu.uark.registerapp.models.enums.EmployeeClassification; + +public class EmployeeType { + private int classification; + public int getClassification() { + return this.classification; + } + public EmployeeType setClassification(final int classification) { + this.classification = classification; + return this; + } + + private String displayLabel; + + public String getDisplayLabel() { + return this.displayLabel; + } + + public EmployeeType setDisplayLabel(final String displayLabel) { + this.displayLabel = displayLabel; + return this; + } + + public static EmployeeType[] allEmployeeTypes() { + final EmployeeClassification[] employeeClassifications = + EmployeeClassification.values(); + final EmployeeType[] employeeTypes = + new EmployeeType[employeeClassifications.length]; + + for (int i = 0; i < employeeClassifications.length; i++) { + employeeTypes[i] = new EmployeeType(employeeClassifications[i]); + } + + return employeeTypes; + } + + public EmployeeType() { + this(-1, StringUtils.EMPTY); + } + + public EmployeeType(final EmployeeClassification employeeClassification) { + this( + employeeClassification.getClassification(), + employeeClassification.getDisplayLabel()); + } + + public EmployeeType(final int classification, final String displayLabel) { + this.displayLabel = displayLabel; + this.classification = classification; + } +} diff --git a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java index 12ce2419..9575faa3 100644 --- a/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java +++ b/src/main/java/edu/uark/registerapp/models/repositories/EmployeeRepository.java @@ -8,8 +8,8 @@ import edu.uark.registerapp.models.entities.EmployeeEntity; public interface EmployeeRepository extends CrudRepository { - boolean Active(boolean isActive); - boolean employeeIdExists(int employeeId); + boolean existsByIsActive(boolean isActive); + boolean existsByEmployeeId(int employeeId); Optional findById(UUID id); Optional findByEmployeeId(int employeeId); } \ No newline at end of file diff --git a/src/main/resources/.DS_Store b/src/main/resources/.DS_Store index 2a43b86b965c6735a338394c4d17e6a7a5a71078..0cb76803b7520acd74714e5841cf34217723e6f9 100644 GIT binary patch delta 51 zcmZoMXfc@J&&V_}z(!;AALaNM*=nC}7B8NCdNsH_q{4-^|YOmmdH| CbPUu0 delta 28 icmZoMXfc@J&&W72z#2&ONNhap!anhV{APBJzx)7(6bZTj diff --git a/src/main/resources/static/.DS_Store b/src/main/resources/static/.DS_Store index eba13223f6781b153af5c8a6f35630fbb0dc1cc4..c71596a8c56d5643516b3ca8ff7f7b7f3d961cd9 100644 GIT binary patch delta 110 zcmZoMXfc=|&e%S&P;8=}q9`*10|O%ig8&0VDnoHfadJ*letyoxjp|Y$2{wjIhFpe3 zhIC{}hKW6L+-wZR3?&Se3^@#`8|PTEZ)WEZ;9zXuxbZvlWPTA{MuyFfB8QnLHn0Ey D>XRJC delta 90 zcmZoMXfc=|&e%4wP;8=}q9`K+0|O8XFfbG_lrR*h6es5-7GIp>_a(3m+}_QaKE&z3o3q#8Dyh{QRup`m9ZI--K<7}2X0QOT_6(UfB~ zfFo0iTM!k^g5q{n0XmBNtqSl%ad9?U37>C(0R|XgKr-NscF2HNf}kBL;Dz{~wmv|? zI=>#2+PS61|;Y%W<1K>^~fyX^@&QOFD-2!R*p^-#O=;bI$z%Lh{J>>1jl;00000 LNkvXXu0mjf0)eri literal 0 HcmV?d00001 diff --git a/src/main/resources/static/images/signout-img.svg b/src/main/resources/static/images/signout-img.svg new file mode 100644 index 00000000..0c8c0350 --- /dev/null +++ b/src/main/resources/static/images/signout-img.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/main/resources/static/scripts/employeeDetail.js b/src/main/resources/static/scripts/employeeDetail.js new file mode 100644 index 00000000..b8e22393 --- /dev/null +++ b/src/main/resources/static/scripts/employeeDetail.js @@ -0,0 +1,207 @@ +let hideEmployeeSavedAlertTimer = undefined; + +document.addEventListener("DOMContentLoaded", () => { + document.getElementById("saveButton") + .addEventListener("click", saveActionClick); + + const employeeFirstNameEditElement = + getEmployeeFirstNameEditElement(); + employeeFirstNameEditElement.focus(); + employeeFirstNameEditElement.select(); +}); + +// Save +function saveActionClick(event) { + if (!validateSave()) { + return; + } + + const saveActionElement = event.target; + saveActionElement.disabled = true; + + const employeeId = getEmployeeId(); + const employeeIdIsDefined = (employeeId.trim() !== ""); + const saveActionUrl = ("/api/employee/" + + (employeeIdIsDefined ? employeeId : "")); + const saveEmployeeRequest = { + id: employeeId, + managerId: getEmployeeManagerId(), + lastName: getEmployeeLastNameEditElement().value, + password: getEmployeePasswordEditElement().value, + firstName: getEmployeeFirstNameEditElement().value, + classification: getEmployeeTypeSelectElement().value + }; + + if (employeeIdIsDefined) { + ajaxPatch(saveActionUrl, saveEmployeeRequest, (callbackResponse) => { + saveActionElement.disabled = false; + + if (isSuccessResponse(callbackResponse)) { + completeSaveAction(callbackResponse); + } + }); + } else { + ajaxPost(saveActionUrl, saveEmployeeRequest, (callbackResponse) => { + saveActionElement.disabled = false; + + if (isSuccessResponse(callbackResponse)) { + completeSaveAction(callbackResponse); + } + }); + } +} + + +//task 10 validation of client info +function validateSave() { + const firstNameEditElement = getEmployeeFirstNameEditElement(); + + //error display for first name + if (firstNameEditElement.value.trim() === "") + { + displayError("Please provide a valid employee first name."); + //focus and reset + firstNameEditElement.focus(); + firstNameEditElement.select(); + return false; + } + + //error display for last name + const lastNameEditElement = getEmployeeLastNameEditElement(); + if (lastNameEditElement.value.trim() === "") + { + displayError("Please provide a valid employee last name."); + //focus and select + lastNameEditElement.focus(); + lastNameEditElement.select(); + return false; + } + + //error display for password if blank + const passwordEditElement = getEmployeePasswordEditElement(); + if (passwordEditElement.value.trim() === "") + { + displayError("Please provide a valid employee password."); + //focus and select + passwordEditElement.focus(); + passwordEditElement.select(); + return false; + } + + //error display for password + if (passwordEditElement.value !== getEmployeeConfirmPassword()) + { + displayError("Passwords do not match."); + //focus and select + passwordEditElement.focus() + passwordEditElement.select(); + return false; + } + + const employeeTypeSelectElement = getEmployeeTypeSelectElement(); + + //error display for employee type + if (!employeeTypeSelectElement.closest("tr").classList.contains("hidden")) + { + if (employeeTypeSelectElement.value <= 0) + { + displayError("Please provide a valid employee Type."); + //focus + employeeTypeSelectElement.focus(); + return false; + } + } + + return true; +} + +//Save employee info and flag it so there's no more editing +function completeSaveAction(callbackResponse) { + if (callbackResponse.data == null) { + return; + } + + if ((callbackResponse.data.redirectUrl != null) + && (callbackResponse.data.redirectUrl !== "")) { + + window.location.replace(callbackResponse.data.redirectUrl); + return; + } + + displayEmployeeSavedAlertModal(); + + const employeeEmployeeIdElement = getEmployeeEmployeeIdElement(); + const employeeEmployeeIdRowElement = employeeEmployeeIdElement.closest("tr"); + if (employeeEmployeeIdRowElement.classList.contains("hidden")) + { + setEmployeeId(callbackResponse.data.id); + employeeEmployeeIdElement.value = callbackResponse.data.employeeId; + employeeEmployeeIdRowElement.classList.remove("hidden"); + } +} + +function displayEmployeeSavedAlertModal() { + if (hideEmployeeSavedAlertTimer) { + clearTimeout(hideEmployeeSavedAlertTimer); + } + + const savedAlertModalElement = getSavedAlertModalElement(); + savedAlertModalElement.style.display = "none"; + savedAlertModalElement.style.display = "block"; + + hideEmployeeSavedAlertTimer = setTimeout(hideEmployeeSavedAlertModal, 1200); +} + +//hide after saving +function hideEmployeeSavedAlertModal() { + if (hideEmployeeSavedAlertTimer) { + clearTimeout(hideEmployeeSavedAlertTimer); + } + + getSavedAlertModalElement().style.display = "none"; +} +// End save + +//Getters and setters +function getEmployeeId() { + return document.getElementById("employeeId").value; +} +function setEmployeeId(employeeId) { + document.getElementById("employeeId").value = employeeId; +} + +function getEmployeeManagerId() { + return document.getElementById("employeeManagerId").value; +} + +function getEmployeeEmployeeId() { + return getEmployeeEmployeeIdElement().value; +} +function getEmployeeEmployeeIdElement() { + return document.getElementById("employeeEmployeeId"); +} + +function getSavedAlertModalElement() { + return document.getElementById("employeeSavedAlertModal"); +} + +function getEmployeeFirstNameEditElement() { + return document.getElementById("employeeFirstName"); +} + +function getEmployeeLastNameEditElement() { + return document.getElementById("employeeLastName"); +} + +function getEmployeePasswordEditElement() { + return document.getElementById("employeePassword"); +} + +function getEmployeeConfirmPassword() { + return document.getElementById("employeeConfirmPassword").value; +} + +function getEmployeeTypeSelectElement() { + return document.getElementById("employeeType"); +} +//End getters and setters diff --git a/src/main/resources/static/scripts/mainMenu.js b/src/main/resources/static/scripts/mainMenu.js index d2467edc..e3777091 100644 --- a/src/main/resources/static/scripts/mainMenu.js +++ b/src/main/resources/static/scripts/mainMenu.js @@ -1,45 +1,43 @@ -// This is the functionality for the main menu. -document.addEventListener("DOMContentLoaded", function(event) -{ - startTransaction.addEventListener("click", transactionActionElement); +document.addEventListener("DOMContentLoaded", function(event) { + getStartTransactionActionElement().addEventListener( + "click", + () => { displayError("Functionality has not yet been implemented."); }); - viewProducts.addEventListener("click", productsActionElement); + getViewProductsActionElement().addEventListener( + "click", + () => { window.location.assign("/productListing"); }); - createEmployee.addEventListener("click", employeeActionElement); + getCreateEmployeeActionElement().addEventListener( + "click", + () => { window.location.assign("/employeeDetail"); }); - salesReport.addEventListener("click", productSalesReportActionElement); + getProductSalesReportActionElement().addEventListener( + "click", + () => { displayError("Functionality has not yet been implemented."); }); - cashiersReport.addEventListener("click", cashierSalesReportActionElement); + getCashierSalesReportActionElement().addEventListener( + "click", + () => { displayError("Functionality has not yet been implemented."); }); - signOut.addEventListener("click", signOutActionElement); // signOut is the id of the sign out button - }); - -function transactionActionElement() { - document.getElementById("errorMessage").innerHTML = "Functionality has not yet been implemented"; +// Getters and setters +function getViewProductsActionElement() { + return document.getElementById("viewProducts"); } -function productsActionElement() { - window.location.href = "\productListing.html"; // go to the product listing page +function getCreateEmployeeActionElement() { + return document.getElementById("createEmployee"); } -function employeeActionElement() { - window.location.href = "\employeeDetails.html"; // go to the employee detail page +function getStartTransactionActionElement() { + return document.getElementById("startTransaction"); } -function productSalesReportActionElement() { - document.getElementById("errorMessage").innerHTML = "Functionality has not yet been implemented"; - +function getProductSalesReportActionElement() { + return document.getElementById("salesReport"); } -function cashierSalesReportActionElement() { - document.getElementById("errorMessage").innerHTML = "Functionality has not yet been implemented"; - -} - -function signOutActionElement() { - - window.location.href = "\signIn.html"; // Go to the sign in page - +function getCashierSalesReportActionElement() { + return document.getElementById("cashiersReport"); } \ No newline at end of file diff --git a/src/main/resources/static/scripts/master.js b/src/main/resources/static/scripts/master.js index 88b039d0..a990676c 100644 --- a/src/main/resources/static/scripts/master.js +++ b/src/main/resources/static/scripts/master.js @@ -1,3 +1,10 @@ +document.addEventListener("DOMContentLoaded", () => { + const signOutActionElement = getSignOutActionElement(); + if (signOutActionElement != null) { + signOutActionElement.addEventListener("click", signOutActionClickHandler); + } +}); + // AJAX function ajaxGet(resourceRelativeUri, callback) { return ajax(resourceRelativeUri, "GET", null, callback); @@ -19,10 +26,12 @@ function ajaxDelete(resourceRelativeUri, callback) { return ajax(resourceRelativeUri, "DELETE", null, callback); } +//ajax method to use in saving employee details function ajax(resourceRelativeUri, verb, data, callback) { const httpRequest = new XMLHttpRequest(); - if (httpRequest == null) { + if (httpRequest == null) + { return httpRequest; } @@ -158,6 +167,10 @@ function displayError(errorMessage) { // End display error message //Getters and setters +function getSignOutActionElement() { + return document.getElementById("signOutImage"); +} + function getErrorMessageContainerElement() { return document.getElementById("error"); } @@ -166,3 +179,30 @@ function getErrorMessageDisplayElement() { return document.getElementById("errorMessage"); } // End getters and setters + +//Sign out +function signOutActionClickHandler() { + ajaxDelete("/api/signOut", (callbackResponse) => { + if ((callbackResponse.data != null) + && (callbackResponse.data.redirectUrl != null) + && (callbackResponse.data.redirectUrl !== "")) { + + window.location.replace(callbackResponse.data.redirectUrl); + } else { + window.location.replace("/"); + } + }); +} +//End sign out + +// Menu button function +document.addEventListener("DOMContentLoaded", function(event) { + goToMenu().addEventListener( + "click", + () => { window.location.assign("/mainMenu"); }); +}); + +function goToMenu() +{ + return document.getElementById("menuButton"); +} \ No newline at end of file diff --git a/src/main/resources/static/styles/master.css b/src/main/resources/static/styles/master.css index 2b060e6d..585a4a08 100644 --- a/src/main/resources/static/styles/master.css +++ b/src/main/resources/static/styles/master.css @@ -1,3 +1,36 @@ +/* Beginning of menu style */ +.container { + display: inline-block; + cursor: pointer; + } + + .bar1, .bar2, .bar3 { + width: 35px; + height: 5px; + background-color: #333; + margin: 6px 0; + transition: 0.4s; + } + /* End of menu style */ + +div.footer { + left: 0; + right: 0; + bottom: 0; + position: absolute; +} + +img.footer { + z-index: 1; + height: 50px; + text-decoration: none; + display: inline-block; + cursor: pointer; + position: fixed; + right: 20; + bottom: 20; +} + div.header { padding-top: 1px; text-align: right; diff --git a/src/main/resources/templates/employeeDetail.html b/src/main/resources/templates/employeeDetail.html index 85b4cb05..f5f7bde7 100644 --- a/src/main/resources/templates/employeeDetail.html +++ b/src/main/resources/templates/employeeDetail.html @@ -11,38 +11,49 @@ +
-

Employee Detail

+

Employee Detail

+

- + +
+
Employee ID: +
First Name: @@ -51,24 +62,28 @@

Last Name: +
Password: +
Confirm Password: +
Employee Type: + @@ -80,9 +95,11 @@


+
+ - + diff --git a/src/main/resources/templates/mainMenu.html b/src/main/resources/templates/mainMenu.html new file mode 100644 index 00000000..a07afacc --- /dev/null +++ b/src/main/resources/templates/mainMenu.html @@ -0,0 +1,62 @@ + + + + Register - Main Menu + + + + + + + + + + +
+

Main Menu

+
+ +
+
+

+
+ + +
+ + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/productDetail.html b/src/main/resources/templates/productDetail.html index ac183115..4b3e5248 100644 --- a/src/main/resources/templates/productDetail.html +++ b/src/main/resources/templates/productDetail.html @@ -12,6 +12,11 @@ +

Product Detail

@@ -34,27 +39,27 @@

Lookup Code: - +
Count: - +
-
+

- +

- +
+ + diff --git a/src/main/resources/templates/productListing.html b/src/main/resources/templates/productListing.html index c4b758ae..6fd1a6db 100644 --- a/src/main/resources/templates/productListing.html +++ b/src/main/resources/templates/productListing.html @@ -12,6 +12,12 @@ + +

Products Listing

@@ -22,7 +28,7 @@

-
+ @@ -54,6 +60,10 @@

+ + diff --git a/src/main/resources/templates/signIn.html b/src/main/resources/templates/signIn.html index 27d9be30..6f4c3994 100644 --- a/src/main/resources/templates/signIn.html +++ b/src/main/resources/templates/signIn.html @@ -1,4 +1,3 @@ -