Skip to content

Commit c9afbc7

Browse files
committed
version 0.6
1 parent 4bcdb2b commit c9afbc7

File tree

11 files changed

+716
-21
lines changed

11 files changed

+716
-21
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959
<artifactId>mysql-connector-j</artifactId>
6060
<scope>runtime</scope>
6161
</dependency>
62+
<dependency>
63+
<groupId>net.sf.jasperreports</groupId>
64+
<artifactId>jasperreports</artifactId>
65+
<version>6.19.0</version>
66+
</dependency>
6267
</dependencies>
6368

6469
<build>

src/main/java/com/cibertec/shoesformen_api/controller/EmpleadoController.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import com.cibertec.shoesformen_api.repository.EmpleadoRepository;
1010
import com.cibertec.shoesformen_api.repository.EstadoRepository;
1111
import com.cibertec.shoesformen_api.service.EmpleadoService;
12+
import jakarta.servlet.http.HttpServletResponse;
1213
import jakarta.validation.Valid;
14+
import net.sf.jasperreports.engine.JRException;
1315
import org.springframework.beans.factory.annotation.Autowired;
1416
import org.springframework.http.HttpStatus;
17+
import org.springframework.http.MediaType;
1518
import org.springframework.http.ResponseEntity;
1619
import org.springframework.stereotype.Controller;
17-
import org.springframework.web.bind.MethodArgumentNotValidException;
1820
import org.springframework.web.bind.annotation.*;
1921

22+
import java.io.IOException;
2023
import java.util.List;
2124

2225
@Controller
@@ -32,7 +35,6 @@ public class EmpleadoController {
3235
private EmpleadoService empleadoServ;
3336
@Autowired
3437
private EmpleadoRepository empleadoRepo;
35-
3638
@Autowired
3739
private DistritoRepository distritoRepo;
3840
@Autowired
@@ -53,17 +55,36 @@ public ResponseEntity<Empleado> newEmpleado (@RequestBody @Valid EmpleadoDTO dto
5355

5456
@PutMapping("/{id}") // ACTUALIZACION, su objetivo es actualizar no crear, por eso debe verificar si existe.
5557
public ResponseEntity replaceEmpleado(@RequestBody @Valid EmpleadoDTO dto, @PathVariable String id) throws IllegalArgumentException, EntidadNotFoundException{ // imaginate que el id no coincida con el de empleado que envia. Ademas el empleado no deberia venir con ID, ¿porque?
56-
5758
if(empleadoServ.getEmpleadoByCodigo(id).isPresent() ){
5859
Empleado empleado = empleadoServ.buildEmpleado(dto);
5960
empleado.setCodEmpleado(id);
6061
return new ResponseEntity(empleadoServ.guardar(empleado), HttpStatus.OK);
6162
} else {
62-
throw new EntidadNotFoundException("Empleado no encontrado");
63+
throw new EntidadNotFoundException(id);
6364
}
6465
}
6566

67+
@DeleteMapping("/{id}")
68+
public ResponseEntity delete(@PathVariable String id) throws IllegalArgumentException, EntidadNotFoundException{
69+
if(empleadoServ.getEmpleadoByCodigo(id).isPresent() ){
70+
empleadoServ.eliminarByCodigo(id);
71+
return new ResponseEntity<>(HttpStatus.OK);
72+
} else {
73+
throw new EntidadNotFoundException(id + " - No eliminado");
74+
}
75+
}
76+
77+
@GetMapping("/rpt_EXCEL")
78+
private ResponseEntity<Void> reporteEmpleadoEXCEL(HttpServletResponse response) throws JRException, IOException {
79+
empleadoServ.exportarReporte("excel", response);
80+
return ResponseEntity.ok().build();
81+
}
6682

83+
@GetMapping(value = "/rpt_PDF", produces = MediaType.APPLICATION_PDF_VALUE)
84+
private ResponseEntity<Void> reporteEmpleadoPDF(HttpServletResponse response) throws JRException,IOException{
85+
empleadoServ.exportarReporte("pdf", response);
86+
return ResponseEntity.ok().build();
87+
}
6788

6889

6990
/* Configurar la respuesta HTTP

src/main/java/com/cibertec/shoesformen_api/exception/EmpleadoNotFoundException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.cibertec.shoesformen_api.exception;
22

3-
public class EmpleadoNotFoundException extends Exception{
3+
public class EmpleadoNotFoundException extends RuntimeException{
44
public EmpleadoNotFoundException(String message){
55
super(message);
66
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.cibertec.shoesformen_api.exception;
22

3-
public class EntidadNotFoundException extends Exception{
4-
public EntidadNotFoundException(String message){
5-
super(message);
3+
public class EntidadNotFoundException extends RuntimeException{
4+
public EntidadNotFoundException(String id){
5+
super("No se encontro Entidad con codigo " + id);
66
}
77
}

src/main/java/com/cibertec/shoesformen_api/exception/ListEmptyException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.cibertec.shoesformen_api.exception;
22

3-
public class ListEmptyException extends Exception{
3+
public class ListEmptyException extends RuntimeException{
44
public ListEmptyException(String message){
55
super(message);
66
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cibertec.shoesformen_api.model.pojo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor()
8+
public class EmpleadoPOJO {
9+
10+
private String codEmpleado;
11+
private String distrito; // --
12+
private String estado; // --
13+
private String nombre;
14+
private String apellidos;
15+
private String dni;
16+
private String direccion;
17+
private String telefono;
18+
private String email;
19+
private String usuario;
20+
private String contrasena;
21+
22+
}

src/main/java/com/cibertec/shoesformen_api/repository/EmpleadoRepository.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
package com.cibertec.shoesformen_api.repository;
22

33
import com.cibertec.shoesformen_api.model.Empleado;
4+
import com.cibertec.shoesformen_api.model.dto.EmpleadoDTO;
5+
import com.cibertec.shoesformen_api.model.pojo.EmpleadoPOJO;
46
import org.springframework.data.jpa.repository.JpaRepository;
57
import org.springframework.data.jpa.repository.Query;
68
import org.springframework.stereotype.Repository;
79

10+
import java.util.List;
11+
812
@Repository
913
public interface EmpleadoRepository extends JpaRepository<Empleado, String> {
1014

15+
// INNER JOIN con JPQL
16+
@Query("SELECT new com.cibertec.shoesformen_api.model.pojo.EmpleadoPOJO("
17+
+ "e.codEmpleado,"
18+
+ "e.distrito.nombreDistrito,"
19+
+ "e.estado.nombreEstado,"
20+
+ "e.nombre,"
21+
+ "e.apellidos,"
22+
+ "e.dni,"
23+
+ "e.direccion,"
24+
+ "e.telefono,"
25+
+ "e.email,"
26+
+ "e.usuario,"
27+
+ "e.contrasena"
28+
+ ") "
29+
+ "FROM Empleado e ORDER BY e.codEmpleado ASC")
30+
public abstract List<EmpleadoPOJO> listaPOJO();
31+
1132
// CONSULTA NATIVA
1233
@Query(value="SELECT CONCAT('EM',CAST(SUBSTRING(MAX(e.cod_empleado),3) AS INT) + 1) FROM empleado AS e", nativeQuery = true)
1334
public abstract String getUltimoCodigo();

src/main/java/com/cibertec/shoesformen_api/service/EmpleadoService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55
import com.cibertec.shoesformen_api.exception.ListEmptyException;
66
import com.cibertec.shoesformen_api.model.Empleado;
77
import com.cibertec.shoesformen_api.model.dto.EmpleadoDTO;
8+
import jakarta.servlet.http.HttpServletResponse;
9+
import net.sf.jasperreports.engine.JRException;
810
import org.springframework.http.ResponseEntity;
911

12+
import javax.management.JMRuntimeException;
13+
import java.io.IOException;
1014
import java.util.List;
1115
import java.util.Optional;
1216

1317
public interface EmpleadoService {
1418

1519
public List<Empleado> listar() throws ListEmptyException;
16-
public void eliminar(Empleado empleado);
17-
public void eliminarByCodigo(String codigo);
20+
public void eliminarByCodigo(String codigo) throws IllegalArgumentException;
1821
public Empleado guardar(Empleado empleado) throws IllegalArgumentException, EntidadNotFoundException;
1922
public Optional<Empleado> getEmpleadoByCodigo(String codigo) throws EntidadNotFoundException;
2023
public String createNewCodigo();
2124
public Empleado buildEmpleado(EmpleadoDTO dto) throws EntidadNotFoundException;
22-
25+
public void exportarReporte(String tipo, HttpServletResponse response) throws JRException, IOException;
2326

2427
}

src/main/java/com/cibertec/shoesformen_api/service/EmpleadoServiceImpl.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@
1111
import com.cibertec.shoesformen_api.repository.EmpleadoRepository;
1212
import com.cibertec.shoesformen_api.repository.EstadoRepository;
1313
import com.cibertec.shoesformen_api.repository.RolRepository;
14+
import jakarta.servlet.http.HttpServletResponse;
15+
import net.sf.jasperreports.engine.*;
16+
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
17+
import net.sf.jasperreports.engine.export.JRPdfExporter;
18+
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
19+
import net.sf.jasperreports.export.SimpleExporterInput;
20+
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
1421
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.http.HttpHeaders;
1523
import org.springframework.stereotype.Service;
1624

17-
import java.util.Arrays;
18-
import java.util.List;
19-
import java.util.Optional;
25+
import javax.management.JMRuntimeException;
26+
import java.io.FileInputStream;
27+
import java.io.IOException;
28+
import java.text.SimpleDateFormat;
29+
import java.util.*;
2030

2131
@Service
2232
public class EmpleadoServiceImpl implements EmpleadoService{
@@ -41,12 +51,7 @@ public List<Empleado> listar() throws ListEmptyException {
4151
}
4252

4353
@Override
44-
public void eliminar(Empleado empleado) {
45-
empleadoRepo.delete(empleado);
46-
}
47-
48-
@Override
49-
public void eliminarByCodigo(String codigo) {
54+
public void eliminarByCodigo(String codigo) throws IllegalArgumentException{
5055
empleadoRepo.deleteById(codigo);
5156
}
5257

@@ -102,4 +107,49 @@ public Empleado buildEmpleado(EmpleadoDTO dto) throws IllegalArgumentException,
102107
return empleado;
103108
}
104109

110+
@Override
111+
public void exportarReporte(String tipo, HttpServletResponse response) throws JRException, IOException {
112+
113+
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(empleadoRepo.listaPOJO());
114+
Map<String, Object> parametros = new HashMap<>();
115+
SimpleDateFormat formato1 = new SimpleDateFormat("dd 'de' MMMM 'del' yyyy 'a las' HH:mm:ss");
116+
SimpleDateFormat formato2 = new SimpleDateFormat("yyyyMMdd-HHmmss");
117+
Date fecha = new Date();
118+
String fecha1 = formato1.format(fecha);
119+
String fecha2 = formato2.format(fecha);
120+
121+
String imagen = "logo_reporte_2.png";
122+
parametros.put("imagen_logo","src/main/resources/static/img/" + imagen);
123+
parametros.put("nombre_empresa","SHOES FOR MEN");
124+
parametros.put("direccion_empresa","AV. URUGUAY N 000 ");
125+
parametros.put("distrito_empresa","SAN ISIDRO");
126+
parametros.put("nombre_empleado","KEVIN B");
127+
parametros.put("ruc_empresa","55555555555");
128+
parametros.put("telefono_empresa","777-7777");
129+
parametros.put("fecha_generacion", fecha1);
130+
parametros.put("DataEmpleado", dataSource);
131+
132+
JasperReport compileReport = JasperCompileManager.compileReport(new FileInputStream("src/main/resources/reporte_jasper/rpt_empleado.jrxml"));
133+
JasperPrint jasperPrint = JasperFillManager.fillReport(compileReport, parametros, new JREmptyDataSource());
134+
135+
136+
if(tipo.equalsIgnoreCase("pdf")) {
137+
JRPdfExporter exporter = new JRPdfExporter();
138+
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
139+
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
140+
response.setContentType("application/pdf");
141+
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=listaEmpleado_" + fecha2 + ".pdf");
142+
exporter.exportReport();
143+
}
144+
else if(tipo.equalsIgnoreCase("excel")) {
145+
JRXlsxExporter exporter = new JRXlsxExporter(); // la configuracion se hace en el mismo JASPER STUDIO
146+
exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); //
147+
response.setContentType("application/octet-stream");
148+
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
149+
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=listaEmpleado_" + fecha2 + ".xlsx");
150+
exporter.exportReport();
151+
}
152+
153+
}
154+
105155
}

0 commit comments

Comments
 (0)