SimplePropertyDefinition.java 4.05 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 */
package com.adobe.cq.dam.index.builder;

import com.adobe.cq.dam.index.builder.api.PropertyDefinition;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

public class SimplePropertyDefinition
implements PropertyDefinition {
    String nodeName;
    String property;
    String propertyType;
    boolean propertyIndex;
    float boost = 1.0f;
    boolean nodeScopeIndex;
    boolean analyzed;
    boolean useInSuggest;
    boolean useInSpellcheck;

    public SimplePropertyDefinition(String property, String nodeName) {
        this.setNodeName(nodeName);
        this.setProperty(property);
        this.setPropertyIndex(true);
    }

    public SimplePropertyDefinition(String property, String nodeName, float boost) {
        this(property, nodeName);
        this.setBoost(boost);
    }

    public SimplePropertyDefinition(String property, String nodeName, String propertyType) {
        this(property, nodeName);
        this.setPropertyType(propertyType);
    }

    public SimplePropertyDefinition(String property, String nodeName, float boost, boolean analyzed) {
        this(property, nodeName, boost);
        this.setAnalyzed(analyzed);
    }

    public SimplePropertyDefinition(String property, String nodeName, float boost, boolean analyzed, boolean useInSuggest) {
        this(property, nodeName, boost);
        this.setAnalyzed(analyzed);
        this.setUseInSuggest(useInSuggest);
    }

    public SimplePropertyDefinition(String property, String nodeName, float boost, boolean analyzed, boolean useInSuggest, boolean useInSpellcheck) {
        this(property, nodeName, boost);
        this.setAnalyzed(analyzed);
        this.setUseInSuggest(useInSuggest);
        this.setUseInSpellcheck(useInSpellcheck);
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public void setProperty(String prop) {
        this.property = prop;
    }

    public void setPropertyType(String type) {
        this.propertyType = type;
    }

    private void setPropertyIndex(boolean propertyIndex) {
        this.propertyIndex = propertyIndex;
    }

    public void setBoost(float boost) {
        this.boost = boost;
        this.nodeScopeIndex = true;
    }

    public void setAnalyzed(boolean analyzed) {
        this.analyzed = analyzed;
    }

    public void setUseInSuggest(boolean useInSuggest) {
        this.useInSuggest = useInSuggest;
    }

    public void setUseInSpellcheck(boolean useInSpellcheck) {
        this.useInSpellcheck = useInSpellcheck;
    }

    @Override
    public void build(Resource resource) throws PersistenceException {
        ResourceResolver resolver = resource.getResourceResolver();
        if (resolver.getResource(resource, this.nodeName) != null) {
            resolver.delete(resolver.getResource(resource, this.nodeName));
        }
        HashMap<String, Object> propsMap = new HashMap<String, Object>();
        propsMap.put("name", this.property);
        propsMap.put("propertyIndex", this.propertyIndex);
        if (this.analyzed) {
            propsMap.put("analyzed", true);
        }
        if (this.useInSuggest) {
            propsMap.put("useInSuggest", true);
            propsMap.put("nodeScopeIndex", true);
        }
        if (this.useInSpellcheck) {
            propsMap.put("useInSpellcheck", true);
            propsMap.put("nodeScopeIndex", true);
        }
        if (this.boost != 1.0f) {
            propsMap.put("boost", Float.valueOf(this.boost));
            propsMap.put("nodeScopeIndex", this.nodeScopeIndex);
        }
        if (this.propertyType != null && !this.propertyType.isEmpty()) {
            propsMap.put("type", this.propertyType);
        }
        resolver.create(resource, this.nodeName, propsMap);
    }
}