Contact.java

  1. package org.melati.example.contacts;

  2. import org.melati.admin.AnticipatedException;
  3. import org.melati.example.contacts.generated.ContactBase;
  4. import org.melati.poem.*;
  5. import org.melati.poem.util.EnumUtils;

  6. import java.util.ArrayList;
  7. import java.util.Enumeration;
  8. import java.util.Vector;
  9. /**
  10.  * Melati POEM generated, programmer modifiable stub
  11.  * for a <code>Persistent</code> <code>Contact</code> object.
  12.  *
  13.  * <p>
  14.  * Description:
  15.  *   A Contact.
  16.  * </p>
  17.  *
  18.  * <table>
  19.  * <caption>
  20.  * Field summary for SQL table <code>Contact</code>
  21.  * </caption>
  22.  * <tr><th>Name</th><th>Type</th><th>Description</th></tr>
  23.  * <tr><td> id </td><td> Integer </td><td> &nbsp; </td></tr>
  24.  * <tr><td> name </td><td> String </td><td> Contact Name </td></tr>
  25.  * <tr><td> owner </td><td> Contact </td><td> Contact who owns this contact
  26.  * </td></tr>
  27.  * <tr><td> address </td><td> String </td><td> Contact Address </td></tr>
  28.  * <tr><td> updates </td><td> Integer </td><td> How many times has this
  29.  * record been updated? </td></tr>
  30.  * <tr><td> lastupdated </td><td> Date </td><td> When was this last updated?
  31.  * </td></tr>
  32.  * <tr><td> lastupdateuser </td><td> User </td><td> Who last updated this?
  33.  * </td></tr>
  34.  * </table>
  35.  *
  36.  * see org.melati.poem.prepro.TableDef#generatePersistentJava
  37.  */
  38. public class Contact extends ContactBase implements Treeable {
  39.   /**
  40.    * Thrown when an attempt to make a descendant an ancestor is made.
  41.    * @author timp
  42.    */
  43.   public class DescendantParentException extends AnticipatedException {
  44.     private static final long serialVersionUID = 1L;
  45.     /**
  46.      * Constructor.
  47.      * @param message the message to display
  48.      */
  49.     public DescendantParentException(String message) {
  50.       super(message);
  51.     }
  52.   }
  53. /**
  54.   * Constructor
  55.   * for a <code>Persistent</code> <code>Contact</code> object.
  56.   * <p>
  57.   * Description:
  58.   *   A Contact.
  59.   * </p>
  60.   *
  61.   * see org.melati.poem.prepro.TableDef#generatePersistentJava
  62.   */
  63.   public Contact() { }
  64.   // programmer's domain-specific code here

  65.   /**
  66.    * @return whether contact is in the category
  67.    */
  68.   public boolean isIn(Category category) {
  69.      ContactsDatabase db = (ContactsDatabase)getContactsDatabaseTables();
  70.      String sql = db.quotedName("contact") + " = " + getTroid() + " AND " +
  71.        db.quotedName("category") + " = " + category.getTroid();
  72.      return db.getContactCategoryTable().exists(sql);
  73.   }

  74.   protected void writeLock() {
  75.     super.writeLock();
  76.     setLastupdated_unsafe(new java.sql.Date(new java.util.Date().getTime()));
  77.     if (PoemThread.accessToken() instanceof User)
  78.       setLastupdateuser_unsafe(((User)PoemThread.accessToken()).getTroid());
  79.     else
  80.       setLastupdateuser_unsafe(new Integer(1));

  81.     Integer count = getUpdates();
  82.     if (count == null) count = new Integer(0);
  83.     setUpdates_unsafe(new Integer(count.intValue()+1));
  84.   }

  85.   public Treeable[] getChildren() {
  86.     return (Contact.arrayOf(getContactTable().getOwnerColumn().
  87.                selectionWhereEq(troid())));
  88.   }

  89.   /**
  90.    * @return the ancestors
  91.    */
  92.   public ArrayList<Integer> getAncestors() {
  93.     ArrayList<Integer> l = new ArrayList<Integer>();
  94.     Contact p = getOwner();
  95.     while (p != null) {
  96.         l.add(new Integer(p.troid().intValue()));
  97.         p = p.getOwner();
  98.     }
  99.     return l;
  100. }
  101.   public void setOwner(Contact cooked)
  102.     throws AccessPoemException {
  103.     if (cooked != null && cooked.getAncestors().contains(this.troid())) {
  104.       throw new DescendantParentException("Owner must not be a descendant.");
  105.     }
  106.     super.setOwner(cooked);
  107.   }

  108.   /**
  109.    * @param v vector of Treeables
  110.    * @return an array of Treeables
  111.    */
  112.   public static Treeable[] arrayOf(Vector<Persistent> v) {
  113.     Treeable[] arr;
  114.     synchronized (v) {
  115.       arr = new Treeable[v.size()];
  116.       v.copyInto(arr);
  117.     }
  118.     return arr;
  119.   }
  120.   /**
  121.    * @param e enumeration of Treeables
  122.    * @return an array of Treeables
  123.    */
  124.   public static Treeable[] arrayOf(Enumeration<Persistent> e) {
  125.     Vector<Persistent> v = EnumUtils.vectorOf(e);
  126.     return arrayOf(v);
  127.   }

  128. }