Search in sources :

Example 6 with AdminEntity

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();
}
Also used : DefaultPrettyPrinter(org.codehaus.jackson.util.DefaultPrettyPrinter) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) GeometrySerializer(com.bedatadriven.geojson.GeometrySerializer) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 7 with AdminEntity

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();
}
Also used : NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) AdminLevel(org.activityinfo.server.database.hibernate.entity.AdminLevel) NewAdminLevel(org.activityinfo.server.endpoint.rest.model.NewAdminLevel) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 8 with AdminEntity

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();
}
Also used : NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) AdminLevel(org.activityinfo.server.database.hibernate.entity.AdminLevel) NewAdminLevel(org.activityinfo.server.endpoint.rest.model.NewAdminLevel) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType)

Example 9 with AdminEntity

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();
        }
    };
}
Also used : AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Example 10 with AdminEntity

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;
}
Also used : AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) Point(com.vividsolutions.jts.geom.Point) Criteria(org.hibernate.Criteria) Envelope(com.vividsolutions.jts.geom.Envelope)

Aggregations

AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)10 Point (com.vividsolutions.jts.geom.Point)3 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)3 NewAdminEntity (org.activityinfo.server.endpoint.rest.model.NewAdminEntity)3 Timed (org.activityinfo.server.util.monitoring.Timed)3 Criteria (org.hibernate.Criteria)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 SweepLineInterval (com.vividsolutions.jts.index.sweepline.SweepLineInterval)2 AdminLevel (org.activityinfo.server.database.hibernate.entity.AdminLevel)2 NewAdminLevel (org.activityinfo.server.endpoint.rest.model.NewAdminLevel)2 GeometrySerializer (com.bedatadriven.geojson.GeometrySerializer)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 SweepLineOverlapAction (com.vividsolutions.jts.index.sweepline.SweepLineOverlapAction)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Date (java.util.Date)1 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)1 Location (org.activityinfo.server.database.hibernate.entity.Location)1 NewLocation (org.activityinfo.server.endpoint.rest.model.NewLocation)1 JsonFactory (org.codehaus.jackson.JsonFactory)1