package de.sw4j.blog.webservices.web;

import de.sw4j.blog.webservices.calculator.Addition;
import de.sw4j.blog.webservices.calculator.CalculatorPortType;
import de.sw4j.blog.webservices.calculator.DivByZeroMsg;
import de.sw4j.blog.webservices.calculator.Division;
import de.sw4j.blog.webservices.calculator.Multiplication;
import de.sw4j.blog.webservices.calculator.Subtraction;
import java.math.BigInteger;
import javax.jws.WebService;

/**
 *
 * @author Uwe Plonus <uwe.plonus@sw4j.org>
 */
@WebService(endpointInterface="de.sw4j.blog.webservices.calculator.CalculatorPortType",
    serviceName="CalculatorService",
    portName="CalculatorPort",
    targetNamespace="http://blog.sw4j.de/webservices/calculator")
public class CalculatorService implements CalculatorPortType {

  @Override
  public BigInteger add(Addition operands) {
    BigInteger result = BigInteger.ZERO;
    for (BigInteger operand: operands.getAddend()) {
      result = result.add(operand);
    }
    return result;
  }

  @Override
  public BigInteger sub(Subtraction operands) {
    BigInteger result = operands.getMinuend();
    result = result.subtract(operands.getSubtrahend());
    return result;
  }

  @Override
  public BigInteger mul(Multiplication operands) {
    BigInteger result = operands.getMultiplicand();
    result = result.multiply(operands.getMultiplier());
    return result;
  }

  @Override
  public BigInteger div(Division operands) throws DivByZeroMsg {
    BigInteger result = operands.getDividend();
    try {
      result = result.divide(operands.getDivisor());
    } catch (ArithmeticException aex) {
      throw new DivByZeroMsg("Division by zero.", null, aex);
    }
    return result;
  }
  
}
