0
|
1 /*
|
|
2
|
|
3 Copyright (C) 2003 Franklin Schmidt <frank@gustos.com>
|
|
4
|
|
5 */
|
|
6
|
|
7 package nabble.model;
|
|
8
|
|
9 import fschmidt.db.DbDatabase;
|
|
10 import fschmidt.db.Listener;
|
|
11 import fschmidt.util.mail.Mail;
|
|
12 import fschmidt.util.mail.MailException;
|
|
13 import fschmidt.util.mail.MailHome;
|
|
14 import nabble.modules.ModuleManager;
|
|
15 import nabble.naml.compiler.Template;
|
|
16 import nabble.naml.compiler.TemplatePrintWriter;
|
|
17 import nabble.naml.namespaces.BasicNamespace;
|
|
18 import nabble.view.web.template.NabbleNamespace;
|
|
19 import nabble.view.web.template.NodePageNamespace;
|
|
20 import org.slf4j.Logger;
|
|
21 import org.slf4j.LoggerFactory;
|
|
22
|
|
23 import java.io.File;
|
|
24 import java.io.Serializable;
|
|
25 import java.lang.reflect.Constructor;
|
|
26 import java.lang.reflect.InvocationTargetException;
|
|
27 import java.sql.ResultSet;
|
|
28 import java.sql.SQLException;
|
|
29 import java.util.ArrayList;
|
|
30 import java.util.Collections;
|
|
31 import java.util.HashSet;
|
|
32 import java.util.List;
|
|
33 import java.util.Map;
|
|
34 import java.util.Set;
|
|
35
|
|
36
|
|
37 public final class ModelHome {
|
|
38 private ModelHome() {} // never
|
|
39
|
|
40 private static final Logger logger = LoggerFactory.getLogger(ModelHome.class);
|
|
41
|
|
42 public static final String AVATAR_SMALL = "avatar24.png";
|
|
43 public static final String AVATAR_BIG = "avatar100.png";
|
|
44
|
|
45 public static volatile long lastDigestRun = System.currentTimeMillis();
|
|
46
|
|
47 public static Site newSite(String type,String subject,String message,Message.Format msgFmt,String email,String username)
|
|
48 throws ModelException
|
|
49 {
|
|
50 return DbSiteCreator.newSite(type,subject,message,msgFmt,email,username);
|
|
51 }
|
|
52
|
|
53 public static Site restoreSiteToOldId(String filename) {
|
|
54 return DbSiteCreator.restoreSiteToOldId(new File(filename));
|
|
55 }
|
|
56
|
|
57 public static Site restoreSiteToNewId(String filename) {
|
|
58 return DbSiteCreator.restoreSiteToNewId(new File(filename));
|
|
59 }
|
|
60
|
|
61 public static void validateEmail(String email) throws ModelException {
|
|
62 UserImpl.validateEmail(email);
|
|
63 }
|
|
64
|
|
65 public static String hideEmail(String emailTo) {
|
|
66 return "[hidden email]";
|
|
67 }
|
|
68
|
|
69 public static String hideAllEmails(String txt) {
|
|
70 return MessageUtils.hideAllEmails(txt);
|
|
71 }
|
|
72
|
|
73 public static Site getSite(long id) {
|
|
74 SiteKey siteKey = SiteKey.getInstance(id);
|
|
75 if( siteKey.siteGlobal() == null )
|
|
76 return null;
|
|
77 return siteKey.siteGlobal().site();
|
|
78 }
|
|
79
|
|
80 public static void setRemoteAddr(Site site, String removeAddr) {
|
|
81 ((SiteImpl) site).siteGlobal().setRemoteAddr(removeAddr);
|
|
82 }
|
|
83
|
|
84 // force these classes to add their listeners first
|
|
85 static {
|
|
86 Init.modelHomeStarted = true;
|
|
87 NodeImpl.nop();
|
|
88 Lucene.nop();
|
|
89 DailyNumber.nop();
|
|
90 FileUpload.nop();
|
|
91 nabble.view.lib.MyJtpServlet.nop();
|
|
92 SubscriptionImpl.nop();
|
|
93 ModuleManager.nop();
|
|
94 }
|
|
95
|
|
96 private static Set<Node> doneSet(Map<String,Object> transMap) {
|
|
97 if( transMap==null )
|
|
98 return new HashSet<Node>();
|
|
99 @SuppressWarnings("unchecked")
|
|
100 Set<Node> done = (Set<Node>)transMap.get("doneNodes");
|
|
101 if( done == null ) {
|
|
102 done = new HashSet<Node>();
|
|
103 transMap.put("doneNodes",done);
|
|
104 }
|
|
105 return done;
|
|
106 }
|
|
107
|
|
108 public static void addDescendantChangeListener(final Listener<Node> listener) {
|
|
109 Listener<Node> myListener = new Listener<Node>() {
|
|
110 public void event(final Node node) {
|
|
111 if( ModelHome.insideImportProcedure.get() )
|
|
112 return;
|
|
113 DbDatabase db = node.getSite().getDb();
|
|
114 final Map<String,Object> transMap = db.transactionMap();
|
|
115 db.runAfterCommit(new Runnable(){public void run(){
|
|
116 Set<Node> done = doneSet(transMap);
|
|
117 for( Node n : node.getAncestors() ) {
|
|
118 if( done.add(n) )
|
|
119 listener.event(n);
|
|
120 }
|
|
121 }});
|
|
122 }
|
|
123 };
|
|
124 NodeImpl.childChangeListeners.add(myListener);
|
|
125 NodeImpl.addPostUpdateListener(myListener);
|
|
126 NodeImpl.addPostDeleteListener(myListener);
|
|
127 NodeImpl.addPostInsertListener(new Listener<Node>() {
|
|
128 public void event(final Node node) {
|
|
129 DbDatabase db = node.getSite().getDb();
|
|
130 final Map<String,Object> transMap = db.transactionMap();
|
|
131 db.runAfterCommit(new Runnable(){public void run(){
|
|
132 Set<Node> done = doneSet(transMap);
|
|
133 NodeIterator<? extends Node> ancestors = node.getAncestors();
|
|
134 done.add(ancestors.next()); // skip self
|
|
135 for( Node n : ancestors ) {
|
|
136 if( done.add(n) )
|
|
137 listener.event(n);
|
|
138 }
|
|
139 }});
|
|
140 }
|
|
141 });
|
|
142 }
|
|
143
|
|
144 public static void addSiteChangeListener(Listener<Site> listener) {
|
|
145 SiteImpl.addChangeListener(listener);
|
|
146 }
|
|
147
|
|
148 public static void addSitePreChangeListener(Listener<Site> listener) {
|
|
149 SiteImpl.addPreChangeListener(listener);
|
|
150 }
|
|
151
|
|
152 public static void addNodeChangeListener(final Listener<Node> listener) {
|
|
153 NodeImpl.addChangeListener(listener);
|
|
154 }
|
|
155
|
|
156 public static void addNodeInsertListener(final Listener<Node> listener) {
|
|
157 NodeImpl.addPostInsertListener(listener);
|
|
158 }
|
|
159
|
|
160 public static void addNodeDeleteListener(final Listener<Node> nodeDeleteListener) {
|
|
161 NodeImpl.addPostDeleteListener(nodeDeleteListener);
|
|
162 }
|
|
163
|
|
164 public static void addNodePreUpdatedListener(final Listener<Node> listener) {
|
|
165 NodeImpl.preUpdateListeners.add(listener);
|
|
166 }
|
|
167
|
|
168 public static void addGotParentListener(Listener<Node> listener) {
|
|
169 NodeImpl.gotParentListeners.add(listener);
|
|
170 }
|
|
171
|
|
172 public static void addUserInsertListener(final Listener<User> listener) {
|
|
173 UserImpl.addPostInsertListener(listener);
|
|
174 }
|
|
175
|
|
176 public static void addUserPreUpdatedListener(final Listener<User> listener) {
|
|
177 UserImpl.preUpdateListeners.add(listener);
|
|
178 }
|
|
179
|
|
180 static ThreadLocal<Boolean> insideImportProcedure = new ThreadLocal<Boolean>() {
|
|
181 protected Boolean initialValue() {
|
|
182 return Boolean.FALSE;
|
|
183 }
|
|
184 };
|
|
185
|
|
186 public static void beginImport() {
|
|
187 insideImportProcedure.set(true);
|
|
188 }
|
|
189
|
|
190 public static void endImport() {
|
|
191 insideImportProcedure.set(false);
|
|
192 }
|
|
193
|
|
194 public static void preloadMessages(List<Node> nodes) {
|
|
195 NodeImpl.preloadMessages(nodes);
|
|
196 }
|
|
197
|
|
198 public static String assignmentCondition(User user,Integer priority) {
|
|
199 if( user==null && priority==null )
|
|
200 return null;
|
|
201 StringBuilder buf = new StringBuilder();
|
|
202 if( user != null ) {
|
|
203 buf.append( "assigned_user=" ).append( user.getId() );
|
|
204 }
|
|
205 if( priority != null ) {
|
|
206 if( buf.length() > 0 )
|
|
207 buf.append( " and " );
|
|
208 buf.append( "assigned_priority=" ).append( priority );
|
|
209 }
|
|
210 return buf.toString();
|
|
211 }
|
|
212
|
|
213
|
|
214
|
|
215 public static Long getSiteIdFromDomain(String customDomain) {
|
|
216 return SiteGlobal.getSiteIdFromDomain(customDomain);
|
|
217 }
|
|
218
|
|
219
|
|
220 public static final String noReply = "no-reply@nabble.com";
|
|
221 static final String bounces = "bounces@n2.nabble.com";
|
|
222
|
|
223 public static void send(Mail mail) throws MailException {
|
|
224 if( mail.getFrom().getAddrSpec().equals(noReply) ) {
|
|
225 MailHome.getDefaultSmtpServer().sendFrom(mail, bounces);
|
|
226 } else {
|
|
227 MailHome.getDefaultSmtpServer().send(mail);
|
|
228 }
|
|
229 }
|
|
230
|
|
231 public static void send(Mail mail, String smtpFrom) throws MailException {
|
|
232 if( mail.getFrom().getAddrSpec().equals(noReply) ) {
|
|
233 MailHome.getDefaultSmtpServer().sendFrom(mail, bounces);
|
|
234 } else {
|
|
235 MailHome.getDefaultSmtpServer().sendFrom(mail, smtpFrom);
|
|
236 }
|
|
237 }
|
|
238
|
|
239
|
|
240 public static <S,T> ExtensionFactory<S,T> standardExtensionFactory(final String name,final Class<S> sourceClass,final Class<T> extensionClass) {
|
|
241 return new ExtensionFactory<S,T>() {
|
|
242
|
|
243 public String getName() {
|
|
244 return name;
|
|
245 }
|
|
246
|
|
247 public Class<T> extensionClass() {
|
|
248 return extensionClass;
|
|
249 }
|
|
250
|
|
251 public T construct(S source) {
|
|
252 try {
|
|
253 Constructor<T> constructor = extensionClass.getConstructor(sourceClass);
|
|
254 constructor.setAccessible(true);
|
|
255 return constructor.newInstance(source);
|
|
256 } catch(NoSuchMethodException e) {
|
|
257 throw new RuntimeException(e);
|
|
258 } catch(InstantiationException e) {
|
|
259 throw new RuntimeException(e);
|
|
260 } catch(IllegalAccessException e) {
|
|
261 throw new RuntimeException(e);
|
|
262 } catch(InvocationTargetException e) {
|
|
263 Throwable cause = e.getCause();
|
|
264 if( cause instanceof Error )
|
|
265 throw (Error)cause;
|
|
266 if( cause instanceof RuntimeException )
|
|
267 throw (RuntimeException)cause;
|
|
268 throw new RuntimeException(e);
|
|
269 }
|
|
270 }
|
|
271
|
|
272 public T construct(S source,ResultSet rs) {
|
|
273 return construct(source);
|
|
274 }
|
|
275
|
|
276 public Serializable getExportData(S source) {
|
|
277 return null;
|
|
278 }
|
|
279
|
|
280 public void saveExportData(S source,Serializable data) {
|
|
281 throw new RuntimeException();
|
|
282 }
|
|
283 };
|
|
284 }
|
|
285
|
|
286 public static <T> void addSiteExtensionFactory(ExtensionFactory<Site,T> factory) {
|
|
287 SiteImpl.addExtensionFactory(factory);
|
|
288 }
|
|
289
|
|
290 public static <T> void addNodeExtensionFactory(ExtensionFactory<Node,T> factory) {
|
|
291 NodeImpl.addExtensionFactory(factory);
|
|
292 }
|
|
293
|
|
294 public static <T> void addUserExtensionFactory(ExtensionFactory<User,T> factory) {
|
|
295 UserImpl.addExtensionFactory(factory);
|
|
296 }
|
|
297
|
|
298
|
|
299 public static List<Site> getSitesForTask(String task) throws SQLException {
|
|
300 List<Site> sites = new ArrayList<Site>();
|
|
301 for( SiteKey siteKey : SiteKey.getSiteKeys(task) ) {
|
|
302 try {
|
|
303 sites.add( siteKey.site() );
|
|
304 } catch(UpdatingException e) {} // skip it
|
|
305 }
|
|
306 return sites;
|
|
307 }
|
|
308
|
|
309 }
|