ClassificationAttributeImpl.java 2.22 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.ClassificationAttribute;
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 ClassificationAttributeImpl
implements ClassificationAttribute {
    protected final Logger log = LoggerFactory.getLogger(ClassificationAttributeImpl.class);
    protected Resource resource;
    protected ValueMap properties;

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

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

    @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 getName() {
        return (String)this.properties.get("name", (Object)"");
    }

    @Override
    public String getUnit() {
        return (String)this.properties.get("unit", (Object)"");
    }

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

    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);
    }
}