//
// Mapper.java - Mapper Web Service
//
// This implementation exists as a template to generate WSDL from,
// and model to create others from.
// It maps between element names and symbols.
// It uses an ArrayOfString class so as to be sure the wrapped document literal
// SOAP has an element enclosing the elements of the lists of strings.
//

package nyangau.jmsadapter.mapper;

import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.xml.ws.Endpoint;

@WebService(
  targetNamespace="http://www.nyangau.org/jmsadapter/mapper",
  serviceName="MapperService"
  )
public class Mapper
  {
  protected Map<String,String> mNameToSymbol = new HashMap<String,String>()
    {{
    put("Gold"  , "Au");
    put("Silver", "Ag");
    }};
  protected Map<String,String> mSymbolToName = new HashMap<String,String>()
    {{
    put("Au", "Gold"  );
    put("Ag", "Silver");
    }};
  @WebMethod()
  public @WebResult(targetNamespace="http://www.nyangau.org/jmsadapter/mapper", name="mapResult") ArrayOfString map(
    @WebParam(targetNamespace="http://www.nyangau.org/jmsadapter/mapper", name="mapName") String mapName,
    @WebParam(targetNamespace="http://www.nyangau.org/jmsadapter/mapper", name="unmapped") ArrayOfString unmapped
    )
    {
    List<String> mapped = new ArrayList<String>();
    Map<String,String> m;
    if ( mapName.equals("Name_to_Symbol") )
      m = mNameToSymbol;
    else if ( mapName.equals("Symbol_to_Name") )
      m = mSymbolToName;
    else
      // Failures are indicated by returning a list of length 1
      {
      mapped.add("bad mapName: "+mapName);
      return new ArrayOfString( mapped );
      }
    for ( String u : unmapped.getList() )
      {
      String s = m.get(u);
      if ( s != null )
        {
        mapped.add(u);
        mapped.add(s);
        }
      }
    return new ArrayOfString( mapped );
    }
  public static void main(String[] args)
    {
    Endpoint.publish("http://localhost:8888/ElementMapper", new Mapper());
    }
  }
