Java tutorial
/** * (C) Copyright 2016 Johnson Controls, Inc * Use or Copying of all or any part of this program, except as * permitted by License Agreement, is prohibited. */ package com.jci.po.service; import java.net.URISyntaxException; import java.security.InvalidKeyException; import java.util.HashMap; import java.util.List; import org.apache.commons.lang3.StringUtils; 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.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.client.RestTemplate; import com.jci.po.azure.data.DataHelper; import com.jci.po.azure.data.ResultSet; import com.jci.po.azure.query.PaginationParam; import com.jci.po.azure.query.ScrollingParam; import com.jci.po.dto.req.EditUserDetails; import com.jci.po.dto.req.PoDetailsReq; import com.jci.po.dto.req.PoItemDetailReq; import com.jci.po.dto.req.SegmentedDetailReq; import com.jci.po.dto.res.BatchUpdateRes; import com.jci.po.dto.res.FlatFileRes; import com.jci.po.dto.res.PoItemDetailRes; import com.jci.po.dto.res.SegmentedDetailRes; import com.jci.po.entity.UserDetailsEntity; import com.jci.po.repo.PoRepo; import com.jci.po.repo.PoRepoImpl; import com.jci.po.utils.AzureUtils; import com.microsoft.azure.storage.StorageException; @Service @RefreshScope public class PoServiceImpl implements PoService { /** The Constant LOG. */ private static final Logger LOG = LoggerFactory.getLogger(PoServiceImpl.class); /** The repo. */ @Autowired private PoRepo repo; /** The all erps. */ @Value("${all.erp.names}") private String allErps; //@Value("${ffgeneration.service.pourl.resource}") private String poPostURLResource = "/postPOError"; //@Value("${ffgeneration.service.name}") private String ffgenerationMSName = "ffgenerator-service"; @Autowired private DiscoveryClient discoveryClient; @Value("${azure.storage.potablename}") private String poTableName; @Value("${azure.storage.usertablename}") private String userTableName; @Value("${azure.storage.poitemtablename}") private String poItemTableName; @Value("${azure.storage.miscdatatablename}") private String miscDataTableName; @Value("${azure.storage.partionkey.user.details}") private String userPk; @RequestMapping("/service-instances/{applicationName}") public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); } RestTemplate restTemplate = new RestTemplate(); @Override public SegmentedDetailRes getSegmentedResultSet(SegmentedDetailReq request) throws InvalidKeyException, URISyntaxException, StorageException { PaginationParam paginationParam = request.getPaginationParam(); ScrollingParam param = new ScrollingParam(); if (paginationParam != null) { param.setPartition(paginationParam.getNextPartition()); param.setRow(paginationParam.getNextRow()); } // For where condition param.setSize(request.getSize()); DataHelper azureRequest = null; ResultSet resultSet = null; SegmentedDetailRes response = new SegmentedDetailRes(); LOG.info("getGlobalId--->" + request.getGlobalId()); if (StringUtils.isBlank(request.getGlobalId())) { return response; } UserDetailsEntity entity = repo.getUserEntity(userPk, request.getGlobalId()); LOG.info("role entity--->" + entity); if (entity == null) { response.setMessage("User details does not exist!"); return response; } LOG.info("getRole--->" + entity.getRole()); if ("Admin".equalsIgnoreCase(entity.getRole())) { response.setRole("Admin"); response.setUserExist(true); } else if ("User".equalsIgnoreCase(entity.getRole())) { response.setRole("User"); response.setUserExist(true); } else { response.setUserExist(false); return response; } LOG.info("response--->" + response); HashMap<String, ResultSet> resultSetMap = new HashMap<>(); HashMap<String, ResultSet> errorMap = new HashMap<>(); LOG.info("isFirstRequest--->" + request.isFirstRequest()); if (request.isFirstRequest()) { String[] erpArr = allErps.split(","); for (int i = 0; i < erpArr.length; i++) { azureRequest = new DataHelper(); azureRequest.setErrorDataRequired(false); azureRequest.setErpName(erpArr[i]); azureRequest.setPartitionValue(AzureUtils.getPoPartitionKey(erpArr[i])); azureRequest.setTableName(poTableName); resultSet = repo.getSegmentedResultSet(param, azureRequest); resultSetMap.put(erpArr[i], resultSet); azureRequest.setTableName(poTableName); azureRequest.setErrorDataRequired(true); resultSet = repo.getSegmentedResultSet(param, azureRequest); errorMap.put(erpArr[i], resultSet); System.out.println("Error Map in service " + errorMap); System.out.println("resultSetMap in service " + resultSetMap); } response.setGraphData(repo.getGraphData()); //================================================================== //change by Bablu SegmentedDetailRes responseMapics = new SegmentedDetailRes(); PoRepoImpl repoimpl = new PoRepoImpl(); // response.setGraphData(repoimpl.getGraphDataMAP()); //================================================================= response.setResultSet(resultSetMap); response.setErrorData(errorMap); /* if(repo.isAdmin(request.getGlobalId())){ azureRequest.setPartitionValue(userPk); azureRequest.setTableName(userTableName); azureRequest.setErrorDataRequired(false); response.setUserData(repo.getSegmentedResultSet(param, azureRequest)); } */ } else { azureRequest = new DataHelper(); azureRequest.setErpName(request.getErpName()); azureRequest.setPartitionValue(request.getPartition()); azureRequest.setTableName(request.getTableName()); resultSet = repo.getSegmentedResultSet(param, azureRequest); resultSetMap.put(request.getErpName(), resultSet); response.setResultSet(resultSetMap); } response.setMessage("OK"); return response; } /* * (non-Javadoc) * * @see com.jci.po.service.PoService#getErrorResultSet(com.jci.po.dto.req. * SegmentedDetailReq) */ @Override public SegmentedDetailRes getErrorResultSet(SegmentedDetailReq request) throws InvalidKeyException, URISyntaxException, StorageException { PaginationParam paginationParam = request.getPaginationParam(); ScrollingParam param = new ScrollingParam(); if (paginationParam != null) { param.setPartition(paginationParam.getNextPartition()); param.setRow(paginationParam.getNextRow()); } // For where condition param.setSize(request.getSize()); DataHelper azureRequest = null; ResultSet resultSet = null; SegmentedDetailRes response = new SegmentedDetailRes(); HashMap<String, ResultSet> resultSetMap = new HashMap<>(); azureRequest = new DataHelper(); azureRequest.setErrorDataRequired(true); azureRequest.setErpName(request.getErpName()); azureRequest.setPartitionValue(request.getPartition()); azureRequest.setTableName(request.getTableName()); resultSet = repo.getSegmentedResultSet(param, azureRequest); resultSetMap.put(request.getErpName(), resultSet); response.setErrorData(resultSetMap); response.setMessage("OK"); return response; } /* * (non-Javadoc) * * @see com.jci.po.service.PoService#processErrorPos(com.jci.po.dto.req. * PoDetailsReq) */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public BatchUpdateRes processErrorPos(PoDetailsReq request) throws InvalidKeyException, URISyntaxException, StorageException { LOG.info("starting processErrorPos request--->" + request); BatchUpdateRes res = new BatchUpdateRes(); String partitionKey = AzureUtils.getPoPartitionKey(request.getErpName()); BatchUpdateRes response = new BatchUpdateRes(); if (StringUtils.isBlank(partitionKey)) { res.setError(true); res.setMessage("Invalid request parameters !"); return res; } List<String> poList = request.getPoNo(); if (poList == null || poList.size() < 1) { res.setError(true); res.setMessage("Invalid request parameters !"); return res; } List<ServiceInstance> ffmsInstanceList = discoveryClient.getInstances(ffgenerationMSName); ServiceInstance ffmsInstance = ffmsInstanceList.get(0); LOG.info("Before calling from Flat File Generation Service: " + "http://" + ffmsInstance.getServiceId().toLowerCase() + ":" + Integer.toString(ffmsInstance.getPort()) + poPostURLResource + "?userName=" + request.getUserName() + "&globalId=" + request.getGlobalId() + "&comment=" + request.getComment()); // start new HttpHeaders headers = new HttpHeaders(); headers.set("Accept", "application/json"); /* * Map<String, String> params = new HashMap<String, String>(); * params.put("userName", request.getUserName()); params.put("globalId", * request.getGlobalId()); params.put("comment", request.getComment()); */ HttpEntity entity = new HttpEntity(poList, headers); /* * HttpEntity<HashMap<String, String>> ffResponse = * restTemplate.exchange( * "http://api-gateway:8765/ffgenerator-service/postPOError", * HttpMethod.POST, entity, new * ParameterizedTypeReference<HashMap<String, String>>() { }); */ /* * HttpEntity<HashMap<String, String>> ffResponse = * restTemplate.exchange( * "http://ffgenerator-service:9200/postPOError?userName=" + * request.getUserName() + "&globalId=" + request.getGlobalId() + * "&comment=" + request.getComment(), HttpMethod.POST, entity, new * ParameterizedTypeReference<HashMap<String, String>>() { }); */ HttpEntity<FlatFileRes> ffResponse = restTemplate.exchange( "http://" + ffmsInstance.getServiceId().toLowerCase() + ":" + Integer.toString(ffmsInstance.getPort()) + poPostURLResource + "?userName=" + request.getUserName() + "&globalId=" + request.getGlobalId() + "&comment=" + request.getComment(), HttpMethod.POST, entity, new ParameterizedTypeReference<FlatFileRes>() { }); // client.processErrorPosFlatFiles(poList); FlatFileRes ffPos = ffResponse.getBody(); /*// Update status in DB boolean isUpdated = updateStatus(pkToSuccessList, request.getGlobalId(), request.getUserName(), request.getComment());*/ response = new BatchUpdateRes(); response.setGraphData(repo.getGraphData()); response.setSuccessList(ffPos.getSuccessList()); response.setErrorList(ffPos.getErrorList()); return response; } /* * (non-Javadoc) * * @see com.jci.po.service.PoService#getPoItemDetail(com.jci.po.dto.req. * PoItemDetailReq) */ @Override public PoItemDetailRes getPoItemDetail(PoItemDetailReq request) throws InvalidKeyException, URISyntaxException, StorageException { PaginationParam paginationParam = request.getPaginationParam(); ScrollingParam param = new ScrollingParam(); if (paginationParam != null) { param.setPartition(paginationParam.getNextPartition()); param.setRow(paginationParam.getNextRow()); } // For where condition param.setSize(request.getSize()); DataHelper azureRequest = new DataHelper(); azureRequest.setErpName(request.getErpName()); azureRequest.setPoNum(request.getPoNum()); azureRequest.setPartitionValue(AzureUtils.getPoItemPartitionKey(request.getErpName())); azureRequest.setTableName(poItemTableName); ResultSet resultSet = repo.getPoItemDetail(param, azureRequest); List<HashMap<String, Object>> series = resultSet.getSeries(); HashMap<String, Object> oldMap = series.get(0); LOG.info("oldMap--->" + oldMap); azureRequest.setTableName(poTableName); azureRequest.setPartitionValue(AzureUtils.getPoPartitionKey(request.getErpName())); resultSet = repo.getPoItemDetail(param, azureRequest); List<HashMap<String, Object>> series2 = resultSet.getSeries(); LOG.info("series2--->" + series2.get(0)); oldMap.putAll(series2.get(0)); series.clear(); series.add(oldMap); //series.addAll(resultSet.getSeries()); resultSet.setSeries(series); PoItemDetailRes response = new PoItemDetailRes(); response.setResultSet(resultSet); response.setMessage("OK"); return response; } @Override public SegmentedDetailRes getUserDetails(SegmentedDetailReq request) throws InvalidKeyException, URISyntaxException, StorageException { PaginationParam paginationParam = request.getPaginationParam(); ScrollingParam param = new ScrollingParam(); if (paginationParam != null) { param.setPartition(paginationParam.getNextPartition()); param.setRow(paginationParam.getNextRow()); } // For where condition param.setSize(request.getSize()); DataHelper azureRequest = null; SegmentedDetailRes response = new SegmentedDetailRes(); LOG.info("getGlobalId--->" + request.getGlobalId()); if (StringUtils.isBlank(request.getGlobalId())) { return response; } if (repo.isAdmin(request.getGlobalId())) { azureRequest = new DataHelper(); azureRequest.setPartitionValue(userPk); azureRequest.setTableName(userTableName); azureRequest.setErrorDataRequired(false); response.setUserData(repo.getSegmentedResultSet(param, azureRequest)); } response.setMessage("OK"); LOG.info("response--->" + response); return response; } @Override public EditUserDetails editUserDetails(EditUserDetails request) throws InvalidKeyException, URISyntaxException, StorageException { if (StringUtils.isBlank(request.getGlobalId()) || StringUtils.isBlank(request.getRole())) { request.setSuccess(false); return request; } LOG.info("userPk--->" + userPk); LOG.info("getGlobalId--->" + request.getGlobalId()); UserDetailsEntity entity = new UserDetailsEntity(userPk, request.getGlobalId()); LOG.info("details--->" + request.getGlobalId()); entity.setRole(request.getRole()); entity.setEmail(request.getEmail()); entity.setUserName(request.getUserName()); LOG.info("entity--->" + entity); boolean isSuccess = false; if (request.isDeleteUser()) { isSuccess = repo.deleteUserDetails(entity); request.setSuccess(true); } else { isSuccess = repo.updateUserDetails(entity); if (isSuccess) { entity = repo.getUserEntity(userPk, request.getGlobalId()); request.setId(String.valueOf(entity.getTimestamp())); request.setEmail(entity.getEmail()); request.setGlobalId(entity.getRowKey()); request.setRole(entity.getRole()); request.setUserName(entity.getUserName()); request.setId(String.valueOf(entity.getTimestamp())); request.setSuccess(true); } } return request; } }