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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class Contact extends ContactBase implements Treeable {
43
44
45
46
47 public class DescendantParentException extends AnticipatedException {
48 private static final long serialVersionUID = 1L;
49
50
51
52
53 public DescendantParentException(String message) {
54 super(message);
55 }
56 }
57
58
59
60
61
62
63
64
65
66
67 public Contact() { }
68
69
70
71
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
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
120
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
132
133
134 public static Treeable[] arrayOf(Enumeration e) {
135 Vector v = EnumUtils.vectorOf(e);
136 return arrayOf(v);
137 }
138
139 }
140