001package org.apache.turbine.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * A message class for holding information about a message that
027 * relates to a specific form and field.  Used together with
028 * FormMessages class.
029 *
030 * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
031 * @version $Id$
032 */
033public class FormMessage
034{
035    private String message;
036    private String formName;
037    private final List<String> fieldNames;
038
039    /**
040     * Constructor.
041     */
042    public FormMessage()
043    {
044        fieldNames = new ArrayList<>();
045    }
046
047    /**
048     * Constructor.
049     *
050     * @param formName A String with the form name.
051     */
052    public FormMessage(String formName)
053    {
054        this();
055        setFormName(formName);
056    }
057
058    /**
059     * Constructor.
060     *
061     * @param formName A String with the form name.
062     * @param fieldName A String with the field name.
063     */
064    public FormMessage(String formName,
065                       String fieldName)
066    {
067        this(formName);
068        setFieldName(fieldName);
069    }
070
071    /**
072     * Constructor.
073     *
074     * @param formName A String with the form name.
075     * @param fieldName A String with the field name.
076     * @param message A String with the message.
077     */
078    public FormMessage(String formName,
079                       String fieldName,
080                       String message)
081    {
082        this(formName, fieldName);
083        setMessage(message);
084    }
085
086    /**
087     * Return the message.
088     *
089     * @return A String with the message.
090     */
091    public String getMessage()
092    {
093        return message;
094    }
095
096    /**
097     * Return the form name.
098     *
099     * @return A String with the form name.
100     */
101    public String getFormName()
102    {
103        return formName;
104    }
105
106    /**
107     * Return the field names.
108     *
109     * @return A String[] with the field names.
110     */
111    public String[] getFieldNames()
112    {
113        return fieldNames.toArray(new String[fieldNames.size()]);
114    }
115
116    /**
117     * Set the message.
118     *
119     * @param message A String with the message.
120     */
121    public void setMessage(String message)
122    {
123        this.message = message;
124    }
125
126    /**
127     * Set the form name.
128     *
129     * @param formName A String with the form name.
130     */
131    public void setFormName(String formName)
132    {
133        this.formName = formName;
134    }
135
136    /**
137     * Adds one field name.
138     *
139     * @param fieldName A String with the field name.
140     */
141    public void setFieldName(String fieldName)
142    {
143        fieldNames.add(fieldName);
144    }
145
146    /**
147     * Write out the contents of the message in a friendly manner.
148     *
149     */
150    @Override
151    public String toString()
152    {
153        StringBuilder sb = new StringBuilder("formName:" + getFormName() + ", fieldNames:");
154        for (int i = 0; i< getFieldNames().length; i++){
155            sb.append(getFieldNames()[i] + " ");
156        }
157        sb.append(", message:" + getMessage());
158
159        return sb.toString();
160    }
161}