SUPPORT-9122:saves
This commit is contained in:
parent
f343cd577b
commit
709d957558
2 changed files with 283 additions and 0 deletions
|
|
@ -0,0 +1,33 @@
|
|||
package ervu_business_metrics.dao;
|
||||
|
||||
|
||||
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.DomainRecord;
|
||||
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.RoleRecord;
|
||||
|
||||
import static ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.Tables.DOMAIN;
|
||||
import static ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.Tables.ROLE;
|
||||
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
@Repository
|
||||
public class IdmDirectoriesDaoService {
|
||||
private final DSLContext dsl;
|
||||
|
||||
public IdmDirectoriesDaoService(DSLContext dsl) {
|
||||
this.dsl = dsl;
|
||||
}
|
||||
|
||||
public RoleRecord getRoleRecord() {
|
||||
return dsl.newRecord(ROLE);
|
||||
}
|
||||
|
||||
public DomainRecord getDomainRecord() {
|
||||
return dsl.newRecord(DOMAIN);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,250 @@
|
|||
package ervu_business_metrics.service;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import ervu_business_metrics.dao.IdmDirectoriesDaoService;
|
||||
import ervu_business_metrics.model.dto.AccountResponse;
|
||||
import ervu_business_metrics.model.dto.DomainResponse;
|
||||
import ervu_business_metrics.model.dto.PersonResponse;
|
||||
import ervu_business_metrics.model.dto.RoleResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.DomainRecord;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
@Component
|
||||
public class IdmDirectoriesService {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(
|
||||
MethodHandles.lookup().lookupClass());
|
||||
@Value("${idm.url}")
|
||||
private String idmUrl;
|
||||
@Value("${ervu.collection:domain, role , account , person}")
|
||||
private String ervuCollection;
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private final IdmDirectoriesDaoService idmDirectoriesDaoService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public IdmDirectoriesService(RestTemplate restTemplate,
|
||||
IdmDirectoriesDaoService idmDirectoriesDaoService, ObjectMapper objectMapper) {
|
||||
this.restTemplate = restTemplate;
|
||||
this.idmDirectoriesDaoService = idmDirectoriesDaoService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
|
||||
@Caching(evict = {
|
||||
@CacheEvict(value = "domain-ids", allEntries = true),
|
||||
@CacheEvict(value = "role-ids", allEntries = true)
|
||||
})
|
||||
public void updateDirectories() {
|
||||
try {
|
||||
String[] ervuCollectionArray = ervuCollection.split(",");
|
||||
Arrays.stream(ervuCollectionArray).forEach(ervuCollection -> {
|
||||
String targetUrl = idmUrl + "/reconcile/"+ ervuCollection.trim() + "/to/kafka/v1";
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
String emptyJson = "{}";
|
||||
HttpEntity<String> requestEntity = new HttpEntity<>(emptyJson, headers);
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(targetUrl, requestEntity, String.class);
|
||||
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||
LOGGER.error(
|
||||
"Error in " + ervuCollection + " request. Status code: " + response.getStatusCode()
|
||||
+ "; Body: " + response.getBody());
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void upsertKafkaPersonMessage(String kafkaMessage) {
|
||||
PersonResponse[] personResponses;
|
||||
try {
|
||||
personResponses = objectMapper.readValue(kafkaMessage, PersonResponse[].class);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (personResponses.length > 0 && personResponses[0].getData() != null && !personResponses[0].getData().isEmpty()) {
|
||||
upsertRecruitmentData(personResponses[0].getData());
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void upsertKafkaAccountMessage(String kafkaMessage) {
|
||||
AccountResponse[] accountResponses;
|
||||
try {
|
||||
accountResponses = objectMapper.readValue(kafkaMessage, AccountResponse[].class);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (accountResponses.length > 0 && accountResponses[0].getData() != null && !accountResponses[0].getData().isEmpty()) {
|
||||
upsertRecruitmentData(accountResponses[0].getData());
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void upsertKafkaDomainMessage(String kafkaMessage) {
|
||||
DomainResponse[] domainResponses;
|
||||
try {
|
||||
domainResponses = objectMapper.readValue(kafkaMessage, DomainResponse[].class);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (domainResponses.length > 0 && domainResponses[0].getData() != null && !domainResponses[0].getData().isEmpty()) {
|
||||
upsertDomainData(domainResponses[0].getData());
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void upsertKafkaRoleMessage(String kafkaMessage) {
|
||||
RoleResponse[] roleResponses;
|
||||
try {
|
||||
roleResponses = objectMapper.readValue(kafkaMessage, RoleResponse[].class);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (roleResponses.length > 0 && roleResponses[0].getData() != null && !roleResponses[0].getData().isEmpty()) {
|
||||
upsertRoleData(roleResponses[0].getData());
|
||||
}
|
||||
}
|
||||
|
||||
private void upsertDomainData(List<DomainResponse.Data> dataList) {
|
||||
List<DomainRecord> newRecruitmentRecords = new ArrayList<>();
|
||||
List<DomainRecord> recruitmentRecords = new ArrayList<>();
|
||||
List<String> existingIds = idmDirectoriesDaoService.getDomainIds();
|
||||
|
||||
for (DomainResponse.Data data : dataList) {
|
||||
DomainRecord domainRecord = idmDirectoriesDaoService.getDomainRecord();
|
||||
|
||||
|
||||
Timestamp createdAt = Timestamp.from(Instant.now());
|
||||
Timestamp modifiedAt = Timestamp.from(Instant.ofEpochMilli(data.getModified()));
|
||||
|
||||
|
||||
domainRecord.setId(data.getId());
|
||||
domainRecord.setVersion(data.getVersion());
|
||||
domainRecord.setModified(modifiedAt);
|
||||
domainRecord.setSchema(data.getSchema());
|
||||
domainRecord.setName(data.getName());
|
||||
domainRecord.setShortname(data.getShortname());
|
||||
domainRecord.setFullname(data.getFullname());
|
||||
domainRecord.setDns(data.getDns());
|
||||
domainRecord.setEmail(data.getEmail());
|
||||
domainRecord.setPhone(data.getPhone());
|
||||
domainRecord.setAddress(data.getAddress());
|
||||
domainRecord.setPostalAddress(data.getPostalAddress());
|
||||
domainRecord.setAddressId(data.getAddressId());
|
||||
domainRecord.setPostalAddressId(data.getPostalAddressId());
|
||||
domainRecord.setMilitaryCode(data.getMilitaryCode());
|
||||
domainRecord.setTimezone(data.getTimezone());
|
||||
domainRecord.setReportsEnabled(data.isReportsEnabled());
|
||||
domainRecord.setInn(data.getInn());
|
||||
domainRecord.setLeg(data.getLeg());
|
||||
domainRecord.setOgrn(data.getOgrn());
|
||||
domainRecord.setRegion(data.getRegion());
|
||||
domainRecord.setEpguId(data.getEpguId());
|
||||
domainRecord.setType(data.getType());
|
||||
domainRecord.setEsiaEmployeeAuthorization(data.isEsiaEmployeeAuthorization());
|
||||
domainRecord.setDefaultS3Bucket(data.getDefaultS3Bucket());
|
||||
domainRecord.setOpf(data.getOpf());
|
||||
domainRecord.setKpp(data.getKpp());
|
||||
domainRecord.setCheckingAccount(data.getCheckingAccount());
|
||||
domainRecord.setBik(data.getBik());
|
||||
domainRecord.setBankName(data.getBankName());
|
||||
domainRecord.setBankCorrespondentAccount(data.getBankCorrespondentAccount());
|
||||
domainRecord.setOktmo(data.getOktmo());
|
||||
domainRecord.setOkato(data.getOkato());
|
||||
domainRecord.setGovRegistrationDate(data.getGovRegistrationDate());
|
||||
domainRecord.setGovOrganizationType(data.getGovOrganizationType());
|
||||
domainRecord.setAliasKey(data.getAliasKey());
|
||||
domainRecord.setPassKey(data.getPassKey());
|
||||
domainRecord.setCertificate(data.getCertificate());
|
||||
domainRecord.setAccountNumberTofk(data.getAccountNumberTOFK());
|
||||
domainRecord.setBikTofk(data.getBikTOFK());
|
||||
domainRecord.setCorrespondentBankAccountTofk(data.getCorrespondentBankAccountTOFK());
|
||||
domainRecord.setNameTofk(data.getNameTOFK());
|
||||
domainRecord.setNsiOrganizationId(data.getNsiOrganizationId());
|
||||
domainRecord.setDocHandle(data.getDocHandle());
|
||||
domainRecord.setDivisionType(data.getDivisionType());
|
||||
domainRecord.setTnsDepartmentId(data.getTnsDepartmentId());
|
||||
domainRecord.setEnabled(data.isEnabled() != null ? data.isEnabled() : true);
|
||||
domainRecord.setParent(data.getParent());
|
||||
domainRecord.setRegionId(data.getRegionId());
|
||||
domainRecord.setManaged(data.getManaged());
|
||||
domainRecord.setCreatedAt(createdAt);
|
||||
|
||||
if (existingIds.contains(data.getId())) {
|
||||
recruitmentRecords.add(domainRecord);
|
||||
} else {
|
||||
newRecruitmentRecords.add(domainRecord);
|
||||
}
|
||||
}
|
||||
|
||||
ervuDirectoriesDaoService.insertRecruitmentRecords(newRecruitmentRecords);
|
||||
ervuDirectoriesDaoService.updateRecruitmentRecords(recruitmentRecords);
|
||||
}
|
||||
|
||||
private void upsertRoleData(List<RoleResponse.Data> dataList) {
|
||||
String[] adminRoles = ervuAdminRole.split(",");
|
||||
List<UserApplicationRoleRecord> newRoleRecords = new ArrayList<>();
|
||||
List<UserApplicationRoleRecord> roleRecords = new ArrayList<>();
|
||||
List<String> ids = ervuDirectoriesDaoService.getRoleIds();
|
||||
dataList.forEach(data -> {
|
||||
if (data.getErvuRole() == null || !data.getErvuRole()) {
|
||||
return;
|
||||
}
|
||||
Timestamp updatedAt = Timestamp.from(Instant.ofEpochSecond(data.getModified()));
|
||||
Timestamp createdAt = Timestamp.from(Instant.ofEpochSecond(data.getCreateDate()));
|
||||
Timestamp finishAt = null;
|
||||
if (data.getFinish() != null) {
|
||||
finishAt = Timestamp.from(Instant.ofEpochSecond(data.getFinish()));
|
||||
}
|
||||
UserApplicationRoleRecord roleRecord = ervuDirectoriesDaoService.getRoleRecord();
|
||||
roleRecord.setUserRoleId(data.getId());
|
||||
roleRecord.setRoleCode(data.getName());
|
||||
roleRecord.setRoleName(data.getDisplayName());
|
||||
roleRecord.setCreated(createdAt);
|
||||
roleRecord.setUpdated(updatedAt);
|
||||
roleRecord.setFinished(finishAt);
|
||||
Arrays.stream(adminRoles).forEach(role -> {
|
||||
if (role.trim().equals(data.getName())) {
|
||||
roleRecord.setAdminRole(true);
|
||||
}
|
||||
});
|
||||
if (ids.contains(data.getId())) {
|
||||
roleRecords.add(roleRecord);
|
||||
}
|
||||
else {
|
||||
newRoleRecords.add(roleRecord);
|
||||
}
|
||||
});
|
||||
ervuDirectoriesDaoService.insertRoleRecords(newRoleRecords);
|
||||
ervuDirectoriesDaoService.updateRoleRecords(roleRecords);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue