org.xlcloud.console.virtualClusters.controllers.wizard.utils.EditableVirtualClusterParameter.java Source code

Java tutorial

Introduction

Here is the source code for org.xlcloud.console.virtualClusters.controllers.wizard.utils.EditableVirtualClusterParameter.java

Source

/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.console.virtualClusters.controllers.wizard.utils;

import java.util.List;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.xlcloud.console.controllers.utils.VirtualClusterDisplayUtils;

import org.xlcloud.service.VirtualCluster;
import org.xlcloud.service.VirtualClusterDefinition;
import org.xlcloud.service.heat.template.Parameter;

/**
 * <p>Wrapper for {@link VirtualCluster.Parameters.Parameter}
 *  and {@link VirtualClusterDefinition.Parameters.Parameter}.</p>
 * <p>It allows to create a VirtualCluster.Parameters.Parameter template 
 *  from VirtualClusterDefinition.Parameters.Parameter template.</p>
 *  
 * @author Konrad Krl, AMG.net
 */
public class EditableVirtualClusterParameter {

    private VirtualCluster.Parameters.Parameter virtualClusterParameter;

    private Parameter templateParameter;

    /**
     * Constructor of Editable VirtualClusterParameter
     * @param templateParameter wrapped object
     */
    public EditableVirtualClusterParameter(Parameter templateParameter) {
        this.templateParameter = templateParameter;

        this.virtualClusterParameter = new VirtualCluster.Parameters.Parameter(templateParameter.getName(),
                templateParameter.getDefaultValue());
    }

    /**
     * @return {@link VirtualClusterDefinition.Parameters.Parameter#getName()}
     */
    public String getName() {
        return templateParameter.getName();
    }

    /**
     * @return {@link VirtualClusterDefinition.Parameters.Parameter#getDescription()}
     */
    public String getDescription() {
        return templateParameter.getDescription();
    }

    /**
     * If it is password field, the value is hidden, otherwise it returns the original wrapped parameter value.
     * @return {@link VirtualCluster.Parameters.Parameter#getValue()}
     */
    public String getValue() {
        if (renderPassword() && StringUtils.isNotBlank(virtualClusterParameter.getValue())) {
            return VirtualClusterDisplayUtils.PASSWORD_DISPLAY;
        } else {
            return virtualClusterParameter.getValue();
        }
    }

    /**
     * Sets the value of virtual cluster parameter
     * @param value virtual cluster parameter
     */
    public void setValue(String value) {
        if (!VirtualClusterDisplayUtils.PASSWORD_DISPLAY.equals(value)) {
            virtualClusterParameter.setValue(value);
        }
    }

    /**
     * @return {@link VirtualClusterDefinition.Parameters.Parameter#getMinValue()}
     */
    public Long getMinValue() {
        return templateParameter.getMinValue();
    }

    /**
     * @return {@link VirtualClusterDefinition.Parameters.Parameter#getMaxValue()}
     */
    public Long getMaxValue() {
        return templateParameter.getMaxValue();
    }

    /**
     * @return wrapped virtual cluster parameter
     */
    public VirtualCluster.Parameters.Parameter getVirtualClusterParameter() {
        return virtualClusterParameter;
    }

    /**
     * @return wrapped template parameter
     */
    public Parameter getTemplateParameter() {
        return templateParameter;
    }

    /**
     * Indicates whether definition parameter contains allowed values
     * @return {@code true} if definition parameter contains allowed values, {@code false} otherwise
     */
    public Boolean hasAllowedValues() {
        return (templateParameter.getAllowedValues() != null && !templateParameter.getAllowedValues().isEmpty());
    }

    /**
     * Returns the list of parameter allowed values 
     * @return allowed values
     */
    public List<String> getAllowedValues() {
        if (this.hasAllowedValues()) {
            return this.templateParameter.getAllowedValues();
        }

        return null;
    }

    /**
     * Indicates whether the input for this parameter should be a password input
     * @return {@code true} if this is password input, {@code false} otherwise
     */
    public boolean renderPassword() {
        return BooleanUtils.isTrue(templateParameter.getNoEcho());
    }

    /**
     * Indicates whether the input for this parameter should be a drop-down menu input
     * @return {@code true} if this is drop-down menu input, {@code false} otherwise
     */
    public boolean renderList() {
        return "String".equalsIgnoreCase(templateParameter.getType()) && hasAllowedValues();
    }

    /**
     * Indicates whether the input for this parameter should be a numeric input
     * @return {@code true} if this is numeric input, {@code false} otherwise
     */
    public boolean isNumber() {
        return "Number".equalsIgnoreCase(templateParameter.getType());
    }

    /**
     * Indicates whether the input for this parameter should be a simple text input
     * @return {@code true} if this is simple text input, {@code false} otherwise
     */
    public boolean renderText() {
        return !renderList() && !renderPassword();
    }
}