Search in sources :

Example 6 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class ReportMailer method execute.

@Timed(name = "mail.report")
public void execute(Date today, ReportSubscription sub, Report report) throws IOException {
    // set up authentication for the subscriber of this report
    authProvider.set(sub.getUser());
    // render the report to a temp file
    // generate the report
    reportGenerator.generate(sub.getUser(), report, null, new DateRange());
    ByteArrayOutputStream rtf = new ByteArrayOutputStream();
    rtfReportRenderer.render(report, rtf);
    rtf.close();
    try {
        mailReport(sub, report, today, rtf.toByteArray());
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Report mailing of " + sub.getTemplate().getId() + " failed for user " + sub.getUser().getEmail(), e);
    }
}
Also used : DateRange(org.activityinfo.legacy.shared.reports.model.DateRange) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 7 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class LocationsResource method query.

@GET
@Timed(name = "api.rest.locations.get")
@Produces(MediaType.APPLICATION_JSON)
public Response query(@QueryParam("type") int typeId) throws IOException {
    GetLocations query = new GetLocations();
    query.setLocationTypeId(typeId);
    LocationResult result = dispatcher.execute(query);
    StringWriter writer = new StringWriter();
    JsonGenerator json = Jackson.createJsonFactory(writer);
    json.writeStartArray();
    for (LocationDTO location : result.getData()) {
        json.writeStartObject();
        json.writeNumberField("id", location.getId());
        json.writeStringField("name", location.getName());
        if (location.hasAxe()) {
            json.writeStringField("code", location.getAxe());
        }
        if (location.hasCoordinates()) {
            json.writeNumberField("latitude", location.getLatitude());
            json.writeNumberField("longitude", location.getLongitude());
        }
        if (!location.getAdminEntities().isEmpty()) {
            json.writeObjectFieldStart("adminEntities");
            for (AdminEntityDTO entity : location.getAdminEntities()) {
                json.writeFieldName(Integer.toString(entity.getLevelId()));
                json.writeStartObject();
                json.writeNumberField("id", entity.getId());
                json.writeStringField("name", entity.getName());
                json.writeEndObject();
            }
            json.writeEndObject();
        }
        json.writeEndObject();
    }
    json.writeEndArray();
    json.close();
    return Response.ok(writer.toString()).type(MediaType.APPLICATION_JSON_TYPE).build();
}
Also used : StringWriter(java.io.StringWriter) GetLocations(org.activityinfo.legacy.shared.command.GetLocations) AdminEntityDTO(org.activityinfo.legacy.shared.model.AdminEntityDTO) JsonGenerator(org.codehaus.jackson.JsonGenerator) LocationDTO(org.activityinfo.legacy.shared.model.LocationDTO) LocationResult(org.activityinfo.legacy.shared.command.result.LocationResult) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 8 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class LocationsResource method postNewLocations.

@POST
@Path("/{typeId}")
@Timed(name = "api.rest.locations.post")
public Response postNewLocations(@InjectParam EntityManager entityManager, @PathParam("typeId") int locationTypeId, List<NewLocation> locations) {
    KeyGenerator generator = new KeyGenerator();
    entityManager.getTransaction().begin();
    LocationType locationType = entityManager.getReference(LocationType.class, locationTypeId);
    for (NewLocation newLocation : locations) {
        Location location = new Location();
        location.setId(generator.generateInt());
        System.out.println(location.getId());
        location.setName(newLocation.getName());
        location.setLocationType(locationType);
        location.setX(newLocation.getLongitude());
        location.setY(newLocation.getLatitude());
        location.setTimeEdited(new Date());
        location.setAdminEntities(new HashSet<AdminEntity>());
        for (int entityId : newLocation.getAdminEntityIds()) {
            location.getAdminEntities().add(entityManager.getReference(AdminEntity.class, entityId));
        }
        entityManager.persist(location);
    }
    entityManager.getTransaction().commit();
    return Response.ok().build();
}
Also used : AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) NewLocation(org.activityinfo.server.endpoint.rest.model.NewLocation) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Date(java.util.Date) NewLocation(org.activityinfo.server.endpoint.rest.model.NewLocation) Location(org.activityinfo.server.database.hibernate.entity.Location) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 9 with Timed

use of org.activityinfo.server.util.monitoring.Timed in project activityinfo by bedatadriven.

the class SitesResources method queryPoints.

@GET
@Path("/points")
@Timed(name = "api.rest.sites.points")
public Response queryPoints(@QueryParam("activity") List<Integer> activityIds, @QueryParam("database") List<Integer> databaseIds, @QueryParam("callback") String callback) throws IOException {
    Filter filter = new Filter();
    filter.addRestriction(DimensionType.Activity, activityIds);
    filter.addRestriction(DimensionType.Database, databaseIds);
    GetSites command = new GetSites(filter);
    List<SiteDTO> sites = dispatcher.execute(command).getData();
    StringWriter writer = new StringWriter();
    JsonGenerator json = Jackson.createJsonFactory(writer);
    writeGeoJson(sites, json);
    if (Strings.isNullOrEmpty(callback)) {
        return Response.ok(writer.toString()).type("application/json; charset=UTF-8").build();
    } else {
        return Response.ok(callback + "(" + writer.toString() + ");").type("application/javascript; charset=UTF-8").build();
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 10 with Timed

use of org.activityinfo.server.util.monitoring.Timed 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)

Aggregations

Timed (org.activityinfo.server.util.monitoring.Timed)14 JsonGenerator (org.codehaus.jackson.JsonGenerator)5 StringWriter (java.io.StringWriter)4 Date (java.util.Date)3 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 Filter (org.activityinfo.legacy.shared.command.Filter)2 Month (org.activityinfo.model.type.time.Month)2 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)2 NewAdminEntity (org.activityinfo.server.endpoint.rest.model.NewAdminEntity)2 GeometrySerializer (com.bedatadriven.geojson.GeometrySerializer)1 GeneralPath (com.google.code.appengine.awt.geom.GeneralPath)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKBReader (com.vividsolutions.jts.io.WKBReader)1 DataInputStream (java.io.DataInputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 MessagingException (javax.mail.MessagingException)1