ClassificationServlet.java 19.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.i18n.I18n
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.impl.classification;

import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.classification.Classification;
import com.adobe.cq.commerce.api.classification.ClassificationAttribute;
import com.adobe.cq.commerce.api.classification.ClassificationCategory;
import com.adobe.cq.commerce.api.classification.ClassificationManager;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
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.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="Adobe CQ Commerce Product Classification Servlet", description="Manages Product Classifications")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.methods", value={"GET", "POST"}), @Property(name="sling.servlet.selectors", value={"productclassification"})})
public class ClassificationServlet
extends SlingAllMethodsServlet {
    private final Logger log = LoggerFactory.getLogger(ClassificationServlet.class);
    private static final String REQUEST_PARAM_OPERATION = ":operation";
    private static final String REQUEST_PARAM_PARENT_PATH = "parentPath";
    private static final String REQUEST_PARAM_PATH = "path";
    private static final String REQUEST_PARAM_TITLE = "title";
    private static final String REQUEST_PARAM_DESC = "description";
    private static final String REQUEST_PARAM_ATTRIBUTE_NAME = "attributeName";
    private static final String REQUEST_PARAM_ATTRIBUTE_UNIT = "attributeUnit";
    private static final String REQUEST_PARAM_BASE_PATH = "basePath";
    private static final String REQUEST_PARAM_MATCH_TAGS = "matchTags";
    private static final String REQUEST_PARAM_QUERY_TEXT = "search";
    private static final String REQUEST_PARAM_QUERY_TYPE = "searchType";
    private static final String CREATE_CLASSIFICATION_OPERATION = "createClassification";
    private static final String UPDATE_CLASSIFICATION_OPERATION = "updateClassification";
    private static final String REMOVE_CLASSIFICATION_OPERATION = "removeClassification";
    private static final String CREATE_ROOT_CATEGORY_OPERATION = "createRootCategory";
    private static final String CREATE_CATEGORY_OPERATION = "createCategory";
    private static final String UPDATE_CATEGORY_OPERATION = "updateCategory";
    private static final String REMOVE_CATEGORY_OPERATION = "removeCategory";
    private static final String ADD_ATTRIBUTE_OPERATION = "addAttribute";
    private static final String UPDATE_ATTRIBUTE_OPERATION = "updateAttribute";
    private static final String REMOVE_ATTRIBUTE_OPERATION = "removeAttribute";
    private static final String ASSIGN_PRODUCTS_OPERATION = "assignProducts";
    @Reference
    private XSSAPI xssApi;

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        XSSAPI xssAPI = this.xssApi.getRequestSpecificAPI(request);
        I18n i18n = new I18n((HttpServletRequest)request);
        Resource res = request.getResource();
        String redirectUrl = request.getContextPath() + res.getPath();
        String parentLocation = request.getContextPath() + res.getParent().getPath();
        HtmlResponse htmlRes = new HtmlResponse(xssAPI, i18n, request.getLocale());
        ResourceResolver resolver = request.getResourceResolver();
        ClassificationManager classificationManager = (ClassificationManager)resolver.adaptTo(ClassificationManager.class);
        String operation = request.getParameter(":operation");
        String parentPath = request.getParameter("parentPath");
        String path = request.getParameter("path");
        String title = request.getParameter("title");
        String description = request.getParameter("description");
        String attributeName = request.getParameter("attributeName");
        String attributeUnit = request.getParameter("attributeUnit");
        String basePath = request.getParameter("basePath");
        String[] matchTags = request.getParameterValues("matchTags");
        String search = request.getParameter("search");
        String searchType = request.getParameter("searchType");
        HashMap<String, Object> properties = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty((String)title)) {
            properties.put("jcr:title", title);
        }
        if (StringUtils.isNotEmpty((String)description)) {
            properties.put("jcr:description", description);
        }
        if (StringUtils.isNotEmpty((String)attributeName)) {
            properties.put("name", attributeName);
        }
        if (StringUtils.isNotEmpty((String)attributeUnit)) {
            properties.put("unit", attributeUnit);
        }
        if (StringUtils.isNotEmpty((String)basePath)) {
            properties.put("basePath", basePath);
        }
        if (matchTags != null && matchTags.length > 0) {
            properties.put("matchTags", matchTags);
        }
        if (StringUtils.isNotEmpty((String)search)) {
            properties.put("search", search);
        }
        if (StringUtils.isNotEmpty((String)searchType)) {
            properties.put("searchType", searchType);
        }
        if (StringUtils.isEmpty((String)operation)) {
            return;
        }
        if ("createClassification".equals(operation)) {
            try {
                String status;
                String name = JcrUtil.createValidName((String)title);
                Classification newClassification = classificationManager.createClassification(parentPath, name, properties);
                String statusTitle = status = i18n.get("Created");
                String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{newClassification.getPath()});
                this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, newClassification.getPath(), request.getContextPath() + newClassification.getPath());
                htmlRes.onCreated(newClassification.getPath());
                htmlRes.setCreateRequest(true);
            }
            catch (CommerceException e) {
                String errorMsg = "Error while creating the new classification.";
                this.log.error(errorMsg);
                String status = errorMsg;
                String statusTitle = errorMsg;
                String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("updateClassification".equals(operation)) {
            try {
                String status;
                classificationManager.updateClassification(path, properties);
                String statusTitle = status = i18n.get("Updated classification");
                String statusDesc = i18n.get("<div id=\"Message\">Updated {0}</div>", "response description", new Object[]{path});
                this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
            catch (Exception e) {
                String errorMsg;
                this.log.error("Could not update the classification {}.", (Object)path);
                String status = errorMsg = "Error while updating the classification.";
                String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                String statusDesc = errorMsg;
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("removeClassification".equals(operation)) {
            try {
                String status;
                classificationManager.removeClassification(path);
                String statusTitle = status = i18n.get("Removed classification");
                String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
                this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
            catch (Exception e) {
                String errorMsg;
                this.log.error("Could not remove the classification {}", (Object)path);
                String status = errorMsg = "Could not remove the classification.";
                String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                String statusDesc = errorMsg;
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("createRootCategory".equals(operation)) {
            try {
                String status;
                ClassificationCategory rootCategory = classificationManager.addRootCategory(path, properties);
                String statusTitle = status = i18n.get("Created");
                String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{rootCategory.getPath()});
                this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, rootCategory.getPath(), request.getContextPath() + rootCategory.getPath());
                htmlRes.onCreated(rootCategory.getPath());
                htmlRes.setCreateRequest(true);
            }
            catch (CommerceException e) {
                String errorMsg = "Error while creating the root category.";
                this.log.error(errorMsg);
                String status = errorMsg;
                String statusTitle = errorMsg;
                String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("createCategory".equals(operation)) {
            try {
                String status;
                String name = JcrUtil.createValidName((String)title);
                ClassificationCategory category = classificationManager.addCategory(parentPath, name, properties);
                String statusTitle = status = i18n.get("Created");
                String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{category.getPath()});
                this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, category.getPath(), request.getContextPath() + category.getPath());
                htmlRes.onCreated(category.getPath());
                htmlRes.setCreateRequest(true);
            }
            catch (CommerceException e) {
                String errorMsg = "Error while creating the sub category.";
                this.log.error(errorMsg);
                String status = errorMsg;
                String statusTitle = errorMsg;
                String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("updateCategory".equals(operation)) {
            try {
                String status;
                classificationManager.updateCategory(path, properties);
                String statusTitle = status = i18n.get("Updated category");
                String statusDesc = i18n.get("<div id=\"Message\">Updated {0}</div>", "response description", new Object[]{path});
                this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
            catch (Exception e) {
                String errorMsg;
                this.log.error("Could not update the category of the classification {}.", (Object)path);
                String status = errorMsg = "Error while updating the category.";
                String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                String statusDesc = errorMsg;
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("removeCategory".equals(operation)) {
            try {
                String status;
                classificationManager.removeCategory(path);
                String statusTitle = status = i18n.get("Removed category");
                String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
                this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
            catch (Exception e) {
                String errorMsg;
                this.log.error("Could not remove the category {}", (Object)path);
                String status = errorMsg = "Could not remove the category.";
                String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                String statusDesc = errorMsg;
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("addAttribute".equals(operation)) {
            try {
                String status;
                String name = JcrUtil.createValidName((String)title);
                ClassificationAttribute attribute = classificationManager.addAttribute(parentPath, name, properties);
                String statusTitle = status = i18n.get("Created");
                String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{attribute.getPath()});
                this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, attribute.getPath(), request.getContextPath() + attribute.getPath());
                htmlRes.onCreated(attribute.getPath());
                htmlRes.setCreateRequest(true);
            }
            catch (CommerceException e) {
                String errorMsg = "Error while creating the attribute.";
                this.log.error(errorMsg);
                String status = errorMsg;
                String statusTitle = errorMsg;
                String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("updateAttribute".equals(operation)) {
            try {
                String status;
                classificationManager.updateAttribute(path, properties);
                String statusTitle = status = i18n.get("Updated attribute");
                String statusDesc = i18n.get("<div id=\"Message\">Updated {0}</div>", "response description", new Object[]{path});
                this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
            catch (Exception e) {
                String errorMsg;
                this.log.error("Could not update the attribute {} of the classification.", (Object)path);
                String status = errorMsg = "Error while updating the category.";
                String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                String statusDesc = errorMsg;
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("removeAttribute".equals(operation)) {
            try {
                String status;
                classificationManager.removeAttribute(path);
                String statusTitle = status = i18n.get("Removed attribute");
                String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
                this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
            catch (Exception e) {
                String errorMsg;
                this.log.error("Could not remove the attribute {}", (Object)path);
                String status = errorMsg = "Could not remove the attribute.";
                String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                String statusDesc = errorMsg;
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        } else if ("assignProducts".equals(operation)) {
            try {
                String status;
                classificationManager.assignProducts(path, properties);
                String statusTitle = status = i18n.get("Assigned products");
                String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
                this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
            catch (Exception e) {
                String errorMsg;
                this.log.error("Could not assign products to the category at {}", (Object)path);
                String status = errorMsg = "Could not assign products.";
                String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
                String statusDesc = errorMsg;
                this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
            }
        }
        htmlRes.send((HttpServletResponse)response, true);
    }

    private void setResponseObject(HtmlResponse response, int statusCode, String status, String title, String desc, String parentLocation, String path, String location) {
        response.setStatus(statusCode, status);
        response.setTitle(title);
        response.setDescription(desc);
        response.setParentLocation(parentLocation);
        response.setLocation(location);
        response.setPath(path);
    }

    protected void bindXssApi(XSSAPI xSSAPI) {
        this.xssApi = xSSAPI;
    }

    protected void unbindXssApi(XSSAPI xSSAPI) {
        if (this.xssApi == xSSAPI) {
            this.xssApi = null;
        }
    }
}