Skip to content

Commit ccfe8dc

Browse files
committed
commit mapper application
1 parent 7070670 commit ccfe8dc

File tree

8 files changed

+317
-0
lines changed

8 files changed

+317
-0
lines changed

EVAM04_EMISIONVOTO_MAPPER/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<parent>
6+
<artifactId>EVAM04_EMISIONVOTO</artifactId>
7+
<groupId>edu.cibertec.votoelectronico.application</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<groupId>edu.cibertec.votoelectronico.mapper</groupId>
13+
<artifactId>EVAM04_EMISIONVOTO_MAPPER</artifactId>
14+
<name>EVAM04_EMISIONVOTO_MAPPER</name>
15+
<url>http://www.cibertec.edu.pe</url>
16+
<description>Mapping with MapStruct</description>
17+
<packaging>jar</packaging>
18+
19+
<properties>
20+
<project.build.finalName>EVAM04_EMISIONVOTO_MAPPER</project.build.finalName>
21+
</properties>
22+
23+
<dependencies>
24+
<!-- To map the EVAM04_EMISIONVOTO_API-classes.jar -->
25+
<!-- <dependency> -->
26+
<!-- <groupId>edu.cibertec.votoelectronico.api</groupId> -->
27+
<!-- <artifactId>EVAM04_EMISIONVOTO_API</artifactId> -->
28+
<!-- <version>1.0-SNAPSHOT</version> -->
29+
<!-- <classifier>classes</classifier> -->
30+
<!-- </dependency> -->
31+
32+
<dependency>
33+
<groupId>edu.cibertec.votoelectronico.core</groupId>
34+
<artifactId>EVAM04_EMISIONVOTO_CORE</artifactId>
35+
<version>1.0-SNAPSHOT</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.mapstruct</groupId>
39+
<artifactId>mapstruct</artifactId>
40+
<version>${org.mapstruct.version}</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<artifactId>maven-jar-plugin</artifactId>
48+
<version>${version.jar.plugin}</version>
49+
<configuration>
50+
</configuration>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>${version.compiler.plugin}</version>
56+
<configuration>
57+
<source>${maven.compiler.source}</source>
58+
<target>${maven.compiler.target}</target>
59+
<annotationProcessorPaths>
60+
<path>
61+
<groupId>org.mapstruct</groupId>
62+
<artifactId>mapstruct-processor</artifactId>
63+
<version>${org.mapstruct.version}</version>
64+
</path>
65+
</annotationProcessorPaths>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package edu.cibertec.votoelectronico.application;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("edu.cibertec.votoelectronico")
8+
public class MapperConfig {
9+
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package edu.cibertec.votoelectronico.mapping.impl;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.Mapping;
5+
6+
import edu.cibertec.votoelectronico.domain.GrupoPolitico;
7+
import edu.cibertec.votoelectronico.domain.Voto;
8+
import edu.cibertec.votoelectronico.dto.EmisionVotoDto;
9+
import edu.cibertec.votoelectronico.mapping.EmisionVotoDtoMapper;
10+
import edu.cibertec.votoelectronico.mapping.qualifiers.StringToGrupoPoliticoMapper;
11+
12+
@Mapper(componentModel = "spring")
13+
public interface MSEmisionVotoDtoMapper extends EmisionVotoDtoMapper {
14+
15+
@Override
16+
default EmisionVotoDto convertFrom(Voto object) {
17+
return new EmisionVotoDto();
18+
}
19+
20+
@Mapping(target = "dni", source = "object.dni")
21+
@Mapping(target = "fecha", source = "object.fecha")
22+
@Mapping(target = "grupoPolitico", source = "object.grupoPolitico", qualifiedBy = StringToGrupoPoliticoMapper.class)
23+
Voto convertTo(EmisionVotoDto object);
24+
25+
@StringToGrupoPoliticoMapper
26+
public static GrupoPolitico StringToGrupoPolitico(String object) {
27+
GrupoPolitico grupoPolitico = new GrupoPolitico();
28+
grupoPolitico.setNombre(object);
29+
return grupoPolitico;
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package edu.cibertec.votoelectronico.mapping.impl;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.Mapping;
5+
6+
import edu.cibertec.votoelectronico.domain.Voto;
7+
import edu.cibertec.votoelectronico.dto.VotoDto;
8+
import edu.cibertec.votoelectronico.mapping.VotoDtoMapper;
9+
10+
@Mapper(componentModel = "spring")
11+
public interface MSVotoDtoMapper extends VotoDtoMapper {
12+
13+
@Mapping(target = "votoId", source = "object.votoId")
14+
@Mapping(target = "dni", source = "object.dni")
15+
@Mapping(target = "fecha", source = "object.fecha")
16+
@Mapping(target = "grupoPolitico", source = "object.grupoPolitico.nombre")
17+
VotoDto convertFrom(Voto object);
18+
19+
@Override
20+
default Voto convertTo(VotoDto object) {
21+
return new Voto();
22+
}
23+
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package edu.cibertec.votoelectronico.mapping.impl;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.Mapping;
5+
6+
import edu.cibertec.votoelectronico.domain.complex.VotoResumen;
7+
import edu.cibertec.votoelectronico.dto.VotoResumenDto;
8+
import edu.cibertec.votoelectronico.mapping.VotoResumenDtoMapper;
9+
10+
@Mapper(componentModel = "spring")
11+
public interface MSVotoResumenDtoMapper extends VotoResumenDtoMapper {
12+
13+
@Mapping(target = "grupoPolitico", source = "object.grupoPolitico")
14+
@Mapping(target = "cantidad", source = "object.cantidad")
15+
VotoResumenDto convertFrom(VotoResumen object);
16+
17+
@Override
18+
default VotoResumen convertTo(VotoResumenDto object) {
19+
return new VotoResumen();
20+
}
21+
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package edu.cibertec.votoelectronico.mapping.qualifiers;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
import org.mapstruct.Qualifier;
9+
10+
@Qualifier
11+
@Target(ElementType.METHOD)
12+
@Retention(RetentionPolicy.CLASS)
13+
public @interface StringToGrupoPoliticoMapper {
14+
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3+
4+
<!-- Document : log4j.xml Created on : 16 de septiembre de 2010, 05:31 PM
5+
Author : ofernandez Description: Purpose of the document follows. -->
6+
7+
<log4j:configuration
8+
xmlns:log4j="http://jakarta.apache.org/log4j/">
9+
<appender name="CONSOLE"
10+
class="org.apache.log4j.ConsoleAppender">
11+
<param name="Target" value="System.out" />
12+
<layout class="org.apache.log4j.PatternLayout">
13+
<param name="ConversionPattern"
14+
value="%d{HH:mm:ss,SSS} [%-10t] %-5p (%c{2}:%L) %m%n" />
15+
</layout>
16+
</appender>
17+
18+
<appender name="DAYFILE"
19+
class="org.apache.log4j.DailyRollingFileAppender">
20+
<!-- param name="File" value="${jboss.server.log.dir}/EVAM04_EMISIONVOTO_CORE.log"
21+
/ -->
22+
<param name="File"
23+
value="${jboss.server.log.dir}/EVAM04_EMISIONVOTO_CORE.log" />
24+
<!-- <param name="File" value="EVAM04_EMISIONVOTO_CORE.log" /> -->
25+
<param name="DatePattern" value="'.'yyyyMMdd" />
26+
<param name="Append" value="true" />
27+
<layout class="org.apache.log4j.PatternLayout">
28+
<param name="ConversionPattern"
29+
value="%d{HH:mm:ss,SSS} [%-10t] %-5p (%c{2}:%L) %m%n" />
30+
</layout>
31+
</appender>
32+
33+
<!-- <category name="org.jboss.remoting.transport.Connector"> <priority
34+
value="ERROR"/> </category> -->
35+
<root>
36+
<priority value="TRACE" />
37+
<appender-ref ref="DAYFILE" />
38+
<appender-ref ref="CONSOLE" />
39+
</root>
40+
41+
</log4j:configuration>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package edu.cibertec.votoelectronico.main;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
import javax.transaction.SystemException;
7+
import javax.transaction.TransactionManager;
8+
9+
import static com.arjuna.ats.jta.TransactionManager.transactionManager;
10+
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.context.ApplicationContext;
14+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
15+
16+
import edu.cibertec.votoelectronico.application.MapperConfig;
17+
import edu.cibertec.votoelectronico.domain.GrupoPolitico;
18+
import edu.cibertec.votoelectronico.domain.Voto;
19+
import edu.cibertec.votoelectronico.repository.GrupoPoliticoRepository;
20+
import edu.cibertec.votoelectronico.repository.VotoRepository;
21+
import edu.cibertec.votoelectronico.repository.impl.GrupoPoliticoRepositoryImpl;
22+
import edu.cibertec.votoelectronico.repository.impl.VotoRepositoryImpl;
23+
24+
public class MapperTest {
25+
private static final Logger LOG = LoggerFactory.getLogger(MapperTest.class);
26+
27+
public static void main(String[] args) {
28+
TransactionManager transactionManager = null;
29+
try {
30+
transactionManager = transactionManager();
31+
transactionManager.begin();
32+
33+
ApplicationContext ctx = new AnnotationConfigApplicationContext(MapperConfig.class);
34+
35+
GrupoPoliticoRepository grupoPoliticoRepositorio = ctx.getBean(GrupoPoliticoRepositoryImpl.class);
36+
VotoRepository votoRepositorio = ctx.getBean(VotoRepositoryImpl.class);
37+
38+
grupoPoliticoRepositorio.getAll().forEach(p -> {
39+
LOG.info("Grupo Politico: " + p);
40+
});
41+
42+
LOG.info("Deleting all records...");
43+
votoRepositorio.deleteAll();
44+
grupoPoliticoRepositorio.deleteAll();
45+
46+
LOG.info("Creating GrupoPolitico records...");
47+
GrupoPolitico grupoPolitico1 = new GrupoPolitico();
48+
grupoPolitico1.setNombre("P1");
49+
grupoPolitico1.setDescripcion("Partido Politico Alianza");
50+
grupoPoliticoRepositorio.create(grupoPolitico1);
51+
52+
LOG.info("Partido Politico creado: " + grupoPolitico1.toString());
53+
GrupoPolitico grupoPolitico2 = new GrupoPolitico();
54+
grupoPolitico2.setNombre("P2");
55+
grupoPolitico2.setDescripcion("Partido Politico Peru Posible");
56+
grupoPoliticoRepositorio.create(grupoPolitico2);
57+
LOG.info("Partido Politico creado: " + grupoPolitico2.toString());
58+
59+
LOG.info("Creating Voto records...");
60+
Voto voto1 = new Voto();
61+
voto1.setDni("48048360");
62+
voto1.setFecha(new Date());
63+
voto1.setGrupoPolitico(grupoPolitico1);
64+
votoRepositorio.create(voto1);
65+
LOG.info("Voto creado: " + voto1.toString());
66+
Voto voto2 = new Voto();
67+
voto2.setDni("48048361");
68+
voto2.setFecha(new Date());
69+
voto2.setGrupoPolitico(grupoPolitico2);
70+
votoRepositorio.create(voto2);
71+
LOG.info("Voto creado: " + voto2.toString());
72+
73+
LOG.info("Listing GrupoPolitico records...");
74+
List<GrupoPolitico> grupoPoliticos = grupoPoliticoRepositorio.getAll();
75+
if (grupoPoliticos != null && grupoPoliticos.size() > 0)
76+
grupoPoliticos.forEach(p -> {
77+
LOG.info(p.toString());
78+
});
79+
else
80+
LOG.info("No existen grupos politicos");
81+
82+
LOG.info("Listing Voto records...");
83+
List<Voto> votos = votoRepositorio.getAll();
84+
if (votos != null && votos.size() > 0)
85+
votos.forEach(p -> {
86+
LOG.info(p.toString());
87+
});
88+
else
89+
LOG.info("No existen votos");
90+
91+
throw new Exception("rollback");
92+
//transactionManager.commit();
93+
} catch (Exception e) {
94+
try {
95+
if (transactionManager != null)
96+
transactionManager.rollback();
97+
} catch (IllegalStateException | SecurityException | SystemException e1) {
98+
e1.printStackTrace();
99+
}
100+
e.printStackTrace();
101+
}
102+
103+
}
104+
}

0 commit comments

Comments
 (0)