Implementation of SampleUDSFOperationFactory

SampleUDSFOperationFactory is a class holding the following information:

 

Superclass

BQLPluginUDSFOperationFactory class

Description

A Factory class that returns an object holding the properties of UDSF Operation and returns an operation object

 

Implementation of SampleUDSFOperationFactory is as follows:

package com.appresso.ds.dp.modules.adapter.sample;

import com.appresso.ds.common.bql.UDSFFromArgument;
import com.appresso.ds.common.bql.UDSFFromTemplate;
import com.appresso.ds.common.spi.constraint.Item;
import com.appresso.ds.common.spi.constraint.Multi;
import com.appresso.ds.common.spi.constraint.NumberFillin;
import com.appresso.ds.common.spi.param.SimpleParameter;
import com.appresso.ds.common.xmlfw.xml.XmlHandler;
import com.appresso.ds.dp.share.adapter.bql.common.BQLPluginUDSFOperationFactory;
import com.appresso.ds.dp.spi.OperationConfiguration;
import com.appresso.ds.dp.spi.OperationConfigurator;
import com.appresso.ds.dp.spi.OperationContext;
import org.xml.sax.SAXException;

import java.util.stream.Stream;

import static com.appresso.ds.common.bql.BQLSimpleParameterType.FLOAT;
import static com.appresso.ds.common.bql.BQLSimpleParameterType.STRING;

public class SampleUDSFOperationFactory extends BQLPluginUDSFOperationFactory {
 @Override 
 protected String getLabel() {
  return Messages.getString("Sample.UDSF.Label");
 }

 @Override 
 public String getPluginName() {
  return "sample_plugin";
 }

 @Override 
 protected String getTypeName() {
  return "sample_udsf";
 }

 @Override 
 public String getOperationName() {
  return "sample_udsf";
 }

 @Override 
 protected void addArgs(UDSFFromTemplate template) {
  template.addArg(new UDSFFromArgument(STRING.toParameterKey("operator")));
  template.addArg(new UDSFFromArgument(FLOAT.toParameterKey("initial_value")));
 }

 @Override 
 protected void setupOperationConfigurator(OperationConfigurator operationConfigurator) {
  setStreamConfigurationParameter(operationConfigurator);
  operationConfigurator.addSimpleParameter(createOperatorParameter());
  operationConfigurator.addSimpleParameter(createInitialValueParameter());
 }

 @Override 
 protected void setupInputSchema(XmlHandler handler, OperationConfiguration conf, OperationContext context)
   throws Exception {
  handler.startElement("", "payload", "payload", EMPTY_ATTRIBUTE);
  writeElement(handler, "value");
  handler.endElement("", "payload", "payload");
 }

 @Override 
 protected void setupOutputSchema(XmlHandler handler, OperationConfiguration conf, OperationContext context)
   throws Exception {
  handler.startElement("", "payload", "payload", EMPTY_ATTRIBUTE);
  writeElement(handler, "formula");
  writeElement(handler, "value");
  handler.endElement("", "payload", "payload");
 }

 private void writeElement(XmlHandler handler, String name) throws SAXException {
  handler.startElement("", name, name, EMPTY_ATTRIBUTE);
  handler.endElement("", name, name);
 }

 private static SimpleParameter createInitialValueParameter() {
  NumberFillin fillin = new NumberFillin();
  fillin.setPrecision(10);
  fillin.setDecimal(3);
  fillin.setAllowDouble(true);
  fillin.setLabel(Messages.getString("Sample.UDSF.Initial.Value.Label"));
  fillin.setRequired(true);
  return new SimpleParameter(FLOAT.toParameterKey("initial_value"), fillin);
 }

 private static SimpleParameter createOperatorParameter(){
  Multi multi = new Multi(Operator.getItems());
  multi.setLabel(Messages.getString("Sample.UDSF.Operator.Label"));
  multi.setRequired(true);
  SimpleParameter param = new SimpleParameter(STRING.toParameterKey("operator"), multi);
  return param;
 }

 private enum Operator {
  Plus("+","plus"),
  Minus("-","minus"),
  Times("*","times"),
  Divided("/","divided");

  private String getDisplayName() {
   return displayName;
  }

  private String getValue() {
   return value;
  }

  private final String displayName;

  private final String value;

  Operator(String displayName, String value) {
   this.displayName = displayName;
   this.value=value;
  }

  Item toItem(){
   return new Item(value,displayName);
  }

  private static Item[] getItems(){
   return Stream.of(Operator.values()).map(Operator::toItem).toArray(Item[]::new);
  }
 }
}