Coverage Report - org.apache.turbine.util.FormMessages
 
Classes in this File Line Coverage Branch Coverage Complexity
FormMessages
0%
0/52
0%
0/20
2,625
 
 1  
 package org.apache.turbine.util;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 
 5  
 /*
 6  
  * Licensed to the Apache Software Foundation (ASF) under one
 7  
  * or more contributor license agreements.  See the NOTICE file
 8  
  * distributed with this work for additional information
 9  
  * regarding copyright ownership.  The ASF licenses this file
 10  
  * to you under the Apache License, Version 2.0 (the
 11  
  * "License"); you may not use this file except in compliance
 12  
  * with the License.  You may obtain a copy of the License at
 13  
  *
 14  
  *   http://www.apache.org/licenses/LICENSE-2.0
 15  
  *
 16  
  * Unless required by applicable law or agreed to in writing,
 17  
  * software distributed under the License is distributed on an
 18  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 19  
  * KIND, either express or implied.  See the License for the
 20  
  * specific language governing permissions and limitations
 21  
  * under the License.
 22  
  */
 23  
 
 24  
 import java.util.Hashtable;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * Used for adding and accessing messages that relate to a specific form and field. Allows to query for messages by form
 30  
  * name and field name. Used together with FormMessage class.
 31  
  *
 32  
  * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
 33  
  * @version $Id: FormMessages.java 1854688 2019-03-03 10:36:42Z tv $
 34  
  */
 35  
 public class FormMessages
 36  
 {
 37  
     private final Hashtable<String, List<String>> forms_messages;
 38  
 
 39  
     private final Hashtable<String, List<String>> fields_messages;
 40  
 
 41  
     private final Hashtable<String, List<String>> messages_fields;
 42  
 
 43  
     private final Hashtable<String, List<String>> forms_fields;
 44  
 
 45  
     /**
 46  
      * Constructor.
 47  
      */
 48  
     public FormMessages()
 49  0
     {
 50  0
         forms_messages = new Hashtable<String, List<String>>();
 51  0
         fields_messages = new Hashtable<String, List<String>>();
 52  0
         messages_fields = new Hashtable<String, List<String>>();
 53  0
         forms_fields = new Hashtable<String, List<String>>();
 54  0
     }
 55  
 
 56  
     /**
 57  
      * Sets a message for a field of a form. The message is given as a long representing a return code.
 58  
      *
 59  
      * @param formName A String with the form name.
 60  
      * @param fieldName A String with the field name.
 61  
      * @param returnCode A long with the return code.
 62  
      */
 63  
     public void setMessage( String formName, String fieldName, long returnCode )
 64  
     {
 65  0
         setMessage( formName, fieldName, String.valueOf( returnCode ) );
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Sets a message for a field of a form. The message is given as a String.
 70  
      *
 71  
      * @param formName A String with the form name.
 72  
      * @param fieldName A String with the field name.
 73  
      * @param messageName A String with the message.
 74  
      */
 75  
     public void setMessage( String formName, String fieldName, String messageName )
 76  
     {
 77  0
         String formFieldName = formName + "-" + fieldName;
 78  0
         addValue( forms_messages, formName, messageName );
 79  0
         addValue( fields_messages, formFieldName, messageName );
 80  0
         addValue( messages_fields, messageName, formFieldName );
 81  0
         addValue( forms_fields, formName, formFieldName );
 82  0
     }
 83  
 
 84  
     /**
 85  
      * Adds a pair key/value to a table, making sure not to add duplicate keys.
 86  
      *
 87  
      * @param table A Hashtable.
 88  
      * @param key A String with the key.
 89  
      * @param value A String with value.
 90  
      */
 91  
     private void addValue( Hashtable<String, List<String>> table, String key, String value )
 92  
     {
 93  
         List<String> values;
 94  
 
 95  0
         if ( !table.containsKey( key ) )
 96  
         {
 97  0
             values = new ArrayList<String>();
 98  0
             values.add( value );
 99  0
             table.put( key, values );
 100  
         }
 101  
         else
 102  
         {
 103  0
             values = table.get( key );
 104  0
             if ( !values.contains( value ) )
 105  
             {
 106  0
                 values.add( value );
 107  
             }
 108  
         }
 109  0
     }
 110  
 
 111  
     /**
 112  
      * Gets a pair key/value from a table.
 113  
      *
 114  
      * @param table A Hashtable.
 115  
      * @param key A String with the key.
 116  
      * @return A List with the pair key/value, or null.
 117  
      */
 118  
     private final List<String> getValues( Hashtable<String, List<String>> table, String key )
 119  
     {
 120  0
         return table.get( key );
 121  
     }
 122  
 
 123  
     /**
 124  
      * Gets all form messages for a given form.
 125  
      *
 126  
      * @param formName A String with the form name.
 127  
      * @return A FormMessage[].
 128  
      */
 129  
     public FormMessage[] getFormMessages( String formName )
 130  
     {
 131  
         List<String> messages, fields;
 132  
         String messageName, fieldName;
 133  0
         messages = getValues( forms_messages, formName );
 134  0
         if ( messages != null )
 135  
         {
 136  0
             FormMessage[] result = new FormMessage[messages.size()];
 137  0
             for ( int i = 0; i < messages.size(); i++ )
 138  
             {
 139  0
                 result[i] = new FormMessage( formName );
 140  0
                 messageName = messages.get( i );
 141  0
                 result[i].setMessage( messageName );
 142  0
                 fields = getValues( messages_fields, messageName );
 143  0
                 for ( int j = 0; j < fields.size(); j++ )
 144  
                 {
 145  0
                     fieldName = fields.get( j );
 146  0
                     if ( formHasField( formName, fieldName ) )
 147  
                     {
 148  0
                         result[i].setFieldName( fieldName );
 149  
                     }
 150  
                 }
 151  
             }
 152  0
             return result;
 153  
         }
 154  0
         return null;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Get form messages for a given form and field.
 159  
      *
 160  
      * @param formName A String with the form name.
 161  
      * @param fieldName A String with the field name.
 162  
      * @return A FormMessage[].
 163  
      */
 164  
     public FormMessage[] getFormMessages( String formName, String fieldName )
 165  
     {
 166  0
         String key = formName + "-" + fieldName;
 167  
 
 168  0
         List<String> messages = getValues( fields_messages, key );
 169  
         String messageName;
 170  
 
 171  0
         if ( messages != null )
 172  
         {
 173  0
             FormMessage[] result = new FormMessage[messages.size()];
 174  0
             for ( int i = 0; i < messages.size(); i++ )
 175  
             {
 176  0
                 result[i] = new FormMessage( formName, fieldName );
 177  0
                 messageName = messages.get( i );
 178  0
                 result[i].setMessage( messageName );
 179  
             }
 180  0
             return result;
 181  
         }
 182  0
         return null;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Check whether a form as a field.
 187  
      *
 188  
      * @param formName A String with the form name.
 189  
      * @param fieldName A String with the field name.
 190  
      * @return True if form has the field.
 191  
      */
 192  
     private boolean formHasField( String formName, String fieldName )
 193  
     {
 194  0
         List<String> fields = getValues( forms_fields, formName );
 195  0
         for ( Iterator<String> iter = fields.iterator(); iter.hasNext(); )
 196  
         {
 197  0
             if ( fieldName.equals( iter.next().toString() ) )
 198  
             {
 199  0
                 return true;
 200  
             }
 201  
         }
 202  0
         return false;
 203  
     }
 204  
 }