com.concur.ui.WebApp.java Source code

Java tutorial

Introduction

Here is the source code for com.concur.ui.WebApp.java

Source

/*
 * Copyright 2009 IT Mill Ltd.
 *
 * 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 com.concur.ui;

import com.concur.Config;
import com.concur.WsXml;
import com.concur.util.Http;
import com.concur.util.xml.XmlHandler;
import com.concur.util.xml.XmlMap;
import com.vaadin.Application;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;

import java.util.List;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class WebApp extends Application {
    private Window w;
    private NativeSelect actionField;
    private TextField tokenField;
    private TextField tupleKeyField;
    private TextArea tupleDataArea;

    @Override
    public void init() {
        w = createWindow();
        setMainWindow(w);
    }

    private Window createWindow() {
        FormLayout fl = new FormLayout();

        //        final SessionGuard sg = new SessionGuard();
        //        sg.setKeepalive(true);
        //        fl.addComponent(sg);

        fl.setSizeFull();
        fl.setMargin(true);
        fl.addComponent(new Label("<h2>ATS Tuple Store -- Demo App<h2/>", Label.CONTENT_XML));

        actionField = new NativeSelect("Action:");
        actionField.addItem("Authenticate");
        actionField.addItem("GetTuple");
        actionField.addItem("PutTuple");
        actionField.addItem("GetConfigurations");
        actionField.select("Authenticate");
        fl.addComponent(actionField);

        tokenField = new TextField("Authentication Token:");
        tokenField.setColumns(40);
        fl.addComponent(tokenField);

        tupleKeyField = new TextField("TupleKey:");
        tupleKeyField.setColumns(40);
        fl.addComponent(tupleKeyField);

        tupleDataArea = new TextArea("TupleData:");
        tupleDataArea.setColumns(40);
        tupleDataArea.setRows(5);
        fl.addComponent(tupleDataArea);

        Button b = new Button("Send Request");
        b.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                submit();
            }
        });

        fl.addComponent(b);

        final Window w = new Window("ATS Tuple Store -- DEMO");
        w.setContent(fl);
        return w;
    }

    private void submit() {
        String action = (String) actionField.getValue();
        String token = (String) tokenField.getValue();
        String tupleKey = (String) tupleKeyField.getValue();
        String tupleData = (String) tupleDataArea.getValue();

        if (!"Authenticate".equals(action) && "".equals(token.trim())) // if not logging in & not logged in
        {
            w.showNotification("Authentication Required");
            return;
        }

        String xml = WsXml.cmd(action, token, tupleKey, tupleData);
        String responseXml = Http.post(Config.getWsUrl(), xml);
        XmlMap xmlMap = XmlHandler.parse(responseXml).get("response");

        final XmlMap status = xmlMap.get("status");
        if (!"0".equals(status.getText("statuscode"))) {
            w.showNotification("Web Service call failed -- " + status.getText("statusmsg"));
            return;
        }

        if ("Authenticate".equals(action)) {
            tokenField.setValue(xmlMap.getText("token"));
        } else if ("GetTuple".equals(action)) {
            tupleDataArea.setValue(xmlMap.getText("tupledata"));
        } else if ("PutTuple".equals(action)) {
            w.showNotification("OK");
        } else if ("GetConfigurations".equals(action)) {
            List<XmlMap> tuples = xmlMap.get("config").getAll("tuple");

            Window subWin = new Window("Configurations");
            subWin.setHeight("400px");
            subWin.setWidth("400px");
            subWin.center();
            subWin.setModal(true);

            for (XmlMap tuple : tuples) {
                String text = tuple.getText("key") + " ==> " + tuple.getText("data");
                subWin.addComponent(new Label(text));
            }

            w.addWindow(subWin);
        }
    }
}