Commit b2706f2d authored by Arie Valdano's avatar Arie Valdano

DEVELOP : developing API DGI inv/read v 1.2

parent 9b675ccd
......@@ -2,4 +2,7 @@
mvnw
mvnw.cmd
Dockerfile
target/
\ No newline at end of file
target/
.classpath
.project
.settings/
\ No newline at end of file
......@@ -80,6 +80,24 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.eksad.ddms</groupId>
<artifactId>ddms-common</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.eksad.ddms</groupId>
<artifactId>ddms-masterdata-h3-domain</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
......
package com.eksad.masterdata.common;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
/**
*
* @author hello world
*/
@Component
public class RequestUtil {
@Value("${dam.portal.h2.token}")
private String PORTAL_TOKEN;
@Value("${dam.token.key.path}")
private String TOKEN;
private static final String HEADER_KEY = "Authorization";
public HttpEntity<String> getPortalPreFormattedRequestWithToken(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML));
httpHeaders.add("Authorization", PORTAL_TOKEN);
return new HttpEntity<>(httpHeaders);
}
public HttpEntity<String> getPreFormattedRequestWithToken(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
httpHeaders.add(HEADER_KEY,TOKEN);
return new HttpEntity<>(httpHeaders);
}
public HttpEntity<String> getPortalPreFormattedRequestWithRequestedToken(String token){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
httpHeaders.add(HEADER_KEY,token);
return new HttpEntity<>(httpHeaders);
}
public HttpEntity<String> getPortalPreFormattedRequestDAWWithRequestedToken(String token){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.setAccept(Arrays.asList(MediaType.ALL));
httpHeaders.add(HEADER_KEY,token);
return new HttpEntity<>(httpHeaders);
}
}
package com.eksad.masterdata.controller.apidgi.invoicenjbnsc;
import com.eksad.masterdata.service.invoicenjbnsc.DGIInvoiceNjbNscService;
import com.eksad.ddms.common.h2.dto.apigateway.request.PullNotaJBSCRequestDTO;
import com.eksad.ddms.common.h2.dto.ui.ResponseErrorDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
@RestController
@Slf4j
public class DGIInvoiceNjbNscRESTController {
@Autowired
DGIInvoiceNjbNscService pullService;
@Value("${dam.token.key.path}")
private String TOKEN;
@Value("${dam.masterdata.h1}")
private String md_h1_url;
@Value("${dam.portal.daya.path}")
private String PORTAL_DAM_URL;
@Value("${dam.portal.daya.inv}")
private String PORTAL_DAM_INV_URL;
@Value("${daw.portal.daya.inv}")
private String PORTAL_DAW_INV_URL;
@Value("${md.portal.profile}")
public String MD_PROFILE;
@RequestMapping(value = "/dgi-api/v1/inv2/read",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> pullNota(@RequestHeader("GvtDealerId") String dealerid, @RequestBody PullNotaJBSCRequestDTO dto) throws ParseException {
try {
System.out.println(new ObjectMapper().writeValueAsString(dto));
pullService.exceptionHelper(dto.getFromTime(), "yyyy-MM-dd HH:mm:ss", "fromtime");
pullService.exceptionHelperNotMandatory(dto.getToTime(), "yyyy-MM-dd HH:mm:ss", "totime");
if (pullService.isNotValidDate(dto.getFromTime())) {
throw new Exception("format toTime tidak sesuai, seharusnya YYYY-MM-DD hh:mm:ss");
} else if (pullService.isNotValidDate(dto.getToTime())) {
throw new Exception("format toTime tidak sesuai, seharusnya YYYY-MM-DD hh:mm:ss");
}
pullService.checkToTimeIs7Days(dto.getFromTime(), dto.getToTime());
pullService.dealerValidation(dealerid, dto.getDealerId());
} catch (Exception ex) {
Logger.getLogger(DGIInvoiceNjbNscRESTController.class.getName()).log(Level.SEVERE, null, ex);
ResponseErrorDTO res = new ResponseErrorDTO();
res.setMessage(ex.getMessage());
res.setStatus(HttpStatus.BAD_REQUEST.value());
res.setError(HttpStatus.BAD_REQUEST.getReasonPhrase());
return ResponseEntity.badRequest().body(res);
}
return pullService.postRequestPullNJBNSC(dto);
}
}
package com.eksad.masterdata.service.invoicenjbnsc;
import com.eksad.ddms.common.h2.dto.apigateway.request.PullNotaJBSCRequestDTO;
import com.eksad.ddms.common.uri.apigateway.APIGatewayURI;
import com.eksad.masterdata.common.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
@Service
public class DGIInvoiceNjbNscService {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
private RestTemplate restTemplate = new RestTemplate();
@Value("${dam.masterdata.h2}")
private String md_h2_url;
@Value("${dam.gigr.h3}")
private String gigr_h3_url;
@Value("${dam.custpurch.h3}")
private String custpurch_h3_url;
@Autowired
private RequestUtil requestUtil;
public ResponseEntity<Object> postRequestPullNJBNSC(PullNotaJBSCRequestDTO dto) throws ParseException {
Object data = pullNJBNSC(dto);
if(data != null){
return ResponseEntity.status(HttpStatus.OK).body(data);
}
return null;
}
private Object pullNJBNSC(PullNotaJBSCRequestDTO dto) {
ResponseEntity<Object> response = restTemplate.exchange(
md_h2_url
+ APIGatewayURI.PULL_NSC_NJB.getUri(),
HttpMethod.POST,
new HttpEntity<>(dto, requestUtil.getPreFormattedRequestWithToken().getHeaders()),
Object.class);
return response.getBody();
}
public void checkToTimeIs7Days(String fromTime, String toTime) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date from = dateFormat.parse(fromTime);
Date to = dateFormat.parse(toTime);
LocalDateTime fromLocal = from.toInstant().atZone(ZoneId.of("Asia/Jakarta")).toLocalDateTime();
LocalDateTime toLocal = to.toInstant().atZone(ZoneId.of("Asia/Jakarta")).toLocalDateTime();
Duration durasi = Duration.between(fromLocal, toLocal);
long dayDifference = Math.abs(durasi.toHours());
if (dayDifference > (7 * 24) + 1) { // if duration greater than 24*7 + 1 hours
throw new Exception("tenggat waktu hanya diperbolehkan 7 hari");
}
}
public void exceptionHelper(String time, String format, String message) throws Exception {
if (time == null || time.trim().isEmpty()) {
throw new Exception(message + " tidak boleh kosong");
}
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.parse(time);
} catch (ParseException e) {
throw new Exception("format " + message.concat(" tidak sesuai, seharusnya ").concat(format));
}
}
public void exceptionHelperNotMandatory(String time, String format, String message) throws Exception {
if (time != null && !time.trim().isEmpty()) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.parse(time);
} catch (ParseException e) {
throw new Exception("format " + message.concat(" tidak sesuai, seharusnya ").concat(format));
}
}
}
public boolean isNotValidDate(String input) {
try {
dateTimeFormatter.parse(input);
return false;
} catch (Exception e) {
return true;
}
}
public void dealerValidation(String dealerHeader, String dealerRequest) throws Exception {
if (dealerHeader != null && dealerHeader != dealerRequest) {
if (!dealerHeader.equalsIgnoreCase(dealerRequest)) {
throw new Exception("gagal dealerId " + dealerRequest + " tidak sesuai");
}
}
}
}
......@@ -3,6 +3,13 @@ server.port=8081
spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.username = postgres
spring.datasource.password = password.1
dam.token.key.path = ${DGI_TOKEN_KEY: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.5BG9SEVOGo_xRhtT8IkyoSy60kPg8HM9Vpvb0TdNew4}
dam.masterdata.h1 = ${DAM_MD_H1:http://api-cbr.daya-dms.id/masterdata/}
dam.portal.daya.path = ${DAM_PORTAL_GW:https://api-uat.eksad.com/actlog/gw}
dam.portal.daya.inv = ${DAM_PORTAL_DAYA_INV:https://mdlwr.daya-motora.com:8248/dgi-api/prod/v1.3/mdinvh3/read}
daw.portal.daya.inv = ${DAW_PORTAL_DAYA_INV:https://mdlwr.daya-wisesa.com:8248/dgidaw-api/prod/v1.3/mdinvh3/read}
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto = none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
......@@ -11,4 +18,5 @@ spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=ERROR
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
md.portal.profile = DAM
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment