Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b539ef29

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

Added path
- searching and highlighting text
- returning list of found catalog items

re #9490

Zobrazit rozdíly:

backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImpl.java
4 4
import cz.zcu.kiv.backendapi.bibliography.Bibliography;
5 5
import cz.zcu.kiv.backendapi.country.Country;
6 6
import cz.zcu.kiv.backendapi.exception.ApiRequestException;
7
import cz.zcu.kiv.backendapi.path.PathDto;
7 8
import cz.zcu.kiv.backendapi.type.ITypeService;
8 9
import cz.zcu.kiv.backendapi.type.Type;
9 10
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
......
13 14
import org.springframework.stereotype.Service;
14 15
import org.springframework.transaction.annotation.Transactional;
15 16

  
17
import java.util.ArrayList;
16 18
import java.util.List;
17 19
import java.util.Set;
18 20
import java.util.UUID;
21
import java.util.regex.Matcher;
22
import java.util.regex.Pattern;
19 23
import java.util.stream.Collectors;
20 24

  
21 25
/**
......
41 45
     */
42 46
    private static final String WILDCARD_CHARACTERS_REGEX = "\\*";
43 47

  
48
    /**
49
     * Regex for finding punctuation at the start of word
50
     */
51
    private static final String START_PUNCTUATION_REGEX = "^[\\p{Punct}\\p{IsPunctuation}]+";
52

  
53
    /**
54
     * Regex for finding punctuation at the end of word
55
     */
56
    private static final String END_PUNCTUATION_REGEX = "[\\p{Punct}\\p{IsPunctuation}]+$";
57

  
58
    /**
59
     * Pattern for finding punctuation at the start of word
60
     */
61
    private static final Pattern START_PUNCTUATION_PATTERN = Pattern.compile(START_PUNCTUATION_REGEX);
62

  
63
    /**
64
     * Pattern for finding punctuation at the end of word
65
     */
66
    private static final Pattern END_PUNCTUATION_PATTERN = Pattern.compile(END_PUNCTUATION_REGEX);
67

  
44 68
    /**
45 69
     * Regex for any number of arbitrary characters in string used in SQL language
46 70
     */
......
51 75
     */
52 76
    private static final String CATALOG_ITEM_NOT_FOUND = "Catalog item not found";
53 77

  
78
    /**
79
     * Threshold for comparing doubles
80
     */
81
    private static final double DOUBLE_THRESHOLD = 0.001;
82

  
54 83
    /**
55 84
     * Catalog repository
56 85
     */
......
111 140

  
112 141
    @Override
113 142
    public CatalogItemDto getCatalogItem(UUID id) {
114
        var catalogItem = catalogItemRepository.findById(id).orElseThrow(() -> {
143
        CatalogItem catalogItem = catalogItemRepository.findById(id).orElseThrow(() -> {
115 144
            log.error(CATALOG_ITEM_NOT_FOUND);
116 145
            throw new ApiRequestException(CATALOG_ITEM_NOT_FOUND, HttpStatus.NOT_FOUND);
117 146
        });
......
119 148
        return convertEntityToDto(catalogItem);
120 149
    }
121 150

  
151
    @Override
152
    public PathDto getPath(String text) {
153
        PathDto pathDto = new PathDto();
154
        StringBuilder highlightedText = new StringBuilder();
155
        List<List<CatalogItemDto>> foundCatalogItems = new ArrayList<>();
156

  
157
        Set<String> types = typeService.getAllTypesAsString();
158

  
159
        String[] tokens = text.split("\\s+");
160
        for (String token : tokens) {
161
            Matcher matcherStart = START_PUNCTUATION_PATTERN.matcher(token);
162
            Matcher matcherEnd = END_PUNCTUATION_PATTERN.matcher(token);
163
            String prefix = "";
164
            String suffix = "";
165
            String textToFind;
166
            int startTextIndex = 0;
167
            int endTextIndex = token.length();
168

  
169
            if (matcherStart.find()) {
170
                int start = matcherStart.start();
171
                startTextIndex = matcherStart.end();
172
                prefix = token.substring(start, startTextIndex);
173
            }
174
            if (matcherEnd.find()) {
175
                endTextIndex = matcherEnd.start();
176
                int end = matcherEnd.end();
177
                suffix = token.substring(endTextIndex, end);
178
            }
179

  
180
            textToFind = token.substring(startTextIndex, endTextIndex);
181

  
182
            if (types.contains(textToFind)) {
183
                textToFind = "<b>" + textToFind + "</b>";
184
            } else {
185
                List<CatalogItem> foundItems = catalogItemRepository.getItemsByName(textToFind);
186
                if (!foundItems.isEmpty()) {
187
                    if (foundItems.stream().anyMatch(c -> Math.abs(c.getLatitude()) > DOUBLE_THRESHOLD && Math.abs(c.getLongitude()) > DOUBLE_THRESHOLD)) {
188
                        textToFind = "<span style='color:green'>" + textToFind + "</span>";
189
                    } else {
190
                        textToFind = "<span style='color:red'>" + textToFind + "</span>";
191
                    }
192
                    foundCatalogItems.add(foundItems.stream().map(this::convertEntityToDto).collect(Collectors.toList()));
193
                }
194
            }
195
            highlightedText.append(prefix).append(textToFind).append(suffix).append(" ");
196
        }
197
        highlightedText.setLength(highlightedText.length() - 1);
198

  
199
        pathDto.setText(highlightedText.toString());
200
        pathDto.setFoundCatalogItems(foundCatalogItems);
201
        return pathDto;
202
    }
203

  
122 204
    /**
123 205
     * Saves catalog entity to database
124 206
     *

Také k dispozici: Unified diff