it.scoppelletti.wui.PromptDialogAction.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.wui.PromptDialogAction.java

Source

/*
 * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * 
 * 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 it.scoppelletti.wui;

import java.util.*;
import com.opensymphony.xwork2.*;
import org.slf4j.*;
import org.springframework.web.util.*;
import it.scoppelletti.programmerpower.*;
import it.scoppelletti.programmerpower.reflect.*;
import it.scoppelletti.programmerpower.types.*;
import it.scoppelletti.programmerpower.web.control.*;
import it.scoppelletti.programmerpower.web.view.*;

/**
 * Dialogo di conferma.
 */
@Final
public class PromptDialogAction extends DialogAction {
    private static final long serialVersionUID = 1L;

    /**
     * Stato della vista {@code titolo}: Titolo.
     */
    public static final String VIEWSTATE_TITLE = "title";

    /**
     * Stato della vista {@code message}: Messaggio.
     */
    public static final String VIEWSTATE_MESSAGE = "message";

    /**
     * Stato della vista {@code cssClass}: Classe CSS del messaggio.
     */
    public static final String VIEWSTATE_CSSCLASS = "cssClass";

    /**
     * Stato della vista {@code actionUrl}: URL di conferma.
     */
    public static final String VIEWSTATE_ACTIONURL = "actionUrl";

    private static final String VIEWSTATE_PREFIX = "viewState.";
    private static final Logger myLogger = LoggerFactory.getLogger(PromptDialogAction.class);

    /**
     * @serial Stato della vista chiamante.
     */
    private ViewState myCallerState;

    private transient String myActionUrl;

    /**
     * Costruttore.
     */
    public PromptDialogAction() {
    }

    /**
     * Restituisce lo stato della vista chiamante.
     * 
     * @return Oggetto.
     */
    public ViewState getCallerState() {
        if (myCallerState == null) {
            myCallerState = new ViewState();
        }

        return myCallerState;
    }

    /**
     * Imposta lo stato della vista chiamante.
     * 
     * @param obj Oggetto.
     */
    public void setCallerState(ViewState obj) {
        if (obj == null) {
            throw new ArgumentNullException("obj");
        }

        myCallerState = obj;
    }

    /**
     * Restituisce l&rsquo;URL dell&rsquo;azione.
     * 
     * @return Valore.
     */
    public String getActionUrl() {
        return myActionUrl;
    }

    @Override
    public String execute() {
        String uri;
        UriComponentsBuilder uriBuilder;

        uri = getViewState().get(PromptDialogAction.VIEWSTATE_ACTIONURL);
        if (Strings.isNullOrEmpty(uri)) {
            throw new PropertyNotSetException(toString(), PromptDialogAction.VIEWSTATE_ACTIONURL);
        }

        uriBuilder = UriComponentsBuilder.fromHttpUrl(uri);
        uriBuilder.queryParam("closingDialogId", getViewState().get(ActionBase.VIEWSTATE_VIEWID));
        for (Map.Entry<String, String> param : getCallerState().entrySet()) {
            uriBuilder.queryParam(PromptDialogAction.VIEWSTATE_PREFIX.concat(param.getKey()), param.getValue());
        }

        myActionUrl = uriBuilder.build().toUriString();
        myLogger.info("User accepted action {}.", myActionUrl);

        return Action.SUCCESS;
    }
}