Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,30 @@ public String updateUserProfile(@RequestParam("userid") int userid,@RequestParam
return "redirect:index";
}

/* Called by Admin from DisplayCustomer page to deactivate or soft delete the user*/
@GetMapping(value = "/deleteUser/{userId}")
public ModelAndView deletUser(@PathVariable int userId){
try{
User updatedUser = this.userService.deleteUser(userId);
System.out.println(updatedUser);

} catch (Exception e) {
System.out.println("Exception:"+e);
}
return getCustomerDetail();
}

/* Called by Admin from DisplayCustomer page to activate the user after deactivating*/
@GetMapping(value = "/activateUser/{userId}")
public ModelAndView activateUser(@PathVariable int userId){
try{
User updatedUser = this.userService.activateUser(userId);
System.out.println(updatedUser);

} catch (Exception e) {
System.out.println("Exception:"+e);
}
return getCustomerDetail();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.sound.midi.Soundbank;

import org.hibernate.Session;
Expand Down Expand Up @@ -70,7 +71,7 @@ public boolean userExists(String username) {

@Transactional
public User getUserByUsername(String username) {
Query<User> query = sessionFactory.getCurrentSession().createQuery("from User where username = :username", User.class);
Query<User> query = sessionFactory.getCurrentSession().createQuery("from CUSTOMER where username = :username AND isActive = 1", User.class);
query.setParameter("username", username);

try {
Expand All @@ -80,4 +81,27 @@ public User getUserByUsername(String username) {
return null;
}
}

/* Set isActive to False which is meant triggers soft delete*/
@Transactional
public User deleteUser(int userID){
TypedQuery<User> query = sessionFactory.getCurrentSession().createQuery("from CUSTOMER where id = :userID", User.class);
query.setParameter("userID", userID);
User user = query.getSingleResult();
user.setIsActive(false);
this.sessionFactory.getCurrentSession().save(user);
return user;
}

/* Set isActive to True when User wants to access the website again after deactivating the account*/
@Transactional
public User activateUser(int userID){
TypedQuery<User> query = sessionFactory.getCurrentSession().createQuery("from CUSTOMER where id = :userID", User.class);
query.setParameter("userID", userID);
User user = query.getSingleResult();
user.setIsActive(true);
this.sessionFactory.getCurrentSession().save(user);
return user;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class User {
private String role;

private String address;

private Boolean isActive;


public int getId() {
Expand Down Expand Up @@ -77,7 +79,9 @@ public String getAddress() {
public void setAddress(String address) {
this.address = address;
}



public Boolean getIsActive() { return isActive; }

public void setIsActive(Boolean isActive) { this.isActive = isActive;}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ public boolean checkUserExists(String username) {

public User getUserByUsername(String username) {
return userDao.getUserByUsername(username);
}
}

public User deleteUser(int userID) { return this.userDao.deleteUser(userID) ; }

public User activateUser(int userID) { return this.userDao.activateUser(userID) ; }


}
9 changes: 9 additions & 0 deletions JtProject/src/main/webapp/views/displayCustomers.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
${customer.address}

</td>
<td>
<c:if test="${customer.isActive eq false}">
<a href="/admin/activateUser/${customer.id}" class="card-link">Activate</a>
</c:if>
<c:if test="${customer.isActive eq true}">
<a href="/admin/deleteUser/${customer.id}" class="card-link">De-Activate</a>
</c:if>
</td>

</tr>
</c:forEach>

Expand Down
Loading