Skip to content
Snippets Groups Projects
Commit 1d1e37e3 authored by Falucho's avatar Falucho
Browse files

Mapper Funcionando

parent d2485c1b
No related branches found
No related tags found
No related merge requests found
...@@ -50,7 +50,6 @@ ...@@ -50,7 +50,6 @@
<artifactId>mapstruct</artifactId> <artifactId>mapstruct</artifactId>
</dependency> </dependency>
<!-- Test scope dependencies --> <!-- Test scope dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
...@@ -99,10 +98,18 @@ ...@@ -99,10 +98,18 @@
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>${version.surefire.plugin}</version> <version>${version.compiler.plugin}</version>
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<configuration> <configuration>
<skip>true</skip> <annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
......
package uy.edu.fing.tse.practico.db; package uy.edu.fing.tse.practico.db;
import uy.edu.fing.tse.dto.NoticiaDTO;
import uy.edu.fing.tse.dto.PublicacionDTO;
import java.util.List;
public interface DataAccess { public interface DataAccess {
NoticiaDTO alta(final NoticiaDTO noticia);
PublicacionDTO alta(final PublicacionDTO publicacion);
NoticiaDTO find(long idNoticia);
List<NoticiaDTO> getNoticias();
List<PublicacionDTO> getPublicaciones();
public NoticiaDTO addPublicacionToNoticia(final PublicacionDTO pub, final long idNoticia);
} }
package uy.edu.fing.tse.practico.db; package uy.edu.fing.tse.practico.db;
import uy.edu.fing.tse.dto.NoticiaDTO;
import uy.edu.fing.tse.dto.PublicacionDTO;
import uy.edu.fing.tse.practico.db.entity.Noticia;
import uy.edu.fing.tse.practico.db.entity.Publicacion;
import uy.edu.fing.tse.practico.db.mapper.MyMapper;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.ejb.Local; import javax.ejb.Local;
import javax.ejb.LocalBean; import javax.ejb.LocalBean;
...@@ -13,9 +7,6 @@ import javax.ejb.Remote; ...@@ -13,9 +7,6 @@ import javax.ejb.Remote;
import javax.ejb.Singleton; import javax.ejb.Singleton;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;
import java.util.stream.Collectors;
@Singleton @Singleton
@Local({DataAccessLocal.class}) @Local({DataAccessLocal.class})
...@@ -23,7 +14,7 @@ import java.util.stream.Collectors; ...@@ -23,7 +14,7 @@ import java.util.stream.Collectors;
@LocalBean @LocalBean
public class DataAccessBean implements DataAccessLocal, DataAccessRemote { public class DataAccessBean implements DataAccessLocal, DataAccessRemote {
@PersistenceContext(unitName = "practico") @PersistenceContext(unitName = "central")
private EntityManager em; private EntityManager em;
@PostConstruct @PostConstruct
...@@ -31,56 +22,4 @@ public class DataAccessBean implements DataAccessLocal, DataAccessRemote { ...@@ -31,56 +22,4 @@ public class DataAccessBean implements DataAccessLocal, DataAccessRemote {
//Despues del constructor hace esto //Despues del constructor hace esto
} }
@Override
public NoticiaDTO alta(NoticiaDTO noticia) {
final Noticia newN = em.merge(MyMapper.mapper(noticia));
em.flush();
return MyMapper.mapper(newN);
}
@Override
public PublicacionDTO alta(PublicacionDTO publicacion) {
final Publicacion newP = em.merge(MyMapper.mapper(publicacion));
em.flush();
return MyMapper.mapper(newP);
}
@Override
public NoticiaDTO find(long idNoticia) {
final TypedQuery<Noticia> query = em.createNamedQuery("Noticia.findById", Noticia.class);
query.setParameter("id", idNoticia);
final Noticia result = query.getSingleResult();
return MyMapper.mapper(result);
}
@Override
public List<NoticiaDTO> getNoticias() {
final TypedQuery<Noticia> query = em.createNamedQuery("Noticia.findAll", Noticia.class);
return query.getResultList().stream().map(MyMapper::mapper).collect(Collectors.toList());
}
@Override
public List<PublicacionDTO> getPublicaciones() {
final TypedQuery<Publicacion> query = em.createNamedQuery("Publicacion.findAll", Publicacion.class);
return query.getResultList().stream().map(MyMapper::mapper).collect(Collectors.toList());
}
@Override
public NoticiaDTO addPublicacionToNoticia(final PublicacionDTO pub, final long idNoticia) {
Noticia noticiaManaged = em.find(Noticia.class, idNoticia);
if (noticiaManaged == null) {
throw new RuntimeException("No existe Noticia");
}
Publicacion publicacion = MyMapper.mapper(pub);
publicacion = em.merge(publicacion);
em.flush();
noticiaManaged.addPublicacion(publicacion);
noticiaManaged = em.merge(noticiaManaged);
em.flush();
return MyMapper.mapper(noticiaManaged);
}
} }
...@@ -4,7 +4,7 @@ import javax.persistence.*; ...@@ -4,7 +4,7 @@ import javax.persistence.*;
@Entity @Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Usuario { public abstract class Usuario {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
......
...@@ -2,13 +2,70 @@ package uy.edu.fing.tse.practico.db.mapper; ...@@ -2,13 +2,70 @@ package uy.edu.fing.tse.practico.db.mapper;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import uy.edu.fing.tse.practico.db.entity.Hecho; import uy.edu.fing.tse.dto.*;
import uy.edu.fing.tse.practico.db.entity.*;
import java.util.List;
@Mapper @Mapper
public interface MyMapper { public interface MyMapper {
MyMapper INSTANCE = Mappers.getMapper(MyMapper.class); MyMapper INSTANCE = Mappers.getMapper(MyMapper.class);
Hecho convert() default Usuario convert(User u) {
if (u == null) {
return null;
} else if (u instanceof UserBO) {
return convert((UserBO) u);
} else {
return convert((UserFO) u);
}
}
default User convert(Usuario u) {
if (u == null) {
return null;
} else if (u instanceof UsuarioBO) {
return convert((UsuarioBO) u);
} else {
return convert((UsuarioFO) u);
}
}
Hecho convert(Fact f);
Fact convert(Hecho f);
UsuarioBO convert(UserBO u);
UsuarioFO convert(UserFO u);
UserBO convert(UsuarioBO u);
UserFO convert(UsuarioFO u);
Estado convert(State state);
Calificacion convert(Score score);
Categoria convert(Category category);
TransicionEstado convert(StateHistory stateHistory);
List<TransicionEstado> convertList1(List<StateHistory> list);
State convert(Estado estado);
Score convert(Calificacion calificacion);
Category convert(Categoria categoria);
StateHistory convert(TransicionEstado transicionEstado);
List<StateHistory> convertList2(List<TransicionEstado> list);
Rol convert(Role role);
Role convert(Rol rol);
} }
package uy.edu.fing.tse.practico.db.startup; package uy.edu.fing.tse.practico.db.startup;
import uy.edu.fing.tse.dto.NoticiaDTO;
import uy.edu.fing.tse.practico.db.DataAccessBean; import uy.edu.fing.tse.practico.db.DataAccessBean;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
...@@ -18,9 +17,7 @@ public class Config { ...@@ -18,9 +17,7 @@ public class Config {
@PostConstruct @PostConstruct
public void start() { public void start() {
dataAccess.alta(new NoticiaDTO(null, "Primera Noticia", "Esta descripcion es de la noticia"));
dataAccess.alta(new NoticiaDTO(null, "Segunda Noticia", "Se esta escribiendo aun"));
dataAccess.alta(new NoticiaDTO(null, "Extra Extra Noticia", "El coyote se comio al correcaminos, al parecer cambio de proveedor de productos"));
} }
@PreDestroy @PreDestroy
......
package uy.edu.fing.tse.practico.db.mapper;
import org.junit.Assert;
import org.junit.Test;
import uy.edu.fing.tse.dto.*;
public class MyMapperTest {
@Test
public void convert() {
final var state1 = new State();
state1.setValue("Nuevo");
final var state2 = new State();
state2.setValue("Viejo");
final var category = new Category();
category.setDescription("La categoria de las frutaas");
category.setName("Frutero");
final var score = new Score();
score.setValue("Totalmente Falsa");
final var role = new Role();
role.setName("Checker");
final var userFO = new UserFO();
userFO.setMail("faller222@gmail.com");
final var userBO = new UserBO();
userBO.setMail("admin@gmail.com");
userBO.setRole(role);
final var sh1 = new StateHistory();
sh1.setState(state1);
sh1.setUser(userFO);
final var sh2 = new StateHistory();
sh2.setState(state2);
sh2.setUser(userBO);
final var fact = new Fact();
fact.setTitle("soy un titulo");
fact.setDescription("y esta descrip");
fact.setActualState(state2);
fact.setCategory(category);
fact.setScore(score);
fact.addHistory(sh1);
fact.addHistory(sh2);
final var convert = MyMapper.INSTANCE.convert(fact);
Assert.assertNotNull(convert);
final var convert2 = MyMapper.INSTANCE.convert(convert);
Assert.assertNotNull(convert2);
System.out.println("#######################################################\n" +
"#######################################################\n" +
"#######################################################\n");
}
}
\ No newline at end of file
package uy.edu.fing.tse.dto; package uy.edu.fing.tse.dto;
public class User { public abstract class User {
private Long id; private Long id;
private String mail; private String mail;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment