From 3d670e3d80670121e32f6c254f2c3f9758f19adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20=C3=91amendi=20Pineda?= Date: Thu, 11 Aug 2022 13:27:13 -0600 Subject: [PATCH 1/2] Adding backend logic for Expiry date for licenses. --- .../v1/controller/VocabularyController.java | 62 ++++++++++---- .../VocabularyToUserVocabularyDTO.java | 1 + .../controller/dto/vocabulary/AcceptDTO.java | 13 +++ .../dto/vocabulary/AddingUserLicensesDTO.java | 13 +++ .../dto/vocabulary/LicenseRequestDTO.java | 13 +++ .../dto/vocabulary/VocabularyDTO.java | 38 +++++++++ .../athena/config/WebApplicationStarter.java | 6 ++ .../athena/config/WebSecurityConfig.java | 11 +++ .../athena/model/athena/License.java | 16 +++- .../athena/AthenaUserRepository.java | 4 +- .../athena/service/LicenseService.java | 3 +- .../athena/service/VocabularyService.java | 12 ++- .../service/impl/LicenseServiceImpl.java | 9 +- .../athena/service/impl/UserService.java | 5 ++ .../service/impl/VocabularyServiceImpl.java | 84 +++++++++++++++++-- 15 files changed, 257 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java index 49181534..fe3bb8f5 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java @@ -51,11 +51,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.convert.support.GenericConversionService; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.security.access.annotation.Secured; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; @@ -71,8 +71,8 @@ import javax.validation.Valid; import java.io.IOException; import java.security.Principal; -import java.util.List; -import java.util.Objects; +import java.util.*; +import java.util.stream.Collectors; import static com.odysseusinc.athena.util.CDMVersion.getByValue; import static com.odysseusinc.athena.util.CDMVersion.notExist; @@ -93,9 +93,10 @@ public class VocabularyController { private final VocabularyService vocabularyService; private final LicenseService licenseService; private final VocabularyServiceV5 vocabularyServiceV5; + private final GenericConversionService conversionService; @Autowired - public VocabularyController(ConverterUtils converterUtils, DownloadBundleService downloadBundleService, DownloadShareService downloadShareService, LicenseService licenseService, UserService userService, VocabularyConversionService vocabularyConversionService, VocabularyService vocabularyService, VocabularyServiceV5 vocabularyServiceV5) { + public VocabularyController(ConverterUtils converterUtils, DownloadBundleService downloadBundleService, DownloadShareService downloadShareService, LicenseService licenseService, UserService userService, VocabularyConversionService vocabularyConversionService, VocabularyService vocabularyService, VocabularyServiceV5 vocabularyServiceV5, GenericConversionService conversionService) { this.converterUtils = converterUtils; this.downloadBundleService = downloadBundleService; this.downloadShareService = downloadShareService; @@ -104,6 +105,7 @@ public VocabularyController(ConverterUtils converterUtils, DownloadBundleService this.vocabularyService = vocabularyService; this.licenseService = licenseService; this.vocabularyServiceV5 = vocabularyServiceV5; + this.conversionService = conversionService; } @Operation(summary = "Get vocabularies.") @@ -139,8 +141,18 @@ public void save(@RequestParam(value = "cdmVersion", defaultValue = "5") float v public List getDownloadHistory(Principal principal) throws PermissionDeniedException { - final AthenaUser user = userService.getUser(principal); - return vocabularyService.getDownloadHistory(user); +// final AthenaUser user = userService.getUser(principal); +// return vocabularyService.getDownloadHistory(user); + return vocabularyService.checkDownloadHistory(); + } + + @Operation(summary = "Cuong test download history") + @GetMapping("/checkDownloadHistory") + public List checkDownloadHistory() + throws PermissionDeniedException { + +// final AthenaUser user = userService.getUser(principal); + return vocabularyService.checkDownloadHistory(); } @Operation(summary = "Share bundle") @@ -189,7 +201,7 @@ public LicenseExceptionDTO checkBundle(@PathVariable("id") Long bundleId) return new LicenseExceptionDTO(true); } - @Secured("ROLE_ADMIN") +// @Secured("ROLE_ADMIN") @Operation(summary = "Get users' licenses.") @GetMapping("licenses") public Page getLicenses( @@ -200,10 +212,21 @@ public Page getLicenses( final Page users = userService.getUsersWithLicenses(pageRequest, query, pendingOnly); List dtos = converterUtils.convertList(users.getContent(), UserLicensesDTO.class); + users.getContent().forEach(athenaUser -> { + athenaUser.getLicenses().forEach(license -> { + dtos.forEach(userLicensesDTO -> { + userLicensesDTO.getVocabularyDTOs().forEach(a -> { + if (a.getId() == license.getId().intValue()) a.setExpiredDate(license.getExpiredDate()); + }); + }); + }); + + }); + return new CustomPageImpl(dtos, pageRequest, users.getTotalElements()); } - @Secured("ROLE_ADMIN") +// @Secured("ROLE_ADMIN") @Operation(summary = "Suggest licenses.") @GetMapping("licenses/suggest") public List suggestLicenses(@RequestParam("userId") Long userId) { @@ -212,17 +235,17 @@ public List suggestLicenses(@RequestParam("userId") Long userId) return vocabularies; } - @Secured("ROLE_ADMIN") +// @Secured("ROLE_ADMIN") @Operation(summary = "Add user's licenses.") @PostMapping("licenses") public ResponseEntity saveLicenses(@RequestBody @Valid AddingUserLicensesDTO dto) { final AthenaUser user = userService.get(dto.getUserId()); - vocabularyService.grantLicenses(user, dto.getVocabularyV4Ids()); + vocabularyService.grantLicenses(user, dto.getVocabularyV4Ids(), dto.getExpiredDate()); return ResponseEntity.ok().build(); } - @Secured("ROLE_ADMIN") +// @Secured("ROLE_ADMIN") @Operation(summary = "Remove user's licenses.") @DeleteMapping("licenses/{id}") public ResponseEntity removeLicenses(@PathVariable("id") Long licenseId) { @@ -235,17 +258,25 @@ public ResponseEntity removeLicenses(@PathVariable("id") Long licenseId) { @PostMapping("licenses/request") public ResponseEntity requestLicense(Principal principal, @Valid @RequestBody LicenseRequestDTO dto) { - licenseService.requestLicense(principal, dto.getVocabularyId()); + Date expiredDate; + if(dto.getExpiredDate() == null){ + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 2); + expiredDate = cal.getTime(); + }else{ + expiredDate = dto.getExpiredDate(); + } + licenseService.requestLicense(principal, dto.getVocabularyId(), expiredDate); return ResponseEntity.ok().build(); } - @Secured("ROLE_ADMIN") +// @Secured("ROLE_ADMIN") @Operation(summary = "Accept user's license.") @PostMapping("licenses/accept") public ResponseEntity acceptLicense(@Valid @RequestBody AcceptDTO acceptDTO) { licenseService.checkLicense(acceptDTO.getId()); - vocabularyService.acceptLicense(acceptDTO.getId(), acceptDTO.getAccepted()); + vocabularyService.acceptLicense(acceptDTO.getId(), acceptDTO.getAccepted(), acceptDTO.getExpiredDate()); return ResponseEntity.ok().build(); } @@ -254,10 +285,11 @@ public ResponseEntity acceptLicense(@Valid @RequestBody AcceptDTO acceptDT public void acceptLicenseViaMail(@RequestParam("id") Long id, @RequestParam("accepted") Boolean accepted, @RequestParam("token") String token, + @RequestParam("expiredDate") Date expiredDate, HttpServletResponse response) throws IOException { licenseService.checkLicense(id, token); - vocabularyService.acceptLicense(id, accepted); + vocabularyService.acceptLicense(id, accepted, expiredDate); response.sendRedirect("/admin/licenses"); } diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/vocabulary/VocabularyToUserVocabularyDTO.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/vocabulary/VocabularyToUserVocabularyDTO.java index 099bd9ee..8455a49b 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/vocabulary/VocabularyToUserVocabularyDTO.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/vocabulary/VocabularyToUserVocabularyDTO.java @@ -61,6 +61,7 @@ public List convert(@NotNull List vocabularyDT License license = map.get(each.getId()); if (license != null) { res.setStatus(license.getStatus()); + res.setExpiredDate(license.getExpiredDate()); } res.setAvailable( isEmpty(each.getRequired()) || (availableIdV4s.contains(each.getId()) && each.getUrl() != null)); diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AcceptDTO.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AcceptDTO.java index 529bb033..a891e79e 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AcceptDTO.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AcceptDTO.java @@ -23,6 +23,7 @@ package com.odysseusinc.athena.api.v1.controller.dto.vocabulary; import javax.validation.constraints.NotNull; +import java.util.Date; public class AcceptDTO { @NotNull @@ -30,6 +31,8 @@ public class AcceptDTO { @NotNull private Boolean accepted; + private Date expiredDate; + public Long getId() { return id; @@ -40,6 +43,16 @@ public void setId(Long id) { this.id = id; } + public Date getExpiredDate() { + + return expiredDate; + } + + public void setExpiredDate(Date expiredDate) { + + this.expiredDate = expiredDate; + } + public Boolean getAccepted() { return accepted; diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AddingUserLicensesDTO.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AddingUserLicensesDTO.java index 277ecfad..897b3ea1 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AddingUserLicensesDTO.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/AddingUserLicensesDTO.java @@ -22,6 +22,7 @@ package com.odysseusinc.athena.api.v1.controller.dto.vocabulary; +import java.util.Date; import java.util.List; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @@ -35,6 +36,8 @@ public class AddingUserLicensesDTO { @Size(min = 1) private List vocabularyV4Ids; + private Date expiredDate; + public Long getUserId() { return userId; @@ -54,4 +57,14 @@ public void setVocabularyV4Ids(List vocabularyV4Ids) { this.vocabularyV4Ids = vocabularyV4Ids; } + + public Date getExpiredDate() { + + return expiredDate; + } + + public void setVocabularyV4Ids(Date expiredDate) { + + this.expiredDate = expiredDate; + } } diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/LicenseRequestDTO.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/LicenseRequestDTO.java index 536f9ef0..5c2b95a0 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/LicenseRequestDTO.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/LicenseRequestDTO.java @@ -23,6 +23,7 @@ package com.odysseusinc.athena.api.v1.controller.dto.vocabulary; import javax.validation.constraints.NotNull; +import java.util.Date; public class LicenseRequestDTO { @NotNull @@ -37,4 +38,16 @@ public void setVocabularyId(Integer vocabularyId) { this.vocabularyId = vocabularyId; } + + private Date expiredDate; + + public Date getExpiredDate() { + + return expiredDate; + } + + public void setExpiredDate(Date expiredDate) { + + this.expiredDate = expiredDate; + } } diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/VocabularyDTO.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/VocabularyDTO.java index e3a4b56f..a9da6653 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/VocabularyDTO.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/dto/vocabulary/VocabularyDTO.java @@ -33,6 +33,8 @@ public class VocabularyDTO { private boolean clickDefault; private boolean omopReq; private String url; + private Date expiredDate; + private String statusLicense; public VocabularyDTO() { @@ -48,6 +50,8 @@ public VocabularyDTO(VocabularyDTO other) { this.clickDefault = other.clickDefault; this.omopReq = other.omopReq; this.url = other.url; + this.expiredDate = other.expiredDate; + this.statusLicense = other.statusLicense; } public static VocabularyDTO.VocabularyDTOBuilder builder() { @@ -135,6 +139,25 @@ public void setUrl(String url) { this.url = url; } + public Date getExpiredDate() { + + return expiredDate; + } + + public void setExpiredDate(Date expiredDate) { + + this.expiredDate = expiredDate; + } + public String getStatusLicense() { + + return statusLicense; + } + + public void setStatusLicense(String statusLicense) { + + this.statusLicense = statusLicense; + } + public class VocabularyDTOBuilder { private Integer id; private String code; @@ -144,6 +167,9 @@ public class VocabularyDTOBuilder { private boolean clickDefault; private boolean omopReq; private String url; + private Date expiredDate; + private String statusLicense; + private VocabularyDTOBuilder() { @@ -207,6 +233,18 @@ public VocabularyDTOBuilder setUrl(String url) { return this; } + public VocabularyDTOBuilder setExpiredDate(Date expiredDate) { + + VocabularyDTO.this.expiredDate = expiredDate; + return this; + } + + public VocabularyDTOBuilder setStatusLicense(String statusLicense) { + + VocabularyDTO.this.statusLicense = statusLicense; + return this; + } + } } diff --git a/src/main/java/com/odysseusinc/athena/config/WebApplicationStarter.java b/src/main/java/com/odysseusinc/athena/config/WebApplicationStarter.java index 922e85d8..c18f8372 100644 --- a/src/main/java/com/odysseusinc/athena/config/WebApplicationStarter.java +++ b/src/main/java/com/odysseusinc/athena/config/WebApplicationStarter.java @@ -64,4 +64,10 @@ public Executor emailsExecutor() { return Executors.newSingleThreadExecutor(); } + + @Bean(name = "emailSenderExecutor") + public Executor checkDownloadHistory() { + + return Executors.newSingleThreadExecutor(); + } } diff --git a/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java b/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java index 3cb1b77b..5eba039d 100644 --- a/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java +++ b/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java @@ -85,6 +85,17 @@ public void configure(WebSecurity webSecurity) throws Exception { webSecurity .ignoring() .antMatchers("/api/v1/users**") + + .antMatchers("/save") + .antMatchers("/downloads") + .antMatchers("licenses/suggest") + .antMatchers("licenses") + .antMatchers("licenses/{id}") + .antMatchers("licenses/request") + .antMatchers("licenses/accept") + .antMatchers("/admin/licenses**") + .antMatchers("/admin/statistics**") + .antMatchers("/api/v1/users/remind-password**") .antMatchers("/api/v1/users/reset-password**") .antMatchers("/api/v1/users/professional-types**") diff --git a/src/main/java/com/odysseusinc/athena/model/athena/License.java b/src/main/java/com/odysseusinc/athena/model/athena/License.java index ec4096ee..a46a43f3 100644 --- a/src/main/java/com/odysseusinc/athena/model/athena/License.java +++ b/src/main/java/com/odysseusinc/athena/model/athena/License.java @@ -48,12 +48,13 @@ public License() { } - public License(AthenaUser user, VocabularyConversion vocabularyConversion, LicenseStatus status) { + public License(AthenaUser user, VocabularyConversion vocabularyConversion, LicenseStatus status, Date expirationDate) { this.user = user; this.vocabularyConversion = vocabularyConversion; this.status = status; this.token = UUID.randomUUID().toString().replace("-", ""); + this.expiredDate = expiredDate; } @Id @@ -81,6 +82,9 @@ public License(AthenaUser user, VocabularyConversion vocabularyConversion, Licen @Column(name = "request_date") private Date requestDate; + @Column(name = "expired_date") + private Date expiredDate; + public Long getId() { return id; @@ -140,4 +144,14 @@ public void setRequestDate(Date requestDate) { this.requestDate = requestDate; } + + public Date getExpiredDate() { + + return expiredDate; + } + + public void setExpiredDate(Date expiredDate) { + + this.expiredDate = expiredDate; + } } diff --git a/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java b/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java index df95307e..56acb732 100644 --- a/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java +++ b/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java @@ -34,7 +34,7 @@ public interface AthenaUserRepository extends PagingAndSortingRepository { String GET_USERS_WITH_LICENSES = " FROM users us WHERE " - + "id IN (SELECT DISTINCT user_id FROM licenses where status IN ('PENDING') OR :pendingOnly IS FALSE) " + + "id IN (SELECT DISTINCT user_id FROM licenses where status IN ('PENDING') OR :pendingOnly IS FALSE AND expired_date > CURRENT_DATE) " + "AND (lower(firstname) SIMILAR TO :suggestRequest " + "OR lower(lastname) SIMILAR TO :suggestRequest " + "OR lower(middlename) SIMILAR TO :suggestRequest) "; @@ -57,4 +57,6 @@ Page getUsersWithLicenses(@Param("suggestRequest") String suggestReq List findByEmail(String email); + AthenaUser findById(long id); + } diff --git a/src/main/java/com/odysseusinc/athena/service/LicenseService.java b/src/main/java/com/odysseusinc/athena/service/LicenseService.java index 14da69c7..cd5cbdd2 100644 --- a/src/main/java/com/odysseusinc/athena/service/LicenseService.java +++ b/src/main/java/com/odysseusinc/athena/service/LicenseService.java @@ -3,6 +3,7 @@ import com.odysseusinc.athena.api.v1.controller.dto.LicenseExceptionDTO; import java.security.Principal; +import java.util.Date; public interface LicenseService { @@ -12,5 +13,5 @@ public interface LicenseService { void checkLicense(Long id); - Long requestLicense(Principal principal, Integer vocabularyId); + Long requestLicense(Principal principal, Integer vocabularyId, Date expirationDate); } diff --git a/src/main/java/com/odysseusinc/athena/service/VocabularyService.java b/src/main/java/com/odysseusinc/athena/service/VocabularyService.java index 61d83d57..ea3fa0e0 100644 --- a/src/main/java/com/odysseusinc/athena/service/VocabularyService.java +++ b/src/main/java/com/odysseusinc/athena/service/VocabularyService.java @@ -29,9 +29,13 @@ import com.odysseusinc.athena.model.athena.Notification; import com.odysseusinc.athena.model.security.AthenaUser; import com.odysseusinc.athena.util.CDMVersion; +import org.springframework.stereotype.Service; + +import java.util.Date; import java.util.List; import java.util.Optional; +@Service public interface VocabularyService { List getAllForCurrentUser(); @@ -42,6 +46,8 @@ public interface VocabularyService { List getDownloadHistory(AthenaUser user); + List checkDownloadHistory(); + DownloadBundle getDownloadBundle(String uuid); DownloadBundle saveDownloadItems(DownloadBundle bundle, List idV4s); @@ -52,13 +58,13 @@ public interface VocabularyService { void checkBundleAndSharedUser(AthenaUser user, DownloadBundle bundle); - Iterable grantLicenses(AthenaUser user, List vocabularyV4Ids); + Iterable grantLicenses(AthenaUser user, List vocabularyV4Ids, Date expiredDate); - Long requestLicense(AthenaUser user, Integer vocabularyV4Id); + Long requestLicense(AthenaUser user, Integer vocabularyV4Id, Date expiredDate); void deleteLicense(Long licenseId); - void acceptLicense(Long id, boolean accepted); + void acceptLicense(Long id, boolean accepted, Date expiredDate); License get(AthenaUser user, Integer vocabularyId); diff --git a/src/main/java/com/odysseusinc/athena/service/impl/LicenseServiceImpl.java b/src/main/java/com/odysseusinc/athena/service/impl/LicenseServiceImpl.java index 56133206..055af9c2 100644 --- a/src/main/java/com/odysseusinc/athena/service/impl/LicenseServiceImpl.java +++ b/src/main/java/com/odysseusinc/athena/service/impl/LicenseServiceImpl.java @@ -16,6 +16,7 @@ import org.springframework.transaction.annotation.Transactional; import java.security.Principal; +import java.util.Date; import java.util.Optional; @Service @@ -61,14 +62,14 @@ public void checkLicense(Long licenseId) { } @Override - public Long requestLicense(Principal principal, Integer vocabularyId) { + public Long requestLicense(Principal principal, Integer vocabularyId, Date expiredDate) { AthenaUser user = userService.getUser(principal); License license = vocabularyService.get(user, vocabularyId); - if (license != null) { + if (license != null && license.getExpiredDate().after(new Date())) { throw new AlreadyExistException("License already exists"); } - Long licenseId = vocabularyService.requestLicense(user, vocabularyId); + Long licenseId = vocabularyService.requestLicense(user, vocabularyId, expiredDate); emailService.sendLicenseRequestToAdmins(vocabularyService.get(licenseId).get()); return licenseId; } @@ -76,7 +77,7 @@ public Long requestLicense(Principal principal, Integer vocabularyId) { private void check(Optional license) { if (license == null || !license.isPresent()) { throw new NotExistException("License does not exist or has already been declined", License.class); - } else if (LicenseStatus.APPROVED == license.get().getStatus()) { + } else if (LicenseStatus.APPROVED == license.get().getStatus() && license.get().getExpiredDate().after(new Date())) { throw new AlreadyExistException("License has already been approved"); } } diff --git a/src/main/java/com/odysseusinc/athena/service/impl/UserService.java b/src/main/java/com/odysseusinc/athena/service/impl/UserService.java index 9c446548..45aa1289 100644 --- a/src/main/java/com/odysseusinc/athena/service/impl/UserService.java +++ b/src/main/java/com/odysseusinc/athena/service/impl/UserService.java @@ -192,6 +192,11 @@ public AthenaUser getUser(String email) { return null; } + public AthenaUser getUserById(long id){ + AthenaUser athenaUser = athenaUserRepository.findById(id); + return athenaUser; + } + public AthenaUser getUser(Principal principal) throws PermissionDeniedException { if (principal == null) { diff --git a/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java b/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java index 1a20c5f6..e00fe2bd 100644 --- a/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java +++ b/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java @@ -125,7 +125,18 @@ public List getAllForCurrentUser() { List vocabularyDTOs = converterUtils.convertList( vocabularyConversionService.findByOmopReqIsNull(sort), VocabularyDTO.class); - return new VocabularyToUserVocabularyDTO(user.getLicenses()).convert(vocabularyDTOs); + VocabularyToUserVocabularyDTO vocabularyToUserVocabularyDTO = new VocabularyToUserVocabularyDTO(user.getLicenses()); + + user.getLicenses().forEach(license -> { +// vocabularyToUserVocabularyDTO.forEach( vocabularyDTO -> { +// if(vocabularyDTO.getId() == license.getVocabularyConversion().getIdV4()){ +// vocabularyDTO.setExpiredDate(license.getExpiredDate()); +// } +// }); + }); + List vocabularyDTOS = vocabularyToUserVocabularyDTO.convert(vocabularyDTOs); + + return vocabularyDTOS; } @Override @@ -209,6 +220,61 @@ public List getDownloadHistory(AthenaUser user) { return dtos; } + + @Override + public List checkDownloadHistory() { + Sort sort = Sort.by(Sort.Direction.DESC, "created"); + List shares = downloadShareRepository.findByUserEmail("admin@admin.ru"); + List history = downloadBundleRepository.findByUserId(1L, sort); + + List sharedDTOs = new ArrayList<>(); + // add shared bundles to list of available downloads + if (!shares.isEmpty()) { + for (DownloadShare share : shares) { +// DownloadBundleDTO bundleDTO = conversionService.convert(share.getBundle(), DownloadBundleDTO.class); + + DownloadBundle bundle = share.getBundle(); + DownloadBundleDTO bundleDTO = conversionService.convert(bundle, DownloadBundleDTO.class); + List vocaDTOS = new ArrayList<>(); + bundle.getVocabularies().forEach(downloadItem -> vocaDTOS.add(conversionService.convert(bundle.getVocabularies(), VocabularyDTO.class))); +// AthenaUser athenaUser = userService.getCurrentUser(); + List vocabularyDTOS = new ArrayList<>(); + for(VocabularyDTO item : vocaDTOS){ + License license = licenseRepository.findByUserIdAndVocabularyIdV4(1L, item.getId()); + item.setExpiredDate(license!=null ? license.getExpiredDate() : null); + item.setStatusLicense(license!=null ? license.getStatus().toString() : ""); +// VocabularyConversion vocabularyConversion = vocabularyConversionService.findByVocabularyV4Id(item.getId()); +// item.set + vocabularyDTOS.add(item); + } + assert bundleDTO != null; + bundleDTO.setVocabularies(vocabularyDTOS); + // remove from shares all references to shares with other users + List filteredShares = bundleDTO.getDownloadShareDTOs().stream() + .filter(s -> s.getEmail().equals("admin@admin.ru")) + .collect(toList()); + bundleDTO.setDownloadShareDTOs(filteredShares); + try { + checkBundleVocabularies(bundle.getId(), 1L); + } catch (LicenseException e) { + // if some vocabularies require licence and current user does not have it - + // clear link to zip file + bundleDTO.setLink(StringUtils.EMPTY); + } + sharedDTOs.add(bundleDTO); + } + } + + List dtos = converterUtils.convertList(history, DownloadBundleDTO.class); + dtos.addAll(sharedDTOs); + + if (!dtos.isEmpty()) { + + } + + return dtos; + } + @Override public DownloadBundle getDownloadBundle(String uuid) { @@ -256,10 +322,10 @@ public void checkBundleAndSharedUser(AthenaUser user, DownloadBundle bundle){ } @Override - public List grantLicenses(AthenaUser user, List vocabularyV4Ids) { + public List grantLicenses(AthenaUser user, List vocabularyV4Ids, Date expirationDate) { final List newLicenses = vocabularyV4Ids.stream() - .map(v4Id -> buildLicense(user, v4Id, APPROVED)) + .map(v4Id -> buildLicense(user, v4Id, APPROVED, expirationDate)) .collect(toList()); final List savedLicenses = licenseRepository.saveAll(newLicenses); @@ -268,10 +334,11 @@ public List grantLicenses(AthenaUser user, List vocabularyV4Id } @Override - public Long requestLicense(AthenaUser user, Integer vocabularyV4Id) { + public Long requestLicense(AthenaUser user, Integer vocabularyV4Id, Date expiredDate) { - final License requestedLicense = buildLicense(user, vocabularyV4Id, PENDING); + final License requestedLicense = buildLicense(user, vocabularyV4Id, PENDING, expiredDate); requestedLicense.setRequestDate(new Date()); + requestedLicense.setExpiredDate(expiredDate); final License savedLicense = licenseRepository.save(requestedLicense); conceptService.invalidateGraphCache(user.getId()); return savedLicense.getId(); @@ -286,13 +353,14 @@ public void deleteLicense(Long licenseId) { } @Override - public void acceptLicense(Long id, boolean accepted) { + public void acceptLicense(Long id, boolean accepted, Date expirationDate) { License userLicense = licenseRepository.getOne(id); String vocabularyName = userLicense.getVocabularyConversion().getName(); AthenaUser user = userLicense.getUser(); if (accepted) { userLicense.setStatus(LicenseStatus.APPROVED); + userLicense.setExpiredDate(expirationDate); licenseRepository.save(userLicense); } else { licenseRepository.deleteById(id); @@ -338,10 +406,10 @@ private DownloadBundle buildDownloadBundle(CDMVersion version, String uuid, Stri return bundle; } - private License buildLicense(AthenaUser user, Integer vocabularyV4Id, LicenseStatus status) { + private License buildLicense(AthenaUser user, Integer vocabularyV4Id, LicenseStatus status, Date expirationDate) { VocabularyConversion vocabularyConversion = vocabularyConversionService.findByVocabularyV4Id(vocabularyV4Id); - return new License(user, vocabularyConversion, status); + return new License(user, vocabularyConversion, status, expirationDate); } private void checkBundleVocabularies(List bundleVocabularyIdV4s, Long userId) { From a60d350d12452ee4e5526e77c5f62981b5435a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20=C3=91amendi=20Pineda?= Date: Mon, 15 Aug 2022 13:43:08 -0600 Subject: [PATCH 2/2] Update BE for 224 --- .../v1/controller/VocabularyController.java | 25 +++----- .../ConceptSearchDTOToSolrQuery.java | 2 +- .../athena/config/WebSecurityConfig.java | 11 ---- .../athena/AthenaUserRepository.java | 2 - .../athena/service/VocabularyService.java | 2 - .../athena/service/impl/UserService.java | 5 -- .../service/impl/VocabularyServiceImpl.java | 61 +++---------------- src/main/resources/solr/solrconfig.xml | 2 +- .../templates/mail/vocabularies_download.html | 4 +- 9 files changed, 22 insertions(+), 92 deletions(-) diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java index fe3bb8f5..6dad5dcc 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/VocabularyController.java @@ -56,6 +56,7 @@ import org.springframework.data.domain.PageRequest; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.security.access.annotation.Secured; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; @@ -141,18 +142,8 @@ public void save(@RequestParam(value = "cdmVersion", defaultValue = "5") float v public List getDownloadHistory(Principal principal) throws PermissionDeniedException { -// final AthenaUser user = userService.getUser(principal); -// return vocabularyService.getDownloadHistory(user); - return vocabularyService.checkDownloadHistory(); - } - - @Operation(summary = "Cuong test download history") - @GetMapping("/checkDownloadHistory") - public List checkDownloadHistory() - throws PermissionDeniedException { - -// final AthenaUser user = userService.getUser(principal); - return vocabularyService.checkDownloadHistory(); + final AthenaUser user = userService.getUser(principal); + return vocabularyService.getDownloadHistory(user); } @Operation(summary = "Share bundle") @@ -201,7 +192,7 @@ public LicenseExceptionDTO checkBundle(@PathVariable("id") Long bundleId) return new LicenseExceptionDTO(true); } -// @Secured("ROLE_ADMIN") + @Secured("ROLE_ADMIN") @Operation(summary = "Get users' licenses.") @GetMapping("licenses") public Page getLicenses( @@ -226,7 +217,7 @@ public Page getLicenses( return new CustomPageImpl(dtos, pageRequest, users.getTotalElements()); } -// @Secured("ROLE_ADMIN") + @Secured("ROLE_ADMIN") @Operation(summary = "Suggest licenses.") @GetMapping("licenses/suggest") public List suggestLicenses(@RequestParam("userId") Long userId) { @@ -235,7 +226,7 @@ public List suggestLicenses(@RequestParam("userId") Long userId) return vocabularies; } -// @Secured("ROLE_ADMIN") + @Secured("ROLE_ADMIN") @Operation(summary = "Add user's licenses.") @PostMapping("licenses") public ResponseEntity saveLicenses(@RequestBody @Valid AddingUserLicensesDTO dto) { @@ -245,7 +236,7 @@ public ResponseEntity saveLicenses(@RequestBody @Valid AddingUserLicensesD return ResponseEntity.ok().build(); } -// @Secured("ROLE_ADMIN") + @Secured("ROLE_ADMIN") @Operation(summary = "Remove user's licenses.") @DeleteMapping("licenses/{id}") public ResponseEntity removeLicenses(@PathVariable("id") Long licenseId) { @@ -270,7 +261,7 @@ public ResponseEntity requestLicense(Principal principal, @Valid @RequestB return ResponseEntity.ok().build(); } -// @Secured("ROLE_ADMIN") + @Secured("ROLE_ADMIN") @Operation(summary = "Accept user's license.") @PostMapping("licenses/accept") public ResponseEntity acceptLicense(@Valid @RequestBody AcceptDTO acceptDTO) { diff --git a/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/ConceptSearchDTOToSolrQuery.java b/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/ConceptSearchDTOToSolrQuery.java index 53252fe1..29534cab 100644 --- a/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/ConceptSearchDTOToSolrQuery.java +++ b/src/main/java/com/odysseusinc/athena/api/v1/controller/converter/ConceptSearchDTOToSolrQuery.java @@ -168,7 +168,7 @@ private void putIntoJsonFacet(JSONObject jsonFacet, String facetField) { jsonFacet.put(getFacetLabel(facetField), new JSONObject() .put("type", "terms") .put("field", facetField) - .put("limit", -1) + .put("limit", 100) .put("missing", true) .put("mincount", 0) .put("domain", diff --git a/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java b/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java index 5eba039d..3cb1b77b 100644 --- a/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java +++ b/src/main/java/com/odysseusinc/athena/config/WebSecurityConfig.java @@ -85,17 +85,6 @@ public void configure(WebSecurity webSecurity) throws Exception { webSecurity .ignoring() .antMatchers("/api/v1/users**") - - .antMatchers("/save") - .antMatchers("/downloads") - .antMatchers("licenses/suggest") - .antMatchers("licenses") - .antMatchers("licenses/{id}") - .antMatchers("licenses/request") - .antMatchers("licenses/accept") - .antMatchers("/admin/licenses**") - .antMatchers("/admin/statistics**") - .antMatchers("/api/v1/users/remind-password**") .antMatchers("/api/v1/users/reset-password**") .antMatchers("/api/v1/users/professional-types**") diff --git a/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java b/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java index 56acb732..5ad4d15f 100644 --- a/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java +++ b/src/main/java/com/odysseusinc/athena/repositories/athena/AthenaUserRepository.java @@ -57,6 +57,4 @@ Page getUsersWithLicenses(@Param("suggestRequest") String suggestReq List findByEmail(String email); - AthenaUser findById(long id); - } diff --git a/src/main/java/com/odysseusinc/athena/service/VocabularyService.java b/src/main/java/com/odysseusinc/athena/service/VocabularyService.java index ea3fa0e0..58c27512 100644 --- a/src/main/java/com/odysseusinc/athena/service/VocabularyService.java +++ b/src/main/java/com/odysseusinc/athena/service/VocabularyService.java @@ -46,8 +46,6 @@ public interface VocabularyService { List getDownloadHistory(AthenaUser user); - List checkDownloadHistory(); - DownloadBundle getDownloadBundle(String uuid); DownloadBundle saveDownloadItems(DownloadBundle bundle, List idV4s); diff --git a/src/main/java/com/odysseusinc/athena/service/impl/UserService.java b/src/main/java/com/odysseusinc/athena/service/impl/UserService.java index 45aa1289..9c446548 100644 --- a/src/main/java/com/odysseusinc/athena/service/impl/UserService.java +++ b/src/main/java/com/odysseusinc/athena/service/impl/UserService.java @@ -192,11 +192,6 @@ public AthenaUser getUser(String email) { return null; } - public AthenaUser getUserById(long id){ - AthenaUser athenaUser = athenaUserRepository.findById(id); - return athenaUser; - } - public AthenaUser getUser(Principal principal) throws PermissionDeniedException { if (principal == null) { diff --git a/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java b/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java index e00fe2bd..4816dca1 100644 --- a/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java +++ b/src/main/java/com/odysseusinc/athena/service/impl/VocabularyServiceImpl.java @@ -128,11 +128,13 @@ public List getAllForCurrentUser() { VocabularyToUserVocabularyDTO vocabularyToUserVocabularyDTO = new VocabularyToUserVocabularyDTO(user.getLicenses()); user.getLicenses().forEach(license -> { -// vocabularyToUserVocabularyDTO.forEach( vocabularyDTO -> { -// if(vocabularyDTO.getId() == license.getVocabularyConversion().getIdV4()){ -// vocabularyDTO.setExpiredDate(license.getExpiredDate()); -// } -// }); + vocabularyDTOs.forEach( vocabularyDTO -> { + if(license.getVocabularyConversion() != null){ + if(vocabularyDTO.getId() == license.getVocabularyConversion().getIdV4()){ + vocabularyDTO.setExpiredDate(license.getExpiredDate()); + } + } + }); }); List vocabularyDTOS = vocabularyToUserVocabularyDTO.convert(vocabularyDTOs); @@ -198,64 +200,26 @@ public List getDownloadHistory(AthenaUser user) { // add shared bundles to list of available downloads if (!shares.isEmpty()) { for(DownloadShare share: shares) { - DownloadBundleDTO bundleDTO = conversionService.convert(share.getBundle(), DownloadBundleDTO.class); - // remove from shares all references to shares with other users - List filteredShares = bundleDTO.getDownloadShareDTOs().stream() - .filter(s -> s.getEmail().equals(user.getEmail())) - .collect(toList()); - bundleDTO.setDownloadShareDTOs(filteredShares); - try { - checkBundleVocabularies(share.getBundle().getId(), user.getId()); - } catch (LicenseException e) { - // if some vocabularies require licence and current user does not have it - - // clear link to zip file - bundleDTO.setLink(StringUtils.EMPTY); - } - sharedDTOs.add(bundleDTO); - } - } - - List dtos = converterUtils.convertList(history, DownloadBundleDTO.class); - dtos.addAll(sharedDTOs); - return dtos; - } - - - @Override - public List checkDownloadHistory() { - Sort sort = Sort.by(Sort.Direction.DESC, "created"); - List shares = downloadShareRepository.findByUserEmail("admin@admin.ru"); - List history = downloadBundleRepository.findByUserId(1L, sort); - - List sharedDTOs = new ArrayList<>(); - // add shared bundles to list of available downloads - if (!shares.isEmpty()) { - for (DownloadShare share : shares) { -// DownloadBundleDTO bundleDTO = conversionService.convert(share.getBundle(), DownloadBundleDTO.class); - DownloadBundle bundle = share.getBundle(); DownloadBundleDTO bundleDTO = conversionService.convert(bundle, DownloadBundleDTO.class); List vocaDTOS = new ArrayList<>(); bundle.getVocabularies().forEach(downloadItem -> vocaDTOS.add(conversionService.convert(bundle.getVocabularies(), VocabularyDTO.class))); -// AthenaUser athenaUser = userService.getCurrentUser(); List vocabularyDTOS = new ArrayList<>(); for(VocabularyDTO item : vocaDTOS){ - License license = licenseRepository.findByUserIdAndVocabularyIdV4(1L, item.getId()); + License license = licenseRepository.findByUserIdAndVocabularyIdV4(user.getId(), item.getId()); item.setExpiredDate(license!=null ? license.getExpiredDate() : null); item.setStatusLicense(license!=null ? license.getStatus().toString() : ""); -// VocabularyConversion vocabularyConversion = vocabularyConversionService.findByVocabularyV4Id(item.getId()); -// item.set vocabularyDTOS.add(item); } assert bundleDTO != null; bundleDTO.setVocabularies(vocabularyDTOS); // remove from shares all references to shares with other users List filteredShares = bundleDTO.getDownloadShareDTOs().stream() - .filter(s -> s.getEmail().equals("admin@admin.ru")) + .filter(s -> s.getEmail().equals(user.getEmail())) .collect(toList()); bundleDTO.setDownloadShareDTOs(filteredShares); try { - checkBundleVocabularies(bundle.getId(), 1L); + checkBundleVocabularies(bundle.getId(), user.getId()); } catch (LicenseException e) { // if some vocabularies require licence and current user does not have it - // clear link to zip file @@ -267,11 +231,6 @@ public List checkDownloadHistory() { List dtos = converterUtils.convertList(history, DownloadBundleDTO.class); dtos.addAll(sharedDTOs); - - if (!dtos.isEmpty()) { - - } - return dtos; } diff --git a/src/main/resources/solr/solrconfig.xml b/src/main/resources/solr/solrconfig.xml index 7da57d7d..eeb08010 100644 --- a/src/main/resources/solr/solrconfig.xml +++ b/src/main/resources/solr/solrconfig.xml @@ -35,7 +35,7 @@ that you fully re-index after changing this setting as it can affect both how text is indexed and queried. --> - 8.11.1 + 8.8.1