Пример #1
0
 public boolean hasRegistryTotalLines() {
   Iterator<Column> it = registryConfig.getColumns().iterator();
   boolean found = false;
   while (it.hasNext() && !found) {
     Column column = it.next();
     String sfO = column.getSummaryFunction();
     if (sfO != null && sfO.equalsIgnoreCase("sum")) {
       found = true;
     }
   }
   return found;
 }
Пример #2
0
  private void addSumRows(IDataStore dataStore) {
    logger.debug("IN");

    summaryColorCellsArray = new JSONArray();
    summaryCellsArray = new JSONArray();

    ArrayList<Integer> columnsIndexToMerge = new ArrayList<Integer>();
    ArrayList<Integer> columnsIndexToEmpty = new ArrayList<Integer>();
    ArrayList<Integer> columnsIndexToAfter = new ArrayList<Integer>();
    HashMap<Integer, Object> columnsIndexToSum2Counter = new HashMap<Integer, Object>();

    // collect columns to merge and columns to sum and colummsn to empty:
    // -- columns to merge have merge attributes until a columns with summaryFunc is found
    // then other columns that have merge attribute but no
    List<Column> columns = registryConfig.getColumns();

    Integer index = 0;
    boolean summaryFuncFound = false;
    boolean measureFound = false;

    for (Iterator iterator = columns.iterator(); iterator.hasNext(); ) {
      Column column = (Column) iterator.next();

      if (column.isMerge() && summaryFuncFound == false) {
        columnsIndexToMerge.add(index);
      } else if (summaryFuncFound == true && !column.isMeasure() && !measureFound) {
        columnsIndexToEmpty.add(index);
      } else if (summaryFuncFound == true && !column.isMeasure() && measureFound) {
        columnsIndexToAfter.add(index);
      } else if (column.isMeasure()) {
        columnsIndexToSum2Counter.put(index, 0);
        measureFound = true;
      }
      if (column.getSummaryFunction() != null && column.getSummaryFunction().equals("sum"))
        summaryFuncFound = true;
      index++;
    }

    // Map to store previous merge values on iteration
    HashMap<Integer, Object> previousMergeValues = new HashMap<Integer, Object>();
    for (Iterator iterator = columnsIndexToMerge.iterator(); iterator.hasNext(); ) {
      Integer columnIndex = (Integer) iterator.next();
      previousMergeValues.put(columnIndex, null);
    }

    TreeMap<Integer, Record> recordsToAddMap = new TreeMap<Integer, Record>();

    int sumCounter = 0; // add total row only if grouping has more than one member

    // iterate on each store row
    for (int i = 0; i < dataStore.getRecordsCount(); i++) {
      IRecord record = dataStore.getRecordAt(i);

      // get current values of column to merge
      HashMap<Integer, Object> currentMergeValues = new HashMap<Integer, Object>();

      // iterate on each column to merge and store values
      for (Iterator iterator = columnsIndexToMerge.iterator(); iterator.hasNext(); ) {
        Integer columnIndex = (Integer) iterator.next();
        Object value = record.getFieldAt(columnIndex).getValue();
        currentMergeValues.put(columnIndex, value);
      }

      // compare current values with previous ones
      boolean isEqual = compareValuesMaps(previousMergeValues, currentMergeValues);

      // if merging goes on update counters else add summarization line
      if (isEqual) {
        sumCounter++;
        for (Iterator iterator = columnsIndexToSum2Counter.keySet().iterator();
            iterator.hasNext(); ) {
          Integer indexMeasure = (Integer) iterator.next();
          Object value = record.getFieldAt(indexMeasure).getValue();

          // TODO treat the case this is not a number, should keep it to null
          if (value != null) {
            // get previous value

            Object result = operateWithNumbers(columnsIndexToSum2Counter.get(indexMeasure), value);

            columnsIndexToSum2Counter.put(indexMeasure, result);
          } else {
            columnsIndexToSum2Counter.put(indexMeasure, null);
          }
        }
      } else {
        // breaking point, add summarization lines at previous index; i-1

        // add a new record only if sumCounter > 0
        if (sumCounter > 0) {
          addTotalRecord(
              dataStore,
              i,
              columnsIndexToMerge,
              columnsIndexToEmpty,
              columnsIndexToAfter,
              columnsIndexToSum2Counter,
              previousMergeValues,
              recordsToAddMap);
        }

        // put the counters to actual values
        for (Iterator iterator = columnsIndexToSum2Counter.keySet().iterator();
            iterator.hasNext(); ) {
          Integer columnInd = (Integer) iterator.next();
          Object v = record.getFieldAt(columnInd).getValue();
          columnsIndexToSum2Counter.put(columnInd, v);
        }

        sumCounter = 0;
      }

      // update previousValues
      previousMergeValues = currentMergeValues;
    }

    // add  final total if last records were merged
    if (sumCounter > 0) {
      addTotalRecord(
          dataStore,
          null,
          columnsIndexToMerge,
          columnsIndexToEmpty,
          columnsIndexToAfter,
          columnsIndexToSum2Counter,
          previousMergeValues,
          recordsToAddMap);
    }

    // finally add the record (could not add them while cycling the store)
    for (Iterator iterator = recordsToAddMap.keySet().iterator(); iterator.hasNext(); ) {
      Integer indexR = (Integer) iterator.next();
      Record rec = recordsToAddMap.get(indexR);
      if (indexR == -1) {
        dataStore.appendRecord(rec);
      } else {
        dataStore.insertRecord(indexR, rec);
      }
    }

    logger.debug("OUT");
  }