use of org.activityinfo.legacy.shared.exception.CommandException in project activityinfo by bedatadriven.
the class GetSitesHandler method fetchActivityMetadata.
private void fetchActivityMetadata(Filter filter) {
TraceContext activityMetadataTrace = Trace.startSpan("ai/cmd/GetSites/fetchActivityMetadata");
try {
metadataTime.start();
activities = loadMetadata(filter);
} catch (SQLException excp) {
throw new CommandException("Could not fetch metadata from server");
} finally {
metadataTime.stop();
Trace.endSpan(activityMetadataTrace);
}
}
use of org.activityinfo.legacy.shared.exception.CommandException in project activityinfo by bedatadriven.
the class GetSitesHandler method fetchLinkedActivityMetadata.
private void fetchLinkedActivityMetadata(List<Integer> linkedActivitiesToFetch) {
TraceContext linkedActivityMetadataTrace = Trace.startSpan("ai/cmd/GetSites/fetchLinkedActivityMetadata");
try {
metadataTime.start();
LOGGER.info("Fetching Linked Activity Metadata for: " + Arrays.toString(linkedActivitiesToFetch.toArray()));
Filter linkedFilter = new Filter();
linkedFilter.addRestriction(DimensionType.Activity, linkedActivitiesToFetch);
linkedActivities = loadMetadata(linkedFilter);
} catch (SQLException excp) {
throw new CommandException("Could not fetch linked activity metadata from server");
} finally {
metadataTime.stop();
Trace.endSpan(linkedActivityMetadataTrace);
}
}
use of org.activityinfo.legacy.shared.exception.CommandException in project activityinfo by bedatadriven.
the class CreateEntityHandler method execute.
@Override
public CommandResult execute(CreateEntity cmd, User user) {
Map<String, Object> properties = cmd.getProperties().getTransientMap();
PropertyMap propertyMap = new PropertyMap(cmd.getProperties().getTransientMap());
switch(cmd.getEntityName()) {
case UserDatabaseDTO.ENTITY_NAME:
return createDatabase(user, propertyMap);
case FolderDTO.ENTITY_NAME:
return createFolder(user, properties);
case ActivityDTO.ENTITY_NAME:
return createActivity(user, propertyMap);
case AttributeGroupDTO.ENTITY_NAME:
return createAttributeGroup(properties);
case AttributeDTO.ENTITY_NAME:
return createAttribute(properties);
case IndicatorDTO.ENTITY_NAME:
return createIndicator(user, properties);
case LocationTypeDTO.ENTITY_NAME:
return createLocationType(user, propertyMap);
default:
throw new CommandException("Invalid entity class " + cmd.getEntityName());
}
}
use of org.activityinfo.legacy.shared.exception.CommandException in project activityinfo by bedatadriven.
the class RenderReportHtmlHandler method execute.
@Override
public CommandResult execute(RenderReportHtml cmd, User user) throws CommandException {
ReportElement model = cmd.getModel();
LOGGER.fine("Model: " + model);
// don't show the title: it will be rendered by the container
model.setTitle(null);
generator.generateElement(user, model, new Filter(), new DateRange());
StringWriter writer = new StringWriter();
try {
renderer.render(model, writer);
} catch (IOException e) {
throw new CommandException(e);
}
return new HtmlResult(writer.toString());
}
use of org.activityinfo.legacy.shared.exception.CommandException in project activityinfo by bedatadriven.
the class UpdateMonthlyReportsHandler method execute.
@Override
public CommandResult execute(UpdateMonthlyReports cmd, User user) throws CommandException {
// Phantom Row issue occurs when attempting to update Monthly ReportingPeriods concurrently.
// To prevent this, we introduce a locking mechanism to prevent simultaneous insertions into table which result
// in duplicate reporting periods on the given site.
// Once we have acquired a lock, we can then safely execute the command
acquireLock(cmd.getSiteId());
try {
Site site = em.find(Site.class, cmd.getSiteId());
if (site == null) {
throw new CommandException(cmd, "site " + cmd.getSiteId() + " not found for user " + user.getEmail());
}
if (!permissionOracle.isEditAllowed(site, user)) {
throw new IllegalAccessCommandException("Not authorized to modify sites");
}
Map<Month, ReportingPeriod> periods = Maps.newHashMap();
Map<String, Object> siteHistoryChangeMap = createChangeMap();
for (ReportingPeriod period : site.getReportingPeriods()) {
periods.put(HandlerUtil.monthFromRange(period.getDate1(), period.getDate2()), period);
}
for (UpdateMonthlyReports.Change change : cmd.getChanges()) {
if (!periods.containsKey(change.getMonth())) {
ReportingPeriod period = new ReportingPeriod(site);
period.setId(keyGenerator.generateInt());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, change.getMonth().getYear());
calendar.set(Calendar.MONTH, change.getMonth().getMonth() - 1);
calendar.set(Calendar.DATE, 1);
period.setDate1(calendar.getTime());
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
period.setDate2(calendar.getTime());
em.persist(period);
periods.put(change.getMonth(), period);
}
}
for (UpdateMonthlyReports.Change change : cmd.getChanges()) {
updateIndicatorValue(em, periods.get(change.getMonth()), change.getIndicatorId(), change.getValue(), false);
siteHistoryChangeMap.put(getPropertyName(change.getIndicatorId(), change.getMonth()), change.getValue());
}
// update the timestamp on the site entity so changes get picked up
// by the synchro mechanism
site.setVersion(site.getActivity().incrementSiteVersion());
siteHistoryProcessor.persistHistory(site, user, ChangeType.UPDATE, siteHistoryChangeMap);
} finally {
releaseLock(cmd.getSiteId());
}
return new VoidResult();
}
Aggregations