Commit 0468e43f authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch 'autocomplete-for-descriptorlists' into 'master'

Autocomplete for DescriptorList

See merge request !161
parents fd87298d 41387fd6
......@@ -15,6 +15,7 @@
*/
package org.genesys.catalog.service;
import java.util.List;
import java.util.UUID;
import org.genesys.catalog.model.traits.Descriptor;
......@@ -158,4 +159,12 @@ public interface DescriptorListService {
*/
DescriptorList setDescriptors(DescriptorList descriptorList, Descriptor[] descriptors);
/**
* Autocomplete descriptor lists.
*
* @param text the text
* @return list of matched descriptor lists
*/
List<DescriptorList> autocompleteDescriptorLists(String text);
}
......@@ -21,10 +21,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.traits.Descriptor;
......@@ -42,7 +44,9 @@ import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
......@@ -50,6 +54,8 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.querydsl.core.types.Predicate;
/**
* Service for working with {@link DescriptorList}.
*
......@@ -186,7 +192,7 @@ public class DescriptorListServiceImpl implements DescriptorListService {
@Transactional
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#descriptorList, 'WRITE')")
public DescriptorList setDescriptors(final DescriptorList descriptorList, final Descriptor... descriptors) {
public DescriptorList setDescriptors(final DescriptorList descriptorList, final Descriptor[] descriptors) {
if (descriptors == null || descriptors.length == 0) {
// Noop
return descriptorList;
......@@ -361,4 +367,18 @@ public class DescriptorListServiceImpl implements DescriptorListService {
loaded.setPublished(false);
return lazyLoad(descriptorListRepository.save(loaded));
}
/**
* {@inheritDoc}
*/
@Override
public List<DescriptorList> autocompleteDescriptorLists(final String text) {
if (StringUtils.isBlank(text)) {
final Predicate predicate = descriptorList.published.eq(true);
return descriptorListRepository.findAll(predicate, new PageRequest(0, 15, new Sort("title"))).getContent();
} else {
final Predicate predicate = descriptorList.published.eq(true).and(descriptorList.title.containsIgnoreCase(text));
return descriptorListRepository.findAll(predicate, new PageRequest(0, 15, new Sort("title"))).getContent();
}
}
}
......@@ -35,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
......@@ -250,4 +251,15 @@ public class DescriptorListController {
return descriptorListService.removeDescriptors(descriptorList, descriptors.toArray(new Descriptor[] {}));
}
/**
* Autocomplete.
*
* @param text the text
* @return the list
*/
@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<DescriptorList> autocomplete(@RequestParam("d") final String text) {
return descriptorListService.autocompleteDescriptorLists(text);
}
}
......@@ -47,6 +47,7 @@ import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
......@@ -205,6 +206,29 @@ public class DescriptorListControllerTest extends AbstractRestTest {
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void autocompleteTest() throws Exception {
DescriptorList descriptorList = setUpDescriptorList(true, UUID.randomUUID(), "Des 2", "First Title", "Vt 2");
descriptorList = descriptorListRepository.save(descriptorList);
descriptorListRepository.save(setUpDescriptorList(false, null, "Description 2", "Second Title", "Vt 1"));
assertThat(descriptorListRepository.count(), is(2l));
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.get(DescriptorListController.API_BASE.concat("/autocomplete"))
.param("d", descriptorList.getTitle().substring(0, 4))
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.[0]", not(nullValue())))
.andExpect(jsonPath("$.[0].description", not(nullValue())))
.andExpect(jsonPath("$.[0].title", not(nullValue())));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void listDescriptorListsByFilterCodeTest() throws Exception {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment