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 // unfiltered, Site.getUser() is preferred
|
|
81 public static User getUserFromId(long id) {
|
|
82 return UserImpl.getUser(id);
|
|
83 }
|
|
84 */
|
|
85 public static String getNodeReplyAddress(Node node, User user) {
|
|
86 return PostByEmail.getMailAddress(user, node);
|
|
87 }
|
|
88
|
|
89 public static String getUserBouncesAddress(User user) {
|
|
90 return PostByEmail.getBouncesAddress(user);
|
|
91 }
|
|
92
|
|
93 public static void setNodeHeaders(Mail mail, Node node) {
|
|
94 MailSubsystem.setHeaders(mail, (NodeImpl) node);
|
|
95 }
|
|
96
|
|
97 public static void setRemoteAddr(Site site, String removeAddr) {
|
|
98 ((SiteImpl) site).siteGlobal().setRemoteAddr(removeAddr);
|
|
99 }
|
|
100
|
|
101 // force these classes to add their listeners first
|
|
102 static {
|
|
103 Init.modelHomeStarted = true;
|
|
104 NodeImpl.nop();
|
|
105 Lucene.nop();
|
|
106 DailyNumber.nop();
|
|
107 FileUpload.nop();
|
|
108 nabble.view.lib.MyJtpServlet.nop();
|
|
109 SubscriptionImpl.nop();
|
|
110 ModuleManager.nop();
|
|
111 }
|
|
112
|
|
113 private static Set<Node> doneSet(Map<String,Object> transMap) {
|
|
114 if( transMap==null )
|
|
115 return new HashSet<Node>();
|
|
116 @SuppressWarnings("unchecked")
|
|
117 Set<Node> done = (Set<Node>)transMap.get("doneNodes");
|
|
118 if( done == null ) {
|
|
119 done = new HashSet<Node>();
|
|
120 transMap.put("doneNodes",done);
|
|
121 }
|
|
122 return done;
|
|
123 }
|
|
124
|
|
125 public static void addDescendantChangeListener(final Listener<Node> listener) {
|
|
126 Listener<Node> myListener = new Listener<Node>() {
|
|
127 public void event(final Node node) {
|
|
128 if( ModelHome.insideImportProcedure.get() )
|
|
129 return;
|
|
130 DbDatabase db = node.getSite().getDb();
|
|
131 final Map<String,Object> transMap = db.transactionMap();
|
|
132 db.runAfterCommit(new Runnable(){public void run(){
|
|
133 Set<Node> done = doneSet(transMap);
|
|
134 for( Node n : node.getAncestors() ) {
|
|
135 if( done.add(n) )
|
|
136 listener.event(n);
|
|
137 }
|
|
138 }});
|
|
139 }
|
|
140 };
|
|
141 NodeImpl.childChangeListeners.add(myListener);
|
|
142 NodeImpl.addPostUpdateListener(myListener);
|
|
143 NodeImpl.addPostDeleteListener(myListener);
|
|
144 NodeImpl.addPostInsertListener(new Listener<Node>() {
|
|
145 public void event(final Node node) {
|
|
146 DbDatabase db = node.getSite().getDb();
|
|
147 final Map<String,Object> transMap = db.transactionMap();
|
|
148 db.runAfterCommit(new Runnable(){public void run(){
|
|
149 Set<Node> done = doneSet(transMap);
|
|
150 NodeIterator<? extends Node> ancestors = node.getAncestors();
|
|
151 done.add(ancestors.next()); // skip self
|
|
152 for( Node n : ancestors ) {
|
|
153 if( done.add(n) )
|
|
154 listener.event(n);
|
|
155 }
|
|
156 }});
|
|
157 }
|
|
158 });
|
|
159 }
|
|
160
|
|
161 public static void addSiteChangeListener(Listener<Site> listener) {
|
|
162 SiteImpl.addChangeListener(listener);
|
|
163 }
|
|
164
|
|
165 public static void addSitePreChangeListener(Listener<Site> listener) {
|
|
166 SiteImpl.addPreChangeListener(listener);
|
|
167 }
|
|
168
|
|
169 public static void addNodeChangeListener(final Listener<Node> listener) {
|
|
170 NodeImpl.addChangeListener(listener);
|
|
171 }
|
|
172
|
|
173 public static void addNodeInsertListener(final Listener<Node> listener) {
|
|
174 NodeImpl.addPostInsertListener(listener);
|
|
175 }
|
|
176
|
|
177 public static void addNodeDeleteListener(final Listener<Node> nodeDeleteListener) {
|
|
178 NodeImpl.addPostDeleteListener(nodeDeleteListener);
|
|
179 }
|
|
180
|
|
181 public static void addNodePreUpdatedListener(final Listener<Node> listener) {
|
|
182 NodeImpl.preUpdateListeners.add(listener);
|
|
183 }
|
|
184
|
|
185 public static void addGotParentListener(Listener<Node> listener) {
|
|
186 NodeImpl.gotParentListeners.add(listener);
|
|
187 }
|
|
188
|
|
189 public static void addUserInsertListener(final Listener<User> listener) {
|
|
190 UserImpl.addPostInsertListener(listener);
|
|
191 }
|
|
192
|
|
193 public static void addUserPreUpdatedListener(final Listener<User> listener) {
|
|
194 UserImpl.preUpdateListeners.add(listener);
|
|
195 }
|
|
196
|
|
197 static ThreadLocal<Boolean> insideImportProcedure = new ThreadLocal<Boolean>() {
|
|
198 protected Boolean initialValue() {
|
|
199 return Boolean.FALSE;
|
|
200 }
|
|
201 };
|
|
202
|
|
203 public static void beginImport() {
|
|
204 insideImportProcedure.set(true);
|
|
205 }
|
|
206
|
|
207 public static void endImport() {
|
|
208 insideImportProcedure.set(false);
|
|
209 }
|
|
210 /*
|
|
211 static {
|
|
212 NodeImpl.addPostInsertListener(new Listener<NodeImpl>() {
|
|
213 public void event(final NodeImpl post) {
|
|
214 if (insideImportProcedure.get())
|
|
215 return;
|
|
216 Node.MailToList mailToList = post.getMailToList();
|
|
217 if( mailToList==null )
|
|
218 return;
|
|
219 MailingList mailingList = post.getAssociatedMailingListImpl();
|
|
220 if( mailingList == null )
|
|
221 return;
|
|
222 Template template = post.getSite().getTemplate( "mail to list",
|
|
223 BasicNamespace.class, NabbleNamespace.class, NodePageNamespace.class
|
|
224 );
|
|
225 template.run( TemplatePrintWriter.NULL, Collections.<String,Object>emptyMap(),
|
|
226 new BasicNamespace(template),
|
|
227 new NabbleNamespace(post.getSite()),
|
|
228 new NodePageNamespace(post)
|
|
229 );
|
|
230 }
|
|
231 });
|
|
232 }
|
|
233 */
|
|
234
|
|
235 public static void preloadMessages(List<Node> nodes) {
|
|
236 NodeImpl.preloadMessages(nodes);
|
|
237 }
|
|
238
|
|
239 public static String assignmentCondition(User user,Integer priority) {
|
|
240 if( user==null && priority==null )
|
|
241 return null;
|
|
242 StringBuilder buf = new StringBuilder();
|
|
243 if( user != null ) {
|
|
244 buf.append( "assigned_user=" ).append( user.getId() );
|
|
245 }
|
|
246 if( priority != null ) {
|
|
247 if( buf.length() > 0 )
|
|
248 buf.append( " and " );
|
|
249 buf.append( "assigned_priority=" ).append( priority );
|
|
250 }
|
|
251 return buf.toString();
|
|
252 }
|
|
253
|
|
254
|
|
255
|
|
256 public static Long getSiteIdFromDomain(String customDomain) {
|
|
257 return SiteGlobal.getSiteIdFromDomain(customDomain);
|
|
258 }
|
|
259
|
|
260
|
|
261 public static final String noReply = "no-reply@nabble.com";
|
|
262 static final String bounces = "bounces@n2.nabble.com";
|
|
263
|
|
264 public static void send(Mail mail) throws MailException {
|
|
265 if( mail.getFrom().getAddrSpec().equals(noReply) ) {
|
|
266 MailHome.getDefaultSmtpServer().sendFrom(mail, bounces);
|
|
267 } else {
|
|
268 MailHome.getDefaultSmtpServer().send(mail);
|
|
269 }
|
|
270 }
|
|
271
|
|
272 public static void send(Mail mail, String smtpFrom) throws MailException {
|
|
273 if( mail.getFrom().getAddrSpec().equals(noReply) ) {
|
|
274 MailHome.getDefaultSmtpServer().sendFrom(mail, bounces);
|
|
275 } else {
|
|
276 MailHome.getDefaultSmtpServer().sendFrom(mail, smtpFrom);
|
|
277 }
|
|
278 }
|
|
279
|
|
280
|
|
281 public static <S,T> ExtensionFactory<S,T> standardExtensionFactory(final String name,final Class<S> sourceClass,final Class<T> extensionClass) {
|
|
282 return new ExtensionFactory<S,T>() {
|
|
283
|
|
284 public String getName() {
|
|
285 return name;
|
|
286 }
|
|
287
|
|
288 public Class<T> extensionClass() {
|
|
289 return extensionClass;
|
|
290 }
|
|
291
|
|
292 public T construct(S source) {
|
|
293 try {
|
|
294 Constructor<T> constructor = extensionClass.getConstructor(sourceClass);
|
|
295 constructor.setAccessible(true);
|
|
296 return constructor.newInstance(source);
|
|
297 } catch(NoSuchMethodException e) {
|
|
298 throw new RuntimeException(e);
|
|
299 } catch(InstantiationException e) {
|
|
300 throw new RuntimeException(e);
|
|
301 } catch(IllegalAccessException e) {
|
|
302 throw new RuntimeException(e);
|
|
303 } catch(InvocationTargetException e) {
|
|
304 Throwable cause = e.getCause();
|
|
305 if( cause instanceof Error )
|
|
306 throw (Error)cause;
|
|
307 if( cause instanceof RuntimeException )
|
|
308 throw (RuntimeException)cause;
|
|
309 throw new RuntimeException(e);
|
|
310 }
|
|
311 }
|
|
312
|
|
313 public T construct(S source,ResultSet rs) {
|
|
314 return construct(source);
|
|
315 }
|
|
316
|
|
317 public Serializable getExportData(S source) {
|
|
318 return null;
|
|
319 }
|
|
320
|
|
321 public void saveExportData(S source,Serializable data) {
|
|
322 throw new RuntimeException();
|
|
323 }
|
|
324 };
|
|
325 }
|
|
326
|
|
327 public static <T> void addSiteExtensionFactory(ExtensionFactory<Site,T> factory) {
|
|
328 SiteImpl.addExtensionFactory(factory);
|
|
329 }
|
|
330
|
|
331 public static <T> void addNodeExtensionFactory(ExtensionFactory<Node,T> factory) {
|
|
332 NodeImpl.addExtensionFactory(factory);
|
|
333 }
|
|
334
|
|
335 public static <T> void addUserExtensionFactory(ExtensionFactory<User,T> factory) {
|
|
336 UserImpl.addExtensionFactory(factory);
|
|
337 }
|
|
338
|
|
339
|
|
340 public static List<Site> getSitesForTask(String task) throws SQLException {
|
|
341 List<Site> sites = new ArrayList<Site>();
|
|
342 for( SiteKey siteKey : SiteKey.getSiteKeys(task) ) {
|
|
343 try {
|
|
344 sites.add( siteKey.site() );
|
|
345 } catch(UpdatingException e) {} // skip it
|
|
346 }
|
|
347 return sites;
|
|
348 }
|
|
349
|
|
350 }
|