View Javadoc

1   package org.melati.example.contacts;
2   
3   import java.util.ArrayList;
4   import java.util.Enumeration;
5   import java.util.Vector;
6   import org.melati.example.contacts.generated.ContactBase;
7   import org.melati.poem.AccessPoemException;
8   import org.melati.poem.User;
9   import org.melati.poem.PoemThread;
10  import org.melati.poem.util.EnumUtils;
11  import org.melati.admin.AnticipatedException;
12  import org.melati.poem.Treeable;
13  /**
14   * A <code>Contact</code> object, embellished from the original,
15   * Melati POEM generated, programmer modifiable stub.
16   *
17   * <p>
18   * Description:
19   *   A Contact.
20   * </p>
21   *
22   * <table>
23   * <tr><th colspan='3'>
24   * Field summary for SQL table <code>Contact</code>
25   * </th></tr>
26   * <tr><th>Name</th><th>Type</th><th>Description</th></tr>
27   * <tr><td> id </td><td> Integer </td><td> &nbsp; </td></tr>
28   * <tr><td> name </td><td> String </td><td> Contact Name </td></tr>
29   * <tr><td> owner </td><td> Contact </td><td> Contact who owns this contact
30   * </td></tr>
31   * <tr><td> address </td><td> String </td><td> Contact Address </td></tr>
32   * <tr><td> updates </td><td> Integer </td><td> How many times has this
33   * record been updated? </td></tr>
34   * <tr><td> lastupdated </td><td> Date </td><td> When was this last updated?
35   * </td></tr>
36   * <tr><td> lastupdateuser </td><td> User </td><td> Who last updated this?
37   * </td></tr>
38   * </table>
39   *
40   * @generator org.melati.poem.prepro.TableDef#generateMainJava
41   */
42  public class Contact extends ContactBase implements Treeable {
43    /**
44     * Thrown when an attempt to make a descendant an ancestor is made.
45     * @author timp
46     */
47    public class DescendantParentException extends AnticipatedException {
48      private static final long serialVersionUID = 1L;
49      /**
50       * Constructor.
51       * @param message the message to display
52       */
53      public DescendantParentException(String message) {
54        super(message);
55      }
56    }
57  /**
58    * Constructor
59    * for a <code>Persistent</code> <code>Contact</code> object.
60    * <p>
61    * Description:
62    *   A Contact.
63    * </p>
64    *
65    * @generator org.melati.poem.prepro.TableDef#generateMainJava
66    */
67    public Contact() { }
68    // programmer's domain-specific code here
69  
70    /**
71     * @return whether contact is in the category
72     */
73    public boolean isIn(Category category) {
74       ContactsDatabase db = (ContactsDatabase)getContactsDatabaseTables();
75       String sql = db.quotedName("contact") + " = " + getTroid() + " AND " +
76         db.quotedName("category") + " = " + category.getTroid();
77       return db.getContactCategoryTable().exists(sql);
78    }
79  
80    protected void writeLock() {
81      super.writeLock();
82      setLastupdated_unsafe(new java.sql.Date(new java.util.Date().getTime()));
83      if (PoemThread.accessToken() instanceof User)
84        setLastupdateuser_unsafe(((User)PoemThread.accessToken()).getTroid());
85      else
86        setLastupdateuser_unsafe(new Integer(1));
87  
88      Integer count = getUpdates();
89      if (count == null) count = new Integer(0);
90      setUpdates_unsafe(new Integer(count.intValue()+1));
91    }
92  
93    public Treeable[] getChildren() {
94      return (Contact.arrayOf(getContactTable().getOwnerColumn().
95                 selectionWhereEq(troid())));
96    }
97  
98    /**
99     * @return the ancestors
100    */
101   public ArrayList getAncestors() {
102     ArrayList l = new ArrayList();
103     Contact p = getOwner();
104     while (p != null) {
105         l.add(new Integer(p.troid().intValue()));
106         p = p.getOwner();
107     }
108     return l;
109 }
110   public void setOwner(Contact cooked)
111     throws AccessPoemException {
112     if (cooked != null && cooked.getAncestors().contains(this.troid())) {
113       throw new DescendantParentException("Owner must not be a descendant.");
114     }
115     super.setOwner(cooked);
116   }
117 
118   /**
119    * @param v vector od Treeables
120    * @return an array of Treeables
121    */
122   public static Treeable[] arrayOf(Vector v) {
123     Treeable[] arr;
124     synchronized (v) {
125       arr = new Treeable[v.size()];
126       v.copyInto(arr);
127     }
128     return arr;
129   }
130   /**
131    * @param e enumeration of Treeables
132    * @return an array of Treeables
133    */
134   public static Treeable[] arrayOf(Enumeration e) {
135     Vector v = EnumUtils.vectorOf(e);
136     return arrayOf(v);
137   }
138 
139 }
140