All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.citytechinc.cq.component.touchuidialog.widget.registry.DefaultTouchUIWidgetRegistry Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package com.citytechinc.cq.component.touchuidialog.widget.registry;

import com.citytechinc.cq.component.maven.util.ComponentMojoUtil;
import com.citytechinc.cq.component.maven.util.LogSingleton;
import com.citytechinc.cq.component.util.TouchUIWidgetConfigHolder;
import javassist.ClassPool;
import javassist.NotFoundException;
import org.codehaus.plexus.util.StringUtils;
import org.reflections.Reflections;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DefaultTouchUIWidgetRegistry implements TouchUIWidgetRegistry {

    private static final String DEFAULT_COMPONENT_PACKAGE = "com.citytechinc.cq.component.touchuidialog";

    private final Map, TouchUIWidgetConfigHolder> annotationToWidgetConfigMap;

    public DefaultTouchUIWidgetRegistry(ClassPool classPool, ClassLoader classLoader, Reflections reflections,
        List additionalFeatures) throws NotFoundException, ClassNotFoundException {
        annotationToWidgetConfigMap = new HashMap, TouchUIWidgetConfigHolder>();

        List widgetConfigurations =
            ComponentMojoUtil.getAllTouchUIWidgetAnnotations(classPool, classLoader, reflections);

        /*
         * When two Widget types are configured for the same Widget Annotation, this sort will order using the
         * following rules.
         *
         * 1) Widgets in the default package will come before Widgets in external packages
         * 2) When two Widgets are in the default package, ones with Feature Flags will be ordered after ones without
         * 3) When two Widgets are in external packages, ones with Feature Flags will be ordered after ones without
         *
         * When adding the widget configurations to the configuration Map, Widgets later in the list will
         * override widgets prior in the list.
         */
        Collections.sort(widgetConfigurations, new Comparator() {

            @Override
            public int compare(TouchUIWidgetConfigHolder widgetConfig1, TouchUIWidgetConfigHolder widgetConfig2) {
                if (widgetConfig1.getAnnotationClass() != null && widgetConfig2.getAnnotationClass() != null) {
                    if (widgetConfig1.getAnnotationClass().equals(widgetConfig2.getAnnotationClass())) {
                        if (widgetConfig1.getWidgetClass().getName().startsWith(DEFAULT_COMPONENT_PACKAGE)) {
                            if (widgetConfig2.getWidgetClass().getName().startsWith(DEFAULT_COMPONENT_PACKAGE)) {
                                if (StringUtils.isNotBlank(widgetConfig1.getFeatureFlag())) {
                                    return 1;
                                }
                                if (StringUtils.isNotBlank(widgetConfig2.getFeatureFlag())) {
                                    return -1;
                                }
                            } else {
                                return -1;
                            }
                        } else {
                            if (widgetConfig2.getWidgetClass().getName().startsWith(DEFAULT_COMPONENT_PACKAGE)) {
                                return 1;
                            }
                            if (StringUtils.isNotBlank(widgetConfig1.getFeatureFlag())) {
                                return 1;
                            }
                            if (StringUtils.isNotBlank(widgetConfig2.getFeatureFlag())) {
                                return -1;
                            }
                        }
                    }
                    return widgetConfig1.getAnnotationClass().getName().compareTo(
                        widgetConfig2.getAnnotationClass().getName());
                } else {
                    if (widgetConfig1.getAnnotationClass() == null) {
                        return -1;
                    }

                    return 1;
                }
            }

            //If the widgets do not have the same annotation class we don't care what order they get sorted in.


				/*
				if (widgetConfig1.getWidgetClass().getName().startsWith(DEFAULT_COMPONENT_PACKAGE)) {
					return -1;
				} else {
					return 1;
				}
				*/
        });

        LogSingleton.getInstance().debug("DefaultTouchUIWidgetRegistry - Sorted unmapped list of touch UI widgets");
        for (TouchUIWidgetConfigHolder currentWidgetConfiguration : widgetConfigurations) {
            LogSingleton.getInstance().debug(
                "DefaultTouchUIWidgetRegistry - Widget " + currentWidgetConfiguration.getWidgetClass()
                    .getName() + " for Annotation " + currentWidgetConfiguration.getAnnotationClass()
                    .getName() + " Using Feature " + currentWidgetConfiguration.getFeatureFlag());
            if (currentWidgetConfiguration.getAnnotationClass() != null
                && (StringUtils.isEmpty(currentWidgetConfiguration.getFeatureFlag()) || additionalFeatures
                .contains(currentWidgetConfiguration.getFeatureFlag()))) {
                annotationToWidgetConfigMap.put(currentWidgetConfiguration.getAnnotationClass(),
                    currentWidgetConfiguration);
            }
        }
    }

    @Override
    public TouchUIWidgetConfigHolder getWidgetForAnnotation(Class annotation) {
        return annotationToWidgetConfigMap.get(annotation);
    }

    @Override
    public Set> getRegisteredAnnotations() {
        return annotationToWidgetConfigMap.keySet();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy