/*
 * Copyright 2014-2015 the original author or authors.
 *
 * 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.lastaflute.thymeleaf.messages;

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

import org.dbflute.util.DfTypeUtil;
import org.lastaflute.web.ruts.message.ActionMessage;
import org.lastaflute.web.ruts.message.ActionMessages;
import org.lastaflute.web.servlet.request.RequestManager;

/**
 * Read-only Action Messages Wrapper. <br>
 * Accessed by Thymeleaf templates, so cannot easily refactor method names.
 * @author schatten
 * @author jflute
 * @author Toshi504
 */
public class ErrorMessages implements Serializable {

    // ===================================================================================
    //                                                                          Definition
    //                                                                          ==========
    private static final long serialVersionUID = 1L;

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    protected final ActionMessages messages;
    protected final RequestManager requestManager;

    // ===================================================================================
    //                                                                         Constructor
    //                                                                         ===========
    public ErrorMessages(ActionMessages origin, RequestManager requestManager) {
        this.messages = origin;
        this.requestManager = requestManager;
    }

    // ===================================================================================
    //                                                                      Convert Access
    //                                                                      ==============
    public List<ResolvedMessage> getAll() { // e.g. th:each="error : ${errors.all}"
        List<ResolvedMessage> list = new ArrayList<ResolvedMessage>();
        messages.accessByFlatIterator().forEachRemaining(message -> {
            list.add(createResolvedMessage(message));
        });
        return list;
    }

    public List<ResolvedMessage> part(String property) { // e.g. th:each="error : ${errors.part('seaName')}"
        List<ResolvedMessage> list = new ArrayList<ResolvedMessage>();
        messages.accessByIteratorOf(property).forEachRemaining(message -> {
            list.add(createResolvedMessage(message));
        });
        return list;
    }

    protected ResolvedMessage createResolvedMessage(ActionMessage message) {
        return new ResolvedMessage(message, requestManager);
    }

    // ===================================================================================
    //                                                                     Delegate Access
    //                                                                     ===============
    /**
     * @see org.lastaflute.web.ruts.message.ActionMessages#hasMessageOf(java.lang.String)
     */
    public boolean exists(String property) { // e.g. th:unless="${errors.exists('seaName')}"
        return messages.hasMessageOf(property);
    }

    /**
     * @see org.lastaflute.web.ruts.message.ActionMessages#hasMessageOf(java.lang.String, java.lang.String)
     */
    public boolean exists(String property, String key) { // e.g. th:unless="${errors.exists('seaName', 'errors.required')}"
        return messages.hasMessageOf(property, key);
    }

    /**
     * @see org.lastaflute.web.ruts.message.ActionMessages#isEmpty()
     */
    public boolean isEmpty() { // e.g. th:unless="${errors.empty}"
        return messages.isEmpty();
    }

    /**
     * @see org.lastaflute.web.ruts.message.ActionMessages#isAccessed()
     */
    public boolean isAccessed() { // e.g. th:unless="${errors.accessed}"
        return messages.isAccessed();
    }

    /**
     * @see org.lastaflute.web.ruts.message.ActionMessages#size()
     */
    public int size() {
        return messages.size();
    }

    /**
     * @see org.lastaflute.web.ruts.message.ActionMessages#size(java.lang.String)
     */
    public int size(String property) {
        return messages.size(property);
    }

    // ===================================================================================
    //                                                                      Basic Override
    //                                                                      ==============
    @Override
    public String toString() { // for request attribute logging
        return DfTypeUtil.toClassTitle(this) + ":{messages=" + messages + "}@" + Integer.toHexString(hashCode());
    }
}

@@ -3681,114 +3681,8 @@
 ===%0A
-    /**%0A     * @see org.lastaflute.web.ruts.message.ActionMessages#hasMessageOf(java.lang.String)%0A     */%0A
     
@@ -3829,132 +3829,8 @@
  %7D%0A%0A
-    /**%0A     * @see org.lastaflute.web.ruts.message.ActionMessages#hasMessageOf(java.lang.String, java.lang.String)%0A     */%0A
     
@@ -4013,93 +4013,8 @@
  %7D%0A%0A
-    /**%0A     * @see org.lastaflute.web.ruts.message.ActionMessages#isEmpty()%0A     */%0A
     
@@ -4122,96 +4122,8 @@
  %7D%0A%0A
-    /**%0A     * @see org.lastaflute.web.ruts.message.ActionMessages#isAccessed()%0A     */%0A
     
@@ -4244,246 +4244,66 @@
     
-/**%0A     * @see org.lastaflute.web.ruts.message.ActionMessages#size()%0A     */%0A    public int size() %7B%0A        return messages.size();%0A    %7D%0A%0A    /**%0A     * @see org.lastaflute.web.ruts.message.ActionMessages#size(java.lang.String)%0A     */
+public int size() %7B%0A        return messages.size();%0A    %7D%0A
 %0A   

remove wrong javadoc