blob: 24baf6a42f4d6dbb6c46a9e50709fa04d68a600a [file] [log] [blame]
/*
* Copyright 2017 The Mifos Initiative.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mifos.deposit.service.internal.service;
import io.mifos.deposit.api.v1.definition.domain.ProductDefinition;
import io.mifos.deposit.api.v1.definition.domain.ProductDefinitionCommand;
import io.mifos.deposit.service.ServiceConstants;
import io.mifos.deposit.service.internal.mapper.ProductDefinitionCommandMapper;
import io.mifos.deposit.service.internal.mapper.ProductDefinitionMapper;
import io.mifos.deposit.service.internal.repository.ProductDefinitionCommandRepository;
import io.mifos.deposit.service.internal.repository.ProductDefinitionRepository;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
public class ProductDefinitionService {
private final Logger logger;
private final ProductDefinitionRepository productDefinitionRepository;
private final ProductDefinitionCommandRepository productDefinitionCommandRepository;
@Autowired
public ProductDefinitionService(@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger,
final ProductDefinitionRepository productDefinitionRepository,
final ProductDefinitionCommandRepository productDefinitionCommandRepository) {
super();
this.logger = logger;
this.productDefinitionRepository = productDefinitionRepository;
this.productDefinitionCommandRepository = productDefinitionCommandRepository;
}
public List<ProductDefinition> fetchProductDefinitions() {
return this.productDefinitionRepository.findAll()
.stream()
.map(ProductDefinitionMapper::map)
.collect(Collectors.toList());
}
public Optional<ProductDefinition> findProductDefinition(final String identifier) {
return this.productDefinitionRepository.findByIdentifier(identifier).map(ProductDefinitionMapper::map);
}
public List<ProductDefinitionCommand> findCommands(final String identifier) {
return this.productDefinitionRepository.findByIdentifier(identifier)
.map(productDefinitionEntity -> this.productDefinitionCommandRepository.findByProductDefinition(productDefinitionEntity)
.stream()
.map(ProductDefinitionCommandMapper::map)
.collect(Collectors.toList()))
.orElseGet(Collections::emptyList);
}
}