SimplePropertyDefinition.java 4.38 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.mobile.index.impl.builder;

import com.adobe.cq.mobile.index.impl.builder.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 {
    private String nodeName;
    private String property;
    private String propertyType;
    private boolean propertyIndex;
    private float boost = 1.0f;
    private boolean nodeScopeIndex;
    private boolean analyzed;
    private boolean useInSuggest;
    private boolean useInSpellcheck;
    private boolean ordered;

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

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

    public SimplePropertyDefinition(String property, String nodeName, String propertyType, boolean nodeScopeIndex, boolean analyzed) {
        this(property, nodeName, propertyType);
        this.setNodeScopeIndex(nodeScopeIndex);
        this.setAnalyzed(analyzed);
    }

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

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

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

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

    public SimplePropertyDefinition setNodeScopeIndex(boolean nodeScopeIndex) {
        this.nodeScopeIndex = nodeScopeIndex;
        return this;
    }

    public SimplePropertyDefinition setBoost(float boost) {
        this.boost = boost;
        return this;
    }

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

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

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

    public SimplePropertyDefinition setOrdered(boolean ordered) {
        this.ordered = ordered;
        return this;
    }

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

    @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.ordered) {
            propsMap.put("ordered", true);
        }
        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));
        }
        if (this.nodeScopeIndex) {
            propsMap.put("nodeScopeIndex", this.nodeScopeIndex);
        }
        if (this.propertyType != null && !this.propertyType.isEmpty()) {
            propsMap.put("type", this.propertyType);
        }
        resolver.create(resource, this.nodeName, propsMap);
    }
}