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

com.opensymphony.xwork2.ValidationAwareSupport Maven / Gradle / Ivy

There is a newer version: 6.3.0.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.opensymphony.xwork2;

import com.opensymphony.xwork2.interceptor.ValidationAware;

import java.io.Serializable;
import java.util.*;

/**
 * Provides a default implementation of ValidationAware. Returns new collections for
 * errors and messages (defensive copy).
 *
 * @author Jason Carreira
 * @author tm_jee
 * @version $Date$ $Id$
 */
public class ValidationAwareSupport implements ValidationAware, Serializable {

    private Collection actionErrors;
    private Collection actionMessages;
    private Map> fieldErrors;


    public synchronized void setActionErrors(Collection errorMessages) {
        this.actionErrors = errorMessages;
    }

    public synchronized Collection getActionErrors() {
        return new LinkedList<>(internalGetActionErrors());
    }

    public synchronized void setActionMessages(Collection messages) {
        this.actionMessages = messages;
    }

    public synchronized Collection getActionMessages() {
        return new LinkedList<>(internalGetActionMessages());
    }

    public synchronized void setFieldErrors(Map> errorMap) {
        this.fieldErrors = errorMap;
    }

    public synchronized Map> getFieldErrors() {
        return new LinkedHashMap<>(internalGetFieldErrors());
    }

    public synchronized void addActionError(String anErrorMessage) {
        internalGetActionErrors().add(anErrorMessage);
    }

    public synchronized void addActionMessage(String aMessage) {
        internalGetActionMessages().add(aMessage);
    }

    public synchronized void addFieldError(String fieldName, String errorMessage) {
        final Map> errors = internalGetFieldErrors();
        List thisFieldErrors = errors.get(fieldName);

        if (thisFieldErrors == null) {
            thisFieldErrors = new ArrayList<>();
            errors.put(fieldName, thisFieldErrors);
        }

        thisFieldErrors.add(errorMessage);
    }

    public synchronized boolean hasActionErrors() {
        return (actionErrors != null) && !actionErrors.isEmpty();
    }

    public synchronized boolean hasActionMessages() {
        return (actionMessages != null) && !actionMessages.isEmpty();
    }

    public synchronized boolean hasErrors() {
        return (hasActionErrors() || hasFieldErrors());
    }

    public synchronized boolean hasFieldErrors() {
        return (fieldErrors != null) && !fieldErrors.isEmpty();
    }

    private Collection internalGetActionErrors() {
        if (actionErrors == null) {
            actionErrors = new ArrayList<>();
        }

        return actionErrors;
    }

    private Collection internalGetActionMessages() {
        if (actionMessages == null) {
            actionMessages = new ArrayList<>();
        }

        return actionMessages;
    }

    private Map> internalGetFieldErrors() {
        if (fieldErrors == null) {
            fieldErrors = new LinkedHashMap<>();
        }

        return fieldErrors;
    }

    /**
     * Clears field errors map.
     *
     * 

* Will clear the map that contains field errors. *

*/ public synchronized void clearFieldErrors() { internalGetFieldErrors().clear(); } /** * Clears action errors list. * *

* Will clear the list that contains action errors. *

*/ public synchronized void clearActionErrors() { internalGetActionErrors().clear(); } /** * Clears messages list. * *

* Will clear the list that contains action messages. *

*/ public synchronized void clearMessages() { internalGetActionMessages().clear(); } /** * Clears all error list/maps. * *

* Will clear the map and list that contain * field errors and action errors. *

*/ public synchronized void clearErrors() { internalGetFieldErrors().clear(); internalGetActionErrors().clear(); } /** * Clears all error and messages list/maps. * *

* Will clear the maps/lists that contain * field errors, action errors and action messages. *

*/ public synchronized void clearErrorsAndMessages() { internalGetFieldErrors().clear(); internalGetActionErrors().clear(); internalGetActionMessages().clear(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy