-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompanyService.java
More file actions
124 lines (101 loc) · 5.89 KB
/
CompanyService.java
File metadata and controls
124 lines (101 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.example.spring.app.company;
import com.example.spring.app.company.dto.CompanyDetails;
import com.example.spring.app.company.dto.NumberOfEmployeeFilterDTO;
import com.example.spring.app.company.objects.ContactDTO;
import com.example.spring.app.company.objects.SocialMediaDTO;
import com.example.spring.common.utils.LogUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.List;
import static com.example.spring.app.company.CompanyUtil.setIfNotEmpty;
@Service
public class CompanyService {
private final CompanyRepository companyRepository;
public CompanyService(CompanyRepository companyRepository) {
this.companyRepository = companyRepository;
}
public CompanyModel getCompanyById(Integer id) {
return companyRepository.findCompanyById(id);
}
public Page<CompanyModel> getCompaniesSeenByUser(String userId, Pageable pageable) {
return companyRepository.findCompaniesSeenByUser(userId, pageable);
}
@Cacheable(value = "companySearch", key = "#companyName + #pageable")
public Page<CompanyDetails> searchCompanies(String companyName, Pageable pageable) {
return companyRepository.findCompanyDetailsByCompanyName(companyName, pageable);
}
public Page<CompanyModel> findCompaniesByFilters(List<String> regionNames, List<String> cityNames, List<String> industrySectorNames, List<String> legalFormNames,
NumberOfEmployeeFilterDTO numberOfEmployeeFilter, SocialMediaDTO socials, ContactDTO contacts, Boolean isCompanySeen,
String userId, Pageable pageable) {
Specification<CompanyModel> specification = Specification.where(CompanySpecification.regionsIn(regionNames))
.and(CompanySpecification.citiesIn(cityNames))
.and(CompanySpecification.industrySectorsIn(industrySectorNames))
.and(CompanySpecification.legalFormsIn(legalFormNames))
.and(CompanySpecification.employeeComparator(numberOfEmployeeFilter))
.and(CompanySpecification.socialMediaNotNull(socials))
.and(CompanySpecification.contactInfoNotNull(contacts))
.and(CompanySpecification.notSeenByUser(isCompanySeen, userId));
return companyRepository.findAll(specification, pageable);
}
public Page<CompanyModel> findRandomUnseenCompanies(String userId, Pageable pageable) {
return companyRepository.findRandomUnseenCompanies(userId, pageable);
}
@RateLimiter(name = "scrapService")
public CompanyModel scrapCompany(CompanyModel company) {
try {
HttpResponse<String> response;
try (HttpClient client = HttpClient.newHttpClient()) {
// Create the request
String encodedCompanyName = URLEncoder.encode(company.getCompanyName(), StandardCharsets.UTF_8);
String encodedAddress = URLEncoder.encode(company.getAddress(), StandardCharsets.UTF_8);
String url = String.format("http://scraping:8081/api/company-info?companyName=%s&address=%s", encodedCompanyName, encodedAddress);
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.GET()
.build();
response = client.send(request, HttpResponse.BodyHandlers.ofString());
}
// Parse the response
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(response.body());
CompanyModel companyScrapped = new CompanyModel();
// TODO: Verify what's ifNotEmpty means. Currently it still updates the values even if they are empty in the response
setIfNotEmpty(jsonNode, "companyName", companyScrapped::setCompanyName);
setIfNotEmpty(jsonNode, "phoneNumber", companyScrapped::setPhoneNumber);
setIfNotEmpty(jsonNode, "website", companyScrapped::setWebsite);
setIfNotEmpty(jsonNode, "instagram", companyScrapped::setInstagram);
setIfNotEmpty(jsonNode, "facebook", companyScrapped::setFacebook);
setIfNotEmpty(jsonNode, "twitter", companyScrapped::setTwitter);
setIfNotEmpty(jsonNode, "linkedin", companyScrapped::setLinkedin);
setIfNotEmpty(jsonNode, "youtube", companyScrapped::setYoutube);
setIfNotEmpty(jsonNode, "email", companyScrapped::setEmail);
setIfNotEmpty(jsonNode, "scrapingDate", (value) -> companyScrapped.setScrapingDate(LocalDate.parse(value)));
setIfNotEmpty(jsonNode, "reviews", (value) -> companyScrapped.setReviews(new ObjectMapper().convertValue(jsonNode.get("reviews"), new TypeReference<>() {
})));
setIfNotEmpty(jsonNode, "schedule", companyScrapped::setSchedule);
return companyScrapped;
} catch (Exception e) {
LogUtil.error("Failed to scrap company information: ", e);
throw new RuntimeException("Failed to scrap company information: ", e);
}
}
@CacheEvict(value = "companyCounts", allEntries = true)
public void saveCompany(CompanyModel company) {
companyRepository.save(company);
}
}