All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.social.enablement.resource.endpoints.api.AbstractEnablementResourceModelOperationService Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2015 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.cq.social.enablement.resource.endpoints.api;

import java.util.Map;

import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.social.enablement.resource.model.api.EnablementResourceModel;
import com.adobe.cq.social.scf.Operation;
import com.adobe.cq.social.scf.OperationException;
import com.adobe.cq.social.scf.OperationExtension;
import com.adobe.cq.social.scf.SocialComponent;
import com.adobe.cq.social.scf.core.operations.AbstractOperationService;

/**
 * Provides abstract implementation of the EnablementResourceModel operation..
 * @param  is a {@link OperationExtension} that will be used as hooks by the extending class.
 * @param  is a {@link Operation} that is being provided by the extending class.
 */
@SuppressWarnings("rawtypes")
@Component(metatype = false, componentAbstract = true)
@Properties({@Property(name = AbstractEnablementResourceModelOperationService.PROPERTY_FIELD_WHITELIST, value = {},
        description = "List of allowed whitelisted custom properties", cardinality = 100)})
public abstract class AbstractEnablementResourceModelOperationService
    extends AbstractOperationService implements EnablementResourceModelOperations {

    /**
     * Logger.
     */
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Reference
    private EnablementResourceModelServiceCreate enablementResourceModelOperationCreateService;

    @Reference
    private EnablementResourceModelServiceUpdate enablementResourceModelOperationUpdateService;

    @Reference
    private EnablementResourceModelServiceDelete enablementResourceModelOperationDeleteService;

    @Reference
    private EnablementResourceModelServiceReport enablementResourceModelOperationReportService;

    @Reference
    private EnablementResourceModelServicePublish enablementResourceModelOperationPublishService;

    /**
     * Custom white list of input parameters
     */
    protected String[] fieldWhitelist;
    protected static final String PROPERTY_FIELD_WHITELIST = "fieldWhitelist";

    /**
     * Activate this component. Open the session and register event listeners.
     * @param context The component context
     */
    @Activate
    protected void activate(final ComponentContext context) {
        Object whitelist = context.getProperties().get(PROPERTY_FIELD_WHITELIST);
        fieldWhitelist = PropertiesUtil.toStringArray(whitelist);
    }

    // /
    // / Report operation
    // /
    @Override
    public SocialComponent report(final SlingHttpServletRequest request) throws OperationException {

        try {

            LOGGER.debug("Enablement Resource Model Report operation.");

            Map properties =
                enablementResourceModelOperationReportService.getOperationProperties(request, fieldWhitelist);

            final U reportOperation = getReportOperation();
            final Resource parentResource = request.getResource();
            final ResourceResolver resolver = parentResource.getResourceResolver();
            final Session session = resolver.adaptTo(Session.class);
            performBeforeActions(reportOperation, session, parentResource, properties);

            EnablementResourceModel enablementResourceModel =
                enablementResourceModelOperationReportService.report(request, properties);

            if (enablementResourceModel != null) {
                performAfterActions(reportOperation, session, enablementResourceModel, properties);
                return enablementResourceModel;
            }

        } catch (final Exception e) {
            throw new OperationException("Internal error creating enablementResourceModel.", e,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        // Get here if we are not able to perform the request
        throw new OperationException("Unable to obtain EnablementResourceModel component.",
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }

    // /
    // / Publish operation
    // /
    @Override
    public void delete(final SlingHttpServletRequest request) throws OperationException {

        try {

            LOGGER.debug("Enablement Resource Model Publish/Unpublish operation.");

            Map properties =
                enablementResourceModelOperationDeleteService.getOperationProperties(request, fieldWhitelist);

            final U deleteOperation = getDeleteOperation();
            final Resource parentResource = request.getResource();
            final ResourceResolver resolver = parentResource.getResourceResolver();
            final Session session = resolver.adaptTo(Session.class);
            performBeforeActions(deleteOperation, session, parentResource, properties);

            enablementResourceModelOperationDeleteService.delete(request, properties);

            performAfterActions(deleteOperation, session, null, properties);

        } catch (final Exception e) {
            throw new OperationException("Internal error deleting enablementResourceModel.", e,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    // /
    // / Publish operation
    // /
    @Override
    public SocialComponent publish(final SlingHttpServletRequest request) throws OperationException {

        try {

            LOGGER.debug("Enablement Resource Model Publish/Unpublish operation.");

            Map properties =
                enablementResourceModelOperationPublishService.getOperationProperties(request, fieldWhitelist);

            final U publishOperation = getPublishOperation();
            final Resource parentResource = request.getResource();
            final ResourceResolver resolver = parentResource.getResourceResolver();
            final Session session = resolver.adaptTo(Session.class);
            performBeforeActions(publishOperation, session, parentResource, properties);

            EnablementResourceModel enablementResourceModel =
                enablementResourceModelOperationPublishService.publish(request, properties);

            if (enablementResourceModel != null) {
                performAfterActions(publishOperation, session, enablementResourceModel, properties);
                return enablementResourceModel;
            }

        } catch (final Exception e) {
            throw new OperationException("Internal error publishing/unpublishing enablementResourceModel.", e,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        // Get here if we are not able to perform the request
        throw new OperationException("Unable to obtain EnablementResourceModel component.",
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }

    // /
    // / Create operation
    // /
    @Override
    public SocialComponent create(final SlingHttpServletRequest request) throws OperationException {

        try {

            LOGGER.debug("Enablement Resource Model Create operation.");

            Map properties =
                enablementResourceModelOperationCreateService.getOperationProperties(request, fieldWhitelist);

            final U createOperation = getCreateOperation();
            final Resource parentResource = request.getResource();
            final ResourceResolver resolver = parentResource.getResourceResolver();
            final Session session = resolver.adaptTo(Session.class);
            performBeforeActions(createOperation, session, parentResource, properties);

            EnablementResourceModel enablementResourceModel =
                enablementResourceModelOperationCreateService.create(request, properties);

            if (enablementResourceModel != null) {
                performAfterActions(createOperation, session, enablementResourceModel, properties);
                return enablementResourceModel;
            }

        } catch (final Exception e) {
            throw new OperationException("Internal error creating enablementResourceModel.", e,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        // Get here if we are not able to perform the request
        throw new OperationException("Unable to obtain EnablementResourceModel component.",
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }

    @Override
    public SocialComponent update(final SlingHttpServletRequest request) throws OperationException {

        try {

            LOGGER.debug("Enablement Resource Model Create operation.");

            Map properties =
                enablementResourceModelOperationUpdateService.getOperationProperties(request, fieldWhitelist);

            final U updateOperation = getUpdateOperation();
            final Resource parentResource = request.getResource();
            final ResourceResolver resolver = parentResource.getResourceResolver();
            final Session session = resolver.adaptTo(Session.class);
            performBeforeActions(updateOperation, session, parentResource, properties);

            EnablementResourceModel enablementResourceModel =
                enablementResourceModelOperationUpdateService.update(request, properties, fieldWhitelist);

            if (enablementResourceModel != null) {
                performAfterActions(updateOperation, session, enablementResourceModel, properties);
                return enablementResourceModel;
            }

        } catch (final Exception e) {
            throw new OperationException("Internal error modify enablementResourceModel.", e,
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        // Get here if we are not able to perform the request
        throw new OperationException("Unable to obtain EnablementResourceModel component.",
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }

    protected String getResourceType() {
        return EnablementResourceModel.RESOURCE_TYPE;
    }

    protected abstract U getCreateOperation();

    protected abstract U getUpdateOperation();

    protected abstract U getDeleteOperation();

    protected abstract U getReportOperation();

    protected abstract U getPublishOperation();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy