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

org.bidib.wizard.common.labels.AccessoryLabelUtils Maven / Gradle / Ivy

package org.bidib.wizard.common.labels;

import java.util.List;

import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.bidib.jbidibc.core.schema.bidibbase.BaseLabel;
import org.bidib.jbidibc.core.schema.bidiblabels.AccessoryLabel;
import org.bidib.jbidibc.core.schema.bidiblabels.AccessoryLabels;
import org.bidib.jbidibc.core.schema.bidiblabels.NodeLabels;
import org.bidib.wizard.api.locale.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AccessoryLabelUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(AccessoryLabelUtils.class);

    /**
     * Get the accessory labels for the node with the provided uniqueId and the provided accessory id.
     * 
     * @param uniqueId
     *            the unique id
     * @param accessoryId
     *            the accessory id
     * @return the accessory labels
     */
    public static AccessoryLabel getAccessoryLabel(NodeLabels labels, int accessoryId) {
        AccessoryLabel accessoryLabel = null;

        if (labels != null && labels.getAccessoryLabels() != null) {
            for (AccessoryLabel currentAccessoryLabel : labels.getAccessoryLabels().getAccessoryLabel()) {

                if (currentAccessoryLabel.getIndex() == accessoryId) {
                    // found the accessory
                    accessoryLabel = currentAccessoryLabel;
                    break;
                }
            }
        }
        else {
            LOGGER.warn("No labels provided for accessoryId: {}", accessoryId);
        }

        if (accessoryLabel == null) {
            // create default value
            accessoryLabel = new AccessoryLabel();
            accessoryLabel.setIndex(accessoryId);

            if (labels.getAccessoryLabels() == null) {
                labels.setAccessoryLabels(new AccessoryLabels().withAccessoryLabel(accessoryLabel));
            }
            else {
                labels.getAccessoryLabels().getAccessoryLabel().add(accessoryLabel);
            }
        }

        return accessoryLabel;
    }

    /**
     * Get the accessory label with the provided accessory id.
     * 
     * @param accessoryId
     *            the accessory id
     * @return the accessory labels
     */
    public static AccessoryLabel getAccessoryLabel(AccessoryLabels labels, int accessoryId) {
        AccessoryLabel accessoryLabel = null;

        // search the accessory
        for (AccessoryLabel label : labels.getAccessoryLabel()) {

            if (label.getIndex() == accessoryId) {
                // found the accessory
                accessoryLabel = label;
                break;
            }

        }

        if (accessoryLabel == null) {
            // create default value
            accessoryLabel = new AccessoryLabel();
            accessoryLabel.setIndex(accessoryId);
        }

        return accessoryLabel;
    }

    /**
     * Replace the accessory label.
     *
     * @param nodeLabels
     *            the node labels
     * @param accessoryId
     *            the id of the accessory
     * @param labelString
     *            the new label string
     */
    public static void replaceAccessoryLabel(NodeLabels nodeLabels, int accessoryId, String labelString) {

        if (nodeLabels.getAccessoryLabels() == null) {
            nodeLabels.setAccessoryLabels(new AccessoryLabels());
        }

        final List accessoryLabels = nodeLabels.getAccessoryLabels().getAccessoryLabel();

        // find the label for the id
        AccessoryLabel accessoryLabel = IterableUtils.find(accessoryLabels, label -> label.getIndex() == accessoryId);
        if (accessoryLabel == null) {
            accessoryLabels.add(new AccessoryLabel().withIndex(accessoryId).withLabel(labelString));
        }
        else {
            accessoryLabel.setLabel(labelString);
        }
    }

    public static void replaceAspectLabel(
        NodeLabels nodeLabels, int accessoryId, int aspectNumber, String labelString) {

        if (nodeLabels.getAccessoryLabels() == null) {
            nodeLabels.setAccessoryLabels(new AccessoryLabels());
        }

        final List accessoryLabels = nodeLabels.getAccessoryLabels().getAccessoryLabel();

        // find the label for the id
        AccessoryLabel accessoryLabel = IterableUtils.find(accessoryLabels, label -> label.getIndex() == accessoryId);
        if (accessoryLabel == null) {
            accessoryLabel = new AccessoryLabel().withIndex(accessoryId);
            accessoryLabels.add(accessoryLabel);
        }

        BaseLabel aspectLabel =
            IterableUtils.find(accessoryLabel.getAspectLabel(), label -> label.getIndex() == aspectNumber);
        if (aspectLabel == null) {
            accessoryLabel.getAspectLabel().add(new BaseLabel().withIndex(aspectNumber).withLabel(labelString));
        }
        else {
            aspectLabel.setLabel(labelString);
        }
    }

    /**
     * Get the accessory labels for the step control.
     *
     * @param accessoryId
     *            the accessory id
     * @return the accessory labels
     */
    public static AccessoryLabel getStepControlAccessoryLabel(final NodeLabels nodeLabels, int accessoryId) {
        AccessoryLabel accessoryLabel = null;

        // create default value
        accessoryLabel = new AccessoryLabel();
        accessoryLabel.setIndex(accessoryId);

        AccessoryLabels accessoryLabels = null;
        if (nodeLabels != null) {
            LOGGER.info("Use node labels from extension in vendorCV.");
            accessoryLabels = nodeLabels.getAccessoryLabels();
        }

        String predefinedLabel = null;
        if (accessoryLabels != null) {
            AccessoryLabel predefinedAccessoryLabel =
                IterableUtils.find(accessoryLabels.getAccessoryLabel(), new Predicate() {

                    @Override
                    public boolean evaluate(AccessoryLabel currentLabel) {
                        return currentLabel.getIndex() == accessoryId;
                    }
                });

            if (predefinedAccessoryLabel != null) {
                predefinedLabel = predefinedAccessoryLabel.getLabel();
            }
        }

        if (StringUtils.isNotBlank(predefinedLabel)) {
            accessoryLabel.setLabel(predefinedLabel);
        }
        else {
            accessoryLabel
                .setLabel(Resources.getString(AccessoryLabelUtils.class, "stepcontrol.accessory." + accessoryId));
        }
        return accessoryLabel;
    }

    public static BaseLabel getAccessoryAspectLabel(final AccessoryLabel accessoryLabel, int aspectIndex) {

        if (accessoryLabel == null) {
            LOGGER.info("No accessory labels available.");
            return null;
        }

        List aspectLabels = accessoryLabel.getAspectLabel();
        return BidibLabelUtils.getLabel(aspectLabels, aspectIndex);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy