package com.adobe.aem.scf.extensions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jcr.Session;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.servlets.post.PostOperation;

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.commons.client.endpoints.AbstractSocialOperation;
import com.adobe.cq.social.commons.client.endpoints.OperationException;
import com.adobe.cq.social.commons.client.endpoints.SocialOperationResult;
import com.adobe.cq.social.forum.client.endpoints.ForumOperations;

@Component(immediate = true)
@Service
@Property(name = PostOperation.PROP_OPERATION_NAME, value = "social:setIdeaStatus")
public class SetStatusOperation extends AbstractSocialOperation implements PostOperation{
    
    @Reference
    private ForumOperations forumService;
    
    @Reference
    private SocialComponentFactoryManager srf;

    @Override
    protected SocialOperationResult performOperation(SlingHttpServletRequest req) throws OperationException {
        final String statusToSet = req.getParameter("status");
        final Resource idea = req.getResource();
        final ValueMap props = idea.adaptTo(ValueMap.class);
        final String[] tags = props.get("cq:tags", new String[]{});
        final List<String> tagList = new ArrayList<String>();
        for(String tag: tags) {
            if(!tag.startsWith("acmeideas:")) {
                tagList.add(tag);
            }
        }
        tagList.add(statusToSet);
        Map<String,Object> updates = new HashMap<String,Object>();
        updates.put("cq:tags", tagList.toArray(new String[]{}));
        Resource updatedResource = forumService.update(req.getResource(), updates, null, req.getResourceResolver().adaptTo(Session.class));
        return new SocialOperationResult(this.getSocialComponentForResource(updatedResource, req), 200, updatedResource.getPath());
    }

    private SocialComponent getSocialComponentForResource(Resource newProject, SlingHttpServletRequest request) {
        if (newProject == null) {
            return null;
        }
        final SocialComponentFactory factory = this.srf.getSocialComponentFactory(newProject);
        return factory.getSocialComponent(newProject, request);
    }

}

Add an SCF Operation endpoint to set the status of an operation
Add an SCF Operation endpoint to set the status of an operation

Implement an AbstractSocialOperation to create an endpoint that listens
for operations of type "social:setIdeaStatus"
Use the ForumOperations SCF Operations Service to update an idea with
the requested status
Return a SocialOperationResult that contains the SocialComponent of the
idea with appropriate status code. The SocailComponent is translated to
JSON as part of the response automatically by SCF
