package com.esri.geoevent.solutions.processor.ll2mgrs;

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

import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.MapGeometry;
import com.esri.core.geometry.Point;
import com.esri.ges.core.ConfigurationException;
import com.esri.ges.core.component.ComponentException;
import com.esri.ges.core.geoevent.DefaultFieldDefinition;
import com.esri.ges.core.geoevent.FieldDefinition;
import com.esri.ges.core.geoevent.FieldType;
import com.esri.ges.core.geoevent.GeoEvent;
import com.esri.ges.core.geoevent.GeoEventDefinition;
import com.esri.ges.core.geoevent.GeoEventPropertyName;
import com.esri.ges.core.property.PropertyType;
import com.esri.ges.manager.geoeventdefinition.GeoEventDefinitionManager;
import com.esri.ges.messaging.GeoEventCreator;
import com.esri.ges.messaging.Messaging;
import com.esri.ges.processor.GeoEventProcessorBase;
import com.esri.ges.processor.GeoEventProcessorDefinition;

public class MGRSProcessor extends GeoEventProcessorBase{
	private Integer accuracy;
	private String newdef;
	private String geofld;
	private List<FieldDefinition> fds;
	private GeoEventDefinitionManager manager;
	private Messaging messaging;
	
	public MGRSProcessor(GeoEventProcessorDefinition definition)
			throws ComponentException {
		super(definition);
		// TODO Auto-generated constructor stub
	}

	@Override
	public GeoEvent process(GeoEvent evt) throws Exception {
		MapGeometry mapGeo = (MapGeometry) evt.getField(geofld);
		Geometry geo = mapGeo.getGeometry();
		if(geo.getType() != Geometry.Type.Point)
		{
			return null;
		}
		Point pt = (Point)geo;
		
		LL ll = new LL(pt.getX(), pt.getY());
		ll.setAccuracy(accuracy);
		
		MGRS2LatLongConverter converter = new MGRS2LatLongConverter();
		String mgrs = converter.LL2MRGS(ll);
		GeoEventDefinition edOut;
		GeoEventDefinition geoDef = evt.getGeoEventDefinition();
		if((edOut=manager.searchGeoEventDefinition(newdef, getId()))==null)
		{
			edOut = geoDef.augment(fds);
			edOut.setOwner(getId());
			edOut.setName(newdef);
			manager.addGeoEventDefinition(edOut);
		}
		GeoEventCreator  geoEventCreator = messaging.createGeoEventCreator();
		GeoEvent geOut = geoEventCreator.create(edOut.getGuid(), new Object[] {
			evt.getAllFields(), mgrs });
		geOut.setProperty(GeoEventPropertyName.TYPE, "message");
		geOut.setProperty(GeoEventPropertyName.OWNER_ID, getId());
		geOut.setProperty(GeoEventPropertyName.OWNER_ID, definition.getUri());
		return geOut;
	}
	
	@Override 
	public void afterPropertiesSet()
	{
		geofld = properties.get("geofld").getValueAsString();
		newdef = properties.get("eventdef").getValueAsString();
		accuracy = (Integer)properties.get("accuracy").getValue();
		try {
			FieldDefinition fd = new DefaultFieldDefinition("mgrs", FieldType.String);
			fds = new ArrayList<FieldDefinition>();
			fds.add(fd);
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//getters setters
	public void setManager(GeoEventDefinitionManager manager)
	{
		this.manager =  manager;
	}
	
	public void setMessaging(Messaging messaging)
	{
		this.messaging = messaging;
	}

}

@@ -1549,24 +1549,118 @@
 Geometry();%0A
+%09%09int wkid = mapGeo.getSpatialReference().getID();%0A%09%09if(wkid != 4326)%0A%09%09%7B%0A%09%09%09return null;%0A%09%09%7D%0A
 %09%09if(geo.get

test for wgs_84 lat lon projection