fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From my...@apache.org
Subject [fineract-cn-deposit-account-management] 20/23: refactored child handling for product definition
Date Mon, 22 Jan 2018 15:28:53 GMT
This is an automated email from the ASF dual-hosted git repository.

myrle pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract-cn-deposit-account-management.git

commit 7fa7746f62a3109c55d749007a1240efba25bb66
Author: mgeiss <mgeiss@mifos.org>
AuthorDate: Wed Jun 28 17:04:01 2017 +0200

    refactored child handling for product definition
---
 .../deposit/api/v1/definition/domain/Charge.java   | 17 +++++-
 .../v1/definition/domain/ProductDefinition.java    |  8 +--
 .../src/main/java/io/mifos/deposit/Fixture.java    |  2 +-
 .../java/io/mifos/deposit/TestProductInstance.java |  3 +-
 .../handler/ProductDefinitionAggregate.java        | 67 ++++++++++++++++------
 .../service/internal/mapper/ChargeMapper.java      |  7 ++-
 .../internal/mapper/ProductDefinitionMapper.java   | 19 +-----
 .../service/internal/repository/ChargeEntity.java  | 14 ++---
 .../internal/repository/ChargeRepository.java      |  4 ++
 .../internal/repository/CurrencyRepository.java    |  1 +
 .../repository/ProductDefinitionEntity.java        | 38 ------------
 .../internal/repository/TermRepository.java        |  1 +
 .../internal/service/ProductDefinitionService.java | 44 +++++++++++++-
 .../mariadb/V4__add_unique_constraint_charges.sql  | 17 ++++++
 14 files changed, 146 insertions(+), 96 deletions(-)

diff --git a/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/Charge.java b/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/Charge.java
index d408616..b10ad89 100644
--- a/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/Charge.java
+++ b/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/Charge.java
@@ -35,7 +35,7 @@ public class Charge {
     super();
   }
 
- public String getActionIdentifier() {
+  public String getActionIdentifier() {
     return this.actionIdentifier;
   }
 
@@ -82,4 +82,19 @@ public class Charge {
   public void setAmount(final Double amount) {
     this.amount = amount;
   }
+
+  @Override
+  public boolean equals(final Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+
+    final Charge charge = (Charge) o;
+
+    return name != null ? name.equals(charge.name) : charge.name == null;
+  }
+
+  @Override
+  public int hashCode() {
+    return name != null ? name.hashCode() : 0;
+  }
 }
diff --git a/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/ProductDefinition.java
b/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/ProductDefinition.java
index d3b1db7..05846b8 100644
--- a/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/ProductDefinition.java
+++ b/api/src/main/java/io/mifos/deposit/api/v1/definition/domain/ProductDefinition.java
@@ -20,7 +20,7 @@ import io.mifos.deposit.api.v1.domain.Type;
 
 import javax.validation.Valid;
 import javax.validation.constraints.NotNull;
-import java.util.List;
+import java.util.Set;
 
 public class ProductDefinition {
 
@@ -45,7 +45,7 @@ public class ProductDefinition {
   @NotNull
   private Term term;
   @Valid
-  private List<Charge> charges;
+  private Set<Charge> charges;
   private Boolean flexible;
   private Boolean active;
 
@@ -133,11 +133,11 @@ public class ProductDefinition {
     this.term = term;
   }
 
-  public List<Charge> getCharges() {
+  public Set<Charge> getCharges() {
     return this.charges;
   }
 
-  public void setCharges(final List<Charge> charges) {
+  public void setCharges(final Set<Charge> charges) {
     this.charges = charges;
   }
 
diff --git a/component-test/src/main/java/io/mifos/deposit/Fixture.java b/component-test/src/main/java/io/mifos/deposit/Fixture.java
index c3efba0..b4929a1 100644
--- a/component-test/src/main/java/io/mifos/deposit/Fixture.java
+++ b/component-test/src/main/java/io/mifos/deposit/Fixture.java
@@ -66,7 +66,7 @@ public class Fixture {
     productDefinition.setIdentifier(RandomStringUtils.randomAlphanumeric(8));
     productDefinition.setName(RandomStringUtils.randomAlphanumeric(256));
     productDefinition.setDescription(RandomStringUtils.randomAlphanumeric(2048));
-    productDefinition.setCharges(Arrays.asList(openingCharge, closingCharge));
+    productDefinition.setCharges(new HashSet<>(Arrays.asList(openingCharge, closingCharge)));
     productDefinition.setCurrency(currency);
     productDefinition.setInterest(1.25D);
     productDefinition.setEquityLedgerIdentifier("20300");
diff --git a/component-test/src/main/java/io/mifos/deposit/TestProductInstance.java b/component-test/src/main/java/io/mifos/deposit/TestProductInstance.java
index ed5d79c..5727908 100644
--- a/component-test/src/main/java/io/mifos/deposit/TestProductInstance.java
+++ b/component-test/src/main/java/io/mifos/deposit/TestProductInstance.java
@@ -226,7 +226,8 @@ public class TestProductInstance extends AbstractDepositAccountManagementTest
{
 
     final ProductInstance fetchedProductInstance = productInstances.get(0);
 
-    Mockito.doAnswer(invocation -> account).when(super.accountingServiceSpy).findAccount(productInstance.getAccountIdentifier());
+    Mockito.doAnswer(invocation -> account)
+        .when(super.accountingServiceSpy).findAccount(fetchedProductInstance.getAccountIdentifier());
 
     final ProductInstance foundProductInstance =
         super.depositAccountManager.findProductInstance(fetchedProductInstance.getAccountIdentifier());
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/command/handler/ProductDefinitionAggregate.java
b/service/src/main/java/io/mifos/deposit/service/internal/command/handler/ProductDefinitionAggregate.java
index 690641e..3e51c69 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/command/handler/ProductDefinitionAggregate.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/command/handler/ProductDefinitionAggregate.java
@@ -36,12 +36,15 @@ import io.mifos.deposit.service.internal.mapper.ProductDefinitionCommandMapper;
 import io.mifos.deposit.service.internal.mapper.ProductDefinitionMapper;
 import io.mifos.deposit.service.internal.mapper.TermMapper;
 import io.mifos.deposit.service.internal.repository.ActionRepository;
+import io.mifos.deposit.service.internal.repository.ChargeEntity;
 import io.mifos.deposit.service.internal.repository.ChargeRepository;
+import io.mifos.deposit.service.internal.repository.CurrencyEntity;
 import io.mifos.deposit.service.internal.repository.CurrencyRepository;
 import io.mifos.deposit.service.internal.repository.ProductDefinitionCommandEntity;
 import io.mifos.deposit.service.internal.repository.ProductDefinitionCommandRepository;
 import io.mifos.deposit.service.internal.repository.ProductDefinitionEntity;
 import io.mifos.deposit.service.internal.repository.ProductDefinitionRepository;
+import io.mifos.deposit.service.internal.repository.TermEntity;
 import io.mifos.deposit.service.internal.repository.TermRepository;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,6 +53,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.time.Clock;
 import java.time.LocalDateTime;
+import java.util.List;
 import java.util.Optional;
 import java.util.stream.Collectors;
 
@@ -89,14 +93,15 @@ public class ProductDefinitionAggregate {
 
     final ProductDefinition productDefinition = createProductDefinitionCommand.productDefinition();
 
-    final ProductDefinitionEntity productDefinitionEntity =
-        ProductDefinitionMapper.map(productDefinition, this.actionRepository);
+    final ProductDefinitionEntity productDefinitionEntity = ProductDefinitionMapper.map(productDefinition);
     productDefinitionEntity.setActive(Boolean.FALSE);
 
     productDefinitionEntity.setCreatedBy(UserContextHolder.checkedGetUser());
     productDefinitionEntity.setCreatedOn(LocalDateTime.now(Clock.systemUTC()));
 
-    this.productDefinitionRepository.save(productDefinitionEntity);
+    final ProductDefinitionEntity savedProductEntity = this.productDefinitionRepository.save(productDefinitionEntity);
+
+    this.saveDependingEntities(productDefinition, savedProductEntity);
 
     return createProductDefinitionCommand.productDefinition().getIdentifier();
   }
@@ -169,28 +174,20 @@ public class ProductDefinitionAggregate {
     if (optionalProductDefinition.isPresent()) {
       final ProductDefinitionEntity productDefinitionEntity = optionalProductDefinition.get();
 
-      this.currencyRepository.delete(productDefinitionEntity.getCurrency());
-      this.termRepository.delete(productDefinitionEntity.getTerm());
-      this.chargeRepository.delete(productDefinitionEntity.getCharges());
+      this.deleteDependingEntities(productDefinitionEntity);
 
       productDefinitionEntity.setName(productDefinition.getName());
       productDefinitionEntity.setDescription(productDefinition.getDescription());
       productDefinitionEntity.setInterest(productDefinition.getInterest());
-      productDefinitionEntity.setTerm(TermMapper.map(productDefinition.getTerm()));
-      productDefinitionEntity.setCharges(
-          productDefinition.getCharges()
-              .stream()
-              .map(charge -> ChargeMapper.map(charge, this.actionRepository))
-              .collect(Collectors.toList())
-      );
-      productDefinitionEntity.setCurrency(CurrencyMapper.map(productDefinition.getCurrency()));
       productDefinitionEntity.setMinimumBalance(productDefinition.getMinimumBalance());
       productDefinitionEntity.setFlexible(productDefinition.getFlexible());
       productDefinitionEntity.setEquityLedgerIdentifier(productDefinition.getEquityLedgerIdentifier());
       productDefinitionEntity.setExpenseAccountIdentifier(productDefinition.getExpenseAccountIdentifier());
-      this.productDefinitionRepository.save(productDefinitionEntity);
+      final ProductDefinitionEntity savedProductDefinition = this.productDefinitionRepository.save(productDefinitionEntity);
+
+      this.saveDependingEntities(productDefinition, savedProductDefinition);
 
-      return productDefinition.getIdentifier();
+      return productDefinitionEntity.getIdentifier();
     } else {
       throw ServiceException.notFound("Product definition {0} not found.", productDefinition.getIdentifier());
     }
@@ -211,9 +208,7 @@ public class ProductDefinitionAggregate {
           this.productDefinitionCommandRepository.findByProductDefinition(productDefinitionEntity)
       );
 
-      this.currencyRepository.delete(productDefinitionEntity.getCurrency());
-      this.termRepository.delete(productDefinitionEntity.getTerm());
-      this.chargeRepository.delete(productDefinitionEntity.getCharges());
+      this.deleteDependingEntities(productDefinitionEntity);
 
       this.productDefinitionRepository.delete(productDefinitionEntity);
       return identifier;
@@ -222,4 +217,38 @@ public class ProductDefinitionAggregate {
       return null;
     }
   }
+
+  void saveDependingEntities(final ProductDefinition productDefinition, final ProductDefinitionEntity
savedProductEntity) {
+    final CurrencyEntity currencyEntity = CurrencyMapper.map(productDefinition.getCurrency());
+    currencyEntity.setProductDefinition(savedProductEntity);
+    this.currencyRepository.save(currencyEntity);
+
+    final TermEntity termEntity = TermMapper.map(productDefinition.getTerm());
+    termEntity.setProductDefinition(savedProductEntity);
+    this.termRepository.save(termEntity);
+
+    this.chargeRepository.save(productDefinition.getCharges()
+        .stream()
+        .map(charge -> {
+          final ChargeEntity chargeEntity = ChargeMapper.map(charge, this.actionRepository);
+          chargeEntity.setProductDefinition(savedProductEntity);
+          return chargeEntity;
+        })
+        .collect(Collectors.toSet())
+    );
+  }
+
+  void deleteDependingEntities(final ProductDefinitionEntity productDefinitionEntity) {
+    final CurrencyEntity currencyEntity = this.currencyRepository.findByProductDefinition(productDefinitionEntity);
+    this.currencyRepository.delete(currencyEntity);
+    this.currencyRepository.flush();
+
+    final TermEntity termEntity = this.termRepository.findByProductDefinition(productDefinitionEntity);
+    this.termRepository.delete(termEntity);
+    this.termRepository.flush();
+
+    final List<ChargeEntity> chargeEntities = this.chargeRepository.findByProductDefinition(productDefinitionEntity);
+    this.chargeRepository.delete(chargeEntities);
+    this.chargeRepository.flush();
+  }
 }
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/mapper/ChargeMapper.java
b/service/src/main/java/io/mifos/deposit/service/internal/mapper/ChargeMapper.java
index 7aa29cb..1ebebef 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/mapper/ChargeMapper.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/mapper/ChargeMapper.java
@@ -33,7 +33,7 @@ public class ChargeMapper {
     final Optional<ActionEntity> optionalActionEntity = actionRepository.findByIdentifier(charge.getActionIdentifier());
     if (optionalActionEntity.isPresent()) {
       final ChargeEntity chargeEntity = new ChargeEntity();
-      chargeEntity.setAction(optionalActionEntity.get());
+      chargeEntity.setActionId(optionalActionEntity.get().getId());
       chargeEntity.setIncomeAccountIdentifier(charge.getIncomeAccountIdentifier());
       chargeEntity.setName(charge.getName());
       chargeEntity.setDescription(charge.getDescription());
@@ -47,9 +47,10 @@ public class ChargeMapper {
     }
   }
 
-  public static Charge map(final ChargeEntity chargeEntity) {
+  public static Charge map(final ChargeEntity chargeEntity, final ActionRepository actionRepository)
{
     final Charge charge = new Charge();
-    charge.setActionIdentifier(chargeEntity.getAction().getIdentifier());
+    final ActionEntity actionEntity = actionRepository.findOne(chargeEntity.getActionId());
+    charge.setActionIdentifier(actionEntity.getIdentifier());
     charge.setIncomeAccountIdentifier(chargeEntity.getIncomeAccountIdentifier());
     charge.setName(chargeEntity.getName());
     charge.setDescription(chargeEntity.getDescription());
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/mapper/ProductDefinitionMapper.java
b/service/src/main/java/io/mifos/deposit/service/internal/mapper/ProductDefinitionMapper.java
index 8c149ef..03165a7 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/mapper/ProductDefinitionMapper.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/mapper/ProductDefinitionMapper.java
@@ -16,34 +16,24 @@
 package io.mifos.deposit.service.internal.mapper;
 
 import io.mifos.deposit.api.v1.definition.domain.ProductDefinition;
-import io.mifos.deposit.service.internal.repository.ActionRepository;
 import io.mifos.deposit.service.internal.repository.ProductDefinitionEntity;
 
-import java.util.stream.Collectors;
-
 public class ProductDefinitionMapper {
 
   private ProductDefinitionMapper() {
     super();
   }
 
-  public static ProductDefinitionEntity map(final ProductDefinition productDefinition, final
ActionRepository actionRepository) {
+  public static ProductDefinitionEntity map(final ProductDefinition productDefinition) {
     final ProductDefinitionEntity productDefinitionEntity = new ProductDefinitionEntity();
     productDefinitionEntity.setType(productDefinition.getType());
     productDefinitionEntity.setIdentifier(productDefinition.getIdentifier());
     productDefinitionEntity.setName(productDefinition.getName());
     productDefinitionEntity.setDescription(productDefinition.getName());
-    productDefinitionEntity.setCurrency(CurrencyMapper.map(productDefinition.getCurrency()));
     productDefinitionEntity.setMinimumBalance(productDefinition.getMinimumBalance());
     productDefinitionEntity.setEquityLedgerIdentifier(productDefinition.getEquityLedgerIdentifier());
     productDefinitionEntity.setExpenseAccountIdentifier(productDefinition.getExpenseAccountIdentifier());
     productDefinitionEntity.setInterest(productDefinition.getInterest());
-    productDefinitionEntity.setTerm(TermMapper.map(productDefinition.getTerm()));
-    productDefinitionEntity.setCharges(productDefinition.getCharges()
-        .stream()
-        .map(charge -> ChargeMapper.map(charge, actionRepository))
-        .collect(Collectors.toList())
-    );
     productDefinitionEntity.setFlexible(productDefinition.getFlexible());
 
     return productDefinitionEntity;
@@ -55,17 +45,10 @@ public class ProductDefinitionMapper {
     productDefinition.setIdentifier(productDefinitionEntity.getIdentifier());
     productDefinition.setName(productDefinitionEntity.getName());
     productDefinition.setDescription(productDefinitionEntity.getName());
-    productDefinition.setCurrency(CurrencyMapper.map(productDefinitionEntity.getCurrency()));
     productDefinition.setMinimumBalance(productDefinitionEntity.getMinimumBalance());
     productDefinition.setEquityLedgerIdentifier(productDefinitionEntity.getEquityLedgerIdentifier());
     productDefinition.setExpenseAccountIdentifier(productDefinitionEntity.getExpenseAccountIdentifier());
     productDefinition.setInterest(productDefinitionEntity.getInterest());
-    productDefinition.setTerm(TermMapper.map(productDefinitionEntity.getTerm()));
-    productDefinition.setCharges(productDefinitionEntity.getCharges()
-        .stream()
-        .map(ChargeMapper::map)
-        .collect(Collectors.toList())
-    );
     productDefinition.setFlexible(productDefinitionEntity.getFlexible());
     productDefinition.setActive(productDefinitionEntity.getActive());
 
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeEntity.java
b/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeEntity.java
index eb5d304..11d850a 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeEntity.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeEntity.java
@@ -17,7 +17,6 @@ package io.mifos.deposit.service.internal.repository;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
-import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
@@ -33,9 +32,8 @@ public class ChargeEntity {
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id", nullable = false, unique = true)
   private Long id;
-  @ManyToOne(fetch = FetchType.EAGER)
-  @JoinColumn(name = "action_id", nullable = false)
-  private ActionEntity action;
+  @Column(name = "action_id", nullable = false)
+  private Long actionId;
   @Column(name = "income_account_identifier", nullable = false, length = 32)
   private String incomeAccountIdentifier;
   @ManyToOne
@@ -62,12 +60,12 @@ public class ChargeEntity {
     this.id = id;
   }
 
-  public ActionEntity getAction() {
-    return this.action;
+  public Long getActionId() {
+    return this.actionId;
   }
 
-  public void setAction(final ActionEntity action) {
-    this.action = action;
+  public void setActionId(final Long action) {
+    this.actionId = action;
   }
 
   public String getIncomeAccountIdentifier() {
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeRepository.java
b/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeRepository.java
index abd346d..211cb6b 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeRepository.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/repository/ChargeRepository.java
@@ -17,5 +17,9 @@ package io.mifos.deposit.service.internal.repository;
 
 import org.springframework.data.jpa.repository.JpaRepository;
 
+import java.util.List;
+
+
 public interface ChargeRepository extends JpaRepository<ChargeEntity, Long> {
+  List<ChargeEntity> findByProductDefinition(final ProductDefinitionEntity productDefinitionEntity);
 }
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/repository/CurrencyRepository.java
b/service/src/main/java/io/mifos/deposit/service/internal/repository/CurrencyRepository.java
index 13248ed..9c9178b 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/repository/CurrencyRepository.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/repository/CurrencyRepository.java
@@ -18,4 +18,5 @@ package io.mifos.deposit.service.internal.repository;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 public interface CurrencyRepository extends JpaRepository<CurrencyEntity, Long> {
+  CurrencyEntity findByProductDefinition(final ProductDefinitionEntity productDefinition);
 }
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/repository/ProductDefinitionEntity.java
b/service/src/main/java/io/mifos/deposit/service/internal/repository/ProductDefinitionEntity.java
index f5ec1d9..802b067 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/repository/ProductDefinitionEntity.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/repository/ProductDefinitionEntity.java
@@ -17,19 +17,14 @@ package io.mifos.deposit.service.internal.repository;
 
 import io.mifos.core.mariadb.util.LocalDateTimeConverter;
 
-import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Convert;
 import javax.persistence.Entity;
-import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
-import javax.persistence.OneToMany;
-import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import java.time.LocalDateTime;
-import java.util.List;
 
 @Entity
 @Table(name = "shed_product_definitions")
@@ -47,8 +42,6 @@ public class ProductDefinitionEntity {
   private String name;
   @Column(name = "description", nullable = true, length = 4096)
   private String description;
-  @OneToOne(mappedBy = "productDefinition", cascade = CascadeType.ALL, optional = false,
fetch = FetchType.EAGER)
-  private CurrencyEntity currency;
   @Column(name = "minimum_balance", nullable = true)
   private Double minimumBalance;
   @Column(name = "equity_ledger_identifier", nullable = false)
@@ -57,10 +50,6 @@ public class ProductDefinitionEntity {
   private String expenseAccountIdentifier;
   @Column(name = "interest", nullable = true)
   private Double interest;
-  @OneToOne(mappedBy = "productDefinition", cascade = CascadeType.ALL, optional = false,
fetch = FetchType.EAGER)
-  private TermEntity term;
-  @OneToMany(mappedBy = "productDefinition", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
-  private List<ChargeEntity> charges;
   @Column(name = "is_flexible", nullable = false)
   private Boolean flexible;
   @Column(name = "is_active", nullable = false)
@@ -120,15 +109,6 @@ public class ProductDefinitionEntity {
     this.description = description;
   }
 
-  public CurrencyEntity getCurrency() {
-    return this.currency;
-  }
-
-  public void setCurrency(final CurrencyEntity currency) {
-    this.currency = currency;
-    this.currency.setProductDefinition(this);
-  }
-
   public Double getMinimumBalance() {
     return this.minimumBalance;
   }
@@ -161,24 +141,6 @@ public class ProductDefinitionEntity {
     this.interest = interest;
   }
 
-  public TermEntity getTerm() {
-    return this.term;
-  }
-
-  public void setTerm(final TermEntity term) {
-    this.term = term;
-    this.term.setProductDefinition(this);
-  }
-
-  public List<ChargeEntity> getCharges() {
-    return this.charges;
-  }
-
-  public void setCharges(final List<ChargeEntity> charges) {
-    this.charges = charges;
-    charges.forEach(chargeEntity -> chargeEntity.setProductDefinition(this));
-  }
-
   public Boolean getFlexible() {
     return this.flexible;
   }
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/repository/TermRepository.java
b/service/src/main/java/io/mifos/deposit/service/internal/repository/TermRepository.java
index 101d997..b20a1ee 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/repository/TermRepository.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/repository/TermRepository.java
@@ -18,4 +18,5 @@ package io.mifos.deposit.service.internal.repository;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 public interface TermRepository extends JpaRepository<TermEntity, Long> {
+  TermEntity findByProductDefinition(final ProductDefinitionEntity productDefinition);
 }
diff --git a/service/src/main/java/io/mifos/deposit/service/internal/service/ProductDefinitionService.java
b/service/src/main/java/io/mifos/deposit/service/internal/service/ProductDefinitionService.java
index 24baf6a..7430850 100644
--- a/service/src/main/java/io/mifos/deposit/service/internal/service/ProductDefinitionService.java
+++ b/service/src/main/java/io/mifos/deposit/service/internal/service/ProductDefinitionService.java
@@ -18,10 +18,18 @@ 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.ChargeMapper;
+import io.mifos.deposit.service.internal.mapper.CurrencyMapper;
 import io.mifos.deposit.service.internal.mapper.ProductDefinitionCommandMapper;
 import io.mifos.deposit.service.internal.mapper.ProductDefinitionMapper;
+import io.mifos.deposit.service.internal.mapper.TermMapper;
+import io.mifos.deposit.service.internal.repository.ActionRepository;
+import io.mifos.deposit.service.internal.repository.ChargeRepository;
+import io.mifos.deposit.service.internal.repository.CurrencyRepository;
 import io.mifos.deposit.service.internal.repository.ProductDefinitionCommandRepository;
+import io.mifos.deposit.service.internal.repository.ProductDefinitionEntity;
 import io.mifos.deposit.service.internal.repository.ProductDefinitionRepository;
+import io.mifos.deposit.service.internal.repository.TermRepository;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -38,26 +46,56 @@ public class ProductDefinitionService {
   private final Logger logger;
   private final ProductDefinitionRepository productDefinitionRepository;
   private final ProductDefinitionCommandRepository productDefinitionCommandRepository;
+  private final ActionRepository actionRepository;
+  private final ChargeRepository chargeRepository;
+  private final CurrencyRepository currencyRepository;
+  private final TermRepository termRepository;
 
   @Autowired
   public ProductDefinitionService(@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger,
                                   final ProductDefinitionRepository productDefinitionRepository,
-                                  final ProductDefinitionCommandRepository productDefinitionCommandRepository)
{
+                                  final ProductDefinitionCommandRepository productDefinitionCommandRepository,
+                                  final ActionRepository actionRepository,
+                                  final ChargeRepository chargeRepository,
+                                  final CurrencyRepository currencyRepository,
+                                  final TermRepository termRepository) {
     super();
     this.logger = logger;
     this.productDefinitionRepository = productDefinitionRepository;
     this.productDefinitionCommandRepository = productDefinitionCommandRepository;
+    this.actionRepository = actionRepository;
+    this.chargeRepository = chargeRepository;
+    this.currencyRepository = currencyRepository;
+    this.termRepository = termRepository;
   }
 
   public List<ProductDefinition> fetchProductDefinitions() {
     return this.productDefinitionRepository.findAll()
         .stream()
-        .map(ProductDefinitionMapper::map)
+        .map(this::getProductDefinition)
         .collect(Collectors.toList());
   }
 
   public Optional<ProductDefinition> findProductDefinition(final String identifier)
{
-    return this.productDefinitionRepository.findByIdentifier(identifier).map(ProductDefinitionMapper::map);
+    return this.productDefinitionRepository.findByIdentifier(identifier)
+        .map(this::getProductDefinition);
+  }
+
+  private ProductDefinition getProductDefinition(final ProductDefinitionEntity productDefinitionEntity)
{
+    final ProductDefinition productDefinition = ProductDefinitionMapper.map(productDefinitionEntity);
+    productDefinition.setCurrency(
+        CurrencyMapper.map(this.currencyRepository.findByProductDefinition(productDefinitionEntity))
+    );
+    productDefinition.setTerm(
+        TermMapper.map(this.termRepository.findByProductDefinition(productDefinitionEntity))
+    );
+    productDefinition.setCharges(
+        this.chargeRepository.findByProductDefinition(productDefinitionEntity)
+            .stream()
+            .map(chargeEntity -> ChargeMapper.map(chargeEntity, this.actionRepository))
+            .collect(Collectors.toSet())
+    );
+    return productDefinition;
   }
 
   public List<ProductDefinitionCommand> findCommands(final String identifier) {
diff --git a/service/src/main/resources/db/migrations/mariadb/V4__add_unique_constraint_charges.sql
b/service/src/main/resources/db/migrations/mariadb/V4__add_unique_constraint_charges.sql
new file mode 100644
index 0000000..a043ad1
--- /dev/null
+++ b/service/src/main/resources/db/migrations/mariadb/V4__add_unique_constraint_charges.sql
@@ -0,0 +1,17 @@
+--
+-- 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.
+--
+
+ALTER TABLE shed_charges ADD CONSTRAINT shed_charges_uq UNIQUE (product_definition_id, a_name);
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
myrle@apache.org.

Mime
View raw message