/* (non-Javadoc)
   * @see com.kp.marsh.ebt.server.importer.dao.impl.ImportController#listOffices()
   */
  @Override
  public List<MarshOffice> listOffices() {
    List<MarshOffice> result = new ArrayList<MarshOffice>();
    Session session = (Session) this.connection.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    try {
      Query query = session.createQuery("from MOffCeAccounts");

      List<MOffCeAccounts> collections = query.list();

      Map<String, MarshOffice> officesMap = new HashMap<String, MarshOffice>();

      for (MOffCeAccounts mOffCeAccounts : collections) {

        // check if the office already exists
        MarshOffice office = officesMap.get(mOffCeAccounts.getCodiceUfficio());
        if (null == office || StringUtils.isEmpty(mOffCeAccounts.getCodiceUfficio())) {
          office = new MarshOffice();
          officesMap.put(mOffCeAccounts.getCodiceUfficio(), office);
          office.setEysCode(mOffCeAccounts.getCodiceUfficio());
          office.setOfficeName(mOffCeAccounts.getNomeUfficio());
          result.add(office);
          if (StringUtils.isEmpty(office.getEysCode()))
            log.error(office.getOfficeName() + " eys code: " + office.getEysCode());
        }

        if (office != null) {
          MarshCE mCE = office.getMarshCEMap().get(mOffCeAccounts.getCodiceEurosys());
          if (null == mCE && StringUtils.isNotEmpty(mOffCeAccounts.getAdUsername())) {
            mCE = new MarshCE();
            office.getMarshCEMap().put(mOffCeAccounts.getCodiceEurosys(), mCE);
            mCE.setEysCode(mOffCeAccounts.getCodiceEurosys());
            mCE.setNominativo(mOffCeAccounts.getNomeIdentificativo());
            mCE.setUsername(mOffCeAccounts.getAdUsername());
            if (StringUtils.isEmpty(mCE.getEysCode()))
              log.debug("\t" + mCE.getNominativo() + " eys code: " + mCE.getEysCode());
          }
        }
      }

      tx.commit();
    } catch (HibernateException ex) {
      log.error("Exception in listOffices", ex);
      ex.printStackTrace();
      tx.rollback();
    } finally {
      session.close();
    }

    return result;
  }
  /* (non-Javadoc)
   * @see com.kp.marsh.ebt.server.importer.dao.impl.ImportController#listClientExecutivesWithFinalBalance()
   */
  @Override
  public List<MarshCE> listClientExecutivesWithFinalBalance() {
    List<MarshCE> result = new ArrayList<MarshCE>();
    Session session = (Session) this.connection.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    try {
      Query query =
          session.createQuery(
              "from MActuals"); // where id.ebtProdotto <> '' and id.ebtProdotto <> 'Out of scope'
      List<MActuals> collections = query.list();
      Map<String, MarshCE> ceMap =
          new HashMap<String, MarshCE>(); // mappa dei CE con chiave il proprio codice eys
      for (MActuals mActual :
          collections) { // Per tutti i record della tabella derivata dal file PRODUZIONE che marsh
                         // ci invia
        // check if the CE already exists
        MarshCE clientExec = ceMap.get(mActual.getIdAeCgruppo());
        if (null == clientExec) {
          clientExec = new MarshCE();
          ceMap.put(mActual.getIdAeCgruppo(), clientExec);
          clientExec.setEysCode(mActual.getIdAeCgruppo());
          clientExec.setNominativo(mActual.getAeCgruppo());
          // clientExec.setUsername(username) qui non ce l'ho e comunque in information owners non
          // serve

          result.add(clientExec);
        }

        MarshGruppoCommerciale mGC =
            clientExec.getGruppoCommercialeMap().get(mActual.getCodiceCapogruppo());

        if (null == mGC) {
          mGC = new MarshGruppoCommerciale();
          clientExec.getGruppoCommercialeMap().put(mActual.getCodiceCapogruppo(), mGC);
          mGC.setCodiceCapoGruppo(mActual.getCodiceCapogruppo());
          mGC.setDenominazione(mActual.getCapogruppo());
        }

        Query queryProduct =
            session.createQuery(
                "from MEbtMapping where eysCode = '" + mActual.getCodGaranzia() + "'");
        MEbtMapping lobProduct = (MEbtMapping) queryProduct.uniqueResult();
        if (lobProduct == null) {
          log.debug("ATTENTION: eysCode not found in MLobProduct");
          continue;
        }
        String ebtProduct = lobProduct.getProdotto(); // nome del prodotto ebt

        MarshProduct product = mGC.getProductMap().get(ebtProduct);
        if (product == null) {
          product = new MarshProduct();
          mGC.getProductMap().put(ebtProduct, product);
          product.setName(ebtProduct);
          product.setYear(mActual.getAnnoContabile());
        }
        if (mActual.getNetRevenues() > 0)
          product.setActual(product.getActual() + mActual.getNetRevenues().longValue());
      }

      tx.commit();
    } catch (HibernateException ex) {
      log.error("Exception in listClientExecutives", ex);
      ex.printStackTrace();
      tx.rollback();
    } finally {
      session.close();
    }

    return result;
  }