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

net.sf.jett.tag.TotalTag Maven / Gradle / Ivy

package net.sf.jett.tag;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import net.sf.jagg.AggregateFunction;
import net.sf.jagg.Aggregations;
import net.sf.jagg.Aggregator;
import net.sf.jagg.model.AggregateValue;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import net.sf.jett.exception.TagParseException;
import net.sf.jett.model.Block;
import net.sf.jett.transform.BlockTransformer;
import net.sf.jett.util.AttributeUtil;
import net.sf.jett.util.SheetUtil;

/**
 * 

A TotalTag represents an aggregate value calculated from a * List of values already exposed to the context. This uses * jAgg functionality.

* *
Attributes: *
    *
  • Inherits all attributes from {@link BaseTag}.
  • *
  • items (required): List
  • *
  • value (required): String
  • *
  • parallel (optional): int
  • *
* * @author Randy Gettman */ public class TotalTag extends BaseTag { /** * Attribute that specifies the List of items to aggregate. */ public static final String ATTR_ITEMS = "items"; /** * Attribute that specifies the aggregator to use. */ public static final String ATTR_VALUE = "value"; /** * Attribute that specifies the degree of parallelism to use. */ public static final String ATTR_PARALLEL = "parallel"; private static final List REQ_ATTRS = new ArrayList(Arrays.asList(ATTR_ITEMS, ATTR_VALUE)); private static final List OPT_ATTRS = new ArrayList(Arrays.asList(ATTR_PARALLEL)); private List myList = null; private AggregateFunction myAggregateFunction = null; private int myParallelism = 1; /** * Returns this Tag's name. * @return This Tag's name. */ public String getName() { return "total"; } /** * Returns a List of required attribute names. * @return A List of required attribute names. */ protected List getRequiredAttributes() { List reqAttrs = new ArrayList(super.getRequiredAttributes()); reqAttrs.addAll(REQ_ATTRS); return reqAttrs; } /** * Returns a List of optional attribute names. * @return A List of optional attribute names. */ protected List getOptionalAttributes() { List optAttrs = new ArrayList(super.getOptionalAttributes()); optAttrs.addAll(OPT_ATTRS); return optAttrs; } /** * Validates the attributes for this Tag. The "items" * attribute must be a List. The "parallel" attribute must be * a positive integer (defaults to 1). The "value" attribute must be a * valid Aggregator specification string. The "total" tag must * not have a body. */ @SuppressWarnings("unchecked") public void validateAttributes() throws TagParseException { super.validateAttributes(); if (!isBodiless()) throw new TagParseException("Total tags must not have a body. Total tag with body found" + getLocation()); TagContext context = getContext(); Map beans = context.getBeans(); Map attributes = getAttributes(); myList = AttributeUtil.evaluateObject(this, attributes.get(ATTR_ITEMS), beans, ATTR_ITEMS, List.class, new ArrayList(0)); myParallelism = AttributeUtil.evaluatePositiveInt(this, attributes.get(ATTR_PARALLEL), beans, ATTR_PARALLEL, 1); String aggSpec = AttributeUtil.evaluateString(this, attributes.get(ATTR_VALUE), beans, null); myAggregateFunction = Aggregator.getAggregator(aggSpec); } /** * Run a "group by" operation on the specified Aggregator, get * the result, and set the cell value appropriately. * @return Whether the first Cell in the Block * associated with this Tag was processed. */ public boolean process() { TagContext context = getContext(); Sheet sheet = context.getSheet(); Block block = context.getBlock(); List propsList = new ArrayList(0); List aggList = new ArrayList(1); aggList.add(myAggregateFunction); List> aggValues = Aggregations.groupBy(myList, propsList, aggList, myParallelism); // There should be only one AggregateValue with no properties to group by. AggregateValue aggValue = aggValues.get(0); Object value = aggValue.getAggregateValue(myAggregateFunction); // Replace the bodiless tag text with the proper result. Row row = sheet.getRow(block.getTopRowNum()); Cell cell = row.getCell(block.getLeftColNum()); SheetUtil.setCellValue(cell, value, getAttributes().get(ATTR_VALUE)); BlockTransformer transformer = new BlockTransformer(); transformer.transform(context, getWorkbookContext()); return true; } }