/* (non-Javadoc)
   * @see #createTaxMap(org.mifosplatform.infrastructure.core.api.JsonCommand)
   */
  @Transactional
  @Override
  public CommandProcessingResult createTaxMap(final JsonCommand command) {

    TaxMap taxmap = null;
    try {
      this.context.authenticatedUser();
      this.apiJsonDeserializer.validateForCreate(command);

      String taxMapString = command.stringValueOfParameterNamed("taxCode");

      TaxMap oldTaxmap = this.taxMapRepository.findByTaxCode(taxMapString);

      if (null != oldTaxmap) {
        oldTaxmap.setEndDate(new Date());
        oldTaxmap.setIsNew(0);
        this.taxMapRepository.save(oldTaxmap);
      }

      taxmap = TaxMap.fromJson(command);
      this.taxMapRepository.save(taxmap);

      if (null == oldTaxmap) {
        return new CommandProcessingResultBuilder().withEntityId(taxmap.getId()).build();
      }

      Collection<LoanProductTaxData> productDatas =
          this.loanProductReadPlatformService.retrieveLoanProductIds(oldTaxmap.getId());

      for (LoanProductTaxData productData : productDatas) {

        LoanProduct loanProduct = this.loanProductRepository.findOne(productData.getProductId());

        Collection<TaxMap> taxMaps = loanProduct.getTaxes();

        for (TaxMap taxMapData : taxMaps) {

          if (taxMapData.getId() == oldTaxmap.getId()) {
            taxMaps.remove(taxMapData);
            break;
          }
        }

        taxMaps.add(taxmap);
        loanProduct.updateTaxes(taxMaps);

        this.loanProductRepository.save(loanProduct);
      }

    } catch (final DataIntegrityViolationException dve) {
      handleDataIntegrityIssues(command, dve);
      return new CommandProcessingResult(Long.valueOf(-1));
    }
    return new CommandProcessingResultBuilder().withEntityId(taxmap.getId()).build();
  }
  /* (non-Javadoc)
   * @see #updateTaxMap(org.mifosplatform.infrastructure.core.api.JsonCommand, java.lang.Long)
   */
  @Transactional
  @Override
  public CommandProcessingResult updateTaxMap(final JsonCommand command, final Long taxMapId) {
    TaxMap taxMap = null;
    try {
      this.context.authenticatedUser();
      this.apiJsonDeserializer.validateForCreate(command);
      taxMap = retrieveTaxMapById(taxMapId);
      final Map<String, Object> changes = taxMap.update(command);

      if (!changes.isEmpty()) {
        this.taxMapRepository.saveAndFlush(taxMap);
      }

      return new CommandProcessingResultBuilder()
          .withCommandId(command.commandId())
          .withEntityId(taxMap.getId())
          .with(changes)
          .build();
    } catch (final DataIntegrityViolationException dve) {
      if (dve.getCause() instanceof ConstraintViolationException) {
        handleDataIntegrityIssues(command, dve);
      }
      return new CommandProcessingResult(Long.valueOf(-1));
    }
  }