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
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 public class Contact extends ContactBase implements Treeable {
41
42
43
44
45 public class DescendantParentException extends AnticipatedException {
46 private static final long serialVersionUID = 1L;
47
48
49
50
51 public DescendantParentException(String message) {
52 super(message);
53 }
54 }
55
56
57
58
59
60
61
62
63
64
65 public Contact() { }
66
67
68
69
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
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
118
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
130
131
132 public static Treeable[] arrayOf(Enumeration<Persistent> e) {
133 Vector<Persistent> v = EnumUtils.vectorOf(e);
134 return arrayOf(v);
135 }
136
137 }
138