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;
}
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());
}
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();
}
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();
}
}
}
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);
}
}
Aggregations