Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM Core
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 Netsia
|
| 6 | * ================================================================================
|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 | * you may not use this file except in compliance with the License.
|
| 9 | * You may obtain a copy of the License at
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 | * See the License for the specific language governing permissions and
|
| 17 | * limitations under the License.
|
| 18 | * ============LICENSE_END=========================================================
|
| 19 | */
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | package org.onap.osam.core;
|
| 24 |
|
| 25 | import org.onap.osam.common.exception.NotFoundException;
|
| 26 | import org.onap.osam.model.dao.BaseEntity;
|
| 27 | import org.slf4j.Logger;
|
| 28 | import org.slf4j.LoggerFactory;
|
| 29 | import org.springframework.data.repository.CrudRepository;
|
| 30 |
|
| 31 | import java.util.ArrayList;
|
| 32 | import java.util.Collections;
|
| 33 | import java.util.List;
|
| 34 | import java.util.Optional;
|
| 35 |
|
| 36 | /**
|
| 37 | * Created by cemturker on 18.09.2018.
|
| 38 | */
|
| 39 |
|
| 40 | public abstract class AbstractBaseServiceImpl {
|
| 41 |
|
| 42 | protected Logger log = LoggerFactory.getLogger(this.getClass());
|
| 43 |
|
| 44 | protected <T extends BaseEntity> T add(T t, CrudRepository<T, Long> repository) {
|
| 45 | t = repository.save(t);
|
| 46 | log.info("{} is added",t);
|
| 47 | return t;
|
| 48 | }
|
| 49 |
|
| 50 | protected <T extends BaseEntity> void remove(Long id, CrudRepository<T, Long> repository, Class classz) {
|
| 51 | repository.deleteById(id);
|
| 52 | log.info("{} is deleted for {}", id, classz.getName());
|
| 53 | }
|
| 54 |
|
| 55 | protected <T extends BaseEntity> T get(Long id, CrudRepository<T, Long> repository) {
|
| 56 | Optional<T> optional = repository.findById(id);
|
| 57 | if (!optional.isPresent()) {
|
| 58 | throw new NotFoundException("id:"+id+" is not found");
|
| 59 | }
|
| 60 | return optional.get();
|
| 61 | }
|
| 62 |
|
| 63 | protected <T extends BaseEntity> List<T> getAll(CrudRepository<T, Long> repository) {
|
| 64 | List<T> ts = new ArrayList<>();
|
| 65 | repository.findAll().iterator().forEachRemaining(ts::add);
|
| 66 | return Collections.unmodifiableList(ts);
|
| 67 | }
|
| 68 |
|
| 69 | protected <T extends BaseEntity> Long count(CrudRepository<T, Long> repository) {
|
| 70 | return repository.count();
|
| 71 | }
|
| 72 | }
|