| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StringKeyReferencePoemType |
|
| 1.6923076923076923;1.692 | ||||
| StringKeyReferencePoemType$1 |
|
| 1.6923076923076923;1.692 |
| 1 | /* | |
| 2 | * $Source$ | |
| 3 | * $Revision$ | |
| 4 | * | |
| 5 | * Copyright (C) 2012 Tim Pizey | |
| 6 | * | |
| 7 | * Part of Melati (http://melati.org), a framework for the rapid | |
| 8 | * development of clean, maintainable web applications. | |
| 9 | * | |
| 10 | * Melati is free software; Permission is granted to copy, distribute | |
| 11 | * and/or modify this software under the terms either: | |
| 12 | * | |
| 13 | * a) the GNU General Public License as published by the Free Software | |
| 14 | * Foundation; either version 2 of the License, or (at your option) | |
| 15 | * any later version, | |
| 16 | * | |
| 17 | * or | |
| 18 | * | |
| 19 | * b) any version of the Melati Software License, as published | |
| 20 | * at http://melati.org | |
| 21 | * | |
| 22 | * You should have received a copy of the GNU General Public License and | |
| 23 | * the Melati Software License along with this program; | |
| 24 | * if not, write to the Free Software Foundation, Inc., | |
| 25 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the | |
| 26 | * GNU General Public License and visit http://melati.org to obtain the | |
| 27 | * Melati Software License. | |
| 28 | * | |
| 29 | * Feel free to contact the Developers of Melati (http://melati.org), | |
| 30 | * if you would like to work out a different arrangement than the options | |
| 31 | * outlined here. It is our intention to allow Melati to be used by as | |
| 32 | * wide an audience as possible. | |
| 33 | * | |
| 34 | * This program is distributed in the hope that it will be useful, | |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 37 | * GNU General Public License for more details. | |
| 38 | * | |
| 39 | * Contact details for copyright holder: | |
| 40 | * | |
| 41 | * Tim Pizey <timp At paneris.org> | |
| 42 | * http://paneris.org/~timp | |
| 43 | */ | |
| 44 | ||
| 45 | package org.melati.poem; | |
| 46 | ||
| 47 | import java.util.Enumeration; | |
| 48 | ||
| 49 | import org.melati.poem.util.MappedEnumeration; | |
| 50 | import org.melati.poem.util.StringUtils; | |
| 51 | ||
| 52 | /** | |
| 53 | * A data type that is a reference to a {@link Persistent} object via a | |
| 54 | * String key to a unique, non-nullable index. | |
| 55 | * NOTE: The field may be a different size to the field in the target table. | |
| 56 | * | |
| 57 | * @author timp | |
| 58 | * @since 2012-05-19 | |
| 59 | */ | |
| 60 | 17 | public class StringKeyReferencePoemType extends StringKeyPoemType implements PersistentReferencePoemType, PoemType<String> { |
| 61 | ||
| 62 | private Table<?> targetTable; | |
| 63 | ||
| 64 | private String targetKeyName; | |
| 65 | ||
| 66 | /** | |
| 67 | * Constructor. | |
| 68 | * | |
| 69 | * @param targetTable the Table the type refers to | |
| 70 | * @param targetKeyName name of key both in this and target table | |
| 71 | * @param nullable whether this type may contain null values | |
| 72 | * @param size the size of the key string | |
| 73 | */ | |
| 74 | public StringKeyReferencePoemType(Table<?> targetTable, | |
| 75 | String targetKeyName, boolean nullable, int size) { | |
| 76 | 69 | super(nullable, size); |
| 77 | 69 | if (targetTable == null) |
| 78 | 2 | throw new NullPointerException(); |
| 79 | 67 | this.targetTable = targetTable; |
| 80 | 67 | if (targetKeyName == null) |
| 81 | 2 | throw new NullPointerException(); |
| 82 | 65 | this.targetKeyName = targetKeyName; |
| 83 | 65 | } |
| 84 | ||
| 85 | /** | |
| 86 | * @see org.melati.poem.PersistentReferencePoemType#targetTable() | |
| 87 | */ | |
| 88 | @Override | |
| 89 | public Table<?> targetTable() { | |
| 90 | 73 | return targetTable; |
| 91 | } | |
| 92 | ||
| 93 | /** @return the key name in target table, also this table */ | |
| 94 | public String targetKeyName() { | |
| 95 | 7 | return targetKeyName; |
| 96 | } | |
| 97 | /** | |
| 98 | * Returns an <code>Enumeration</code> of the possible raw values. | |
| 99 | * <p> | |
| 100 | * In this case the key values of rows in the referenced table. | |
| 101 | */ | |
| 102 | @SuppressWarnings({ "unchecked", "rawtypes" }) | |
| 103 | protected Enumeration<String> _possibleRaws() { | |
| 104 | 20 | return new MappedEnumeration(targetTable.selection()) { |
| 105 | public String mapped(Object p) { | |
| 106 | 15 | return (String)((Persistent)p).getRaw(targetKeyName); |
| 107 | } | |
| 108 | }; | |
| 109 | } | |
| 110 | ||
| 111 | protected void _assertValidCooked(Object cooked) | |
| 112 | throws ValidationPoemException { | |
| 113 | 9 | if (!(cooked instanceof Persistent)) |
| 114 | 3 | throw new TypeMismatchPoemException(cooked, this); |
| 115 | ||
| 116 | 6 | Persistent persistent = (Persistent)cooked; |
| 117 | ||
| 118 | 6 | if (persistent.getTable() != targetTable) |
| 119 | 2 | throw new ValidationPoemException( |
| 120 | this, persistent, | |
| 121 | new TableMismatchPoemException(persistent, targetTable)); | |
| 122 | 4 | } |
| 123 | ||
| 124 | protected Object _cookedOfRaw(Object raw) throws NoSuchRowPoemException { | |
| 125 | 3 | return targetTable.getColumn(targetKeyName).firstWhereEq(raw); |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * NOTE that it is expected that the targetKeyName is also | |
| 130 | * the name of the field in this persistent. | |
| 131 | */ | |
| 132 | protected String _rawOfCooked(Object cooked) { | |
| 133 | 2 | return (String) ((Persistent)cooked).getField(targetKeyName).getRawString(); |
| 134 | } | |
| 135 | ||
| 136 | protected String _stringOfCooked(Object cooked, | |
| 137 | PoemLocale locale, int style) | |
| 138 | throws PoemException { | |
| 139 | 0 | return ((Persistent)cooked).displayString(locale, style); |
| 140 | } | |
| 141 | ||
| 142 | protected boolean _canRepresent(SQLPoemType<?> other) { | |
| 143 | 2 | return |
| 144 | other instanceof StringKeyReferencePoemType && | |
| 145 | ((StringKeyReferencePoemType)other).targetTable == targetTable; | |
| 146 | } | |
| 147 | ||
| 148 | protected void _saveColumnInfo(ColumnInfo columnInfo) | |
| 149 | throws AccessPoemException { | |
| 150 | 21 | columnInfo.setTypefactoryCode(targetTable.tableInfoID()); |
| 151 | 21 | columnInfo.setSize(getSize()); |
| 152 | 21 | } |
| 153 | ||
| 154 | /** | |
| 155 | * {@inheritDoc} | |
| 156 | * @see org.melati.poem.BasePoemType#toString() | |
| 157 | */ | |
| 158 | public String toString() { | |
| 159 | 0 | return |
| 160 | 0 | "string key reference to " + targetTable.getName() + |
| 161 | " on " + targetKeyName + | |
| 162 | 0 | " (" + super.toString() + ")"; |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * {@inheritDoc} | |
| 167 | * @see org.melati.poem.PoemType#toDsdType() | |
| 168 | */ | |
| 169 | public String toDsdType() { | |
| 170 | 2 | return StringUtils.capitalised(targetTable.getName() + " StringKeyReference on " + targetKeyName); |
| 171 | } | |
| 172 | } |