Search in sources :

Example 11 with SiteDTO

use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.

the class SearchPresenter method fromSitesList.

private List<RecentSiteModel> fromSitesList(List<SiteDTO> sites) {
    List<RecentSiteModel> result = new ArrayList<RecentSiteModel>();
    if (sites != null) {
        for (SiteDTO site : sites) {
            RecentSiteModel recent = null;
            if (schema != null) {
                ActivityDTO activity = schema.getActivityById(site.getActivityId());
                if (activity != null) {
                    recent = new RecentSiteModel(site);
                    recent.setActivityName(activity.getName());
                    recent.setActivityLink(PageStateSerializer.asLink(new DataEntryPlace(activity)));
                    recent.setDatabaseName(activity.getDatabase().getName() == null ? "[Database]" : activity.getDatabase().getName());
                }
            }
            if (recent != null) {
                result.add(recent);
            }
        }
    }
    return result;
}
Also used : DataEntryPlace(org.activityinfo.client.page.entry.place.DataEntryPlace) ArrayList(java.util.ArrayList) SiteDTO(org.activityinfo.shared.dto.SiteDTO) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO)

Example 12 with SiteDTO

use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.

the class FormSubmissionResource method createSite.

private void createSite(SiteFormData data, SchemaDTO schemaDTO, ActivityDTO activity) {
    final SiteDTO site = new SiteDTO();
    site.setId(new KeyGenerator().generateInt());
    site.setActivityId(data.getActivity());
    if (activity.getReportingFrequency() == ActivityDTO.REPORT_ONCE) {
        site.setReportingPeriodId(new KeyGenerator().generateInt());
    }
    // set activitymodel
    if (activity.getReportingFrequency() == ActivityDTO.REPORT_ONCE) {
        site.setDate1(data.getDate1());
        site.setDate2(data.getDate2());
    }
    site.setPartner(schemaDTO.getPartnerById(data.getPartner()));
    // set comments
    site.setComments(data.getComments());
    // set attributes
    for (FormAttributeGroup formAttributeGroup : data.getAttributegroups()) {
        AttributeGroupDTO attributeGroup = activity.getAttributeGroupById(formAttributeGroup.getId());
        for (Integer attributeId : attributeGroup.getAttributeIds()) {
            site.setAttributeValue(attributeId, formAttributeGroup.isSelected(attributeId));
        }
    }
    // set indicators
    if (activity.getReportingFrequency() == ActivityDTO.REPORT_ONCE) {
        for (FormIndicator formIndicator : data.getIndicators()) {
            site.setIndicatorValue(formIndicator.getId(), formIndicator.getDoubleValue());
        }
    }
    // create command(s)
    CreateSite cmd = new CreateSite(site);
    cmd.setNestedCommand(createCreateLocationCommand(data, schemaDTO, activity));
    // save
    CreateResult createResult = dispatcher.execute(cmd);
    // create sitehistory entry
    siteHistoryProcessor.process(cmd, getUser().getId(), createResult.getNewId());
}
Also used : FormAttributeGroup(org.activityinfo.server.endpoint.odk.SiteFormData.FormAttributeGroup) AttributeGroupDTO(org.activityinfo.shared.dto.AttributeGroupDTO) FormIndicator(org.activityinfo.server.endpoint.odk.SiteFormData.FormIndicator) CreateResult(org.activityinfo.shared.command.result.CreateResult) SiteDTO(org.activityinfo.shared.dto.SiteDTO) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator) CreateSite(org.activityinfo.shared.command.CreateSite)

Example 13 with SiteDTO

use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.

the class SitesResources method writeJson.

private void writeJson(List<SiteDTO> sites, JsonGenerator json) throws IOException, JsonGenerationException {
    json.writeStartArray();
    for (SiteDTO site : sites) {
        json.writeStartObject();
        json.writeNumberField("id", site.getId());
        json.writeNumberField("activity", site.getActivityId());
        // write start / end date if applicable
        if (site.getDate1() != null && site.getDate2() != null) {
            json.writeStringField("startDate", site.getDate1().toString());
            json.writeStringField("endDate", site.getDate2().toString());
        }
        // write the location as a separate object
        json.writeObjectFieldStart("location");
        json.writeNumberField("id", site.getLocationId());
        json.writeStringField("name", site.getLocationName());
        if (site.hasLatLong()) {
            json.writeFieldName("latitude");
            json.writeNumber(site.getLatitude());
            json.writeFieldName("longitude");
            json.writeNumber(site.getLongitude());
        }
        json.writeEndObject();
        json.writeObjectFieldStart("partner");
        json.writeNumberField("id", site.getPartnerId());
        json.writeStringField("name", site.getPartnerName());
        json.writeEndObject();
        if (site.getProject() != null) {
            json.writeNumberField("projectId", site.getProject().getId());
        }
        // write attributes as a series of ids
        Set<Integer> attributes = getAttributeIds(site);
        if (!attributes.isEmpty()) {
            json.writeFieldName("attributes");
            json.writeStartArray();
            for (Integer attributeId : attributes) {
                json.writeNumber(attributeId);
            }
            json.writeEndArray();
        }
        // write indicators
        Set<Integer> indicatorIds = getIndicatorIds(site);
        if (!indicatorIds.isEmpty()) {
            json.writeObjectFieldStart("indicatorValues");
            for (Integer indicatorId : indicatorIds) {
                json.writeNumberField(Integer.toString(indicatorId), site.getIndicatorValue(indicatorId));
            }
            json.writeEndObject();
        }
        // comments
        if (!Strings.isNullOrEmpty(site.getComments())) {
            json.writeFieldName("comments");
            json.writeString(site.getComments());
        }
        json.writeEndObject();
    }
    json.writeEndArray();
    json.close();
}
Also used : SiteDTO(org.activityinfo.shared.dto.SiteDTO)

Example 14 with SiteDTO

use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.

the class SiteChangeServlet method sendNotifications.

@VisibleForTesting
void sendNotifications(int editorUserId, int siteId, ChangeType type) {
    User user = entityManager.get().find(User.class, editorUserId);
    /*
         * For our purposes, the user who initiated the change will be
         * considered the authenticated user for this thread
         */
    authProvider.set(user);
    SiteResult siteResult = dispatcher.execute(GetSites.byId(siteId));
    SiteDTO siteDTO = siteResult.getData().get(0);
    SchemaDTO schemaDTO = dispatcher.execute(new GetSchema());
    ActivityDTO activityDTO = schemaDTO.getActivityById(siteDTO.getActivityId());
    UserDatabaseDTO userDatabaseDTO = activityDTO.getDatabase();
    Date date = new Date();
    List<User> recipients = findRecipients(userDatabaseDTO.getId());
    for (User recipient : recipients) {
        try {
            // themselves!
            if (recipient.getId() != editorUserId) {
                LOGGER.info("sending sitechange notification email to " + recipient.getEmail());
                UpdateMessageBuilder message = new UpdateMessageBuilder();
                message.setDate(date);
                message.setEditor(user);
                message.setRecipient(recipient);
                message.setUserDatabaseDTO(userDatabaseDTO);
                message.setSiteDTO(siteDTO);
                message.setActivityDTO(activityDTO);
                message.setChangeType(type);
                mailSender.get().send(message.build());
            }
        } catch (Throwable t) {
            LOGGER.warning("failed sending notification email to " + recipient.getName() + " <" + recipient.getEmail() + ">: " + t.getMessage());
            t.printStackTrace();
        }
    }
}
Also used : User(org.activityinfo.server.database.hibernate.entity.User) SiteResult(org.activityinfo.shared.command.result.SiteResult) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) SiteDTO(org.activityinfo.shared.dto.SiteDTO) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Date(java.util.Date) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 15 with SiteDTO

use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.

the class SiteHistoryProcessor method process.

public void process(Command<?> cmd, final int userId, final int siteId) {
    assert (cmd instanceof SiteCommand);
    LOGGER.fine("persisting site history (site: " + siteId + ", user: " + userId + ")");
    EntityManager em = entityManager.get();
    // It's important to use getReference() here rather
    // than find() becuase the site might not actually have
    // been sent to the database at this point
    Site site = em.getReference(Site.class, siteId);
    User user = em.getReference(User.class, userId);
    ChangeType type = ChangeType.getType(cmd);
    if (!type.isNew()) {
        Query q = em.createQuery("select count(*) from SiteHistory where site = :site");
        q.setParameter("site", site);
        Long count = (Long) q.getSingleResult();
        if (count == 0) {
            // update, but first entry -> repair history by adding baseline
            // record with complete site json
            LOGGER.fine("site is not new, but history was empty. Adding baseline record..");
            SiteResult siteResult = dispatcher.execute(GetSites.byId(siteId));
            SiteDTO siteDTO = siteResult.getData().get(0);
            String fulljson = JsonUtil.encodeMap(siteDTO.getProperties()).toString();
            SiteHistory baseline = new SiteHistory();
            baseline.setSite(site);
            baseline.setUser(user);
            baseline.setJson(fulljson);
            baseline.setTimeCreated(new Date().getTime());
            baseline.setInitial(false);
            persist(baseline);
        }
    }
    String json = null;
    if (type.isNewOrUpdate()) {
        Map<String, Object> changeMap = ((SiteCommand) cmd).getProperties().getTransientMap();
        if (!changeMap.isEmpty()) {
            json = JsonUtil.encodeMap(changeMap).toString();
        }
    } else if (type.isDelete()) {
        json = JSON_DELETE;
    }
    if (StringUtils.isNotBlank(json)) {
        persistHistory(site, user, type, json);
    }
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) User(org.activityinfo.server.database.hibernate.entity.User) Query(javax.persistence.Query) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) ChangeType(org.activityinfo.server.event.sitechange.ChangeType) SiteResult(org.activityinfo.shared.command.result.SiteResult) SiteCommand(org.activityinfo.shared.command.SiteCommand) SiteDTO(org.activityinfo.shared.dto.SiteDTO) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory)

Aggregations

SiteDTO (org.activityinfo.shared.dto.SiteDTO)71 Test (org.junit.Test)32 GetSites (org.activityinfo.shared.command.GetSites)16 SiteResult (org.activityinfo.shared.command.result.SiteResult)11 CreateSite (org.activityinfo.shared.command.CreateSite)9 ActivityDTO (org.activityinfo.shared.dto.ActivityDTO)8 SortInfo (com.extjs.gxt.ui.client.data.SortInfo)7 ArrayList (java.util.ArrayList)7 CreateResult (org.activityinfo.shared.command.result.CreateResult)7 Date (java.util.Date)6 OnDataSet (org.activityinfo.server.database.OnDataSet)6 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)6 AiLatLng (org.activityinfo.shared.report.content.AiLatLng)6 ColumnConfig (com.extjs.gxt.ui.client.widget.grid.ColumnConfig)5 ColumnData (com.extjs.gxt.ui.client.widget.grid.ColumnData)5 KeyGenerator (org.activityinfo.client.local.command.handler.KeyGenerator)5 UpdateSite (org.activityinfo.shared.command.UpdateSite)5 LocationDTO (org.activityinfo.shared.dto.LocationDTO)5 PointValue (org.activityinfo.shared.report.model.PointValue)5 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)4