Coverage Report - rs.mgifos.mosquito.model.MetaTable
 
Classes in this File Line Coverage Branch Coverage Complexity
MetaTable
25%
27/109
29%
4/14
0
 
 1  
 /* 
 2  
  * This file is part of Mosquito meta-loader.
 3  
  *
 4  
  * Mosquito meta-loader is free software; you can redistribute it and/or modify
 5  
  * it under the terms of the GNU Lesser General Public License as published by
 6  
  * the Free Software Foundation; either version 3 of the License, or
 7  
  * (at your option) any later version.
 8  
  *
 9  
  * Mosquito meta-loader is distributed in the hope that it will be useful,
 10  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
  * GNU Lesser General Public License for more details.
 13  
  *
 14  
  * You should have received a copy of the GNU Lesser General Public License
 15  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16  
  */
 17  
 
 18  
 package rs.mgifos.mosquito.model;
 19  
 
 20  
 import java.util.Collection;
 21  
 import java.util.Enumeration;
 22  
 import java.util.Hashtable;
 23  
 import java.util.Iterator;
 24  
 import java.util.Vector;
 25  
 
 26  
 /**
 27  
  * Class represent of DB Table's meta data
 28  
  *
 29  
  * @author <a href="mailto:nikola.petkov@gmail.com">Nikola Petkov &lt;nikola.petkov@gmail.com&gt;</a>
 30  
  */
 31  
 public class MetaTable implements Iterable<MetaColumn> {
 32  
 
 33  
     protected String code;
 34  
 
 35  
     protected String comment;
 36  
 
 37  5
     protected Hashtable<String, MetaColumn> htColumns = new Hashtable<String, MetaColumn>();
 38  
 
 39  5
     protected Hashtable<String, MetaKey> htKeys = new Hashtable<String, MetaKey>();
 40  
 
 41  5
     protected Hashtable<String, MetaReference> htRefs = new Hashtable<String, MetaReference>();
 42  
 
 43  
     protected String name;
 44  
 
 45  5
     protected Vector<MetaColumn> vColumns = new Vector<MetaColumn>();
 46  
 
 47  
     /**
 48  
      * @param aCode
 49  
      *            code
 50  
      * @param aName
 51  
      *            name
 52  
      * @param aComment
 53  
      *            comment about this MetaTable
 54  
      */
 55  
     public MetaTable(String aCode, String aName, String aComment) {
 56  5
         super();
 57  5
         code = aCode;
 58  5
         name = aName;
 59  5
         comment = aComment;
 60  5
     }
 61  
 
 62  
     /**
 63  
      * Adds the new MetaColumn into this MetaTable container
 64  
      * 
 65  
      * @param aMC
 66  
      */
 67  
     public void addCol(MetaColumn aMC) {
 68  18
         aMC.setParentTable(code);
 69  18
         Enumeration<MetaKey> eKeys = htKeys.elements();
 70  36
         while (eKeys.hasMoreElements()) {
 71  18
             MetaKey mk = (MetaKey) eKeys.nextElement();
 72  18
             if (mk.containsColumn(aMC))
 73  5
                 aMC.setParentKey(mk);
 74  18
         }
 75  18
         vColumns.add(aMC);
 76  18
         htColumns.put(aMC.getTableDotColumnCode(), aMC);
 77  18
     }
 78  
 
 79  
     /**
 80  
      * @param aMK
 81  
      */
 82  
     public void addKey(MetaKey aMK) {
 83  5
         htKeys.put(aMK.getCode(), aMK);
 84  5
     }
 85  
 
 86  
     /**
 87  
      * @param aMR
 88  
      */
 89  
     public void addRef(MetaReference aMR) {
 90  4
         htRefs.put(aMR.getCode(), aMR);
 91  4
     }
 92  
 
 93  
     /**
 94  
      * Returns collection of MetaColumn(s) of this MetaTable
 95  
      * 
 96  
      * @return collection of MetaColumn(s) of this MetaTable
 97  
      */
 98  
     public Collection cColumns() {
 99  0
         return vColumns;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Checks for specified MetaColumn persistaion within this MetaTable
 104  
      * 
 105  
      * @param aMC
 106  
      *            MetaColumn which persistation is being checked
 107  
      * @return true if specified MetaColumn exist within this MetaTable
 108  
      */
 109  
     public boolean contains(MetaColumn aMC) {
 110  0
         return htColumns.contains(aMC);
 111  
     }
 112  
 
 113  
     /**
 114  
      * Returns enumeration of MetaColums of this MetaTable
 115  
      * 
 116  
      * @return enumeration of MetaColums of this MetaTable
 117  
      */
 118  
     public Enumeration eColumns() {
 119  0
         return vColumns.elements();
 120  
     }
 121  
 
 122  
     /**
 123  
      * @return Enumeration of MetaKeys
 124  
      */
 125  
     public Enumeration eKeys() {
 126  0
         return htKeys.elements();
 127  
     }
 128  
 
 129  
     /**
 130  
      * @return Enumeration of References
 131  
      */
 132  
     public Enumeration eRefs() {
 133  0
         return htRefs.elements();
 134  
     }
 135  
 
 136  
     /**
 137  
      * Returns code of this MetaTable
 138  
      * 
 139  
      * @return code of this MetaTable
 140  
      */
 141  
     public String getCode() {
 142  28
         return code;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Returns MetaColumn of this MetaTable by specified
 147  
      * column.getTabledotColumnCode()
 148  
      * 
 149  
      * @param aTableDotColumnCode
 150  
      *            specified code
 151  
      * @return MetaColumn of this MetaTable by specified
 152  
      *         column.getTabledotColumnCode() if exists, otherwise it returns
 153  
      *         null
 154  
      * @throws NullPointerException
 155  
      *             if aCode is null
 156  
      */
 157  
     public Object getColByTableDotColumnCode(String aTableDotColumnCode)
 158  
             throws NullPointerException {
 159  18
         return htColumns.get(aTableDotColumnCode);
 160  
     }
 161  
 
 162  
     /**
 163  
      * Returns description of this MetaTable
 164  
      * 
 165  
      * @return description of this MetaTable
 166  
      */
 167  
     public String getComment() {
 168  0
         return comment;
 169  
     }
 170  
 
 171  
     /**
 172  
      * @param aKeyCode
 173  
      * @return MetaKey specifed by a aKeyCode, or null if it doesn't exist
 174  
      */
 175  
     public MetaKey getKeyByCode(String aKeyCode) {
 176  0
         MetaKey retVal = null;
 177  
         try {
 178  0
             retVal = (MetaKey) htKeys.get(aKeyCode);
 179  0
         } catch (Exception e) {
 180  0
             retVal = null;
 181  0
         }
 182  0
         return retVal;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Returns name of this MetaTable
 187  
      * 
 188  
      * @return name of this MetaTable
 189  
      */
 190  
     public String getName() {
 191  0
         return name;
 192  
     }
 193  
 
 194  
     /**
 195  
      * Returns the count of all MetaColumn(s) within this MetaTable
 196  
      * 
 197  
      * @return the count of all MetaColumn(s) within this MetaTable
 198  
      */
 199  
     public int getTotalColumns() {
 200  5
         return vColumns.size();
 201  
     }
 202  
 
 203  
     public Iterator<MetaColumn> iterator() {
 204  
         //return htColumns.values().iterator();
 205  5
         return vColumns.iterator();
 206  
     }
 207  
 
 208  
     /**
 209  
      * Sets code for this MetaTable
 210  
      * 
 211  
      * @param code
 212  
      *            the code to set
 213  
      */
 214  
     public void setCode(String code) {
 215  0
         this.code = code;
 216  0
     }
 217  
 
 218  
     /**
 219  
      * Sets description of this MetaTable
 220  
      * 
 221  
      * @param aDescription
 222  
      *            the description to set
 223  
      */
 224  
     public void setComment(String aDescription) {
 225  0
         comment = aDescription;
 226  0
     }
 227  
 
 228  
     /**
 229  
      * Sets name for this MetaTable
 230  
      * 
 231  
      * @param name
 232  
      *            the name to set.
 233  
      */
 234  
     public void setName(String name) {
 235  0
         this.name = name;
 236  0
     }
 237  
 
 238  
     /**
 239  
      * Returns string representation of this MetaTable (name)
 240  
      * 
 241  
      * @see java.lang.Object#toString()
 242  
      */
 243  
     public String toString() {
 244  0
         return name;
 245  
     }
 246  
 
 247  
     /**
 248  
      * Returns verbose description of this MetaTable (HTML) - recursive fks
 249  
      * 
 250  
      * @return verbose description of this MetaTable
 251  
      */
 252  
     public String toStringVerbose() {
 253  0
         String retVal = "<HTML><TABLE border=\"1\">" + "<CAPTION><B>" + code
 254  
                 + "</B></CAPTION>";
 255  0
         retVal += "<TBODY>";
 256  0
         retVal += "<TR><TD align=\"right\">" + "<B>name</B>" + "</TD>"
 257  
                 + "<TD align=\"left\">" + name + "</TD></TR>";
 258  0
         retVal += "<TR><TD align=\"right\">" + "<B>code</B>" + "</TD>"
 259  
                 + "<TD align=\"left\">" + code + "</TD></TR>";
 260  0
         retVal += "<TR><TD align=\"right\">" + "<B>comment</B>" + "</TD>"
 261  
                 + "<TD align=\"left\" ROWSPAN=\"2\">"
 262  
                 + MetaUtils.insertHTMLBreaks(comment, 50) + "</TD></TR>";
 263  0
         retVal += "</TBODY></TABLE>";
 264  
         // KEYS
 265  0
         retVal += "<TABLE border=\"1\">" + "<CAPTION><B> KEYS"
 266  
                 + "</B></CAPTION>";
 267  0
         retVal += "<TH><I>Name</I></TH><TH><I>Code</I></TH><TH><I>Columns</I></TH><TBODY>";
 268  0
         Enumeration<MetaKey> eKeys = htKeys.elements();
 269  0
         while (eKeys.hasMoreElements()) {
 270  0
             MetaKey crntKey = eKeys.nextElement();
 271  0
             retVal += "<TR><TD>";
 272  0
             retVal += crntKey.getName();
 273  0
             retVal += "</TD><TD>";
 274  0
             retVal += crntKey.getCode();
 275  0
             retVal += "</TD><TD><TABLE><TBODY>";
 276  0
             Enumeration<MetaColumn> eKeyCols = crntKey.eColumns();
 277  0
             while (eKeyCols.hasMoreElements()) {
 278  0
                 MetaColumn crntKeyCol = eKeyCols.nextElement();
 279  0
                 retVal += "<TR><TD>" + crntKeyCol.getCode() + "</TD><TD>";
 280  0
                 retVal += crntKeyCol.getName() + "</TD></TR>";
 281  0
             }
 282  0
             retVal += "</TBODY></TABLE>";
 283  0
         }
 284  0
         retVal += "</TBODY></TABLE>";
 285  
 
 286  
         // REFERENCES
 287  0
         retVal += "<TABLE border=\"1\">" + "<CAPTION><B> REFERENCES"
 288  
                 + "</B></CAPTION>";
 289  0
         retVal += "<TH><I>Name</I></TH><TH><I>Code</I></TH><TH><I>Parent Table</I></TH>"
 290  
                 + "<TH><I>Columns</I></TH><TBODY>";
 291  0
         Enumeration<MetaReference> eRefs = htRefs.elements();
 292  0
         while (eRefs.hasMoreElements()) {
 293  0
             MetaReference currRef = eRefs.nextElement();
 294  0
             retVal += "<TR><TD>";
 295  0
             retVal += currRef.getName();
 296  0
             retVal += "</TD><TD>";
 297  0
             retVal += currRef.getCode();
 298  0
             retVal += "</TD><TD>";
 299  0
             retVal += currRef.getParentTable();
 300  0
             retVal += "</TD><TD><TABLE><TBODY>";
 301  0
             Enumeration<MetaColumn> eKeyCols = currRef.eColumns();
 302  0
             while (eKeyCols.hasMoreElements()) {
 303  0
                 MetaColumn crntKeyCol = eKeyCols.nextElement();
 304  0
                 retVal += "<TR><TD>" + crntKeyCol.getCode() + "</TD><TD>";
 305  0
                 retVal += crntKeyCol.getName() + "</TD></TR>";
 306  0
             }
 307  0
             retVal += "</TD></TBODY></TABLE>";
 308  0
         }
 309  0
         retVal += "</TBODY></TABLE>";
 310  
 
 311  
         // COLUMNS
 312  0
         retVal += "<TABLE border=\"1\">" + "<CAPTION><B> COLUMNS"
 313  
                 + "</B></CAPTION>";
 314  0
         retVal += "<TH><I>No</I></TH><TH><I>Name</I></TH><TH><I>Code</I></TH><TBODY>";
 315  0
         int count = 0;
 316  0
         Enumeration<MetaColumn> eCols = vColumns.elements();
 317  0
         while (eCols.hasMoreElements()) {
 318  0
             MetaColumn crntCol = (MetaColumn) eCols.nextElement();
 319  0
             retVal += "<TR><TD align=\"right\">";
 320  0
             retVal += ++count;
 321  0
             retVal += "</TD><TD>";
 322  0
             retVal += crntCol.getName();
 323  0
             retVal += "</TD><TD>";
 324  0
             retVal += crntCol.getCode();
 325  0
             retVal += "</TD></TR>";
 326  0
         }
 327  0
         retVal += "</TBODY></TABLE></HTML>";
 328  
 
 329  0
         return retVal;
 330  
     }
 331  
 }