ClassificationImpl.java 2.65 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  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.ClassificationCategory;
import com.adobe.cq.commerce.impl.classification.ClassificationCategoryImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClassificationImpl
implements Classification {
    protected static final String NN_ROOT_CATEGORY = "rootcategory";
    protected final Logger log = LoggerFactory.getLogger(ClassificationImpl.class);
    protected Resource resource;
    protected ValueMap properties;

    public ClassificationImpl(Resource resource) throws CommerceException {
        if (!this.isClassification(resource)) {
            throw new CommerceException("Resource is not a Classification.");
        }
        this.resource = resource;
        this.properties = (ValueMap)resource.adaptTo(ValueMap.class);
    }

    @Override
    public String getTitle() {
        return (String)this.properties.get("jcr:title", (Object)"");
    }

    @Override
    public String getDescription() {
        return (String)this.properties.get("jcr:description", (Object)"");
    }

    @Override
    public String getPath() {
        return this.resource.getPath();
    }

    @Override
    public ClassificationCategory getRootCategory() {
        ClassificationCategoryImpl rootCategory = null;
        Resource rootCategoryRes = this.resource.getChild("rootcategory");
        try {
            rootCategory = new ClassificationCategoryImpl(rootCategoryRes);
        }
        catch (CommerceException e) {
            this.log.error("The resource at " + rootCategoryRes.getPath() + " is not a category", (Throwable)e);
        }
        return rootCategory;
    }

    private boolean isClassification(Resource resource) {
        return this.isCommerceType(resource, "classification");
    }

    private boolean isCommerceType(Resource res, String value) {
        if (res == null) {
            return false;
        }
        ValueMap properties = (ValueMap)res.adaptTo(ValueMap.class);
        String commerceType = (String)properties.get("cq:commerceType", String.class);
        return StringUtils.equals((String)commerceType, (String)value);
    }
}