diff --git a/.gitignore b/.gitignore index 3466d7bf..00a4d03a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ HELP.md mvnw mvnw.cmd target/ - diff --git a/EmployeeCreateCommand.java b/EmployeeCreateCommand.java new file mode 100644 index 00000000..31281f46 --- /dev/null +++ b/EmployeeCreateCommand.java @@ -0,0 +1,68 @@ +@Service +public class EmployeeCreateCommand implements ResultCommandInterface { + private Employee employee; + private boolean initialEmployee; + private EmployeeRepository employeeRepository; + final EmployeeEntity employeeEntity; + + public Employee validate() { + + if (initialEmployee == true) { + employee.setClassification( + EmployeeClassification.GENERAL_MANAGER.getClassification()); + } + + + employeeEntity = + this.employeeRepository.save(employeeEntity(this.employee)); + this.employee.setId(employeeEntity.getId()); + + this.employee.setPassword(StringUtils.EMPTY); + this.employee.setCreatedOn(employeeEntity.getCreatedOn()); + this.employee.setEmployeeId( + employeeEntity.getEmployeeId()); + + return this.employee; + } + + private void checkIfEmpty() { + if (StringUtils.isBlank(this.eployee.getFirstName())) { + throw new Exception("firstNameIsEmpty"); + } + if (StringUtils.isBlank(this.employee.getLastName())) { + throw new Exception("lastNameIsEmpty"); + } + if (StringUtils.isBlank(this.employee.getPassword())) { + throw new Exception("passwordIsEmpty"); + } + + if (isInitialEmployee == false + && (EmployeeClassification.map(this.employee.getClassification()) == EmployeeClassification.NOT_DEFINED)) { + + throw new Exception("classification"); + } + } + + public Employee getEmployee() { + return employee; + } + + public EmployeeCreateCommand setEmployee(final Employee employee) { + this.employee = employee; + return this.employee; + } + + public boolean getInitialEmployee() { + return this.initialEmployee; + } + + public EmployeeCreateCommand setInitialEmployee(final boolean setInitial) { + + initialEmployee = setInitial; + return initialEmployee; + } + + public EmployeeCreate() { + initialEmployee = false; + } +} \ No newline at end of file diff --git a/EmployeeQuery.java b/EmployeeQuery.java new file mode 100644 index 00000000..ecd2142a --- /dev/null +++ b/EmployeeQuery.java @@ -0,0 +1,28 @@ +import java.util.UUID; + +public class EmployeeQuery { + private UUID employeeID; + + public Employee returnEmployee() { + @Autowired + EmployeeRepository idFinder; + EmployeeEntity findID = idFinder.findById(this.employeeID); + Employee employee = new Employee(findID.get()); ; + + if (findID){ + return employee; + } + else { + throw new Exception("NotFoundException"); + } + } + + public UUID getEmployeeID() { + return this.employeeID; + } + + public setEmployeeID(final UUID idSet) { + employeeID = idSet; + return employeeID; + } +} \ No newline at end of file diff --git a/EmployeeSignIn.java b/EmployeeSignIn.java new file mode 100644 index 00000000..14f881c0 --- /dev/null +++ b/EmployeeSignIn.java @@ -0,0 +1,31 @@ +package edu.uark.registerapp.models.api; +//This is from ApiResponse.java, aka under models/api +import org.apache.commons.lang3.StringUtils; + +public class EmployeeSignIn { + private String employeeId; + + public String getId() { + return this.employeeId; + } + + public SignIn setId(final String employeeId) { + this.employeeId = employeeId; + return this; + } + + private String password; + public String getPword() { + return this.password; + } + public EmployeeSignIn setPword( final String password) { + this.password = password; + return this; + } + + public EmployeeSignIn(final String password, final String employeeId) { + this.password = StringUtils.EMPTY; + this.employeeId = StringUtils.EMPTY; + } + +} \ No newline at end of file diff --git a/EmployeeUpdateCommand.java b/EmployeeUpdateCommand.java new file mode 100644 index 00000000..8468d142 --- /dev/null +++ b/EmployeeUpdateCommand.java @@ -0,0 +1,59 @@ +@Service +public class EmployeeUpdateCommand implements ResultCommandInterface { + + private Employee employee; + private EmployeeRepository employeeRepository; + private UUID employeeId; + + public Employee validate() { + this.validateProperties(); + + this.updateEntity(); + + return this.employee; + } + + // Helper methods + private void validateProperties() { + if (StringUtils.isBlank(this.employee.getFirstName())) { + throw new Exception("firstNameIsEmpty"); + } + if (StringUtils.isBlank(this.employee.getLastName())) { + throw new Exception("lastNameIsEmpty"); + } + if (EmployeeClassification.map(this.employee.getClassification()) == EmployeeClassification.NOT_DEFINED) { + throw new Exception("classification"); + } + } + + private void updateEntity() { + final EmployeeEntity employeeEntity = + this.employeeRepository.findById(this.employeeId); + + if (employeeEntity) { + throw new Exception("employeeDoesNotExist"); + } + + this.employee = employeeEntity.get() + .synchronize(this.employee); + + this.employeeRepository.save(employeeEntity.get()); + } + + public UUID getEmployeeId() { + return this.employeeId; + } + public EmployeeUpdateCommand setEmployeeId(final UUID idSet) { + this.employeeId = idSet; + return this.employeeId; + } + + public Employee getEmployee() { + return this.employee; + } + + public EmployeeUpdate setEmployee(final Employee employee) { + this.employee = employee; + return this; + } +} \ No newline at end of file diff --git a/Main_Menu_View.html b/Main_Menu_View.html new file mode 100644 index 00000000..b5a887fb --- /dev/null +++ b/Main_Menu_View.html @@ -0,0 +1,59 @@ + + + + Main Menu + + + + + +
+
+ +

+ +

+ +
+ + + +
+ +
+ +

+ +

+ +
+
+
+
+ + + + +
+
+ Sign Out + +
+
+ + + + + + + + diff --git a/README.md b/README.md index 86e4a71d..373c2e3f 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,4 @@ The application defaults to port 8080. To start from the IDE press F5 to run. This will open the debug and run view. You will need to create a launch configuration which Visual Studio Code will help you with. # Landing page (list available products) -`https://uarkregappjava.herokuapp.com/` \ No newline at end of file +`https://uarkregappjava.herokuapp.com/` diff --git a/SignIn.html b/SignIn.html new file mode 100644 index 00000000..1383f5cb --- /dev/null +++ b/SignIn.html @@ -0,0 +1,34 @@ + + + + Sign In + + + + + + + + +
+

Sign In

+
+ +
+
+

+
+ + + +
+
+
+ +
+ + + + + + \ No newline at end of file diff --git a/SignIn.js b/SignIn.js new file mode 100644 index 00000000..babedaf5 --- /dev/null +++ b/SignIn.js @@ -0,0 +1,51 @@ +document.addEventListener("DOMContentLoaded",function(event){ + const changeID = getchangeID(); + changeID.focus(); + changeID.select(); +}); + +function getchangePword(){ +//getter for password +return document.getElementById("password"); +} + +function getchangeID(){ +//getter for ID +return document.getElementById("number"); +} + +function ValidationEvent(){ +// This part deals with invalid inputs for the Employee ID + const changeID = getchangeID(); + if(isNaN(Number(changeID.value))){ + displayError("Enter a real ID"); + changeID.focus(); + changeID.select(); + return false; +} + else if (Number(changeID.value) <= 0){ + displayError("Enter a real ID"); + changeID.focus(); + changeID.select(); + return false; +} +//This part deals with invalid inputs for the Password +const changePword = getchangePword(); + if (changePword.value == null){ + displayError("Enter real password"); + changePword.focus(); + changePword.select(); + return false; + } + else if (changePword.value.trim() === ""){ + displayError("Enter a real password); + changePword.focus(); + changePword.select(); + return false; + + } + // Since this is a validation function, it should return true if valid input + + return true; + +} \ No newline at end of file diff --git a/SignInRestController.java b/SignInRestController.java new file mode 100644 index 00000000..79616a40 --- /dev/null +++ b/SignInRestController.java @@ -0,0 +1,158 @@ +package edu.uark.registerapp.controllers; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Map; +import java.util.Optional; +import org.apache.commons.lang3.StringUtils; +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 org.springframework.web.servlet.ModelAndView; +import edu.uark.registerapp.commands.activeUsers.ActiveUserDeleteCommand; +import edu.uark.registerapp.controllers.enums.ViewNames; +import edu.uark.registerapp.models.api.ApiResponse; +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.models.api.Product; +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.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; + +@RestController +@RequestMapping(value = "/api") +public class SignInRestController 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:"; + + + 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; + } + + +@DeleteMapping(value = "/signOut") +public @ResponseBody ApiResponse remove( +final HttpServletRequest req) { + +this.activeUserDeleteCommand +.setSessionKey(req.getSession().getId()) +.execute(); + +return (new ApiResponse()) +.setRedirectUrl(ViewNames.SIGN_IN.getRoute()); +} + +@DeleteMapping(value = "/{productId}") + public @ResponseBody ApiResponse deleteProduct( + @PathVariable final UUID productId + ) { + + this.productdeletecommand + .setProductId(productId) + .execute(); + + return new ApiResponse(); + } + +@Autowired +private ValidateActiveUserCommand validateActiveUserCommand; + +@Autowired +private ProductCreateCommand productcreatecommand; + +@Autowired +private ProductDeleteCommand productdeletecommand; + +@Autowired +private ProductUpdateCommand productupdatecommand; + +@Autowired +private ActiveUserDeleteCommand activeuserdeletecommand; +} + diff --git a/SignInRouteController.java b/SignInRouteController.java new file mode 100644 index 00000000..88942e97 --- /dev/null +++ b/SignInRouteController.java @@ -0,0 +1,186 @@ +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.products.ProductQuery; +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; +import java.util.UUID; +import org.apache.commons.lang3.StringUtils; +import edu.uark.registerapp.models.api.Product; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.servlet.ModelAndView; +import edu.uark.registerapp.commands.activeUsers.ValidateActiveUserCommand; +import edu.uark.registerapp.commands.exceptions.UnauthorizedException; +import edu.uark.registerapp.models.entities.ActiveUserEntity; +import edu.uark.registerapp.models.enums.EmployeeClassification; + + +@Controller +@RequestMapping(value = "/") +public class SignInRouteController 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:"; + + + 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; + } + +///-------------------------------------------------------------------------------------------------------------------------- +@GetMapping //This is the sign method/ it deals with the first part of task 4. +public ModelAndView sign(@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; +} + + + +//------------------------------------------------------------------------------------------------------------------------------------- +// This is the perform method, it is what actually performs the sign in + +@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) +public ModelAndView perform(EmployeeSignIn employee,HttpServletRequest req) { + +try {this.employeesignincommand.setSessionId(req.getSession().getId()).setEmployeeSignIn(employee) +.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(),employee.getEmployeeId()); + +return modelAndView; +} + +return new ModelAndView(REDIRECT_PREPEND.concat(ViewNames.MAIN_MENU.getRoute())); +} + + +// These are the various properties that are inside of the code +@Autowired +private ValidateActiveUserCommand validateActiveUserCommand; + +@Autowired +private ProductQuery productquery; + +@Autowired +private EmployeeSignInCommand employeesignincommand; + +@Autowired +private ActiveEmployeeExistsQuery activeemployeeexistsquery; +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 330a0022..486269f6 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,6 @@ javax.servlet jstl - org.apache.tomcat.embed tomcat-embed-jasper diff --git a/power_settings_new-24px.svg b/power_settings_new-24px.svg new file mode 100644 index 00000000..76c2685c --- /dev/null +++ b/power_settings_new-24px.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/scripts/Main_Menu_View.html b/scripts/Main_Menu_View.html new file mode 100644 index 00000000..95c54f6d --- /dev/null +++ b/scripts/Main_Menu_View.html @@ -0,0 +1,54 @@ + + + + Main Menu + + + + + +
+