use of org.activityinfo.server.database.hibernate.entity.AdminEntity in project activityinfo by bedatadriven.
the class AdminLevelResource method getFeatures.
@GET
@Timed(name = "site.rest.admin.features")
@Path("/entities/features")
public Response getFeatures(@InjectParam EntityManager em) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(baos, Charsets.UTF_8);
List<AdminEntity> entities = em.createQuery("select e from AdminEntity e where e.deleted = false and e.level = :level").setParameter("level", level).getResultList();
JsonFactory jfactory = new JsonFactory();
JsonGenerator json = jfactory.createJsonGenerator(writer);
DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
json.setPrettyPrinter(prettyPrinter);
json.writeStartObject();
json.writeStringField("type", "FeatureCollection");
json.writeFieldName("features");
json.writeStartArray();
GeometrySerializer geometrySerializer = new GeometrySerializer();
for (AdminEntity entity : entities) {
if (entity.getGeometry() != null) {
json.writeStartObject();
json.writeStringField("type", "Feature");
json.writeNumberField("id", entity.getId());
json.writeObjectFieldStart("properties");
json.writeStringField("name", entity.getName());
if (entity.getParentId() != null) {
json.writeNumberField("parentId", entity.getParentId());
}
json.writeEndObject();
json.writeFieldName("geometry");
geometrySerializer.writeGeometry(json, entity.getGeometry());
json.writeEndObject();
}
}
json.writeEndArray();
json.writeEndObject();
json.close();
return Response.ok().entity(baos.toByteArray()).type(MediaType.APPLICATION_JSON).build();
}
use of org.activityinfo.server.database.hibernate.entity.AdminEntity in project activityinfo by bedatadriven.
the class AdminLevelResource method postNewLevel.
@POST
@Timed(name = "site.rest.admin.child_levels")
@Path("/childLevels")
@Consumes(MediaType.APPLICATION_JSON)
public Response postNewLevel(@InjectParam EntityManager em, @InjectParam AuthenticatedUser user, NewAdminLevel newLevel) throws ParseException {
assertAuthorized(user);
em.getTransaction().begin();
em.setFlushMode(FlushModeType.COMMIT);
AdminLevel child = new AdminLevel();
child.setCountry(level.getCountry());
child.setName(newLevel.getName());
child.setParent(level);
em.persist(child);
for (NewAdminEntity entity : newLevel.getEntities()) {
AdminEntity childEntity = new AdminEntity();
childEntity.setName(entity.getName());
childEntity.setLevel(child);
childEntity.setCode(entity.getCode());
childEntity.setBounds(entity.getBounds());
childEntity.setParent(em.getReference(AdminEntity.class, entity.getParentId()));
childEntity.setGeometry(entity.getGeometry());
child.getEntities().add(childEntity);
em.persist(childEntity);
}
// create bound location type
LocationType boundType = new LocationType();
boundType.setBoundAdminLevel(child);
boundType.setCountry(level.getCountry());
boundType.setName(child.getName());
em.persist(boundType);
em.getTransaction().commit();
return Response.ok().build();
}
use of org.activityinfo.server.database.hibernate.entity.AdminEntity in project activityinfo by bedatadriven.
the class CountryResource method postNewLevel.
@POST
@Path("adminLevels")
@Consumes(MediaType.APPLICATION_JSON)
public Response postNewLevel(@InjectParam AuthenticatedUser user, @InjectParam EntityManager em, NewAdminLevel newLevel) {
// assertAuthorized(user);
em.getTransaction().begin();
em.setFlushMode(FlushModeType.COMMIT);
AdminLevel level = new AdminLevel();
level.setCountry(country);
level.setName(newLevel.getName());
level.setVersion(1);
em.persist(level);
for (NewAdminEntity newEntity : newLevel.getEntities()) {
AdminEntity entity = new AdminEntity();
entity.setName(newEntity.getName());
entity.setLevel(level);
entity.setCode(newEntity.getCode());
entity.setBounds(newEntity.getBounds());
entity.setGeometry(newEntity.getGeometry());
level.getEntities().add(entity);
em.persist(entity);
}
// create bound location type
LocationType boundType = new LocationType();
boundType.setBoundAdminLevel(level);
boundType.setCountry(level.getCountry());
boundType.setName(level.getName());
em.persist(boundType);
em.getTransaction().commit();
return Response.ok().build();
}
use of org.activityinfo.server.database.hibernate.entity.AdminEntity in project activityinfo by bedatadriven.
the class AdminHibernateDAO method query.
@Override
public Query query() {
final Criteria criteria = createCriteria();
return new Query() {
@Override
public Query level(int levelId) {
criteria.createAlias("entity.level", "level").add(Restrictions.eq("level.id", levelId));
return this;
}
@Override
public Query withParentEntityId(int id) {
criteria.createAlias("entity.parent", "parent").add(Restrictions.eq("parent.id", id));
return this;
}
@Override
public Query withSitesOfActivityId(int id) {
DetachedCriteria havingActivities = DetachedCriteria.forClass(AdminEntity.class, "entity").createAlias("locations", "location").createAlias("location.sites", "site").createAlias("site.activity", "activity").add(Restrictions.eq("activity.id", id)).setProjection(Projections.property("entity.id"));
criteria.add(Subqueries.propertyIn("entity.id", havingActivities));
return this;
}
@Override
public List<AdminEntity> execute() {
return criteria.list();
}
};
}
use of org.activityinfo.server.database.hibernate.entity.AdminEntity in project activityinfo by bedatadriven.
the class BatchGeocoder method queryEntities.
private List<AdminEntity> queryEntities() {
// to do the job efficiently on the set of points, we'll use a SweepLine algorithm.
// but first we need to compute the bounds of the bounds so we know what to fetch
Envelope pointsMbr = new Envelope();
for (Point point : points) {
pointsMbr.expandToInclude(point.getCoordinate());
}
// now query the x/y ranges of all admin entities that
// might intersect with the ranges
Criteria criteria = session.createCriteria(AdminEntity.class);
criteria.add(SpatialRestrictions.intersects("geometry", gf.toGeometry(pointsMbr)));
List<AdminEntity> entities = criteria.list();
return entities;
}
Aggregations