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 @@ -35,7 +35,8 @@
@Entity
@NamedQueries({
@NamedQuery(name = "findUserByEmail", query = "SELECT u FROM User u where u.email = :p_email"),
@NamedQuery(name = "findUserByEmailAndPassword", query = "SELECT u FROM User u where u.email = :p_email and u.password = :p_password")
@NamedQuery(name = "findUserByEmailAndPassword", query = "SELECT u FROM User u where u.email = :p_email and u.password = :p_password"),
@NamedQuery(name = "findUsersByEmail", query = "SELECT u FROM User u where UPPER(u.email) LIKE :p_email")
})
public class User implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import io.github.todolist.core.domain.User;

import java.util.List;

/**
* Interface for user repository.
*
Expand All @@ -41,6 +43,14 @@ public interface UserRepository {
*/
User getUserByEmail(final String email);

/**
* Search users by (partial) email.
*
* @param email the email fragment to search for
* @return the list of users whose email contains the given fragment
*/
List<User> searchUsersByEmail(final String email);

/**
* Check user's email and password.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public User getUserByEmail(final String email) {
return (users != null && !users.isEmpty()) ? users.get(0) : null;
}

/**
* {@inheritDoc}
*/
public List<User> searchUsersByEmail(final String email) {
TypedQuery<User> query = entityManager.createNamedQuery("findUsersByEmail", User.class);
query.setParameter("p_email", "%" + email.toUpperCase() + "%");
return query.getResultList();
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import io.github.todolist.core.domain.User;

import java.util.List;

/**
* Business interface for user service.
*
Expand All @@ -41,6 +43,14 @@ public interface UserService {
*/
User getUserByEmail(final String email);

/**
* Search users by (partial) email.
*
* @param email the email fragment to search for
* @return the list of users whose email contains the given fragment
*/
List<User> searchUsersByEmail(final String email);

/**
* Check user's email and password.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* Implementation of the {@link UserService}.
*
Expand Down Expand Up @@ -72,6 +74,14 @@ public User getUserByEmail(final String email) {
return userRepository.getUserByEmail(email);
}

/**
* {@inheritDoc}
*/
@Transactional(readOnly = true)
public List<User> searchUsersByEmail(final String email) {
return userRepository.searchUsersByEmail(email);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* The MIT License
*
* Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.github.benas.todolist.web.action.user;

import com.opensymphony.xwork2.Action;
import io.github.benas.todolist.web.action.BaseAction;
import io.github.todolist.core.domain.User;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* Action class to search users by email.
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public class SearchUserAction extends BaseAction {
private static final Logger logger = LogManager.getLogger(SearchUserAction.class);

private String email;

List<User> userList;

public String execute() {
logger.info("Searching for: " + email);
userList = userService.searchUsersByEmail(email);
return Action.SUCCESS;
}

/*
* Getters for model attributes
*/
public String getEmail() {
return email;
}

public List<User> getUserList() {
return userList;
}

/*
* Setters for request parameters binding
*/

public void setEmail(String email) {
this.email = email;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@
<result>/WEB-INF/views/user/account.jsp</result>
</action>

<action name="search" class="io.github.benas.todolist.web.action.user.SearchUserAction">
<interceptor-ref name="loginInterceptor"/>
<interceptor-ref name="defaultStack"/>
<result>/WEB-INF/views/user/search.jsp</result>
</action>

</package>

<package name="public" namespace="/" extends="struts-default">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,14 @@
</div>
</s:form>
</li>
<li class="nav-header">Search users</li>
<li>
<s:form cssClass="form-search" namespace="/user" action="search" id="searchUserForm" method="get">
<div class="input-append">
<s:textfield name="email" cssClass="input-small search-query" placeholder="search by email" required="required"/>
<s:submit cssClass="btn" value="Go!" />
</div>
</s:form>
</li>
</ul>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="tl" uri="http://todolist.org/taglib" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../common/header.jspf"%>

<div class="container">
<div class="row">
<div class="span3">
<%@ include file="../common/sidebar.jspf"%>
</div>
<div class="span9">
<div class="well">
<div class="page-header">
<h1>Search results for email: '${param.email}' </h1>
</div>

<table class="table table-bordered table-striped">

<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>

<tbody>
<c:forEach items="${requestScope.userList}" var="currentUser">
<tr>
<td>${currentUser.id}</td>
<td>${currentUser.name}</td>
<td><tl:highlight pattern="${param.email}" cssClass="label label-warning">${currentUser.email}</tl:highlight></td>
</tr>
</c:forEach>

</tbody>
</table>

<c:if test="${empty requestScope.userList}">
<div class="alert alert-info">
<div align="center">No user with email matching '${param.email}'</div>
</div>
</c:if>

</div>
</div>
</div>
</div>

<%--end content--%>
<%@ include file="../common/footer.jspf"%>