Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ad885d86

Přidáno uživatelem Jakub Šmíd před asi 2 roky(ů)

Loading of path speeded up

re #9753

Zobrazit rozdíly:

backend/pom.xml
10 10
    </parent>
11 11
    <groupId>cz.zcu.kiv</groupId>
12 12
    <artifactId>backend-api</artifactId>
13
    <version>0.0.1-SNAPSHOT</version>
13
    <version>1.0.0</version>
14 14
    <name>backend-api</name>
15 15
    <description>Backend API for Neo-assyrian web application</description>
16 16
    <properties>
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepository.java
1 1
package cz.zcu.kiv.backendapi.catalog;
2 2

  
3 3
import org.springframework.data.jpa.repository.JpaRepository;
4
import org.springframework.data.jpa.repository.Query;
5 4
import org.springframework.stereotype.Repository;
6 5

  
7
import java.util.List;
8 6
import java.util.UUID;
9 7

  
10 8
/**
11
 * Catalog repository
9
 * Catalog item repository
12 10
 */
13 11
@Repository
14 12
public interface CatalogItemRepository extends JpaRepository<CatalogItem, UUID> {
15 13

  
16
    /**
17
     * Selects catalog items with given name (alternative name)
18
     *
19
     * @param name name
20
     * @return list of catalog items with given name
21
     */
22
    @Query("SELECT DISTINCT a.catalogItem FROM CatalogItemName a WHERE ?1 = a.name")
23
    List<CatalogItem> getItemsByName(String name);
24 14
}
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImpl.java
137 137

  
138 138
        Set<String> types = typeService.getAllTypesAsString();
139 139

  
140
        List<CatalogItem> catalog = catalogItemRepository.findAll();
141
        Map<String, List<CatalogItem>> nameToCatalogItemMap = new HashMap<>();
142
        for (CatalogItem catalogItem : catalog) {
143
            for (CatalogItemName name : catalogItem.getAllNames()) {
144
                if (!nameToCatalogItemMap.containsKey(name.getName())) {
145
                    nameToCatalogItemMap.put(name.getName(), new ArrayList<>());
146
                }
147
                nameToCatalogItemMap.get(name.getName()).add(catalogItem);
148
            }
149
        }
150

  
140 151
        String[] tokens = text.split("((?<=\\s)|(?=\\s+))");
141 152
        for (String token : tokens) {
142 153
            if (StringUtils.isBlank(token) || token.matches(START_PUNCTUATION_REGEX)) {
......
169 180
            if (types.contains(textToFind)) {
170 181
                textToFind = "<b>" + textToFind + "</b>";
171 182
            } else {
172
                List<CatalogItem> foundItems = catalogItemRepository.getItemsByName(textToFind);
173
                if (!foundItems.isEmpty()) {
183
                if (nameToCatalogItemMap.containsKey(textToFind)) {
184
                    List<CatalogItem> foundItems = nameToCatalogItemMap.get(textToFind);
174 185
                    if (foundItems.stream().anyMatch(c -> c.getLatitude() != null && c.getLongitude() != null)) {
175 186
                        textToFind = "<span style='color:green'>" + textToFind + "</span>";
176 187
                    } else {
backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepositoryTest.java
1
package cz.zcu.kiv.backendapi.catalog;
2

  
3
import cz.zcu.kiv.backendapi.alternativename.CatalogItemName;
4
import org.junit.jupiter.api.Test;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
7

  
8
import java.util.LinkedHashSet;
9
import java.util.List;
10
import java.util.stream.Collectors;
11
import java.util.stream.Stream;
12

  
13
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
14
import static org.junit.jupiter.api.Assertions.assertTrue;
15

  
16
@DataJpaTest
17
class CatalogItemRepositoryTest {
18

  
19
    @Autowired
20
    private CatalogItemRepository underTest;
21

  
22
    @Test
23
    void testItemsByName() {
24
        // given
25
        String nameFirst = "first";
26
        String nameFirstUpper = "First";
27
        String nameSecond = "second";
28
        String nameSedond = "sedond";
29
        String nameThird = "third";
30
        String nameTwelve = "twelve";
31
        String nameTwentyUpper = "TWENTY";
32

  
33
        CatalogItem catalogItem1 = new CatalogItem();
34
        CatalogItem catalogItem2 = new CatalogItem();
35
        CatalogItem catalogItem3 = new CatalogItem();
36
        CatalogItem catalogItem4 = new CatalogItem();
37
        CatalogItem catalogItem5 = new CatalogItem();
38

  
39
        catalogItem1.setName(nameFirst);
40

  
41
        catalogItem2.setName(nameSecond);
42
        catalogItem2.setAllNames(Stream.of(new CatalogItemName(nameTwelve, catalogItem2, 1), new CatalogItemName(nameThird, catalogItem2, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
43

  
44
        catalogItem3.setName(nameThird);
45
        catalogItem3.setAllNames(Stream.of(new CatalogItemName(nameTwentyUpper, catalogItem3, 1), new CatalogItemName(nameSedond, catalogItem3, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
46

  
47
        catalogItem4.setAllNames(Stream.of(new CatalogItemName(nameTwelve, catalogItem4, 1), new CatalogItemName(nameFirstUpper, catalogItem4, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
48

  
49
        catalogItem5.setName(nameSedond);
50
        catalogItem5.setAllNames(Stream.of(new CatalogItemName(nameThird, catalogItem5, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
51

  
52
        underTest.saveAll(List.of(catalogItem1, catalogItem2, catalogItem3, catalogItem4, catalogItem5));
53

  
54

  
55
        String name = "twelve";
56

  
57
        // when
58
        List<CatalogItem> res = underTest.getItemsByName(name);
59

  
60
        // then
61
        assertThat(res.size()).isEqualTo(2);
62
        assertTrue(res.contains(catalogItem2));
63
        assertTrue(res.contains(catalogItem4));
64
    }
65
}
backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImplTest.java
440 440

  
441 441

  
442 442
        given(typeService.getAllTypesAsString()).willReturn(Stream.of("city", "ocean").collect(Collectors.toCollection(LinkedHashSet::new)));
443
        given(catalogItemRepository.getItemsByName(anyString())).willReturn(Collections.emptyList());
444
        given(catalogItemRepository.getItemsByName("Azbar")).willReturn(Stream.of(catalogItem1, catalogItem2).collect(Collectors.toList()));
445
        given(catalogItemRepository.getItemsByName("U-re-me-re")).willReturn(Stream.of(catalogItem3).collect(Collectors.toList()));
446
        given(catalogItemRepository.getItemsByName("Huga")).willReturn(Stream.of(catalogItem4, catalogItem5).collect(Collectors.toList()));
447
        given(catalogItemRepository.getItemsByName("Duga")).willReturn(Stream.of(catalogItem6).collect(Collectors.toList()));
443
        given(catalogItemRepository.findAll()).willReturn(List.of(catalogItem1, catalogItem2, catalogItem3, catalogItem4, catalogItem5, catalogItem6));
448 444

  
449 445
        // when
450 446
        PathDto pathDto = underTest.getPath(text);
451 447

  
452 448
        // then
453
        verify(catalogItemRepository).getItemsByName("Azbar");
454
        verify(catalogItemRepository).getItemsByName("U-re-me-re");
455
        verify(catalogItemRepository).getItemsByName("Huga");
456
        verify(catalogItemRepository).getItemsByName("Duga");
457
        verify(catalogItemRepository).getItemsByName("Puga");
449
        verify(catalogItemRepository).findAll();
458 450

  
459 451
        assertThat(pathDto.getText()).isEqualTo(highlightedText);
460 452

  

Také k dispozici: Unified diff