package com.napa.pulse.service.impl; import com.google.gson.Gson; import com.napa.pulse.dto.FiledLineGroupCode; import com.napa.pulse.dto.LeadTimeData; import com.napa.pulse.entity.pulseui.LeadTime; import com.napa.pulse.entity.pulseui.MinMaxClacLn; import com.napa.pulse.entity.pulseui.MinMaxSitePref; import com.napa.pulse.repository.LeadTimeRepository; import com.napa.pulse.repository.MinMaxSitePrefRepository; import com.napa.pulse.repository.SiteRepository; import com.napa.pulse.security.GCPAuthenticationService; import com.napa.pulse.service.interfaces.LeadTimeService; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.configurationprocessor.json.JSONObject; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import javax.net.ssl.SSLContext; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Service public class LeadTimeServiceImpl implements LeadTimeService { private static final Logger LOGGER = LoggerFactory.getLogger(LeadTimeServiceImpl.class); @Value("${leadTime.service.url}") private String leadTimeTestUrl; private GCPAuthenticationService gcpAuthenticationService; @Autowired private SiteRepository siteRepo; @Autowired private LeadTimeRepository leadTimeRepo; @Autowired private JdbcTemplate jdbcTemplate; @Autowired private MinMaxSitePrefRepository mmspRepo; @Autowired private LeadTimeServiceImpl(GCPAuthenticationService gcpAuthenticationService) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { this.gcpAuthenticationService = gcpAuthenticationService; } TrustStrategy acceptingTrustStrategy = (x509Certificates, s) -> true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory); public LeadTimeServiceImpl() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { } @Override public LeadTimeData validateOnPremCloudConnection(List sites) { try { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Authorization", "Bearer ".concat(gcpAuthenticationService.getToken())); HttpEntity httpEntity = new HttpEntity<>(httpHeaders); JSONObject jsonObject = new JSONObject(restTemplate.exchange(leadTimeTestUrl.concat("?sites=" + String.join(",", sites)), HttpMethod.GET, httpEntity, String.class).getBody()) .getJSONObject("content"); /*return LeadTimeData.builder() .leadTimeData(new Gson().fromJson(String.valueOf(jsonObject), Map.class)) .build();*/ LeadTimeData leadTimeResponse = LeadTimeData.builder() .leadTimeData(new Gson().fromJson(String.valueOf(jsonObject), Map.class)) .build(); saveLeadTime(leadTimeResponse,sites); return leadTimeResponse; } catch (Exception ex) { LOGGER.error("Exception in LeadTimeService Call: ", ex); return null; } } private void saveLeadTime(LeadTimeData leadTimeData,Listsites) { List site =sites.stream().map(Integer::parseInt).collect(Collectors.toList()); String siteList=siteRepo.findSiteIds(site).stream().map(String::valueOf).collect(Collectors.joining(",")); String sql="update MIN_MAX_SITE_PREFERENCES set LEAD_TIME_STATUS='N' where site_id in ( "+ siteList +" ) "; jdbcTemplate.update(sql); for (Map.Entry> entry : leadTimeData.getLeadTimeData().entrySet()) { Integer macId = Integer.parseInt(entry.getKey()); List leadTime = entry.getValue(); int siteId = siteRepo.findSiteId(macId); LeadTime leadTimeObj = new LeadTime(); for (int i = 0; i < leadTime.size(); i++) { Gson gson = new Gson(); String jsonInString = String.valueOf(leadTime.get(i)); FiledLineGroupCode fdLnGrCd = gson.fromJson(jsonInString, FiledLineGroupCode.class); leadTimeObj.setFieldLine(fdLnGrCd.getFldLineAbbr()); MinMaxClacLn obj = leadTimeRepo.getMinMaxClacLnByFieldLine(fdLnGrCd.getFldLineAbbr()); if(obj != null) { MinMaxSitePref objPref = new MinMaxSitePref(); objPref.setSiteId(siteId); objPref.setLineId(obj.getLineId()); int leadTimeValue = fdLnGrCd.getVndrLeadTime() != null ? fdLnGrCd.getVndrLeadTime() : fdLnGrCd.getDcLeadTime() != null ? fdLnGrCd.getDcLeadTime() : 7; objPref.setLeadTime(leadTimeValue); objPref.setLeadTimeStatus('Y'); mmspRepo.save(objPref); } } } } }