Browse Source

Custom Mapper

main
ddangelorb 3 years ago
parent
commit
2046ca651d
  1. 11
      src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java
  2. 67
      src/main/java/org/waterproofingdata/wpdauth/dto/CustomMapper.java

11
src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java

@ -2,11 +2,11 @@ package org.waterproofingdata.wpdauth.controller;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.waterproofingdata.wpdauth.dto.CustomMapper;
import org.waterproofingdata.wpdauth.dto.UsersRequestDTO; import org.waterproofingdata.wpdauth.dto.UsersRequestDTO;
import org.waterproofingdata.wpdauth.dto.UsersResponseDTO; import org.waterproofingdata.wpdauth.dto.UsersResponseDTO;
import org.waterproofingdata.wpdauth.model.Users; import org.waterproofingdata.wpdauth.model.Users;
import org.waterproofingdata.wpdauth.service.UsersService; import org.waterproofingdata.wpdauth.service.UsersService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -31,9 +31,6 @@ public class UsersController {
@Autowired @Autowired
private UsersService userService; private UsersService userService;
@Autowired
private ModelMapper modelMapper;
@PostMapping("/login") @PostMapping("/login")
@ApiOperation(value = "${UserController.login}") @ApiOperation(value = "${UserController.login}")
@ApiResponses(value = {// @ApiResponses(value = {//
@ -52,7 +49,7 @@ public class UsersController {
@ApiResponse(code = 403, message = "Access denied"), // @ApiResponse(code = 403, message = "Access denied"), //
@ApiResponse(code = 422, message = "Username is already in use")}) @ApiResponse(code = 422, message = "Username is already in use")})
public String signup(@ApiParam("Signup User") @RequestBody UsersRequestDTO user) { public String signup(@ApiParam("Signup User") @RequestBody UsersRequestDTO user) {
return userService.signup(modelMapper.map(user, Users.class));
return userService.signup(CustomMapper.map(user, Users.class));
} }
@PostMapping("/activate") @PostMapping("/activate")
@ -77,7 +74,7 @@ public class UsersController {
@ApiResponse(code = 404, message = "The user doesn't exist"), // @ApiResponse(code = 404, message = "The user doesn't exist"), //
@ApiResponse(code = 500, message = "Expired or invalid JWT token")}) @ApiResponse(code = 500, message = "Expired or invalid JWT token")})
public UsersResponseDTO search(@ApiParam("Username") @PathVariable String username) { public UsersResponseDTO search(@ApiParam("Username") @PathVariable String username) {
UsersResponseDTO urDTO = modelMapper.map(userService.search(username), UsersResponseDTO.class);
UsersResponseDTO urDTO = CustomMapper.map(userService.search(username), UsersResponseDTO.class);
urDTO.setEduCemadenOrganization(userService.findEduCemadenOrganizationById(urDTO.getId())); urDTO.setEduCemadenOrganization(userService.findEduCemadenOrganizationById(urDTO.getId()));
urDTO.setRolesProviderActivationKeys(userService.findRolesproviderActivationKeysById(urDTO.getId())); urDTO.setRolesProviderActivationKeys(userService.findRolesproviderActivationKeysById(urDTO.getId()));
return urDTO; return urDTO;
@ -91,7 +88,7 @@ public class UsersController {
@ApiResponse(code = 403, message = "Access denied"), // @ApiResponse(code = 403, message = "Access denied"), //
@ApiResponse(code = 500, message = "Expired or invalid JWT token")}) @ApiResponse(code = 500, message = "Expired or invalid JWT token")})
public UsersResponseDTO whoami(HttpServletRequest req) { public UsersResponseDTO whoami(HttpServletRequest req) {
UsersResponseDTO urDTO = modelMapper.map(userService.whoami(req), UsersResponseDTO.class);
UsersResponseDTO urDTO = CustomMapper.map(userService.whoami(req), UsersResponseDTO.class);
urDTO.setEduCemadenOrganization(userService.findEduCemadenOrganizationById(urDTO.getId())); urDTO.setEduCemadenOrganization(userService.findEduCemadenOrganizationById(urDTO.getId()));
urDTO.setRolesProviderActivationKeys(userService.findRolesproviderActivationKeysById(urDTO.getId())); urDTO.setRolesProviderActivationKeys(userService.findRolesproviderActivationKeysById(urDTO.getId()));
return urDTO; return urDTO;

67
src/main/java/org/waterproofingdata/wpdauth/dto/CustomMapper.java

@ -0,0 +1,67 @@
package org.waterproofingdata.wpdauth.dto;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
public class CustomMapper {
private static ModelMapper modelMapper = new ModelMapper();
/**
* Model mapper property setting are specified in the following block.
* Default property matching strategy is set to Strict see {@link MatchingStrategies}
* Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
*/
static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
/**
* Hide from public usage.
*/
private CustomMapper() {
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param <D> type of result object.
* @param <T> type of source object to map from.
* @param entity entity that needs to be mapped.
* @param outClass class of result object.
* @return new object of <code>outClass</code> type.
*/
public static <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param entityList list of entities that needs to be mapped
* @param outCLass class of result list element
* @param <D> type of objects in result list
* @param <T> type of entity in <code>entityList</code>
* @return list of mapped object with <code><D></code> type.
*/
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream()
.map(entity -> map(entity, outCLass))
.collect(Collectors.toList());
}
/**
* Maps {@code source} to {@code destination}.
*
* @param source object to map from
* @param destination object to map to
*/
public static <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
}
Loading…
Cancel
Save