package pl.relationsystems.citynav.server.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import pl.relationsystems.citynav.server.utils.PMF;
import com.beoui.geocell.GeocellManager;
import com.beoui.geocell.model.LocationCapable;
import com.beoui.geocell.model.Point;
import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
@PersistenceCapable
public class Hub implements LocationCapable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String key;
@Persistent
@Extension(vendorName = "datanucleus", key = "gae.pk-name", value = "true")
private String name;
@Persistent
protected GeoPt position = new GeoPt(0, 0);
@Persistent
private List<String> geocells = new ArrayList<String>();
@Persistent
protected List<Long> stops = new ArrayList<Long>();
@Persistent
protected Boolean isActive = false;
@Persistent
protected Boolean isTransferable = false;
@NotPersistent
private List<Stop> _stops = new ArrayList<Stop>();
public List<String> getGeocells() {
return geocells;
}
public String getKeyString() {
return name;
}
public Point getLocation() {
GeoPt pos = getPosition();
return new Point(pos.getLatitude(), pos.getLongitude());
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the position
*/
public GeoPt getPosition() {
return position;
}
/**
* @return the stops
*/
public List<Long> getStops() {
return stops;
}
/**
* @param stops
* the stops to set
*/
public void setStops(List<Long> stops) {
this.stops = stops;
}
/**
* @param geocells
* the geocells to set
*/
public void setGeocells(List<String> geocells) {
this.geocells = geocells;
}
public void addStop(Stop stop) {
if (!stops.contains(stop.getId())) {
stops.add(stop.getId());
_stops.add(stop);
}
buildPosition();
}
protected void buildPosition() {
Double longitude = 0.0;
Double latitude = 0.0;
if (_stops.size() > 0) {
for (Stop stop : _stops) {
latitude += stop.getPosition().getLatitude();
longitude += stop.getPosition().getLongitude();
}
this.position = new GeoPt((float) (latitude / _stops.size()),
(float) (longitude / _stops.size()));
}
List<String> cells = GeocellManager.generateGeoCell(this.position);
setGeocells(cells);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Hub [\n\t");
if (name != null) {
builder.append("name=");
builder.append(name);
builder.append(",\n\t");
}
if (position != null) {
builder.append("position=");
builder.append(position);
builder.append(",\n\t");
}
if (stops != null) {
builder.append("stops=");
builder.append(stops);
}
builder.append("\n]");
return builder.toString();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Hub other = (Hub) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
protected static Map<String, Hub> _hubPool = new HashMap<String, Hub>();
public static Hub retrieveOrCreate(String name) {
if (name == null || name.length() == 0) {
return null;
}
PersistenceManager pm = PMF.get().getPersistenceManager();
Hub entity = null;
if (_hubPool.size() == 0) {
Query query = pm.newQuery(Hub.class, "isActive==true");
List<Hub> results = (List<Hub>) query.execute();
results.size();
for (Hub h : results) {
_hubPool.put(h.getName(), h);
}
}
entity = _hubPool.get(name);
if (entity != null)
{
pm.close();
return entity;
}
try {
Key k = KeyFactory.createKey(Hub.class.getSimpleName(), name);
entity = pm.getObjectById(Hub.class, k);
} catch (Exception e) {
}
if (entity == null) {
entity = new Hub();
entity.setName(name);
pm.makePersistent(entity);
}
pm.close();
_hubPool.put(name, entity);
return entity;
}
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
/**
* @return the isActive
*/
public Boolean getIsActive() {
return isActive;
}
/**
* @param isActive
* the isActive to set
*/
public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}
/**
* @return the isTransferable
*/
public Boolean getIsTransferable() {
return isTransferable;
}
/**
* @param isTransferable
* the isTransferable to set
*/
public void setIsTransferable(Boolean isTransferable) {
this.isTransferable = isTransferable;
}
}