Commit 33e38bd18558767cfd838ef9694e7b0bfa7f074b
1 parent
e1962fb1
Kontext, Mapper und Service (Rückweg)
Showing
6 changed files
with
92 additions
and
15 deletions
src/main/java/net/ziemers/swxercise/lg/user/dto/UserDto.java
@@ -50,6 +50,10 @@ public class UserDto { | @@ -50,6 +50,10 @@ public class UserDto { | ||
50 | return firstname; | 50 | return firstname; |
51 | } | 51 | } |
52 | 52 | ||
53 | + public void setFirstname(String firstname) { | ||
54 | + this.firstname = firstname; | ||
55 | + } | ||
56 | + | ||
53 | public UserDto withFirstname(final String firstname) { | 57 | public UserDto withFirstname(final String firstname) { |
54 | this.firstname = firstname; | 58 | this.firstname = firstname; |
55 | return this; | 59 | return this; |
@@ -59,6 +63,10 @@ public class UserDto { | @@ -59,6 +63,10 @@ public class UserDto { | ||
59 | return lastname; | 63 | return lastname; |
60 | } | 64 | } |
61 | 65 | ||
66 | + public void setLastname(String lastname) { | ||
67 | + this.lastname = lastname; | ||
68 | + } | ||
69 | + | ||
62 | public UserDto withLastname(final String lastname) { | 70 | public UserDto withLastname(final String lastname) { |
63 | this.lastname = lastname; | 71 | this.lastname = lastname; |
64 | return this; | 72 | return this; |
src/main/java/net/ziemers/swxercise/lg/user/service/EntityToUserDtoContext.java
0 → 100644
src/main/java/net/ziemers/swxercise/lg/user/service/EntityToUserDtoContextService.java
0 → 100644
1 | +package net.ziemers.swxercise.lg.user.service; | ||
2 | + | ||
3 | +import javax.ejb.Stateless; | ||
4 | + | ||
5 | +/** | ||
6 | + * Service zum Erzeugen eines Mapping-Kontexts. Das Mapping verläuft von {@link User} nach {@link UserDto}. | ||
7 | + */ | ||
8 | +@Stateless | ||
9 | +public class EntityToUserDtoContextService { | ||
10 | + | ||
11 | + /** | ||
12 | + * Erzeugt einen Mapping-Kontext für einen neuen Benutzer. | ||
13 | + * | ||
14 | + * @return den Mapping-Kontext. | ||
15 | + */ | ||
16 | + public EntityToUserDtoContext createContext() { | ||
17 | + final EntityToUserDtoContext ctx = new EntityToUserDtoContext(); | ||
18 | + return ctx; | ||
19 | + } | ||
20 | + | ||
21 | +} |
src/main/java/net/ziemers/swxercise/lg/user/service/EntityToUserDtoMapper.java
0 → 100644
1 | +package net.ziemers.swxercise.lg.user.service; | ||
2 | + | ||
3 | +import net.ziemers.swxercise.lg.user.dto.UserDto; | ||
4 | + | ||
5 | +public class EntityToUserDtoMapper { | ||
6 | + | ||
7 | + /** | ||
8 | + * Erzeugt ein DTO aus dem übergebenem Kontext. | ||
9 | + * | ||
10 | + * @param ctx der Kontext | ||
11 | + * @return das erzeugte DTO. | ||
12 | + */ | ||
13 | + public UserDto map(EntityToUserDtoContext ctx) { | ||
14 | + final UserDto dto = new UserDto(); | ||
15 | + | ||
16 | + if (ctx.user != null) { | ||
17 | + mapEntityId(ctx, dto); | ||
18 | + mapFirstname(ctx, dto); | ||
19 | + mapLastname(ctx, dto); | ||
20 | + } | ||
21 | + return dto; | ||
22 | + } | ||
23 | + | ||
24 | + private void mapEntityId(EntityToUserDtoContext ctx, UserDto dto) { | ||
25 | + dto.setEntityId(ctx.user.getId()); | ||
26 | + } | ||
27 | + | ||
28 | + private void mapFirstname(EntityToUserDtoContext ctx, UserDto dto) { | ||
29 | + dto.setFirstname(ctx.user.getFirstname()); | ||
30 | + } | ||
31 | + | ||
32 | + private void mapLastname(EntityToUserDtoContext ctx, UserDto dto) { | ||
33 | + dto.setLastname(ctx.user.getLastname()); | ||
34 | + } | ||
35 | + | ||
36 | +} |
src/main/java/net/ziemers/swxercise/lg/user/service/UserDtoToEntityContext.java
@@ -5,8 +5,8 @@ import net.ziemers.swxercise.lg.user.dto.UserDto; | @@ -5,8 +5,8 @@ import net.ziemers.swxercise.lg.user.dto.UserDto; | ||
5 | 5 | ||
6 | public class UserDtoToEntityContext { | 6 | public class UserDtoToEntityContext { |
7 | 7 | ||
8 | - public UserDto dto; | 8 | + public UserDto dto; |
9 | 9 | ||
10 | - public User user; | 10 | + public User user; |
11 | 11 | ||
12 | } | 12 | } |
src/main/java/net/ziemers/swxercise/lg/user/service/UserDtoToEntityMapper.java
@@ -5,19 +5,19 @@ import javax.ejb.Stateless; | @@ -5,19 +5,19 @@ import javax.ejb.Stateless; | ||
5 | @Stateless | 5 | @Stateless |
6 | public class UserDtoToEntityMapper { | 6 | public class UserDtoToEntityMapper { |
7 | 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()); | 8 | + /** |
9 | + * Überträgt die Eigenschaften aus dem UserDto 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 | 19 | ||
20 | - return ctx; | ||
21 | - } | 20 | + return ctx; |
21 | + } | ||
22 | 22 | ||
23 | } | 23 | } |