Search in sources :

Example 46 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class ForumTopicsHotGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    // They shouldn't be trying to list of an existing Post or Topic
    if (topic != null || post != null) {
        String error = "Can't list Topics inside an existing Topic or Post";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Grab the date range to search over
    String numDaysS = req.getParameter("numdays");
    int numDays = RECENT_POSTS_PERIOD_DAYS;
    if (numDaysS != null) {
        numDays = Integer.parseInt(numDaysS);
    }
    Date now = new Date();
    Date since = new Date(now.getTime() - numDays * ONE_DAY_MS);
    // Get the topics with recent replies
    PagingResults<Pair<TopicInfo, Integer>> topicsAndCounts = null;
    PagingRequest paging = buildPagingRequest(req);
    if (site != null) {
        topicsAndCounts = discussionService.listHotTopics(site.getShortName(), since, paging);
    } else {
        topicsAndCounts = discussionService.listHotTopics(nodeRef, since, paging);
    }
    // For this, we actually only need the topics, not their counts too
    List<TopicInfo> topics = new ArrayList<TopicInfo>();
    for (Pair<TopicInfo, Integer> tc : topicsAndCounts.getPage()) {
        topics.add(tc.getFirst());
    }
    // been created yet, use the site for the permissions checking
    if (site != null && nodeRef == null) {
        nodeRef = site.getNodeRef();
    }
    // Build the common model parts
    Map<String, Object> model = buildCommonModel(site, topic, post, req);
    model.put("forum", nodeRef);
    // Have the topics rendered
    model.put("data", renderTopics(topics, topicsAndCounts.getTotalResultCount(), paging, site));
    // All done
    return model;
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) PagingRequest(org.alfresco.query.PagingRequest) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) Pair(org.alfresco.util.Pair)

Example 47 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class ResourceInspector method inspectOperations.

/**
 * Inspect a resource to find operations on it.
 * @param api Api
 * @param entityPath String
 * @param metainfo resource metadata
 */
public static void inspectOperations(Api api, Class<?> resource, final String entityPath, List<ResourceMetadata> metainfo) {
    Map<String, Pair<ResourceOperation, Method>> operations = findOperations(entityPath, resource);
    if (operations != null && !operations.isEmpty()) {
        for (Entry<String, Pair<ResourceOperation, Method>> opera : operations.entrySet()) {
            Method annotatedMethod = opera.getValue().getSecond();
            final boolean isNoAuthRequired = isNoAuth(annotatedMethod);
            if (isDeleted(annotatedMethod)) {
                metainfo.add(new OperationResourceMetaData(opera.getKey(), api, new HashSet(Arrays.asList(opera.getValue().getFirst())), isNoAuthRequired));
            } else {
                metainfo.add(new OperationResourceMetaData(opera.getKey(), Arrays.asList(opera.getValue().getFirst()), api, annotatedMethod, isNoAuthRequired));
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) Pair(org.alfresco.util.Pair) HashSet(java.util.HashSet)

Example 48 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class ResourceInspector method findEmbeddedResources.

/**
 * For a given class, looks for @EmbeddedEntityResource annotations, using the annotation produce
 * a Map of the property name key and the entity key
 * @return A map of property key name and a value of the entity path name
 */
public static Map<String, Pair<String, Method>> findEmbeddedResources(Class<?> anyClass) {
    Map<String, Pair<String, Method>> embeds = new HashMap<String, Pair<String, Method>>();
    List<Method> annotatedMethods = ResourceInspectorUtil.findMethodsByAnnotation(anyClass, EmbeddedEntityResource.class);
    if (annotatedMethods != null && !annotatedMethods.isEmpty()) {
        for (Method annotatedMethod : annotatedMethods) {
            Annotation annot = AnnotationUtils.findAnnotation(annotatedMethod, EmbeddedEntityResource.class);
            if (annot != null) {
                Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
                String entityPath = findEntityNameByAnnotationAttributes(annotAttribs);
                String key = String.valueOf(annotAttribs.get("propertyName"));
                embeds.put(key, new Pair<String, Method>(entityPath, annotatedMethod));
            }
        }
    }
    return embeds;
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) Annotation(java.lang.annotation.Annotation) Pair(org.alfresco.util.Pair)

Example 49 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class ResourceWebScriptHelper method processAdditionsToTheResponse.

/**
 * Looks at the object passed in and recursively expands any @EmbeddedEntityResource annotations or related relationship.
 * {@link org.alfresco.rest.framework.resource.EmbeddedEntityResource EmbeddedEntityResource} is expanded by calling the ReadById method for this entity.
 *
 * Either returns a ExecutionResult object or a CollectionWithPagingInfo containing a collection of ExecutionResult objects.
 *
 * @param api Api
 * @param entityCollectionName String
 * @param params  Params
 * @param objectToWrap Object
 * @return Object - Either ExecutionResult or CollectionWithPagingInfo<ExecutionResult>
 */
public Object processAdditionsToTheResponse(WebScriptResponse res, Api api, String entityCollectionName, Params params, Object objectToWrap) {
    PropertyCheck.mandatory(this, null, params);
    if (objectToWrap == null)
        return null;
    if (objectToWrap instanceof CollectionWithPagingInfo<?>) {
        CollectionWithPagingInfo<?> collectionToWrap = (CollectionWithPagingInfo<?>) objectToWrap;
        Object sourceEntity = executeIncludedSource(api, params, entityCollectionName, collectionToWrap);
        Collection<Object> resultCollection = new ArrayList(collectionToWrap.getCollection().size());
        if (!collectionToWrap.getCollection().isEmpty()) {
            for (Object obj : collectionToWrap.getCollection()) {
                resultCollection.add(processAdditionsToTheResponse(res, api, entityCollectionName, params, obj));
            }
        }
        return CollectionWithPagingInfo.asPaged(collectionToWrap.getPaging(), resultCollection, collectionToWrap.hasMoreItems(), collectionToWrap.getTotalItems(), sourceEntity, collectionToWrap.getContext());
    } else {
        if (BeanUtils.isSimpleProperty(objectToWrap.getClass()) || objectToWrap instanceof Collection) {
            // Simple property or Collection that can't be embedded so just return it.
            return objectToWrap;
        }
        final ExecutionResult execRes = new ExecutionResult(objectToWrap, params.getFilter());
        Map<String, Pair<String, Method>> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass());
        if (embeddded != null && !embeddded.isEmpty()) {
            Map<String, Object> results = executeEmbeddedResources(api, params, objectToWrap, embeddded);
            execRes.addEmbedded(results);
        }
        if (params.getRelationsFilter() != null && !params.getRelationsFilter().isEmpty()) {
            Map<String, ResourceWithMetadata> relationshipResources = locator.locateRelationResource(api, entityCollectionName, params.getRelationsFilter().keySet(), HttpMethod.GET);
            String uniqueEntityId = ResourceInspector.findUniqueId(objectToWrap);
            Map<String, Object> relatedResources = executeRelatedResources(api, params, relationshipResources, uniqueEntityId);
            execRes.addRelated(relatedResources);
        }
        return execRes;
    }
}
Also used : ArrayList(java.util.ArrayList) CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) Collection(java.util.Collection) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Pair(org.alfresco.util.Pair)

Example 50 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class RepoService method getFavouriteSites.

private PagingResults<SiteInfo> getFavouriteSites(String userName, PagingRequest pagingRequest) {
    final Collator collator = Collator.getInstance();
    final Set<SiteInfo> sortedFavouriteSites = new TreeSet<SiteInfo>(new Comparator<SiteInfo>() {

        @Override
        public int compare(SiteInfo o1, SiteInfo o2) {
            return collator.compare(o1.getTitle(), o2.getTitle());
        }
    });
    Map<String, Serializable> prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX);
    for (String key : prefs.keySet()) {
        boolean isFavourite = false;
        Serializable s = prefs.get(key);
        if (s instanceof Boolean) {
            isFavourite = (Boolean) s;
        }
        if (isFavourite) {
            String siteShortName = key.substring(FAVOURITE_SITES_PREFIX_LENGTH);
            SiteInfo siteInfo = siteService.getSite(siteShortName);
            if (siteInfo != null) {
                sortedFavouriteSites.add(siteInfo);
            }
        }
    }
    int totalSize = sortedFavouriteSites.size();
    final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
    final List<SiteInfo> page = new ArrayList<SiteInfo>(pageDetails.getPageSize());
    Iterator<SiteInfo> it = sortedFavouriteSites.iterator();
    for (int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) {
        SiteInfo favouriteSite = it.next();
        if (counter < pageDetails.getSkipCount()) {
            continue;
        }
        if (counter > pageDetails.getEnd() - 1) {
            break;
        }
        page.add(favouriteSite);
    }
    return new PagingResults<SiteInfo>() {

        @Override
        public List<SiteInfo> getPage() {
            return page;
        }

        @Override
        public boolean hasMoreItems() {
            return pageDetails.hasMoreItems();
        }

        @Override
        public Pair<Integer, Integer> getTotalResultCount() {
            Integer total = Integer.valueOf(sortedFavouriteSites.size());
            return new Pair<Integer, Integer>(total, total);
        }

        @Override
        public String getQueryExecutionId() {
            return null;
        }
    };
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) Serializable(java.io.Serializable) PagingResults(org.alfresco.query.PagingResults) ArrayList(java.util.ArrayList) Collator(java.text.Collator) PageDetails(org.alfresco.query.PageDetails) TreeSet(java.util.TreeSet) Pair(org.alfresco.util.Pair)

Aggregations

Pair (org.alfresco.util.Pair)61 ArrayList (java.util.ArrayList)34 NodeRef (org.alfresco.service.cmr.repository.NodeRef)23 QName (org.alfresco.service.namespace.QName)22 HashMap (java.util.HashMap)16 PagingRequest (org.alfresco.query.PagingRequest)13 Paging (org.alfresco.rest.framework.resource.parameters.Paging)12 List (java.util.List)11 Serializable (java.io.Serializable)10 HashSet (java.util.HashSet)9 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)9 Set (java.util.Set)8 Map (java.util.Map)7 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)7 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)7 AbstractList (java.util.AbstractList)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 PagingResults (org.alfresco.query.PagingResults)6 Node (org.alfresco.rest.api.model.Node)6