StringKeyReferencePoemType.java

  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. package org.melati.poem;

  45. import java.util.Enumeration;

  46. import org.melati.poem.util.MappedEnumeration;
  47. import org.melati.poem.util.StringUtils;

  48. /**
  49.  * A data type that is a reference to a {@link Persistent} object via a
  50.  * String key to a unique, non-nullable index.
  51.  * NOTE: The field may be a different size to the field in the target table.
  52.  *
  53.  * @author timp
  54.  * @since 2012-05-19
  55.  */
  56. public class StringKeyReferencePoemType extends StringKeyPoemType implements PersistentReferencePoemType, PoemType<String> {

  57.   private Table<?> targetTable;
  58.  
  59.   private String targetKeyName;

  60.   /**
  61.    * Constructor.
  62.    *
  63.    * @param targetTable the Table the type refers to
  64.    * @param targetKeyName name of key both in this and target table
  65.    * @param nullable whether this type may contain null values
  66.    * @param size the size of the key string
  67.    */
  68.   public StringKeyReferencePoemType(Table<?> targetTable,
  69.       String targetKeyName, boolean nullable, int size) {
  70.     super(nullable, size);
  71.     if (targetTable == null)
  72.       throw new NullPointerException();
  73.     this.targetTable = targetTable;
  74.     if (targetKeyName == null)
  75.       throw new NullPointerException();
  76.     this.targetKeyName = targetKeyName;
  77.   }

  78.   /**
  79.    * @see org.melati.poem.PersistentReferencePoemType#targetTable()
  80.    */
  81.   @Override
  82.   public Table<?> targetTable() {
  83.     return targetTable;
  84.   }

  85.   /** @return the key name in target table, also this table */
  86.   public String targetKeyName() {
  87.     return targetKeyName;
  88.   }
  89.   /**
  90.    * Returns an <code>Enumeration</code> of the possible raw values.
  91.    * <p>
  92.    * In this case the key values of rows in the referenced table.
  93.    */
  94.   @SuppressWarnings({ "unchecked", "rawtypes" })
  95.   protected Enumeration<String> _possibleRaws() {
  96.     return new MappedEnumeration(targetTable.selection()) {
  97.       public String mapped(Object p) {
  98.         return (String)((Persistent)p).getRaw(targetKeyName);
  99.       }
  100.     };
  101.   }

  102.   protected void _assertValidCooked(Object cooked)
  103.       throws ValidationPoemException {
  104.     if (!(cooked instanceof Persistent))
  105.       throw new TypeMismatchPoemException(cooked, this);

  106.     Persistent persistent = (Persistent)cooked;

  107.     if (persistent.getTable() != targetTable)
  108.       throw new ValidationPoemException(
  109.           this, persistent,
  110.           new TableMismatchPoemException(persistent, targetTable));
  111.   }

  112.   protected Object _cookedOfRaw(Object raw) throws NoSuchRowPoemException {
  113.     return targetTable.getColumn(targetKeyName).firstWhereEq(raw);
  114.   }

  115.   /**
  116.    * NOTE that it is expected that the targetKeyName is also
  117.    * the name of the field in this persistent.
  118.    */
  119.   protected String  _rawOfCooked(Object cooked) {
  120.     return (String) ((Persistent)cooked).getField(targetKeyName).getRawString();
  121.   }

  122.   protected String _stringOfCooked(Object cooked,
  123.                                    PoemLocale locale, int style)
  124.       throws PoemException {
  125.     return ((Persistent)cooked).displayString(locale, style);
  126.   }

  127.   protected boolean _canRepresent(SQLPoemType<?> other) {
  128.     return
  129.         other instanceof StringKeyReferencePoemType &&
  130.         ((StringKeyReferencePoemType)other).targetTable == targetTable;
  131.   }

  132.   protected void _saveColumnInfo(ColumnInfo columnInfo)
  133.       throws AccessPoemException {
  134.     columnInfo.setTypefactoryCode(targetTable.tableInfoID());
  135.     columnInfo.setSize(getSize());
  136.   }

  137.   /**
  138.    * {@inheritDoc}
  139.    * @see org.melati.poem.BasePoemType#toString()
  140.    */
  141.   public String toString() {
  142.     return
  143.         "string key reference to " + targetTable.getName() +
  144.         " on " + targetKeyName +
  145.         " (" + super.toString() + ")";
  146.   }

  147.   /**
  148.    * {@inheritDoc}
  149.    * @see org.melati.poem.PoemType#toDsdType()
  150.    */
  151.   public String toDsdType() {
  152.     return StringUtils.capitalised(targetTable.getName() + " StringKeyReference on " + targetKeyName);
  153.   }
  154. }