Search in sources :

Example 1 with AsyncResult

use of org.springframework.scheduling.annotation.AsyncResult in project uPortal by Jasig.

the class MarketplaceService method loadMarketplaceEntriesFor.

/**
     * Load the list of marketplace entries for a user. Will load entries async. This method is
     * primarily intended for seeding data. Most impls should call browseableMarketplaceEntriesFor()
     * instead.
     *
     * <p>Note: Set is immutable since it is potentially shared between threads. If the set needs
     * mutability, be sure to consider the thread safety implications. No protections have been
     * provided against modifying the MarketplaceEntry itself, so be careful when modifying the
     * entities contained in the list.
     *
     * @param user The non-null user
     * @param categories Restricts the output to entries within the specified categories if
     *     non-empty
     * @return a Future that will resolve to a set of MarketplaceEntry objects the requested user
     *     has browse access to.
     * @throws java.lang.IllegalArgumentException if user is null
     * @since 4.2
     */
@Async
public Future<ImmutableSet<MarketplaceEntry>> loadMarketplaceEntriesFor(final IPerson user, final Set<PortletCategory> categories) {
    final IAuthorizationPrincipal principal = AuthorizationPrincipalHelper.principalFromUser(user);
    List<IPortletDefinition> allDisplayablePortletDefinitions = this.portletDefinitionRegistry.getAllPortletDefinitions();
    if (!categories.isEmpty()) {
        // Indicates we plan to restrict portlets displayed in the Portlet
        // Marketplace to those that belong to one or more specified groups.
        Element portletDefinitionsElement = marketplaceCategoryCache.get(categories);
        if (portletDefinitionsElement == null) {
            /*
                 * Collection not in cache -- need to recreate it
                 */
            // Gather the complete collection of allowable categories (specified categories & their descendants)
            final Set<PortletCategory> allSpecifiedAndDecendantCategories = new HashSet<>();
            for (PortletCategory pc : categories) {
                collectSpecifiedAndDescendantCategories(pc, allSpecifiedAndDecendantCategories);
            }
            // Filter portlets that match the criteria
            Set<IPortletDefinition> filteredPortletDefinitions = new HashSet<>();
            for (final IPortletDefinition portletDefinition : allDisplayablePortletDefinitions) {
                final Set<PortletCategory> parents = portletCategoryRegistry.getParentCategories(portletDefinition);
                for (final PortletCategory parent : parents) {
                    if (allSpecifiedAndDecendantCategories.contains(parent)) {
                        filteredPortletDefinitions.add(portletDefinition);
                        break;
                    }
                }
            }
            portletDefinitionsElement = new Element(categories, new ArrayList<>(filteredPortletDefinitions));
            marketplaceCategoryCache.put(portletDefinitionsElement);
        }
        allDisplayablePortletDefinitions = (List<IPortletDefinition>) portletDefinitionsElement.getObjectValue();
    }
    final Set<MarketplaceEntry> visiblePortletDefinitions = new HashSet<>();
    for (final IPortletDefinition portletDefinition : allDisplayablePortletDefinitions) {
        if (mayBrowsePortlet(principal, portletDefinition)) {
            final MarketplacePortletDefinition marketplacePortletDefinition = getOrCreateMarketplacePortletDefinition(portletDefinition);
            final MarketplaceEntry entry = new MarketplaceEntry(marketplacePortletDefinition, user);
            // flag whether this use can add the portlet...
            boolean canAdd = mayAddPortlet(user, portletDefinition);
            entry.setCanAdd(canAdd);
            visiblePortletDefinitions.add(entry);
        }
    }
    logger.trace("These portlet definitions {} are browseable by {}.", visiblePortletDefinitions, user);
    Future<ImmutableSet<MarketplaceEntry>> result = new AsyncResult<>(ImmutableSet.copyOf(visiblePortletDefinitions));
    Element cacheElement = new Element(user.getUserName(), result);
    marketplaceUserPortletDefinitionCache.put(cacheElement);
    return result;
}
Also used : Element(net.sf.ehcache.Element) ArrayList(java.util.ArrayList) MarketplaceEntry(org.apereo.portal.rest.layout.MarketplaceEntry) ImmutableSet(com.google.common.collect.ImmutableSet) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) HashSet(java.util.HashSet) PortletCategory(org.apereo.portal.portlet.om.PortletCategory) Async(org.springframework.scheduling.annotation.Async)

Example 2 with AsyncResult

use of org.springframework.scheduling.annotation.AsyncResult in project dhis2-core by dhis2.

the class JdbcAnalyticsManager method getAggregatedDataValues.

// -------------------------------------------------------------------------
// AnalyticsManager implementation
// -------------------------------------------------------------------------
@Override
@Async
public Future<Map<String, Object>> getAggregatedDataValues(DataQueryParams params, int maxLimit) {
    try {
        ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap = params.getDataPeriodAggregationPeriodMap();
        if (params.isDisaggregation() && params.hasDataPeriodType()) {
            params = DataQueryParams.newBuilder(params).withDataPeriodsForAggregationPeriods(dataPeriodAggregationPeriodMap).build();
        }
        String sql = getSelectClause(params);
        if (params.spansMultiplePartitions()) {
            sql += getFromWhereClauseMultiplePartitionFilters(params);
        } else {
            sql += getFromWhereClause(params, params.getPartitions().getSinglePartition());
        }
        sql += getGroupByClause(params);
        if (params.isDataType(DataType.NUMERIC) && !params.getMeasureCriteria().isEmpty()) {
            sql += getMeasureCriteriaSql(params);
        }
        log.debug(sql);
        Map<String, Object> map = null;
        try {
            map = getKeyValueMap(params, sql, maxLimit);
        } catch (BadSqlGrammarException ex) {
            log.info("Query failed, likely because the requested analytics table does not exist", ex);
            return new AsyncResult<>(new HashMap<String, Object>());
        }
        replaceDataPeriodsWithAggregationPeriods(map, params, dataPeriodAggregationPeriodMap);
        return new AsyncResult<>(map);
    } catch (RuntimeException ex) {
        log.error(DebugUtils.getStackTrace(ex));
        throw ex;
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) Async(org.springframework.scheduling.annotation.Async)

Example 3 with AsyncResult

use of org.springframework.scheduling.annotation.AsyncResult in project camel by apache.

the class AbstractTestCommand method execute.

// needs to run on a spring background thread
@Async
@Override
public Future<Object> execute(Object[] parameters) throws Exception {
    Assert.assertNotNull("Parameters cannot be null", parameters);
    Assert.assertEquals("Parameters should contain two elements", 2, parameters.length);
    Object configObj = parameters[0];
    Assert.assertNotNull("The first parameter cannot be null", configObj);
    Assert.assertTrue("First parameter should be of type ITestConfig, found type " + configObj.getClass().getName(), configObj instanceof ITestConfig);
    Object compNameObj = parameters[1];
    Assert.assertNotNull("The second parameter cannot be null", compNameObj);
    Assert.assertTrue("Second parameter should be of type String, found type " + compNameObj.getClass().getName(), compNameObj instanceof String);
    String compName = (String) compNameObj;
    ITestConfig config = (ITestConfig) configObj;
    Object result = this.executeTest(config, compName);
    return new AsyncResult<>(result);
}
Also used : ITestConfig(org.apache.camel.itest.springboot.ITestConfig) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) Async(org.springframework.scheduling.annotation.Async)

Aggregations

Async (org.springframework.scheduling.annotation.Async)3 AsyncResult (org.springframework.scheduling.annotation.AsyncResult)3 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Element (net.sf.ehcache.Element)1 ITestConfig (org.apache.camel.itest.springboot.ITestConfig)1 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)1 PortletCategory (org.apereo.portal.portlet.om.PortletCategory)1 MarketplaceEntry (org.apereo.portal.rest.layout.MarketplaceEntry)1 IAuthorizationPrincipal (org.apereo.portal.security.IAuthorizationPrincipal)1 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1