FormsManagerImpl.java 5.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.foundation.forms.impl;

import com.day.cq.wcm.foundation.forms.FormsManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FormsManagerImpl
implements FormsManager {
    private final Logger logger;
    private ResourceResolver resolver;
    private String[] searchPaths;

    protected FormsManagerImpl(ResourceResolver resolver) {
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.resolver = resolver;
        this.searchPaths = resolver.getSearchPath();
        for (int i = 0; i < this.searchPaths.length; ++i) {
            this.searchPaths[i] = this.searchPaths[i].substring(0, this.searchPaths[i].length() - 1);
        }
    }

    @Override
    public Iterator<FormsManager.ComponentDescription> getActions() {
        return this.search("foundation/components/form/action").iterator();
    }

    @Override
    public Iterator<FormsManager.ComponentDescription> getConstraints() {
        return this.search("foundation/components/form/constraint").iterator();
    }

    @Override
    public String getDialogPathForAction(String resourceType) {
        if (resourceType == null) {
            return null;
        }
        for (String path : this.searchPaths) {
            String dialogPath = path + '/' + resourceType + "/dialog";
            if (this.resolver.getResource(dialogPath) == null) continue;
            return dialogPath;
        }
        return null;
    }

    private Collection<FormsManager.ComponentDescription> search(String type) {
        HashMap<String, ComponentDescriptionImpl> map = new HashMap<String, ComponentDescriptionImpl>();
        ArrayList<String> disabledComponents = new ArrayList<String>();
        for (String path : this.searchPaths) {
            StringBuilder buffer = new StringBuilder("/jcr:root");
            buffer.append(path);
            buffer.append("//* [@");
            buffer.append("sling:resourceType");
            buffer.append("='");
            buffer.append(type);
            buffer.append("']");
            this.logger.debug("Query: {}", (Object)buffer.toString());
            Iterator i = this.resolver.findResources(buffer.toString(), "xpath");
            while (i.hasNext()) {
                Resource rsrc = (Resource)i.next();
                ValueMap properties = ResourceUtil.getValueMap((Resource)rsrc);
                String rt = rsrc.getPath().substring(path.length() + 1);
                if (((Boolean)properties.get("enabled", (Object)Boolean.TRUE)).booleanValue()) {
                    if (map.containsKey(rt) || disabledComponents.contains(rt)) continue;
                    map.put(rt, new ComponentDescriptionImpl(rt, ResourceUtil.getName((Resource)rsrc), properties));
                    continue;
                }
                disabledComponents.add(rt);
            }
        }
        ArrayList<FormsManager.ComponentDescription> entries = new ArrayList<FormsManager.ComponentDescription>(map.values());
        Collections.sort(entries);
        return entries;
    }

    public static final class ComponentDescriptionImpl
    implements FormsManager.ComponentDescription,
    Comparable<FormsManager.ComponentDescription> {
        private final String resourceType;
        private final String title;
        private final String hint;
        private final int order;

        public ComponentDescriptionImpl(String rt, String defaultName, ValueMap props) {
            this.resourceType = rt;
            this.title = (String)props.get("jcr:title", (Object)defaultName);
            this.order = (Integer)props.get("order", (Object)0);
            this.hint = (String)props.get("hint", String.class);
        }

        @Override
        public String getResourceType() {
            return this.resourceType;
        }

        @Override
        public String getTitle() {
            return this.title;
        }

        public int getOrder() {
            return this.order;
        }

        @Override
        public String getHint() {
            return this.hint;
        }

        @Override
        public int compareTo(FormsManager.ComponentDescription o) {
            ComponentDescriptionImpl obj = (ComponentDescriptionImpl)o;
            if (this.order < obj.order) {
                return -1;
            }
            if (this.order == obj.order) {
                return this.title.compareTo(obj.title);
            }
            return 1;
        }
    }

}