Package custom.energypro.vinnica.f116

Source Code of custom.energypro.vinnica.f116.f116Scriptlet

package custom.energypro.vinnica.f116;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import system.LangLogic;
import system.PeriodLogic;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;

import com.uens.query.dao.SqlUtil;

public class f116Scriptlet extends JRDefaultScriptlet {
  private static final String F_CODE = "CODE";
  private static final String F_NAME = "NAME";
  private static final String F_INDEBT = "INDEBT";
  private static final String F_NARAH_KWT = "NARAH_KWT";
  private static final String F_NARAH_GRN = "NARAH_GRN";
  private static final String F_REALIZ_KWT = "REALIZ_KWT";
  private static final String F_REALIZ_GRN = "REALIZ_GRN";
  private static final String F_OUTDEBT = "OUTDEBT";
  private static final String F_FULLCRED = "FULLCRED";
  private static final String F_PAID = "PAID";
  private static final String F_INCRED = "INCRED";
  private static final String F_OUTCRED = "OUTCRED";
  private static final String F_CHILDREN = "CHILDREN";

  private static final String[] AGR_FIELDS = new String[] { F_INDEBT,
      F_NARAH_KWT, F_NARAH_GRN, F_REALIZ_KWT, F_REALIZ_GRN, F_OUTDEBT,
      F_FULLCRED, F_PAID, F_INCRED, F_OUTCRED };

  private static final Set<Integer> node2Collapse = new HashSet<Integer>();

  static {
    node2Collapse.add(1);
  }

  private Collection<Map<String, ?>> data;
  private Map<Integer, Map<String, ?>> code2Node;

  @Override
  public void beforeReportInit() throws JRScriptletException {
    super.beforeReportInit();
    loadData();
    calculateTotal(null);
    collapseNodes();
  }

  @SuppressWarnings("unchecked")
  private void calculateTotal(Map<String, Object> parent) {
    Collection<Map<String, ?>> parentChildren = null;
    if (parent == null) {
      parentChildren = data;
    } else {
      parentChildren = (Collection<Map<String, ?>>) parent
          .get(F_CHILDREN);
    }
    for (Iterator<Map<String, ?>> iterator = parentChildren.iterator(); iterator
        .hasNext();) {
      Map<String, Object> childNode = (Map<String, Object>) iterator
          .next();
      calculateTotal(childNode);
      if (parent != null) {
        agregate(childNode, parent);
      }
    }
  }

  private void agregate(Map<String, Object> child, Map<String, Object> parent) {
    for (int i = 0; i < AGR_FIELDS.length; i++) {
      String field = AGR_FIELDS[i];
      BigDecimal valChild = (BigDecimal) child.get(field);
      if (valChild == null) {
        valChild = BigDecimal.ZERO;
      }
      BigDecimal valParent = (BigDecimal) parent.get(field);
      if (valParent == null) {
        valParent = BigDecimal.ZERO;
      }
      valParent = valParent.add(valChild);
      parent.put(field, valParent);
    }
  }

  @SuppressWarnings("unchecked")
  private void collapseNodes() {
    for (Iterator<Integer> iterator = node2Collapse.iterator(); iterator
        .hasNext();) {
      Integer code = iterator.next();
      Map<String, ?> node = getNode(code);
      Collection<Map<String, ?>> children = (Collection<Map<String, ?>>) node
          .get(F_CHILDREN);
      children.clear();
    }
  }

  @SuppressWarnings("unchecked")
  private void loadData() throws JRScriptletException {
    data = new ArrayList<Map<String, ?>>();
    code2Node = new HashMap<Integer, Map<String, ?>>();

    Connection conn = (Connection) getParameterValue("REPORT_CONNECTION");
    Integer renCode = (Integer) getParameterValue("renCode");
    Integer periodCode = (Integer) getParameterValue("periodCode");

    String sql = SqlUtil.loadSQL(getClass(), "main.sql");
    PreparedStatement st = null;
    ResultSet rs = null;

    try {
      st = conn.prepareStatement(sql);
      st.setInt(1, periodCode);
      st.setInt(2, renCode);
      st.setInt(3, periodCode);
      st.setInt(4, renCode);
      st.setInt(5, periodCode);
      st.setInt(6, renCode);
      st.setInt(7, periodCode);
      st.setInt(8, renCode);
      st.setInt(9, periodCode);
      st.setInt(10, renCode);

      rs = st.executeQuery();
      while (rs.next()) {
        int code = rs.getInt(1);
        int parent = rs.getInt(2);
        if (rs.wasNull()) {
          parent = Integer.MIN_VALUE;
        }

        Collection<Map<String, ?>> parentChildren = null;
        if (parent == Integer.MIN_VALUE) {
          parentChildren = data;
        } else {
          Map<String, ?> parentNode = getNode(parent);
          parentChildren = (List<Map<String, ?>>) parentNode
              .get(F_CHILDREN);
        }

        Map<String, ?> newNode = getNode(code);
        loadRow((Map<String, Object>) newNode, rs);
        parentChildren.add(newNode);
      }
      System.out.println(data.size());
    } catch (SQLException e) {
      throw new JRScriptletException(e.getMessage(), e);
    } finally {
      SqlUtil.close(rs);
      SqlUtil.close(st);
    }
  }

  private void loadRow(Map<String, Object> newNode, ResultSet rs)
      throws SQLException {
    newNode.put(F_NAME, rs.getString(F_NAME));
    for (int i = 0; i < AGR_FIELDS.length; i++) {
      newNode.put(AGR_FIELDS[i], rs.getBigDecimal(AGR_FIELDS[i]));
    }
  }

  @SuppressWarnings("unchecked")
  private Map<String, ?> getNode(int code) {
    Map<String, Object> result = (Map<String, Object>) code2Node.get(code);
    if (result == null) {
      result = new HashMap<String, Object>();
      result.put(F_CODE, code);
      result.put(F_CHILDREN, new ArrayList<Map<String, ?>>());
      code2Node.put(code, result);
    }
    return result;
  }

  public JRDataSource getDS() {
    return new JRMapCollectionDataSource(data);
  }

  public String getMonthYearUA(int periodCode) throws JRScriptletException {
    PeriodLogic pl = new PeriodLogic(
        (Connection) getParameterValue("REPORT_CONNECTION"));
    pl.loadPeriodByCode(periodCode);
    return LangLogic.getMonthYearUA(pl.getStartDate());
  }

  public String getOperator() throws JRScriptletException {
    PreparedStatement st = null;
    ResultSet rs = null;
    String userAlias = (String) getParameterValue("username"); //$NON-NLS-1$
    String sql = "SELECT COALESCE(user_name, user_alias)  FROM auth_user WHERE LCASE(user_alias) = ?"; //$NON-NLS-1$

    try {
      Connection conn = (Connection) getParameterValue("REPORT_CONNECTION");
      st = conn.prepareStatement(sql);
      st.setString(1, userAlias);
      rs = st.executeQuery();
      if (rs.next()) {
        return rs.getString(1);
      }
      return "";
    } catch (SQLException ex) {
      System.out.println("Statement: " + sql); //$NON-NLS-1$
      ex.printStackTrace();
      throw new JRScriptletException(ex);
    } finally {
      SqlUtil.close(rs);
      SqlUtil.close(st);
    }
  }

}
TOP

Related Classes of custom.energypro.vinnica.f116.f116Scriptlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.