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

com.citytechinc.cq.component.touchuidialog.util.TouchUIDialogUtil Maven / Gradle / Ivy

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

import com.citytechinc.cq.component.annotations.Component;
import com.citytechinc.cq.component.annotations.DialogField;
import com.citytechinc.cq.component.annotations.HideDialogField;
import com.citytechinc.cq.component.annotations.IgnoreDialogField;
import com.citytechinc.cq.component.annotations.IncludeDialogFields;
import com.citytechinc.cq.component.annotations.widgets.Selection;
import com.citytechinc.cq.component.dialog.ComponentNameTransformer;
import com.citytechinc.cq.component.dialog.DialogFieldConfig;
import com.citytechinc.cq.component.dialog.exception.InvalidComponentClassException;
import com.citytechinc.cq.component.dialog.exception.InvalidComponentFieldException;
import com.citytechinc.cq.component.dialog.util.DialogUtil;
import com.citytechinc.cq.component.maven.util.ComponentMojoUtil;
import com.citytechinc.cq.component.touchuidialog.TouchUIDialog;
import com.citytechinc.cq.component.touchuidialog.exceptions.TouchUIDialogGenerationException;
import com.citytechinc.cq.component.touchuidialog.exceptions.TouchUIDialogWriteException;
import com.citytechinc.cq.component.touchuidialog.factory.TouchUIDialogFactory;
import com.citytechinc.cq.component.touchuidialog.widget.DefaultTouchUIWidgetParameters;
import com.citytechinc.cq.component.touchuidialog.widget.maker.TouchUIWidgetMakerParameters;
import com.citytechinc.cq.component.touchuidialog.widget.radiogroup.RadioGroupWidget;
import com.citytechinc.cq.component.touchuidialog.widget.registry.TouchUIWidgetRegistry;
import com.citytechinc.cq.component.touchuidialog.widget.selection.options.Option;
import com.citytechinc.cq.component.touchuidialog.widget.selection.options.OptionParameters;
import com.citytechinc.cq.component.util.ComponentUtil;
import com.citytechinc.cq.component.xml.XmlElement;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMember;
import javassist.CtMethod;
import javassist.NotFoundException;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class TouchUIDialogUtil {

    private static final String OPTION_FIELD_NAME_PREFIX = "option";

    private TouchUIDialogUtil() {

    }

    public static List buildDialogsFromClassList(List classList, ClassLoader classLoader,
        ClassPool classPool, TouchUIWidgetRegistry widgetRegistry, ComponentNameTransformer transformer,
        File buildDirectory, String componentPathBase, String defaultComponentPathSuffix,
        ZipArchiveOutputStream archiveStream, Set reservedNames, String touchUIDialogType)
        throws TouchUIDialogGenerationException,
        TouchUIDialogWriteException, ClassNotFoundException, NotFoundException, InvalidComponentClassException {

        List dialogList = new ArrayList();

        for (CtClass currentComponentClass : classList) {
            TouchUIDialog currentDialog =
                TouchUIDialogFactory.make(currentComponentClass, classLoader, classPool, widgetRegistry,
                    touchUIDialogType);

            if (currentDialog != null && isWidgetInComponentClass(currentComponentClass)) {
                File currentDialogOutput =
                    writeDialogToFile(transformer, currentDialog, currentComponentClass, buildDirectory,
                        componentPathBase, defaultComponentPathSuffix);
                writeDialogToArchiveFile(transformer, currentDialogOutput, currentComponentClass, archiveStream,
                    reservedNames, componentPathBase, defaultComponentPathSuffix);
                dialogList.add(currentDialog);
            }
        }

        return dialogList;
    }

    public static File writeDialogToFile(ComponentNameTransformer transformer, TouchUIDialog dialog,
        CtClass componentClass, File buildDirectory, String componentPathBase, String defaultComponentPathSuffix)
        throws TouchUIDialogWriteException {
        try {
            return ComponentMojoUtil.writeElementToFile(transformer, dialog, componentClass, buildDirectory,
                componentPathBase, defaultComponentPathSuffix, dialog.getFileName());
        } catch (Exception e) {
            throw new TouchUIDialogWriteException("Exception encountered writing Dialog to File", e);
        }
    }

    public static void writeDialogToArchiveFile(ComponentNameTransformer transformer, File dialogFile,
        CtClass componentClass, ZipArchiveOutputStream archiveStream, Set reservedNames,
        String componentPathBase, String defaultComponentPathSuffix) throws TouchUIDialogWriteException {

        try {
            ComponentMojoUtil.writeElementToArchiveFile(transformer, dialogFile, componentClass, archiveStream,
                reservedNames, componentPathBase, defaultComponentPathSuffix, "/" + dialogFile.getName());
        } catch (Exception e) {
            throw new TouchUIDialogWriteException("Exception encountered while writing Dialog File to Archive", e);
        }

    }

    public static List getWidgetMakerParametersForComponentClass(CtClass componentClass,
        ClassLoader classLoader, ClassPool classPool, TouchUIWidgetRegistry widgetRegistry, String touchUIDialogType)
        throws NotFoundException,
        ClassNotFoundException, InvalidComponentClassException, InvalidComponentFieldException {

        List widgetMakerParametersList = new ArrayList();

        List fieldsAndMethods = new ArrayList();

        Component componentAnnotation = (Component) componentClass.getAnnotation(Component.class);

        fieldsAndMethods.addAll(ComponentMojoUtil.collectFields(componentClass,
            componentAnnotation.suppressFieldInheritanceForTouchUI()));
        fieldsAndMethods.addAll(ComponentMojoUtil.collectMethods(componentClass,
            componentAnnotation.suppressFieldInheritanceForTouchUI()));

        // Load the true class
        Class trueComponentClass = classLoader.loadClass(componentClass.getName());

        // Iterate through all the fields creating configs for each and
        // preparing the necessary widget maker parameters
        for (CtMember member : fieldsAndMethods) {
            widgetMakerParametersList.addAll(
                getTouchUIWidgetMakerParameters(trueComponentClass, member, classLoader, classPool, widgetRegistry,
                    touchUIDialogType));
        }

        return widgetMakerParametersList;

    }

    public static boolean isWidgetInComponentClass(CtClass componentClass) throws NotFoundException,
        ClassNotFoundException, InvalidComponentClassException {

        List fieldsAndMethods = new ArrayList();
        Component componentAnnotation = (Component) componentClass.getAnnotation(Component.class);
        fieldsAndMethods.addAll(ComponentMojoUtil.collectFields(componentClass,
            componentAnnotation.suppressFieldInheritanceForTouchUI()));
        fieldsAndMethods.addAll(ComponentMojoUtil.collectMethods(componentClass,
            componentAnnotation.suppressFieldInheritanceForTouchUI()));

        // Iterate through all the fields creating configs for each and
        // preparing the necessary widget maker parameters
        for (CtMember member : fieldsAndMethods) {
            if (!member.hasAnnotation(IgnoreDialogField.class)) {
                DialogFieldConfig dialogFieldConfig = null;
                if (member instanceof CtMethod) {
                    dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses((CtMethod) member);
                } else {
                    if (member.hasAnnotation(DialogField.class)) {
                        dialogFieldConfig =
                            new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
                    }
                }

                if (dialogFieldConfig != null && !dialogFieldConfig.isSuppressTouchUI()) {
                    return true;
                }
            }
        }

        return false;
    }

    @Deprecated
    public static List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy