Skip to content

Commit

Permalink
Merge branch 'elasticsearch-20240519-squash'
Browse files Browse the repository at this point in the history
  • Loading branch information
cleydyr committed Jun 1, 2024
2 parents 088f72e + f8c8a4c commit da075ab
Show file tree
Hide file tree
Showing 39 changed files with 930 additions and 487 deletions.
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<developers>
Expand Down Expand Up @@ -349,7 +349,19 @@
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.3</version>
<version>2.2.22</version>
</dependency>

<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.13.4</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public boolean save(AccessCardDTO dto) {
return false;
}

public DTOCollection<AccessCardDTO> search(PagedAccessCardSearchDTO pagedAccessCardSearchDTO) {
return this.accessCardDAO.search(
pagedAccessCardSearchDTO.code(),
pagedAccessCardSearchDTO.status(),
pagedAccessCardSearchDTO.limit(),
pagedAccessCardSearchDTO.offset());
}

public DTOCollection<AccessCardDTO> search(
String code, AccessCardStatus status, int limit, int offset) {
return this.accessCardDAO.search(code, status, limit, offset);
Expand Down
43 changes: 10 additions & 33 deletions src/main/java/biblivre/administration/accesscards/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,32 @@
import biblivre.core.ExtendedRequest;
import biblivre.core.ExtendedResponse;
import biblivre.core.enums.ActionResult;
import biblivre.core.utils.Constants;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Handler extends AbstractHandler {
private AccessCardBO accessCardBO;
@Autowired private PagedAccessCardSearchWebHelper pagedAccessCardSearchWebHelper;

public void search(ExtendedRequest request, ExtendedResponse response) {
DTOCollection<AccessCardDTO> list = this.searchHelper(request, response, this);
var pagedAccessCardSearchDTO =
pagedAccessCardSearchWebHelper.getPagedAccessCardSearchDTO(request);

try {
put("search", list.toJSONObject());
} catch (JSONException e) {
this.setMessage(ActionResult.WARNING, ERROR_INVALID_JSON);
}
}

public DTOCollection<AccessCardDTO> searchHelper(
ExtendedRequest request, ExtendedResponse response, AbstractHandler handler) {
String searchParameters = request.getString("search_parameters");
DTOCollection<AccessCardDTO> accessCards = accessCardBO.search(pagedAccessCardSearchDTO);

String query;
AccessCardStatus status;
try {
JSONObject json = new JSONObject(searchParameters);
query = json.optString("query");
status = AccessCardStatus.fromString(json.optString("status"));
} catch (JSONException je) {
this.setMessage(ActionResult.WARNING, "error.invalid_parameters");
return DTOCollection.empty();
}

Integer limit =
request.getInteger(
"limit", configurationBO.getInt(Constants.CONFIG_SEARCH_RESULTS_PER_PAGE));
int offset = (request.getInteger("page", 1) - 1) * limit;

DTOCollection<AccessCardDTO> list = accessCardBO.search(query, status, limit, offset);

if (list.size() == 0) {
if (accessCards.isEmpty()) {
this.setMessage(ActionResult.WARNING, "administration.accesscards.error.no_card_found");
}

return list;
try {
put("search", accessCards.toJSONObject());
} catch (JSONException e) {
this.setMessage(ActionResult.WARNING, ERROR_INVALID_JSON);
}
}

public void paginate(ExtendedRequest request, ExtendedResponse response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package biblivre.administration.accesscards;

public record PagedAccessCardSearchDTO(
String code, AccessCardStatus status, int limit, int offset) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package biblivre.administration.accesscards;

import biblivre.core.ExtendedRequest;
import biblivre.core.configurations.ConfigurationBO;
import biblivre.core.utils.Constants;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PagedAccessCardSearchWebHelper {
@Autowired private ConfigurationBO configurationBO;

public PagedAccessCardSearchDTO getPagedAccessCardSearchDTO(ExtendedRequest request) {
String searchParameters = request.getString("search_parameters");

JSONObject json = new JSONObject(searchParameters);
String query = json.optString("query");
var status = AccessCardStatus.fromString(json.optString("status"));

Integer limit =
request.getInteger(
"limit", configurationBO.getInt(Constants.CONFIG_SEARCH_RESULTS_PER_PAGE));
int offset = (request.getInteger("page", 1) - 1) * limit;

return new PagedAccessCardSearchDTO(query, status, limit, offset);
}
}
6 changes: 5 additions & 1 deletion src/main/java/biblivre/administration/indexing/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
package biblivre.administration.indexing;

import biblivre.cataloging.enums.RecordType;
import biblivre.circulation.user.UserDAO;
import biblivre.core.AbstractHandler;
import biblivre.core.ExtendedRequest;
import biblivre.core.ExtendedResponse;
import biblivre.core.enums.ActionResult;
import biblivre.search.SearchException;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -32,8 +34,9 @@
public class Handler extends AbstractHandler {
private IndexingBO indexingBO;

public void reindex(ExtendedRequest request, ExtendedResponse response) {
@Autowired private UserDAO userDAO;

public void reindex(ExtendedRequest request, ExtendedResponse response) throws SearchException {
String strRecordType = request.getString("record_type", "biblio");

RecordType recordType = RecordType.fromString(strRecordType);
Expand All @@ -48,6 +51,7 @@ public void reindex(ExtendedRequest request, ExtendedResponse response) {
long end;

start = new Date().getTime();
userDAO.reindexAll();
indexingBO.reindex(recordType);
end = new Date().getTime();

Expand Down
30 changes: 19 additions & 11 deletions src/main/java/biblivre/administration/permissions/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
******************************************************************************/
package biblivre.administration.permissions;

import biblivre.circulation.user.PagedUserSearchWebHelper;
import biblivre.circulation.user.UserBO;
import biblivre.circulation.user.UserDTO;
import biblivre.core.AbstractHandler;
Expand All @@ -29,6 +30,7 @@
import biblivre.core.utils.TextUtils;
import biblivre.login.LoginBO;
import biblivre.login.LoginDTO;
import biblivre.search.SearchException;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -44,25 +46,31 @@ public class Handler extends AbstractHandler {

biblivre.circulation.user.Handler userHandler;

@Autowired private PagedUserSearchWebHelper pagedUserSearchWebHelper;

public void search(ExtendedRequest request, ExtendedResponse response) {
DTOCollection<UserDTO> userList = userHandler.searchHelper(request, response, this);
var pagedSearchDTO = pagedUserSearchWebHelper.getPagedUserSearchDTO(request);

if (userList == null || userList.size() == 0) {
this.setMessage(ActionResult.WARNING, "circulation.error.no_users_found");
return;
}
try {
DTOCollection<UserDTO> userList = userBO.search(pagedSearchDTO);

DTOCollection<PermissionDTO> list = new DTOCollection<>();
list.setPaging(userList.getPaging());
if (userList.isEmpty()) {
this.setMessage(ActionResult.WARNING, "circulation.error.no_users_found");
return;
}

for (UserDTO user : userList) {
list.add(this.populatePermission(user));
}
DTOCollection<PermissionDTO> list = new DTOCollection<>();
list.setPaging(userList.getPaging());

for (UserDTO user : userList) {
list.add(this.populatePermission(user));
}

try {
put("search", list.toJSONObject());
} catch (JSONException e) {
this.setMessage(ActionResult.WARNING, ERROR_INVALID_JSON);
} catch (SearchException e) {
this.setMessage(ActionResult.ERROR, "error.internal_error");
}
}

Expand Down
31 changes: 17 additions & 14 deletions src/main/java/biblivre/administration/reports/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import biblivre.cataloging.enums.RecordDatabase;
import biblivre.circulation.lending.LendingListDTO;
import biblivre.circulation.user.PagedUserSearchWebHelper;
import biblivre.circulation.user.UserBO;
import biblivre.circulation.user.UserDTO;
import biblivre.core.AbstractHandler;
import biblivre.core.DTOCollection;
Expand All @@ -29,30 +31,36 @@
import biblivre.core.enums.ActionResult;
import biblivre.core.file.DiskFile;
import biblivre.core.utils.TextUtils;
import biblivre.search.SearchException;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Handler extends AbstractHandler {
private ReportsBO reportsBO;
private biblivre.circulation.user.Handler userHandler;
@Autowired private PagedUserSearchWebHelper pagedUserSearchWebHelper;
@Autowired private UserBO userBO;

public void userSearch(ExtendedRequest request, ExtendedResponse response) {
DTOCollection<UserDTO> userList = userHandler.searchHelper(request, response, this);
try {
var pagedSearchDTO = pagedUserSearchWebHelper.getPagedUserSearchDTO(request);

if (userList == null || userList.size() == 0) {
this.setMessage(ActionResult.WARNING, "circulation.error.no_users_found");
return;
}
DTOCollection<UserDTO> userList = userBO.search(pagedSearchDTO);

DTOCollection<LendingListDTO> list = new DTOCollection<>();
list.setPaging(userList.getPaging());
if (userList.isEmpty()) {
this.setMessage(ActionResult.WARNING, "circulation.error.no_users_found");
return;
}

DTOCollection<LendingListDTO> list = new DTOCollection<>();
list.setPaging(userList.getPaging());

try {
put("search", list.toJSONObject());
} catch (JSONException e) {
this.setMessage(ActionResult.WARNING, ERROR_INVALID_JSON);
} catch (SearchException e) {
this.setMessage(ActionResult.ERROR, "error.internal_error");
}
}

Expand Down Expand Up @@ -126,9 +134,4 @@ private ReportsDTO populateDto(ExtendedRequest request) throws Exception {
public void setReportsBO(ReportsBO reportsBO) {
this.reportsBO = reportsBO;
}

@Autowired
public void setUserHandler(biblivre.circulation.user.Handler userHandler) {
this.userHandler = userHandler;
}
}
13 changes: 9 additions & 4 deletions src/main/java/biblivre/administration/usertype/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import biblivre.core.ExtendedResponse;
import biblivre.core.enums.ActionResult;
import biblivre.core.utils.Constants;
import biblivre.search.SearchException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -120,10 +121,14 @@ public void save(ExtendedRequest request, ExtendedResponse response) {
public void delete(ExtendedRequest request, ExtendedResponse response) {
Integer id = request.getInteger("id");

if (userTypeBO.delete(id)) {
this.setMessage(ActionResult.SUCCESS, "administration.user_type.success.delete");
} else {
this.setMessage(ActionResult.WARNING, "administration.user_type.error.delete");
try {
if (userTypeBO.delete(id)) {
this.setMessage(ActionResult.SUCCESS, "administration.user_type.success.delete");
} else {
this.setMessage(ActionResult.WARNING, "administration.user_type.error.delete");
}
} catch (SearchException e) {
this.setMessage(ActionResult.ERROR, "error.internal_error");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import biblivre.core.AbstractBO;
import biblivre.core.DTOCollection;
import biblivre.core.exceptions.ValidationException;
import biblivre.search.SearchException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -61,13 +62,13 @@ public boolean save(UserTypeDTO userTypeDTO) {
return this.userTypeDAO.save(userTypeDTO);
}

public boolean delete(int id) {
public boolean delete(int id) throws SearchException {
// Check if there's any user for this user_type
UserSearchDTO dto = new UserSearchDTO();
dto.setType(id);

DTOCollection<UserDTO> userList = userDAO.search(dto, 1, 0);
boolean existingUser = userList.size() > 0;
boolean existingUser = !userList.isEmpty();

if (existingUser) {
throw new ValidationException("administration.user_type.error.type_has_users");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/biblivre/cataloging/RecordDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,13 @@ public List<RecordDTO> getSearchResults(SearchDTO search) {
}

if (useLimit) {
pst.setInt(index++, search.getRecordLimit());
pst.setLong(index++, search.getRecordLimit());
}

pst.setInt(index++, search.getSort());

pst.setInt(index++, paging.getRecordOffset());
pst.setInt(index, paging.getRecordsPerPage());
pst.setLong(index++, paging.getRecordOffset());
pst.setLong(index, paging.getRecordsPerPage());

ResultSet rs = pst.executeQuery();

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/biblivre/cataloging/holding/HoldingDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -789,15 +789,15 @@ public List<RecordDTO> getSearchResults(SearchDTO search) {
}

if (useLimit) {
pst.setInt(index++, search.getRecordLimit());
pst.setLong(index++, search.getRecordLimit());
}

index = this.populatePreparedStatement(pst, index, search);

pst.setInt(index++, search.getSort());

pst.setInt(index++, paging.getRecordOffset());
pst.setInt(index, paging.getRecordsPerPage());
pst.setLong(index++, paging.getRecordOffset());
pst.setLong(index, paging.getRecordsPerPage());

ResultSet rs = pst.executeQuery();

Expand Down
Loading

0 comments on commit da075ab

Please sign in to comment.