com.mobicage.rogerthat.flow.FormFlowStep.java Source code

Java tutorial

Introduction

Here is the source code for com.mobicage.rogerthat.flow.FormFlowStep.java

Source

/*
 * Copyright (c) 2011-2014, MOBICAGE NV
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 * This product includes software developed by Mobicage NV.
 * 4. Neither the name of the Mobicage NV nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY MOBICAGE NV ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MOBICAGE NV BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @@license_version:1.7@@
 */
package com.mobicage.rogerthat.flow;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.DatatypeConverter;

import org.json.simple.JSONObject;

import com.mobicage.rogerthat.RogerthatAPIException;
import com.mobicage.rogerthat.form.FloatListWidgetResult;
import com.mobicage.rogerthat.form.FloatWidgetResult;
import com.mobicage.rogerthat.form.FormResult;
import com.mobicage.rogerthat.form.LocationWidgetResult;
import com.mobicage.rogerthat.form.LongListWidgetResult;
import com.mobicage.rogerthat.form.LongWidgetResult;
import com.mobicage.rogerthat.form.UnicodeListWidgetResult;
import com.mobicage.rogerthat.form.UnicodeWidgetResult;
import com.mobicage.rogerthat.mfd.FloatValue;
import com.mobicage.rogerthat.mfd.FormButton;
import com.mobicage.rogerthat.mfd.GPSLocationWidgetStep;
import com.mobicage.rogerthat.mfd.MemberRun;
import com.mobicage.rogerthat.mfd.PhotoUploadWidgetStep;
import com.mobicage.rogerthat.mfd.RangeSliderWidgetStep;
import com.mobicage.rogerthat.mfd.SelectDateWidgetStep;
import com.mobicage.rogerthat.mfd.SelectMultiWidgetStep;
import com.mobicage.rogerthat.mfd.SelectSingleWidgetStep;
import com.mobicage.rogerthat.mfd.SliderWidgetStep;
import com.mobicage.rogerthat.mfd.TextWidgetStep;
import com.mobicage.rogerthat.mfd.Value;

public class FormFlowStep extends FlowStep {

    public final static String TYPE = "form_step";

    private FormResult result;

    @Override
    public String getStepType() {
        return TYPE;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JSONObject toJSONObject() {
        JSONObject obj = super.toJSONObject();
        obj.put("form_result", result == null ? null : result.toJSONObject());
        return obj;
    }

    @Override
    public void fromJSONObject(JSONObject source) throws RogerthatAPIException {
        super.fromJSONObject(source);
        JSONObject obj = (JSONObject) source.get("form_result");
        if (obj != null) {
            result = new FormResult();
            result.fromJSONObject(obj);
        }
    }

    public static FormFlowStep fromFormStep(MemberRun run, TextWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            UnicodeWidgetResult widget = new UnicodeWidgetResult();
            String value = stepDefinition.getValue();
            if (value != null && value.startsWith("base64:"))
                try {
                    value = new String(DatatypeConverter.parseBase64Binary(value.substring(7)), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    // do nothing, value will pass with base64: prefix.
                }
            widget.value = value;
            step.result = new FormResult(widget);
        }
        return step;
    }

    public static FormFlowStep fromFormStep(MemberRun run, SliderWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            FloatWidgetResult widget = new FloatWidgetResult();
            widget.value = stepDefinition.getValue();
            step.result = new FormResult(widget);
        }
        return step;
    }

    public static FormFlowStep fromFormStep(MemberRun run, RangeSliderWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            FloatListWidgetResult widget = new FloatListWidgetResult();
            widget.values = new ArrayList<Double>(stepDefinition.getValue().size());
            for (FloatValue val : stepDefinition.getValue()) {
                widget.values.add(new Double(val.getValue()));
            }
            step.result = new FormResult(widget);
        }
        return step;
    }

    public static FormFlowStep fromFormStep(MemberRun run, SelectSingleWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            UnicodeWidgetResult widget = new UnicodeWidgetResult();
            widget.value = stepDefinition.getValue();
            step.result = new FormResult(widget);
        }
        return step;
    }

    public static FormFlowStep fromFormStep(MemberRun run, SelectMultiWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            UnicodeListWidgetResult widget = new UnicodeListWidgetResult();
            widget.values = new ArrayList<String>();
            for (Value val : stepDefinition.getSelection()) {
                widget.values.add(val.getValue());
            }
            step.result = new FormResult(widget);
        }
        return step;
    }

    public static FormFlowStep fromFormStep(MemberRun run, SelectDateWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            LongWidgetResult widget = new LongWidgetResult();
            widget.value = stepDefinition.getDate();
            step.result = new FormResult(widget);
        }
        return step;
    }

    public static FormFlowStep fromFormStep(MemberRun run, PhotoUploadWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            UnicodeWidgetResult widget = new UnicodeWidgetResult();
            widget.value = stepDefinition.getValue();
            step.result = new FormResult(widget);
        }
        return step;
    }

    public static FormFlowStep fromFormStep(MemberRun run, GPSLocationWidgetStep stepDefinition) {
        FormFlowStep step = new FormFlowStep();
        populateFromBaseMessageStep(step, stepDefinition);
        if (FormButton.POSITIVE.equals(stepDefinition.getFormButton())) {
            LocationWidgetResult widget = new LocationWidgetResult();
            widget.horizontalAccuracy = new Double(stepDefinition.getHorizontalAccuracy());
            widget.verticalAccuracy = new Double(stepDefinition.getVerticalAccuracy());
            widget.latitude = new Double(stepDefinition.getLatitude());
            widget.longitude = new Double(stepDefinition.getLongitude());
            widget.altitude = new Double(stepDefinition.getAltitude());
            widget.timestamp = stepDefinition.getTimestamp();
            step.result = new FormResult(widget);
        }
        return step;
    }

    @Override
    public Long getLongResult() throws FlowLogicException {
        if (result == null || result.widget == null)
            return null;
        if (result.widget instanceof LongWidgetResult)
            return ((LongWidgetResult) result.widget).value;
        throw new FlowLogicException("Widget is not of a compatible type.");
    }

    @Override
    public List<Long> getLongListResult() throws FlowLogicException {
        if (result == null || result.widget == null)
            return null;
        if (result.widget instanceof LongListWidgetResult)
            return ((LongListWidgetResult) result.widget).values;
        throw new FlowLogicException("Widget is not of a compatible type.");
    }

    @Override
    public String getStringResult() throws FlowLogicException {
        if (result == null || result.widget == null)
            return null;
        if (result.widget instanceof UnicodeWidgetResult)
            return ((UnicodeWidgetResult) result.widget).value;
        throw new FlowLogicException("Widget is not of a compatible type.");
    }

    @Override
    public List<String> getStringListResult() throws FlowLogicException {
        if (result == null || result.widget == null)
            return null;
        if (result.widget instanceof UnicodeListWidgetResult)
            return ((UnicodeListWidgetResult) result.widget).values;
        throw new FlowLogicException("Widget is not of a compatible type.");
    }

    @Override
    public Double getFloatResult() throws FlowLogicException {
        if (result == null || result.widget == null)
            return null;
        if (result.widget instanceof FloatWidgetResult)
            return ((FloatWidgetResult) result.widget).value;
        throw new FlowLogicException("Widget is not of a compatible type.");
    }

    @Override
    public List<Double> getFloatListResult() throws FlowLogicException {
        if (result == null || result.widget == null)
            return null;
        if (result.widget instanceof FloatListWidgetResult)
            return ((FloatListWidgetResult) result.widget).values;
        throw new FlowLogicException("Widget is not of a compatible type.");
    }

    @Override
    public LocationWidgetResult getLocationResult() throws FlowLogicException {
        if (result == null || result.widget == null)
            return null;
        if (result.widget instanceof LocationWidgetResult)
            return (LocationWidgetResult) result.widget;
        throw new FlowLogicException("Widget is not of a compatible type.");
    }

}