All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.avacodo.filebased.SimpleBankConfigReader Maven / Gradle / Ivy

The newest version!
/**
 * #%L
 * Avacodo
 * %%
 * Copyright (C) 2013 - 2014 infiniteam
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package org.avacodo.filebased;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.avacodo.model.BankConfig;
import org.avacodo.model.BankConfig.Builder;
import org.avacodo.model.BankConfigRepository;
import org.avacodo.model.UnknownBankCodeException;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.joda.time.LocalDate;

/**
 * does not support obtaining configurations for a particular date, only the initialized file!!
 * currently does not support re-initialization with an updated file
 */
@SuppressWarnings("all")
public class SimpleBankConfigReader implements BankConfigRepository {
  private Map theMap;
  
  public SimpleBankConfigReader(final URL blzFile, final Charset encoding) {
    this.init(blzFile, encoding);
  }
  
  private void init(final URL blzFile, final Charset encoding) {
    try {
      List _readLines = Resources.readLines(blzFile, encoding);
      this.init(_readLines);
      this.ensureInitialized();
      List _readLines_1 = Resources.readLines(blzFile, encoding);
      String _get = _readLines_1.get(0);
      boolean _startsWith = _get.startsWith("simplifiedBLZ");
      boolean _not = (!_startsWith);
      if (_not) {
      }
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public BankConfig getBankConfig(final int bankCode) {
    BankConfig _xblockexpression = null;
    {
      final BankConfig config = this.theMap.get(Integer.valueOf(bankCode));
      BankConfig _xifexpression = null;
      boolean _tripleEquals = (config == null);
      if (_tripleEquals) {
        String _plus = ("unknown bank code " + Integer.valueOf(bankCode));
        UnknownBankCodeException _unknownBankCodeException = new UnknownBankCodeException(_plus);
        throw _unknownBankCodeException;
      } else {
        _xifexpression = config;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  /**
   * ignores the given date
   */
  public BankConfig getBankConfig(final int bankCode, final LocalDate date) {
    BankConfig _bankConfig = this.getBankConfig(bankCode);
    return _bankConfig;
  }
  
  ImmutableSet getKnownBankCodes() {
    Set _keySet = this.theMap.keySet();
    ImmutableSet _copyOf = ImmutableSet.copyOf(_keySet);
    return _copyOf;
  }
  
  private void ensureInitialized() {
    boolean _or = false;
    boolean _tripleEquals = (this.theMap == null);
    if (_tripleEquals) {
      _or = true;
    } else {
      int _size = this.theMap.size();
      boolean _equals = (_size == 0);
      _or = (_tripleEquals || _equals);
    }
    if (_or) {
      IllegalStateException _illegalStateException = new IllegalStateException("blz file has not been initialized properly");
      throw _illegalStateException;
    }
  }
  
  private void init(final List lines) {
    this.theMap = null;
    final HashMap result = CollectionLiterals.newHashMap();
    boolean _and = false;
    int _size = lines.size();
    boolean _greaterThan = (_size > 0);
    if (!_greaterThan) {
      _and = false;
    } else {
      String _get = lines.get(0);
      boolean _startsWith = _get.startsWith("simplifiedBLZ");
      _and = (_greaterThan && _startsWith);
    }
    if (_and) {
      Iterable _tail = IterableExtensions.tail(lines);
      final Procedure1 _function = new Procedure1() {
        public void apply(final String it) {
          String _trim = it.trim();
          int _length = _trim.length();
          boolean _greaterThan = (_length > 0);
          if (_greaterThan) {
            final String[] split = it.split(";");
            String _get = split[0];
            int _parseInt = Integer.parseInt(_get);
            Builder _builder = BankConfig.builder();
            String _get_1 = split[1];
            Builder _checkMethod = _builder.checkMethod(_get_1);
            String _get_2 = split[2];
            Builder _ibanRule = _checkMethod.ibanRule(_get_2);
            String _get_3 = split[3];
            boolean _equals = Objects.equal(_get_3, "true");
            Builder _deletionAnnounced = _ibanRule.deletionAnnounced(_equals);
            String _get_4 = split[4];
            Builder _bic = _deletionAnnounced.bic(_get_4);
            String _get_5 = split[5];
            Builder _succeedingBlz = _bic.succeedingBlz(_get_5);
            BankConfig _build = _succeedingBlz.build();
            result.put(Integer.valueOf(_parseInt), _build);
          }
        }
      };
      IterableExtensions.forEach(_tail, _function);
    } else {
      final Procedure1 _function_1 = new Procedure1() {
        public void apply(final String it) {
          String _trim = it.trim();
          int _length = _trim.length();
          boolean _greaterThan = (_length > 0);
          if (_greaterThan) {
            SimpleBankConfigReader.this.addNonDeletedEntry(it, result);
          }
        }
      };
      IterableExtensions.forEach(lines, _function_1);
      Collection _values = result.values();
      final Function1 _function_2 = new Function1() {
        public Boolean apply(final BankConfig it) {
          Integer _succeedingBlz = it.getSucceedingBlz();
          boolean _tripleNotEquals = (_succeedingBlz != null);
          return Boolean.valueOf(_tripleNotEquals);
        }
      };
      Iterable _filter = IterableExtensions.filter(_values, _function_2);
      final Procedure1 _function_3 = new Procedure1() {
        public void apply(final BankConfig it) {
          Integer _succeedingBlz = it.getSucceedingBlz();
          final BankConfig successor = result.get(_succeedingBlz);
          Integer _succeedingBlz_1 = successor.getSucceedingBlz();
          boolean _tripleNotEquals = (_succeedingBlz_1 != null);
          if (_tripleNotEquals) {
            IllegalStateException _illegalStateException = new IllegalStateException("successor bank code may not have a successor itself");
            throw _illegalStateException;
          }
        }
      };
      IterableExtensions.forEach(_filter, _function_3);
    }
    this.theMap = result;
  }
  
  private BankConfig addNonDeletedEntry(final String it, final Map result) {
    BankConfig _xblockexpression = null;
    {
      final Pair newEntry = this.getEntry(it);
      BankConfig _xifexpression = null;
      boolean _tripleNotEquals = (newEntry != null);
      if (_tripleNotEquals) {
        BankConfig _xblockexpression_1 = null;
        {
          Integer _key = newEntry.getKey();
          final BankConfig oldEntry = result.get(_key);
          BankConfig _xifexpression_1 = null;
          boolean _isPrimary = this.isPrimary(it);
          boolean _not = (!_isPrimary);
          if (_not) {
            boolean _tripleEquals = (oldEntry == null);
            if (_tripleEquals) {
              Integer _key_1 = newEntry.getKey();
              String _plus = ("there cannot be two primary entries for the same bank code " + _key_1);
              IllegalStateException _illegalStateException = new IllegalStateException(_plus);
              throw _illegalStateException;
            } else {
              BankConfig _value = newEntry.getValue();
              boolean _match = this.match(oldEntry, _value);
              boolean _not_1 = (!_match);
              if (_not_1) {
                Integer _key_2 = newEntry.getKey();
                String _plus_1 = ("primary and non-primary entries do not match for bank code " + _key_2);
                IllegalStateException _illegalStateException_1 = new IllegalStateException(_plus_1);
                throw _illegalStateException_1;
              }
            }
          } else {
            BankConfig _xblockexpression_2 = null;
            {
              boolean _tripleNotEquals_1 = (oldEntry != null);
              if (_tripleNotEquals_1) {
                Integer _key_3 = newEntry.getKey();
                String _plus_2 = ("missing primary entry for " + _key_3);
                IllegalStateException _illegalStateException_2 = new IllegalStateException(_plus_2);
                throw _illegalStateException_2;
              }
              Integer _key_4 = newEntry.getKey();
              BankConfig _value_1 = newEntry.getValue();
              BankConfig _put = result.put(_key_4, _value_1);
              _xblockexpression_2 = (_put);
            }
            _xifexpression_1 = _xblockexpression_2;
          }
          _xblockexpression_1 = (_xifexpression_1);
        }
        _xifexpression = _xblockexpression_1;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  final boolean match(final BankConfig a, final BankConfig b) {
    boolean _xblockexpression = false;
    {
      boolean _and = false;
      boolean _and_1 = false;
      boolean _isDeletionAnnounced = a.isDeletionAnnounced();
      boolean _isDeletionAnnounced_1 = b.isDeletionAnnounced();
      boolean _equals = (_isDeletionAnnounced == _isDeletionAnnounced_1);
      if (!_equals) {
        _and_1 = false;
      } else {
        int _ibanRule = a.getIbanRule();
        int _ibanRule_1 = b.getIbanRule();
        boolean _equals_1 = (_ibanRule == _ibanRule_1);
        _and_1 = (_equals && _equals_1);
      }
      if (!_and_1) {
        _and = false;
      } else {
        int _ibanRuleVersion = a.getIbanRuleVersion();
        int _ibanRuleVersion_1 = b.getIbanRuleVersion();
        boolean _equals_2 = (_ibanRuleVersion == _ibanRuleVersion_1);
        _and = (_and_1 && _equals_2);
      }
      Integer _succeedingBlz = a.getSucceedingBlz();
      Integer _succeedingBlz_1 = b.getSucceedingBlz();
      /* Objects.equal(_succeedingBlz, _succeedingBlz_1); */
      String _accountCheckMethod = a.getAccountCheckMethod();
      String _accountCheckMethod_1 = b.getAccountCheckMethod();
      boolean _equals_3 = Objects.equal(_accountCheckMethod, _accountCheckMethod_1);
      _xblockexpression = (_equals_3);
    }
    return _xblockexpression;
  }
  
  /**
   * null if entry is actually deleted
   */
  final Pair getEntry(final String s) {
    Pair _xblockexpression = null;
    {
      final String deleted = s.substring(158, 159);
      Pair _xifexpression = null;
      boolean _notEquals = (!Objects.equal(deleted, "D"));
      if (_notEquals) {
        Pair _xblockexpression_1 = null;
        {
          String _substring = s.substring(0, 8);
          final int blz = Integer.parseInt(_substring);
          final String checkMethod = s.substring(150, 152);
          final String deletionAnnounced = s.substring(159, 160);
          final String bic = s.substring(139, 150);
          final String successor = s.substring(160, 168);
          final String ibanRule = s.substring(168, 174);
          char _charAt = deletionAnnounced.charAt(0);
          final boolean delAnnounced = this.charEqual(_charAt, '1');
          Builder _builder = BankConfig.builder();
          Builder _checkMethod = _builder.checkMethod(checkMethod);
          Builder _ibanRule = _checkMethod.ibanRule(ibanRule);
          Builder _deletionAnnounced = _ibanRule.deletionAnnounced(delAnnounced);
          Builder _bic = _deletionAnnounced.bic(bic);
          Builder _succeedingBlz = _bic.succeedingBlz(successor);
          final BankConfig config = _succeedingBlz.build();
          Pair _mappedTo = Pair.of(Integer.valueOf(blz), config);
          _xblockexpression_1 = (_mappedTo);
        }
        _xifexpression = _xblockexpression_1;
      } else {
        _xifexpression = null;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  final boolean isPrimary(final String s) {
    boolean _xblockexpression = false;
    {
      final char prime = s.charAt(8);
      boolean _charEqual = this.charEqual(prime, '1');
      _xblockexpression = (_charEqual);
    }
    return _xblockexpression;
  }
  
  final boolean charEqual(final char a, final char b) {
    return (a == b);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy