Commit e1962fb1489751c899f15e45bb7a936baf252fcf

Authored by Thomas Ziemer
1 parent bcfa4333

Kontext, Mapper und Service

src/main/java/net/ziemers/swxercise/lg/user/dto/UserDto.java
@@ -8,6 +8,8 @@ import javax.validation.constraints.NotNull; @@ -8,6 +8,8 @@ import javax.validation.constraints.NotNull;
8 */ 8 */
9 public class UserDto { 9 public class UserDto {
10 10
  11 + private Long entityId;
  12 +
11 @NotNull 13 @NotNull
12 private String username; 14 private String username;
13 15
@@ -20,6 +22,14 @@ public class UserDto { @@ -20,6 +22,14 @@ public class UserDto {
20 22
21 private String mailaddress; 23 private String mailaddress;
22 24
  25 + public Long getEntityId() {
  26 + return entityId;
  27 + }
  28 +
  29 + public void setEntityId(Long entityId) {
  30 + this.entityId = entityId;
  31 + }
  32 +
23 public String getUsername() { 33 public String getUsername() {
24 return username; 34 return username;
25 } 35 }
src/main/java/net/ziemers/swxercise/lg/user/service/UserDtoToEntityContext.java 0 → 100644
  1 +package net.ziemers.swxercise.lg.user.service;
  2 +
  3 +import net.ziemers.swxercise.lg.model.user.User;
  4 +import net.ziemers.swxercise.lg.user.dto.UserDto;
  5 +
  6 +public class UserDtoToEntityContext {
  7 +
  8 + public UserDto dto;
  9 +
  10 + public User user;
  11 +
  12 +}
src/main/java/net/ziemers/swxercise/lg/user/service/UserDtoToEntityContextService.java 0 → 100644
  1 +package net.ziemers.swxercise.lg.user.service;
  2 +
  3 +import javax.ejb.Stateless;
  4 +import javax.inject.Inject;
  5 +
  6 +import net.ziemers.swxercise.db.dao.GenericDao;
  7 +import net.ziemers.swxercise.lg.model.user.User;
  8 +import net.ziemers.swxercise.lg.user.dto.UserDto;
  9 +
  10 +@Stateless
  11 +public class UserDtoToEntityContextService {
  12 +
  13 + @Inject
  14 + private GenericDao dao;
  15 +
  16 + /**
  17 + * Erzeugt den Aktualisierungskontext zur übergebenen Zielentität.
  18 + *
  19 + * @param dto das Benutzer-DTO
  20 + * @return den erzeugten Kontext.
  21 + */
  22 + public UserDtoToEntityContext createContext(final UserDto dto) {
  23 + final UserDtoToEntityContext ctx = new UserDtoToEntityContext();
  24 +
  25 + ctx.dto = dto;
  26 + ctx.user = dto.getEntityId() == null ? new User() : dao.findById(User.class, dto.getEntityId());
  27 +
  28 + return ctx;
  29 + }
  30 +
  31 +}
src/main/java/net/ziemers/swxercise/lg/user/service/UserDtoToEntityMapper.java 0 → 100644
  1 +package net.ziemers.swxercise.lg.user.service;
  2 +
  3 +import javax.ejb.Stateless;
  4 +
  5 +@Stateless
  6 +public class UserDtoToEntityMapper {
  7 +
  8 + /**
  9 + * Überträgt die Eigenschaften aus dem DTO in die Zielentität.
  10 + *
  11 + * @param ctx der Kontext mit den Eigenschaften und der Zielentität
  12 + * @return den Kontext.
  13 + */
  14 + public UserDtoToEntityContext map(UserDtoToEntityContext ctx) {
  15 + ctx.user.setFirstname(ctx.dto.getFirstname());
  16 + ctx.user.setLastname(ctx.dto.getLastname());
  17 + //ctx.user.setProfile(ctx.dto.getProfile());
  18 + //ctx.user.setAddress(ctx.fto.getAddress());
  19 +
  20 + return ctx;
  21 + }
  22 +
  23 +}