Projekt

Obecné

Profil

« Předchozí | Další » 

Revize abaa5f46

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

  • ID abaa5f461242c640ae1e7daaddb6bfc1b1419db9
  • Rodič c1df89f5

Experiment with database

Zobrazit rozdíly:

backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImpl.java
11 11
import lombok.extern.slf4j.Slf4j;
12 12
import org.springframework.http.HttpStatus;
13 13
import org.springframework.stereotype.Service;
14
import org.springframework.transaction.annotation.Transactional;
15 14

  
16 15
import java.util.List;
17
import java.util.Set;
18 16
import java.util.UUID;
17
import java.util.function.Predicate;
18
import java.util.regex.Pattern;
19 19
import java.util.stream.Collectors;
20 20

  
21 21
/**
22 22
 * Catalog item service implementation
23 23
 */
24 24
@Service
25
@Transactional
25
//@Transactional(readOnly = true)
26 26
@RequiredArgsConstructor
27 27
@Slf4j
28 28
public class CatalogItemServiceImpl implements ICatalogItemService {
......
62 62
    private final ITypeService typeService;
63 63

  
64 64
    @Override
65
    public void saveCatalog(List<CatalogItem> catalogItems) {
65
    public void saveCatalog(List<CatalogItemDto> catalogItems) {
66 66
        log.info("Saving catalog");
67
        catalogItems.forEach(this::saveCatalogEntity);
67

  
68
        catalogItems.forEach(this::saveCatalogItem);
68 69
    }
69 70

  
70 71
    @Override
......
98 99

  
99 100
    @Override
100 101
    public List<CatalogItemDto> getCatalog(String name, String country, String type) {
101
        log.info("Retrieving catalog");
102
        List<CatalogItem> catalogItems;
103
//        if (name.equals("") && country.equals("") && type.equals("")) {
104
//            catalogItems = catalogItemRepository.findAll();
105
//        } else {
102 106
        name = name.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
103 107
        name = name.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
104 108
        country = country.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
105 109
        country = country.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
106 110
        type = type.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
107 111
        type = type.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
108
        Set<CatalogItem> entities = catalogItemRepository.filterCatalog(name, country, type);
109
        return entities.stream().map(this::convertEntityToDto).collect(Collectors.toList());
112
        long start = System.currentTimeMillis();
113
        catalogItems = catalogItemRepository.filterCatalog(name, country, type);
114
        catalogItemRepository.flush();
115
//        System.out.println("time: " + (System.currentTimeMillis() - start));
116
//        }
117
        return catalogItems.stream().map(this::convertEntityToDto).collect(Collectors.toList());
118
    }
119

  
120
    public List<CatalogItemDto> getCatalog2(String name, String country, String type) {
121
        List<CatalogItem> catalogItems;
122
//        if (name.equals("") && country.equals("") && type.equals("")) {
123
//            catalogItems = catalogItemRepository.findAll();
124
//        } else {
125
        name = name.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
126
        name = name.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
127
        country = country.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
128
        country = country.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
129
        type = type.replaceAll(WILDCARD_CHARACTER_REGEX, ".");
130
//        type = type.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
131
        long start = System.currentTimeMillis();
132
        catalogItems = catalogItemRepository.filterCatalog2(name, country);
133
        if (!type.equals("")) {
134
            Pattern pattern = Pattern.compile(type);
135
            catalogItems = catalogItems.stream().filter(i -> i.getTypes().stream().anyMatch(t -> pattern.matcher(t.getType()).matches())).collect(Collectors.toList());
136
        }
137
//        System.out.println("time: " + (System.currentTimeMillis() - start));
138
//        }
139
        return catalogItems.stream().map(this::convertEntityToDto).collect(Collectors.toList());
140
    }
141

  
142
    public List<CatalogItemDto> getCatalog3(String name, String country, String type) {
143
        List<CatalogItem> catalogItems;
144
//        if (name.equals("") && country.equals("") && type.equals("")) {
145
//            catalogItems = catalogItemRepository.findAll();
146
//        } else {
147

  
148
        name = name.replaceAll(WILDCARD_CHARACTER_REGEX, ".");
149
        country = country.replaceAll(WILDCARD_CHARACTER_REGEX, ".");
150
        type = type.replaceAll(WILDCARD_CHARACTER_REGEX, ".");
151
        name = name.replaceAll(WILDCARD_CHARACTERS_REGEX, ".*");
152
        country = country.replaceAll(WILDCARD_CHARACTERS_REGEX, ".*");
153
        type = type.replaceAll(WILDCARD_CHARACTERS_REGEX, ".*");
154
        Pattern patternName = Pattern.compile(name, Pattern.CASE_INSENSITIVE);
155
        Pattern patternCountry = Pattern.compile(country, Pattern.CASE_INSENSITIVE);
156
        Pattern patternType = Pattern.compile(type, Pattern.CASE_INSENSITIVE);
157
        long start = System.currentTimeMillis();
158
        catalogItems = catalogItemRepository.findAll();
159
        String finalType = type;
160
        Predicate<CatalogItem> predicateType = i -> finalType.equals("") || i.getTypes().stream().anyMatch(t -> patternType.matcher(t.getType()).matches());
161
        String finalCountry = country;
162
        Predicate<CatalogItem> predicateCountry = i -> finalCountry.equals("") || i.getCountries().stream().anyMatch(t -> patternCountry.matcher(t.getName()).matches());
163
        String finalName = name;
164
        Predicate<CatalogItem> predicateName = i -> finalName.equals("") || patternName.matcher(i.getName()).matches() || i.getAlternativeNames().stream().anyMatch(t -> patternName.matcher(t.getName()).matches());
165
//        System.out.println("time: " + (System.currentTimeMillis() - start));
166
//        }
167
        catalogItems = catalogItems.stream().filter(predicateName.and(predicateCountry).and(predicateType)).collect(Collectors.toList());
168
        return catalogItems.stream().map(this::convertEntityToDto).collect(Collectors.toList());
110 169
    }
111 170

  
112 171
    /**
......
134 193
        catalogItem.setCertainty(catalogItemDto.getCertainty());
135 194
        catalogItem.setLatitude(catalogItemDto.getLatitude());
136 195
        catalogItem.setLongitude(catalogItemDto.getLongitude());
137
        catalogItem.setBibliography(catalogItemDto.getBibliography()
138
                .stream().map(s -> new Bibliography(s, catalogItem)).collect(Collectors.toSet()));
139
        catalogItem.setTypes(catalogItemDto.getTypes()
140
                .stream().map(Type::new).collect(Collectors.toSet()));
141
        catalogItem.setCountries(catalogItemDto.getCountries()
142
                .stream().map(s -> new Country(s, catalogItem)).collect(Collectors.toSet()));
143
        catalogItem.setAlternativeNames(catalogItemDto.getAlternativeNames()
144
                .stream().map(s -> new AlternativeName(s, catalogItem)).collect(Collectors.toSet()));
145
        catalogItem.setWrittenForms(catalogItemDto.getWrittenForms()
146
                .stream().map(s -> new WrittenForm(s, catalogItem)).collect(Collectors.toSet()));
196

  
197
        catalogItem.getBibliography().clear();
198
        catalogItem.getBibliography().addAll((catalogItemDto.getBibliography()
199
                .stream().map(s -> new Bibliography(s, catalogItem)).collect(Collectors.toSet())));
200

  
201
        catalogItem.getTypes().clear();
202
        catalogItem.getTypes().addAll((catalogItemDto.getTypes()
203
                .stream().map(Type::new).collect(Collectors.toSet())));
204

  
205
        catalogItem.getCountries().clear();
206
        catalogItem.getCountries().addAll((catalogItemDto.getCountries()
207
                .stream().map(s -> new Country(s, catalogItem)).collect(Collectors.toSet())));
208

  
209
        catalogItem.getAlternativeNames().clear();
210
        catalogItem.getAlternativeNames().addAll((catalogItemDto.getAlternativeNames()
211
                .stream().map(s -> new AlternativeName(s, catalogItem)).collect(Collectors.toSet())));
212

  
213
        catalogItem.getWrittenForms().clear();
214
        catalogItem.getWrittenForms().addAll((catalogItemDto.getWrittenForms()
215
                .stream().map(s -> new WrittenForm(s, catalogItem)).collect(Collectors.toSet())));
147 216
    }
148 217

  
149 218
    /**

Také k dispozici: Unified diff