forked from nvsecurity/java-github-actions-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchService.java
More file actions
49 lines (39 loc) · 1.62 KB
/
SearchService.java
File metadata and controls
49 lines (39 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package hawk.service;
import hawk.entity.Item;
import hawk.form.Search;
import org.hibernate.Session;
import org.hibernate.jdbc.ReturningWork;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.EntityManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SearchService {
private static final Logger LOGGER = Logger.getLogger(SearchService.class.getName());
@Autowired
EntityManager entityManager;
public List<Item> search(Search search) {
final Session session = (Session) entityManager.unwrap(Session.class);
return session.doReturningWork(new ReturningWork<List<Item>>() {
@Override
public List<Item> execute(Connection connection) throws SQLException {
List<Item> items = new ArrayList<>();
String query = "select id, name, description from ITEM where description like ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, "%" + search.getSearchText() + "%");
LOGGER.log(Level.INFO, "SQL Query {0}", statement);
ResultSet rs = statement.executeQuery();
while (rs.next()) {
items.add(new Item(rs.getLong("id"), rs.getString("name"), rs.getString("description")));
}
rs.close();
return items;
}
});
}
}