MaterielServiceImpl.java

  1. /*
  2.  * Copyright 2023 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gringlobal.service.impl;

  17. import org.gringlobal.custom.elasticsearch.SearchException;
  18. import org.gringlobal.model.Materiel;
  19. import org.gringlobal.persistence.MaterielRepository;
  20. import org.gringlobal.service.MaterielService;
  21. import org.gringlobal.service.filter.MaterielFilter;
  22. import org.springframework.data.domain.Page;
  23. import org.springframework.data.domain.Pageable;
  24. import org.springframework.security.access.prepost.PreAuthorize;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.validation.annotation.Validated;

  28. @Service
  29. @Transactional(readOnly = true)
  30. @Validated
  31. public class MaterielServiceImpl extends FilteredCRUDServiceImpl<Materiel, MaterielFilter, MaterielRepository> implements MaterielService {
  32.    
  33.     @Override
  34.     @Transactional
  35.     @PreAuthorize("@ggceSec.actionAllowed('Materiel', 'CREATE', #source.site) || hasRole('ADMINISTRATOR')")
  36.     public Materiel create(Materiel source) {
  37.         Materiel saved = new Materiel();
  38.         saved.apply(source);
  39.         return repository.save(saved);
  40.     }

  41.     @Override
  42.     @Transactional
  43.     @PreAuthorize("@ggceSec.actionAllowed('Materiel', 'WRITE', #target.site) || hasRole('ADMINISTRATOR')")
  44.     public Materiel update(Materiel input, Materiel target) {
  45.         target.apply(input);
  46.         Materiel saved = repository.save(target);
  47.         return _lazyLoad(saved);
  48.     }

  49.     @Override
  50.     @PreAuthorize("@ggceSec.actionAllowed('Materiel', 'READ') || hasRole('ADMINISTRATOR')")
  51.     public Page<Materiel> list(MaterielFilter filter, Pageable page) throws SearchException {
  52.         var list = super.list(filter, page);
  53.         list.getContent().forEach(Materiel::lazyLoad);
  54.         return list;
  55.     }

  56.     @Override
  57.     @PreAuthorize("@ggceSec.actionAllowed('Materiel', 'READ') || hasRole('ADMINISTRATOR')")
  58.     public Page<Materiel> list(Pageable page) {
  59.         return super.list(page);
  60.     }

  61.     @Override
  62.     @Transactional
  63.     @PreAuthorize("@ggceSec.actionAllowed('Materiel', 'DELETE', #entity.site) || hasRole('ADMINISTRATOR')")
  64.     public Materiel remove(Materiel entity) {
  65.         return super.remove(entity);
  66.     }
  67. }