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