API - Is there a way to retrieve the data of a social component in a structured way?

Avatar

Avatar
Validate 1
Level 1
matthieu_théria
Level 1

Like

1 like

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
matthieu_théria
Level 1

Like

1 like

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
matthieu_théria
Level 1

15-10-2015

Hi,

I have not found how to do what I want in the AEM documentation or in a tutorial...

Is there a way to retrieve the data of a social component in a structured way? I know that I can retrieve specific nodes in the repository, but I need to figure out how the social component is implemented in the repository (nodes structure).

Is it possible to get the forum and its posts for example. What would be the best method to do this (JCR API, Sling, AEM API)?

Thanks,

Matthieu

View Entire Topic

Avatar

Avatar
Boost 5
Employee
kalyanar
Employee

Likes

5 likes

Total Posts

182 posts

Correct reply

78 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Affirm 50
Affirm 5
View profile

Avatar
Boost 5
Employee
kalyanar
Employee

Likes

5 likes

Total Posts

182 posts

Correct reply

78 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Affirm 50
Affirm 5
View profile
kalyanar
Employee

15-10-2015

Please find below a sample that queries for posts posted after a given time. This is just a sample that i wrote to explain to you.

package com.adobe.acs.commons.soco.servlets;

import com.adobe.cq.social.commons.client.api.SocialComponent;
import com.adobe.cq.social.commons.client.api.SocialComponentFactory;
import com.adobe.cq.social.commons.client.api.SocialComponentFactoryManager;
import com.adobe.cq.social.ugc.api.*;
import com.adobe.cq.social.ugcbase.SocialResourceUtils;
import com.adobe.cq.social.ugcbase.SocialUtils;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.*;
import org.apache.felix.scr.annotations.Properties;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.*;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@Service
@Properties({
        @Property(name = "service.description",
                value = "com.adobe.acs.commons.servlets.SampleJSONServlet"),
        @Property(name = "sling.servlet.resourceTypes", value = {"sling/servlet/default"}, propertyPrivate = true),
        @Property(name = "sling.servlet.methods", value = {HttpConstants.METHOD_GET}, propertyPrivate = true),
        @Property(name = "sling.servlet.selectors", value = {"sample"}, propertyPrivate = false),
        @Property(name = "sling.servlet.extensions", value = {"json"}, propertyPrivate = false)})
public class SampleJSONServlet extends SlingSafeMethodsServlet {
    @Reference
    private SocialUtils socialUtils;
    @Reference
    private SocialComponentFactoryManager socialComponentFactoryManager;

    @Reference
    private UgcSearch ugcSearch;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String lastreloadtime = request.getParameter("lrt");
        if("undefined".equals(lastreloadtime)){
            lastreloadtime=null;
        }

        JSONObject obj = new JSONObject();
        JSONArray arr = new JSONArray();
        final ResourceResolver resolver = request.getResourceResolver();
        final Session session = resolver.adaptTo(Session.class);
        PageManager pageManager = resolver.adaptTo(PageManager.class);
        Page page = pageManager.getContainingPage(request.getResource());
        List<SocialComponent> hits = new ArrayList<SocialComponent>();
        try {
            Resource resource = request.getResource();
            if(request.getParameter("lrt")!=null) {
                final UgcFilter ugcFilter = getFilter(request, page.getPath());
                if (ResourceUtil.isSyntheticResource(resource)) {
                    page = pageManager.getContainingPage(resource);
                } else if (SocialResourceUtils.isSocialResource(resource)) {
                    final ValueMap vm = resource.adaptTo(ValueMap.class);
                    final String rootResource = vm.get(SocialUtils.PN_CS_ROOT, "");
                    page = pageManager.getContainingPage(rootResource);

                }
                final SearchResults<Resource> results = ugcSearch.find("ugc-sc", resolver, ugcFilter, 1, 10000);
                for (final Resource res : results.getResults()) {
                    if (res != null) {
                        try {
                            final SocialComponentFactory factory = socialComponentFactoryManager.getSocialComponentFactory(res);


                            SocialComponent component = (factory != null) ? factory.getSocialComponent(res, request)
                                    : null;
                            if (component != null) {
                                arr.put(new JSONObject(component.toJSONString(false)));
                            }
                        } catch (Exception e) {

                        }
                    }
                }
            }
            obj.put("items", arr);
            SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            Date date1 = new Date();

            obj.put("lastreloadtime",  dateFormatGmt.format(new Date()));
            response.setContentType("text/json");
            obj.write(response.getWriter());
        } catch (ParseException e) {
            //e.printStackTrace();
        } catch (RepositoryException e) {
           // e.printStackTrace();
        } catch (JSONException e) {
            //e.printStackTrace();
        }catch(Exception e){
           // e.printStackTrace(System.out);
            e.printStackTrace(response.getWriter());
        }
    }

    private UgcFilter getFilter(SlingHttpServletRequest request,String path) throws ParseException {
        final UgcFilter ugcFilter = new UgcFilter();
        final ConstraintGroup pathFilters = new ConstraintGroup();

        PathConstraint pathConstraint = new PathConstraint(path, PathConstraintType.IsDescendantNode,Operator.And);
        pathFilters.and(pathConstraint);
        SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        String ltr= request.getParameter("lrt");
        ValueConstraint<Long> valueConstraint  = new ValueConstraint<Long>(SocialUtils.PN_DATE,dateFormatGmt.parse(ltr).getTime(),ComparisonType.GreaterThan);
        pathFilters.and(valueConstraint);
        ugcFilter.and(pathFilters);
        return ugcFilter;
    }
}