Skip to content

Commit

Permalink
70-BE/license-page-missing-pagination-sorting (#250)
Browse files Browse the repository at this point in the history
* Added a new searching endpoint into ClarinLicenseRestRepository to find Clarin License by substring of it's name

* Fixed checkstyle issue

---------

Co-authored-by: MilanMajchrák <milan.majchak@dataquest.sk>
  • Loading branch information
milanmajchrak and MilanMajchrák authored Apr 28, 2023
1 parent 202e196 commit de32dfb
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public ClarinLicense findByName(Context context, String name) throws SQLExceptio
return clarinLicenseDAO.findByName(context, name);
}

@Override
public List<ClarinLicense> findByNameLike(Context context, String name) throws SQLException {
return clarinLicenseDAO.findByNameLike(context, name);
}

@Override
public void addLicenseMetadataToItem(Context context, ClarinLicense clarinLicense, Item item) throws SQLException {
if (Objects.isNull(clarinLicense) || Objects.isNull(item)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.dspace.content.dao.clarin;

import java.sql.SQLException;
import java.util.List;

import org.dspace.content.clarin.ClarinLicense;
import org.dspace.core.Context;
Expand All @@ -25,4 +26,6 @@ public interface ClarinLicenseDAO extends GenericDAO<ClarinLicense> {

ClarinLicense findByName(Context context, String name) throws SQLException;

List<ClarinLicense> findByNameLike(Context context, String name) throws SQLException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
package org.dspace.content.dao.impl.clarin;

import java.sql.SQLException;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.dspace.content.clarin.ClarinLicense;
import org.dspace.content.clarin.ClarinLicense_;
import org.dspace.content.dao.clarin.ClarinLicenseDAO;
import org.dspace.core.AbstractHibernateDAO;
import org.dspace.core.Context;
Expand Down Expand Up @@ -38,4 +43,15 @@ public ClarinLicense findByName(Context context, String name) throws SQLExceptio

return singleResult(query);
}

@Override
public List<ClarinLicense> findByNameLike(Context context, String name) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, ClarinLicense.class);
Root<ClarinLicense> clarinLicenseRoot = criteriaQuery.from(ClarinLicense.class);
criteriaQuery.select(clarinLicenseRoot);
criteriaQuery.where(criteriaBuilder.like(clarinLicenseRoot.get(ClarinLicense_.name), "%" + name + "%"));
criteriaQuery.orderBy(criteriaBuilder.asc(clarinLicenseRoot.get(ClarinLicense_.name)));
return list(context, criteriaQuery, false, ClarinLicense.class, -1, -1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,26 @@ public interface ClarinLicenseService {
*/
ClarinLicense find(Context context, int valueId) throws SQLException;

/**
* Find the Clarin License by the full clarin license name.
*
* @param context DSpace context object
* @param name the full clarin license name
* @return Clarin License with searching name.
* @throws SQLException
*/
ClarinLicense findByName(Context context, String name) throws SQLException;

/**
* Find the Clarin License by the substring of the clarin license name.
*
* @param context DSpace context object
* @param name substring of the clarin license name
* @return List of clarin licenses which contains searching string in it's name
* @throws SQLException
*/
List<ClarinLicense> findByNameLike(Context context, String name) throws SQLException;

void addLicenseMetadataToItem(Context context, ClarinLicense clarinLicense, Item item) throws SQLException;

void clearLicenseMetadataFromItem(Context context, Item item) throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
* This is the repository responsible to manage Clarin License Rest object
Expand Down Expand Up @@ -101,6 +102,22 @@ public Page<ClarinLicenseRest> findByName(@Parameter(value = "name", required =
return converter.toRestPage(clarinLicenseList, pageable, utils.obtainProjection());
}

@SearchRestMethod(name = "byNameLike")
public Page<ClarinLicenseRest> findByNameLike(@Parameter(value = "name", required = true) String name,
Pageable pageable) {
List<ClarinLicense> clarinLicenseList;
try {
Context context = obtainContext();
clarinLicenseList = clarinLicenseService.findByNameLike(context, name);
if (CollectionUtils.isEmpty(clarinLicenseList)) {
return null;
}
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
return converter.toRestPage(clarinLicenseList, pageable, utils.obtainProjection());
}

@Override
@PreAuthorize("hasAuthority('ADMIN')")
public Page<ClarinLicenseRest> findAll(Context context, Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,51 @@ public void findAll() throws Exception {
Matchers.containsString("/api/core/clarinlicenses")));
}

/**
* Should find one license by the name.
*/
@Test
public void searchBy() throws Exception {
String authTokenAdmin = getAuthToken(admin.getEmail(), password);
getClient(authTokenAdmin).perform(get("/api/core/clarinlicenses/search/byName")
.param("name", "CL Name1"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.clarinlicenses", Matchers.hasItem(
ClarinLicenseMatcher.matchClarinLicense(firstCLicense))
));
}

/**
* Should find all licenses by the common substring of it's the name.
*/
@Test
public void searchByLike() throws Exception {
String authTokenAdmin = getAuthToken(admin.getEmail(), password);
getClient(authTokenAdmin).perform(get("/api/core/clarinlicenses/search/byNameLike")
.param("name", "Name"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.clarinlicenses", Matchers.hasItem(
ClarinLicenseMatcher.matchClarinLicense(firstCLicense))
))
.andExpect(jsonPath("$._embedded.clarinlicenses", Matchers.hasItem(
ClarinLicenseMatcher.matchClarinLicense(secondCLicense))
))
.andExpect(jsonPath("$._embedded.clarinlicenses[0].clarinLicenseLabel", Matchers.is(
ClarinLicenseLabelMatcher.matchClarinLicenseLabel(
Objects.requireNonNull(getNonExtendedLicenseLabel(firstCLicense.getLicenseLabels()))))
))
.andExpect(jsonPath("$._embedded.clarinlicenses[0].extendedClarinLicenseLabels",
Matchers.hasItem(
ClarinLicenseLabelMatcher.matchClarinLicenseLabel(
Objects.requireNonNull(getExtendedLicenseLabels(
firstCLicense.getLicenseLabels())))
)))
.andExpect(jsonPath("$._links.self.href",
Matchers.containsString("/api/core/clarinlicenses")));
}

@Test
public void create() throws Exception {
ClarinLicenseRest clarinLicenseRest = new ClarinLicenseRest();
Expand Down
8 changes: 8 additions & 0 deletions scripts/fast-build/config-update.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
call ..\envs\__basic.bat

call tomcat\stop.bat

rem copy all config files
xcopy /e /h /i /q /y %dspace_source%\dspace\config\ %dspace_application%\config\

cd %dspace_source%\scripts\fast-build\

0 comments on commit de32dfb

Please sign in to comment.