package fgis.server.services;

import fgis.server.entity.fgis.Resource;
import fgis.server.entity.fgis.ResourceTrack;
import fgis.server.entity.fgis.dao.ResourceRepository;
import fgis.server.entity.fgis.dao.ResourceTrackRepository;
import java.util.Date;
import java.util.Random;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import org.geolatte.geom.DimensionalFlag;
import org.geolatte.geom.Point;
import org.geolatte.geom.PointSequenceBuilder;
import org.geolatte.geom.PointSequenceBuilders;
import org.geolatte.geom.crs.CrsId;

@javax.ejb.Local
@Stateless
public class DataGeneratorEJB
{
  private static final double INITIAL_X = 144.92978643046;
  private static final double INITIAL_Y = -37.794939500455;
  private static final double SCALE_FACTOR = 100.0;
  private static final Random RANDOM = new Random();

  @EJB
  private ResourceRepository _resourceService;

  @EJB
  private ResourceTrackRepository _resourceTrackRepository;


  @Schedule( second = "*/5", minute = "*", hour = "*", persistent = false )
  public void insertData()
  {
    jitterResource( "Person", "Peter" );
    jitterResource( "Person", "Phil" );
    jitterResource( "Person", "James" );
    jitterResource( "Person", "Karen" );
    jitterResource( "Slip-On", "THX-1138" );
    jitterResource( "Passenger Vehicle", "LUH-3417" );
    jitterResource( "Tanker", "SEN-5241" );
  }

  private void jitterResource( final String resourceType, final String resourceName )
  {
    final Resource resource = findOrCreateResource( resourceType, resourceName );

    final Point lastLocation = resource.getLocation();
    final double startX = null == lastLocation ? INITIAL_X : lastLocation.getX();
    final double startY = null == lastLocation ? INITIAL_Y : lastLocation.getY();
    final double latPosition = startX + jitterLocation();
    final double longPosition = startY + jitterLocation();

    final Point point = toPoint( latPosition, longPosition );

    updateResourceLocation( resource, point );
  }

  private Point toPoint( final double latPosition, final double longPosition )
  {
    final PointSequenceBuilder builder =
      PointSequenceBuilders.variableSized( DimensionalFlag.d2D, CrsId.UNDEFINED );
    builder.add( latPosition, longPosition );
    return new Point( builder.toPointSequence() );
  }

  private double jitterLocation()
  {
    return ( RANDOM.nextDouble() / SCALE_FACTOR ) - ( 1 / SCALE_FACTOR / 2 );
  }

  private Resource findOrCreateResource( final String resourceType, final String resourceName )
  {
    Resource resource = _resourceService.findByName( resourceName );
    if ( null == resource )
    {
      resource = new Resource( resourceType, resourceName );
      _resourceService.persist( resource );
    }
    return resource;
  }

  private void updateResourceLocation( final Resource resource, final Point point )
  {
    _resourceTrackRepository.persist( new ResourceTrack( resource, new Date(), point ) );
    resource.setLocation( point );
  }
}

@@ -380,186 +380,46 @@
 eom.
-DimensionalFlag;%0Aimport org.geolatte.geom.Point;%0Aimport org.geolatte.geom.PointSequenceBuilder;%0Aimport org.geolatte.geom.PointSequenceBuilders;%0Aimport org.geolatte.geom.crs.CrsId
+Point;%0Aimport org.geolatte.geom.Points
 ;%0A%0A@
@@ -1779,23 +1779,31 @@
 point = 
-to
 Point
+s.create2D
 ( latPos
@@ -1882,317 +1882,8 @@
  %7D%0A%0A
-  private Point toPoint( final double latPosition, final double longPosition )%0A  %7B%0A    final PointSequenceBuilder builder =%0A      PointSequenceBuilders.variableSized( DimensionalFlag.d2D, CrsId.UNDEFINED );%0A    builder.add( latPosition, longPosition );%0A    return new Point( builder.toPointSequence() );%0A  %7D%0A%0A
   pr

Simplify creation of a point