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

org.xlcloud.console.stacks.converters.HeatTemplateValueConverter Maven / Gradle / Ivy

The newest version!
/*
 * 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.stacks.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.json.JSONException;
import org.xlcloud.service.heat.parsers.commons.HeatTemplateValueParser;
import org.xlcloud.service.heat.template.commons.HeatTemplateValue;
import org.xlcloud.service.heat.template.commons.JsonHeatTemplateValue;
import org.xlcloud.service.heat.template.commons.functions.IntrinsicFunction;

/**
 * JSF {@link Converter} for {@link HeatTemplateValue}s
 * 
 * @author Tomek Adamczewski, AMG.net
 */
public class HeatTemplateValueConverter implements Converter {

    /** {@inheritDoc} */
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        try {
            return new HeatTemplateValueParser().fromString(value);
        } catch (JSONException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /** {@inheritDoc} */
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        HeatTemplateValue htValue = (HeatTemplateValue) value;
        if (htValue instanceof IntrinsicFunction) {
            try {
                return ((IntrinsicFunction) htValue).get().toString(2);
            } catch (JSONException e) {
            }
        } else if (htValue instanceof JsonHeatTemplateValue) {
            try {
                return ((JsonHeatTemplateValue) htValue).get().toString(2);
            } catch (JSONException e) {
            }
        }
        return htValue.get().toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy