Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1601924205543</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Binary file added src/main/java/.DS_Store
Binary file not shown.
Binary file added src/main/java/edu/.DS_Store
Binary file not shown.
Binary file added src/main/java/edu/uark/.DS_Store
Binary file not shown.
Binary file added src/main/java/edu/uark/registerapp/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package edu.uark.registerapp.commands.activeUsers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;

import edu.uark.registerapp.commands.VoidCommandInterface;
import edu.uark.registerapp.commands.exceptions.NotFoundException;
import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException;
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> activeUserEntity =
this.activeUserRepository.findBySessionKey(this.sessionKey);
validateProperties(activeUserEntity);
if (!activeUserEntity.isPresent()) { // No record with the associated record ID exists in the database.
throw new NotFoundException("Active User");
}

this.activeUserRepository.delete(activeUserEntity.get());
}


private void validateProperties(final Optional<ActiveUserEntity> activeUserEntity) {

if (StringUtils.isBlank(activeUserEntity.get().getName())) {
throw new UnprocessableEntityException("name");
}
}


// 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package edu.uark.registerapp.commands.employees.helpers;

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() {

boolean check = this.employeeRepository.existsByIsActive(true);
if (!check) {
throw new NotFoundException("Employee");
}
}

@Autowired
private EmployeeRepository employeeRepository;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package edu.uark.registerapp.commands.employees.helpers;

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.VoidCommandInterface;
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.models.api.EmployeeSignIn;
import edu.uark.registerapp.models.entities.ActiveUserEntity;
import edu.uark.registerapp.models.repositories.ActiveUserRepository;
import edu.uark.registerapp.models.entities.EmployeeEntity;
import edu.uark.registerapp.models.repositories.EmployeeRepository;


@Service
public class EmployeeSignInCommand implements VoidCommandInterface {
@Override
public void execute() {
this.validateProperties();


final Optional<EmployeeEntity> employeeEntity =
this.employeeRepository.findByEmployeeId(Integer.valueOf(employeeSignIn.getEmployeeId()));
byte[] pass = this.employeeSignIn.getPassword().getBytes();
if (employeeEntity.isPresent()) {
byte[] dataPass = employeeEntity.get().getPassword();
if(!pass.equals(dataPass))
throw new UnauthorizedException();
} else {
throw new NotFoundException("Product");
}

this.createActiveUserEntity(employeeEntity);

}

@Transactional
private ActiveUserEntity createActiveUserEntity(final Optional<EmployeeEntity> employeeEntity) {

final Optional<ActiveUserEntity> queriedActiveUserEntity =
this.activeUserRepository
.findByEmployeeId(employeeEntity.get().getId());

if (queriedActiveUserEntity.isPresent()) {
queriedActiveUserEntity.get().setSessionKey(this.getSessionKey());
return this.activeUserRepository.save(queriedActiveUserEntity.get());
} else {
return this.activeUserRepository.save(new ActiveUserEntity()
.setClassification(employeeEntity.get().getClassification())
.setName(employeeEntity.get().getFirstName() + " " + employeeEntity.get().getLastName())
.setEmployeeId((employeeEntity.get().getId()))
.setSessionKey(this.getSessionKey())
);
}
}


//Helper method
private void validateProperties() {
if (StringUtils.isBlank(this.employeeSignIn.getEmployeeId()) || !StringUtils.isNumeric(this.employeeSignIn.getEmployeeId())) {
throw new UnprocessableEntityException("employeeID");
}
if (StringUtils.isBlank(this.employeeSignIn.getPassword())) {
throw new UnprocessableEntityException("password");
}
}


//Properties
private EmployeeSignIn employeeSignIn;
private String sessionKey;

public EmployeeSignIn getEmployeeSignIn() { return this.employeeSignIn; }

public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn) {
this.employeeSignIn = employeeSignIn;
return this;
}

public String getSessionKey() { return this.sessionKey; }

public EmployeeSignInCommand setSessionKey(final String sessionKey) {
this.sessionKey = sessionKey;
return this;
}


@Autowired
private EmployeeRepository employeeRepository;
private ActiveUserRepository activeUserRepository;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

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;

Expand All @@ -17,10 +19,15 @@ public class SignInRestController extends BaseRestController {
public @ResponseBody ApiResponse removeActiveUser(
final HttpServletRequest request
) {

// TODO: Sign out the user associated with request.getSession().getId()
this.activeUserDeleteCommand
.setSessionKey(request.getSession().getId())
.execute();

return (new ApiResponse())
.setRedirectUrl(ViewNames.SIGN_IN.getRoute());
return new ApiResponse().setRedirectUrl(ViewNames.SIGN_IN.getRoute());
}
}

@Autowired
private ActiveUserDeleteCommand activeUserDeleteCommand;

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,65 @@
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.helpers.ActiveEmployeeExistsQuery;
import edu.uark.registerapp.commands.employees.helpers.EmployeeSignInCommand;
import edu.uark.registerapp.controllers.enums.ViewNames;
import edu.uark.registerapp.models.api.EmployeeSignIn;

@Controller
@RequestMapping(value = "/")
public class SignInRouteController extends BaseRouteController {
// TODO: Route for initial page load
@RequestMapping(method = RequestMethod.GET)
public ModelAndView start(
@RequestParam final Map<String, String> queryParameters
) {
try {
this.activeEmployeeExistsQuery.execute();
} catch (final Exception e) {
return new ModelAndView(REDIRECT_PREPEND.concat(ViewNames.EMPLOYEE_DETAIL.getRoute()));
}
return new ModelAndView(ViewNames.SIGN_IN.getViewName());
}

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ModelAndView performSignIn(
// TODO: Define an object that will represent the sign in request and add it as a parameter here
HttpServletRequest request
EmployeeSignIn employeeSignIn,
final HttpServletRequest request
) {

// TODO: Use the credentials provided in the request body
// and the "id" property of the (HttpServletRequest)request.getSession() variable
// to sign in the user

try {
this.employeeSignInCommand
.setEmployeeSignIn(employeeSignIn)
.setSessionKey(request.getSession().getId())
.execute();
} catch (final Exception e) {
return this.buildInvalidSessionResponse();
}

return new ModelAndView(
REDIRECT_PREPEND.concat(
ViewNames.MAIN_MENU.getRoute()));
}

@Autowired
private ActiveEmployeeExistsQuery activeEmployeeExistsQuery;

@Autowired
private EmployeeSignInCommand employeeSignInCommand;
}
40 changes: 40 additions & 0 deletions src/main/java/edu/uark/registerapp/models/api/EmployeeSignIn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.uark.registerapp.models.api;


import org.apache.commons.lang3.StringUtils;

public class EmployeeSignIn {

private String employeeId;
private String password;

public String getEmployeeId() {
return this.employeeId;
}

public void setEmployeeId(final String employeeId) {
this.employeeId = employeeId;
}

public String getPassword() {
return this.password;
}

public void setPassword(final String password) {
this.password = password;
}

public EmployeeSignIn() {
this.employeeId = StringUtils.EMPTY;
this.password = StringUtils.EMPTY;
}

public EmployeeSignIn(final EmployeeSignIn employeeSignIn) {
this.password = employeeSignIn.getPassword();
this.employeeId = employeeSignIn.getEmployeeId();

}

}