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

net.sf.jett.tag.AnaTag 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.Analytic;
import net.sf.jagg.AnalyticAggregator;
import net.sf.jagg.model.AnalyticValue;
import net.sf.jagg.exception.JaggException;
import org.apache.poi.ss.usermodel.RichTextString;

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

/**
 * 

An AnaTag represents analytic values calculated from a * List of values already exposed to the context. It uses * jAgg functionality and exposes the results and * AnalyticAggregators used for display later.

* *
Attributes: *
    *
  • Inherits all attributes from {@link BaseTag}.
  • *
  • items (required): List
  • *
  • analytics (required): String
  • *
  • analyticsVar (optional): String
  • *
  • valuesVar (required): String
  • *
* * @author Randy Gettman * @since 0.9.0 */ public class AnaTag extends BaseTag { /** * Attribute that specifies the List of items to analyze. */ public static final String ATTR_ITEMS = "items"; /** * Attribute that specifies the List of analytic functions to * use. */ public static final String ATTR_ANALYTICS = "analytics"; /** * Attribute that specifies the name of the List of exposed * AnalyticAggregators. */ public static final String ATTR_ANALYTICS_VAR = "analyticsVar"; /** * Attribute that specifies name of the List of exposed * analytic values. */ public static final String ATTR_VALUES_VAR = "valuesVar"; private static final List REQ_ATTRS = new ArrayList(Arrays.asList(ATTR_ITEMS, ATTR_ANALYTICS, ATTR_VALUES_VAR)); private static final List OPT_ATTRS = new ArrayList(Arrays.asList(ATTR_ANALYTICS_VAR)); private List myList = null; private List myAnalytics = null; private String myAnalyticsVar = null; private String myValuesVar = null; private Analytic myAnalytic; /** * Returns this Tag's name. * @return This Tag's name. */ public String getName() { return "ana"; } /** * Returns a List of required attribute names. * @return A List of required attribute names. */ @Override 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. */ @Override 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 "analytics" attribute must be * a semicolon-separated list of valid analytic specification strings. * The "valuesVar" attribute must be a string that indicates the name to * which the analytic values will be exposed in the Map of * beans. The "analyticsVar" attribute must be a string that indicates the * name of the List that contains all created * AnalyticAggregators and to which that will be exposed in the * Map of beans. The "ana" tag must have a body. */ @SuppressWarnings("unchecked") public void validateAttributes() throws TagParseException { super.validateAttributes(); if (isBodiless()) throw new TagParseException("Ana tags must have a body. Bodiless agg tag 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, null); List analyticsList = AttributeUtil.evaluateList(this, attributes.get(ATTR_ANALYTICS), beans, null); myAnalytics = new ArrayList(analyticsList.size()); for (String anaSpec : analyticsList) myAnalytics.add(AnalyticAggregator.getAnalytic(anaSpec)); myAnalyticsVar = AttributeUtil.evaluateString(this, attributes.get(ATTR_ANALYTICS_VAR), beans, null); myValuesVar = AttributeUtil.evaluateString(this, attributes.get(ATTR_VALUES_VAR), beans, null); Analytic.Builder builder = new Analytic.Builder() .setAnalytics(myAnalytics); try { myAnalytic = builder.build(); } catch (JaggException e) { throw new TagParseException("AnaTag: Problem executing jAgg call: " + getLocation() + ": " + e.getMessage(), e); } catch (RuntimeException e) { throw new TagParseException("AnaTag: RuntimeException caught during jAgg execution" + getLocation() + ": " + e.getMessage(), e); } } /** * Run an "analyze" operation on the specified AnalyticFunctions, get * the results, and expose the analytic values and the * AnalyticAggregators used. * @return Whether the first Cell in the Block * associated with this Tag was processed. */ public boolean process() { TagContext context = getContext(); Map beans = context.getBeans(); List> aggValues = myAnalytic.analyze(myList); beans.put(myValuesVar, aggValues); if (myAnalyticsVar != null) beans.put(myAnalyticsVar, myAnalytics); BlockTransformer transformer = new BlockTransformer(); transformer.transform(context, getWorkbookContext()); return true; } }