salmanoe 3c46431352 Update SlipGajiService
Pembuatan service unggah daftar slip gaji pegawai dari excel template
2023-02-01 17:47:12 +07:00

130 lines
5.4 KiB
Java

package com.jasamedika.medifirst2000.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.dto.SlipGajiDto;
import com.jasamedika.medifirst2000.service.SlipGajiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.ResourceUtils;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
@RestController
@RequestMapping("/slip-gaji")
public class SlipGajiController {
@Autowired
private SlipGajiService slipGajiService;
@RequestMapping(value = "/init", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> init() {
try {
slipGajiService.init();
return new ResponseEntity<>(true, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> save(@RequestBody @Valid SlipGajiDto dto) {
try {
slipGajiService.save(dto);
return new ResponseEntity<>(true, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/unggah", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> save(@RequestBody @Valid List<SlipGajiDto> dtoList) {
try {
slipGajiService.save(dtoList);
return new ResponseEntity<>(true, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/detail/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> updateDetail(@RequestBody @Valid SlipGajiDto dto) {
try {
slipGajiService.updateDetail(dto);
return new ResponseEntity<>(true, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/get/{pegawaiId}/{bulan}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> get(@PathVariable Integer pegawaiId, @PathVariable Long bulan) {
SlipGajiDto entity = slipGajiService.get(pegawaiId, bulan);
return new ResponseEntity<>(entity, HttpStatus.OK);
}
@RequestMapping(value = "/template/generate/{bulan}", method = RequestMethod.GET)
public ResponseEntity<Object> generateTemplate(@PathVariable Long bulan, HttpServletRequest request,
HttpServletResponse response) {
try {
String fileNamePath = slipGajiService.writeExcel(bulan);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + fileNamePath);
return new ResponseEntity<>(true, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/template/download", method = RequestMethod.GET)
public ResponseEntity<Resource> downloadTemplate(@RequestParam(value = "filename", required = true) String fileName,
HttpServletRequest request) {
Resource resource = null;
Map<String, String> mapHeaderMessage = new HashMap<String, String>();
if (CommonUtil.isNotNullOrEmpty(fileName)) {
try {
resource = ResourceUtils.loadFile(fileName);
} catch (Exception e) {
mapHeaderMessage.put(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
}
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (Exception e) {
mapHeaderMessage.put(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
}
if (CommonUtil.isNullOrEmpty(contentType))
contentType = "application/octet-stream";
return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + resource.getFilename())
.body(resource);
} else {
return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
}
}
}