Search in sources :

Example 1 with CommandException

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);
    }
}
Also used : SQLException(java.sql.SQLException) TraceContext(com.google.cloud.trace.core.TraceContext) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 2 with CommandException

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);
    }
}
Also used : Filter(org.activityinfo.legacy.shared.command.Filter) QueryFilter(org.activityinfo.server.command.QueryFilter) SQLException(java.sql.SQLException) TraceContext(com.google.cloud.trace.core.TraceContext) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 3 with CommandException

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());
    }
}
Also used : PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 4 with CommandException

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());
}
Also used : DateRange(org.activityinfo.legacy.shared.reports.model.DateRange) StringWriter(java.io.StringWriter) Filter(org.activityinfo.legacy.shared.command.Filter) HtmlResult(org.activityinfo.legacy.shared.command.result.HtmlResult) ReportElement(org.activityinfo.legacy.shared.reports.model.ReportElement) IOException(java.io.IOException) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 5 with CommandException

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();
}
Also used : UpdateMonthlyReports(org.activityinfo.legacy.shared.command.UpdateMonthlyReports) VoidResult(org.activityinfo.legacy.shared.command.result.VoidResult) Calendar(java.util.Calendar) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) CommandException(org.activityinfo.legacy.shared.exception.CommandException) Month(org.activityinfo.model.type.time.Month) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)

Aggregations

CommandException (org.activityinfo.legacy.shared.exception.CommandException)18 TraceContext (com.google.cloud.trace.core.TraceContext)3 Filter (org.activityinfo.legacy.shared.command.Filter)3 IllegalAccessCommandException (org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)3 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 CommandResult (org.activityinfo.legacy.shared.command.result.CommandResult)2 DateRange (org.activityinfo.legacy.shared.reports.model.DateRange)2 PropertyMap (org.activityinfo.server.command.handler.crud.PropertyMap)2 User (org.activityinfo.server.database.hibernate.entity.User)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 Calendar (java.util.Calendar)1 CreateLocation (org.activityinfo.legacy.shared.command.CreateLocation)1 CreateLockedPeriod (org.activityinfo.legacy.shared.command.CreateLockedPeriod)1 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)1 OldGetSites (org.activityinfo.legacy.shared.command.OldGetSites)1 UpdateMonthlyReports (org.activityinfo.legacy.shared.command.UpdateMonthlyReports)1