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 @@
<artifactId>mapstruct</artifactId>
</dependency>
<!-- Test scope dependencies -->
<dependency>
<groupId>junit</groupId>
......@@ -99,10 +98,18 @@
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire.plugin}</version>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.compiler.plugin}</version>
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<configuration>
<skip>true</skip>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
......
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 {
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;
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.ejb.Local;
import javax.ejb.LocalBean;
......@@ -13,9 +7,6 @@ import javax.ejb.Remote;
import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;
import java.util.stream.Collectors;
@Singleton
@Local({DataAccessLocal.class})
......@@ -23,7 +14,7 @@ import java.util.stream.Collectors;
@LocalBean
public class DataAccessBean implements DataAccessLocal, DataAccessRemote {
@PersistenceContext(unitName = "practico")
@PersistenceContext(unitName = "central")
private EntityManager em;
@PostConstruct
......@@ -31,56 +22,4 @@ public class DataAccessBean implements DataAccessLocal, DataAccessRemote {
//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.*;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Usuario {
public abstract class Usuario {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
......
......@@ -2,13 +2,70 @@ package uy.edu.fing.tse.practico.db.mapper;
import org.mapstruct.Mapper;
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
public interface MyMapper {
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;
import uy.edu.fing.tse.dto.NoticiaDTO;
import uy.edu.fing.tse.practico.db.DataAccessBean;
import javax.annotation.PostConstruct;
......@@ -18,9 +17,7 @@ public class Config {
@PostConstruct
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
......
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;
public class User {
public abstract class User {
private Long id;
private String mail;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment