package br.com.log.controller;


import java.io.IOException;

import java.text.ParseException;

import java.util.List;


import javax.management.relation.RoleInfoNotFoundException;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.multipart.MultipartFile;


import br.com.log.model.Log;

import br.com.log.service.LogService;


@RestController

@RequestMapping("/api/log")

public class LogController {


@Autowired

private LogService logService;


private final Logger logger = LoggerFactory.getLogger(LogController.class);


@PostMapping("/upload/multi")

public ResponseEntity<?> uploadFileMulti(@RequestParam("file") MultipartFile uploadfile) throws IOException {


logger.debug("File upload!");


logService.importFile(uploadfile);


return new ResponseEntity<>(HttpStatus.OK);

}


@PostMapping("/create")

public ResponseEntity<Log> createLog(@RequestBody Log log) throws RoleInfoNotFoundException {

return new ResponseEntity<Log>(logService.createLog(log), HttpStatus.OK);

}


@GetMapping("/retrieve")

public ResponseEntity<List<Log>> listando() {

return new ResponseEntity<List<Log>>(logService.retrieveLog(), new HttpHeaders(), HttpStatus.OK);

}


@PutMapping("/update/{id}")

public ResponseEntity<HttpStatus> updateLog(@PathVariable("id") long id, @RequestBody Log log)

throws RoleInfoNotFoundException {


logService.updateLogById(id, log);


return new ResponseEntity<HttpStatus>(HttpStatus.OK);

}


@DeleteMapping("/delete/{id}")

public ResponseEntity<HttpStatus> deleteLogById(@PathVariable("id") long id) throws RoleInfoNotFoundException {


logService.deleteLogById(id);


return new ResponseEntity<HttpStatus>(HttpStatus.OK);

}


@GetMapping("/search")

public ResponseEntity<List<Log>> searching(String datai, String dataf, String ip) throws ParseException {

return new ResponseEntity<List<Log>>(logService.search(datai, dataf, ip), new HttpHeaders(), HttpStatus.OK);

}


}


<----->


package br.com.log.model;


import java.util.Date;


import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.SequenceGenerator;

import javax.persistence.Table;


@Entity

@Table(name = "log_chibata")

public class Log {


@Id

// @GeneratedValue(strategy = GenerationType.IDENTITY) for MySql

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")

@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")

private Long id;

private Date data;

private String ip;

private String request;

private String status;

private String useragent;



public Long getId() {

return id;

}


public void setId(Long id) {

this.id = id;

}


public Date getData() {

return data;

}


public void setData(Date dataf) {

this.data = dataf;

}


public String getIp() {

return ip;

}


public void setIp(String ip) {

this.ip = ip;

}


public String getRequest() {

return request;

}


public void setRequest(String request) {

this.request = request;

}


public String getStatus() {

return status;

}


public void setStatus(String status) {

this.status = status;

}


public String getUseragent() {

return useragent;

}


public void setUseragent(String useragent) {

this.useragent = useragent;

}

}


<----->


package br.com.log.service;


import java.io.IOException;

import java.text.ParseException;

import java.time.LocalDate;

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.time.format.DateTimeFormatter;

import java.util.Arrays;

import java.util.Date;

import java.util.List;

import java.util.Optional;


import javax.management.relation.RoleInfoNotFoundException;

import javax.persistence.EntityManager;

import javax.persistence.TypedQuery;


import org.apache.commons.io.IOUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;


import com.github.dozermapper.core.DozerBeanMapperBuilder;


import br.com.log.model.Log;

import br.com.log.repository.LogRepository;


@Service

public class LogService {


@Autowired

private LogRepository logRepository;


@Autowired

private EntityManager em;


public void importFile(MultipartFile uploadfile) throws IOException {


System.out.println(uploadfile);


List<String> lines = IOUtils.readLines(uploadfile.getInputStream(), "UTF-8");


for (String lin : lines) {


String[] columns = lin.split("\\|");

System.out.println(Arrays.toString(columns));


System.out.println(lin);

Log log = new Log();


// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");

LocalDate localDate = LocalDate.parse(columns[0], formatter);


log.setData(Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));

log.setIp(columns[1]);

log.setRequest(columns[2]);

log.setStatus(columns[3]);

log.setUseragent(columns[4]);


logRepository.save(log);

}

}


public Log createLog(Log log) throws RoleInfoNotFoundException {


return logRepository.save(DozerBeanMapperBuilder.buildDefault().map(log, Log.class));


/*

* Log log2 = new Log();

*

* log2.setData(log.getData()); log2.setIp(log.getIp());

* log2.setRequest(log.getRequest()); log2.setStatus(log.getStatus());

* log2.setUseragent(log.getUseragent());

*

* return logRepository.save(log);

*/

}


public List<Log> retrieveLog() {

return logRepository.findAll();

}


public void updateLogById(long id, Log log) throws RoleInfoNotFoundException {


Optional<Log> updatelog = logRepository.findById(id);

System.out.println(updatelog);


if (updatelog.isPresent()) {


log.setData(log.getData());

log.setIp(log.getIp());

log.setRequest(log.getRequest());

log.setStatus(log.getStatus());

log.setUseragent(log.getUseragent());


logRepository.save(log);


} else {

throw new RoleInfoNotFoundException("No exist for given id");

}

}


public void deleteLogById(Long id) throws RoleInfoNotFoundException {


Optional<Log> deletelog = logRepository.findById(id);


System.out.println(deletelog);


if (deletelog.isPresent()) {

logRepository.deleteById(id);

} else {

throw new RoleInfoNotFoundException("No employee record exist for given id");

}

}


public List<Log> search(String dataIn, String dataFim, String ip) throws ParseException {


TypedQuery<Log> query = em.createQuery("select l from Log l where data between :data and :data1 and ip = :ip",Log.class);

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


LocalDateTime dataLocDtTmIn = LocalDateTime.parse(dataIn, format);

LocalDateTime dataLocDtTmFm = LocalDateTime.parse(dataFim, format);

Date dataConvertDeLocDtTmIn = Date.from(dataLocDtTmIn.atZone(ZoneId.systemDefault()).toInstant());

Date dataConvertDeLocDtTmFm = Date.from(dataLocDtTmFm.atZone(ZoneId.systemDefault()).toInstant());

query.setParameter("data", dataConvertDeLocDtTmIn);

query.setParameter("data1", dataConvertDeLocDtTmFm);

query.setParameter("ip", ip);

return query.getResultList();

}

}



<----->


package br.com.log.repository;


import java.util.Date;

import java.util.List;


import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;


import br.com.log.model.Log;


public interface LogRepository extends JpaRepository<Log, Long> {


// Usando JPQL

@Query(value = "select l from Log l where data between :data and :data1 and ip = :ip")

List<Log> findByDataBetweenAndIpOrderByDataDesc(Date data, Date data1, String ip);


}


<----->


<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>


<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.7.RELEASE</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>br.com.teste</groupId>

<artifactId>log</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>log</name>

<description>Demo project for Spring Boot</description>


<properties>

<java.version>1.8</java.version>

</properties>


<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<optional>true</optional>

</dependency>


<dependency>

<groupId>org.webjars</groupId>

<artifactId>jquery</artifactId>

<version>2.2.4</version>

</dependency>


<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.17</version>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>


<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.6</version>

</dependency>


<dependency>

<groupId>com.github.dozermapper</groupId>

<artifactId>dozer-core</artifactId>

<version>6.5.0</version>

</dependency>


</dependencies>


<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>


</project>



Fale conosco

  • Google+ Social Icon
  • LinkedIn Social Icon

Atendimento Comercial:

© 2020 by DescompTec