Mercurial Hosting > nabble
comparison src/nabble/model/export/AbstractImportImpl.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 72765b66e2c3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.model.export; | |
2 | |
3 import fschmidt.db.SQLRuntimeException; | |
4 import fschmidt.util.java.ProxyIntoThread; | |
5 import nabble.model.MailingList; | |
6 import nabble.model.ModelException; | |
7 import nabble.model.Node; | |
8 import nabble.model.Site; | |
9 import nabble.view.lib.Jtp; | |
10 import nabble.view.web.template.UrlMapperNamespace; | |
11 import org.slf4j.Logger; | |
12 import org.slf4j.LoggerFactory; | |
13 | |
14 import java.util.regex.Matcher; | |
15 import java.util.regex.Pattern; | |
16 | |
17 | |
18 public abstract class AbstractImportImpl implements Import { | |
19 | |
20 private static final Logger logger = LoggerFactory.getLogger(AbstractImportImpl.class); | |
21 | |
22 final Site site; | |
23 private ProxyIntoThread pit = null; | |
24 private MailingList currentMailingList = null; | |
25 private boolean isStarted = false; | |
26 private boolean isClosed = false; | |
27 | |
28 AbstractImportImpl(Site site) { | |
29 this.site = site; | |
30 } | |
31 | |
32 protected abstract String createNode(NodeData data) throws ModelException; | |
33 protected abstract void start(); | |
34 protected abstract void end(); | |
35 | |
36 void setProxy(ProxyIntoThread<Import> pit) { | |
37 this.pit = pit; | |
38 } | |
39 /* | |
40 public Pattern[] getRedirectUrlPatterns() { | |
41 Pattern rootPtn = Pattern.compile("http://.+\\.(\\d+)\\..+/"); | |
42 Pattern appPtn = Pattern.compile("-f(\\d+)\\.html$"); | |
43 Pattern postPtn = Pattern.compile("-td(\\d+)\\.html$"); | |
44 Pattern postPermalinkPtn = Pattern.compile("-tp\\d+p(\\d+)\\.html$"); | |
45 return new Pattern[] { appPtn, postPtn, postPermalinkPtn, rootPtn }; | |
46 } | |
47 */ | |
48 private static final Pattern[] redirectUrlPatterns = new Pattern[]{ | |
49 Pattern.compile("-td(\\d+)\\.html$"), // post | |
50 Pattern.compile("-tp(\\d+)\\.html$"), // topic permalink | |
51 Pattern.compile("-tp\\d+p(\\d+)\\.html$"), // post permalink | |
52 }; | |
53 | |
54 public long getNodeId(String permalink) throws BadLink { | |
55 Node node = UrlMapperNamespace.getNodeFromUrl(permalink); | |
56 if( node != null ) | |
57 return node.getId(); | |
58 for (Pattern p : redirectUrlPatterns) { | |
59 Matcher m = p.matcher(permalink); | |
60 if (m.find()) { | |
61 String s = m.group(m.groupCount()); | |
62 return Long.parseLong(s); | |
63 } | |
64 } | |
65 throw new BadLink(); | |
66 } | |
67 | |
68 public String importNode(NodeData data) { | |
69 if( isClosed ) | |
70 throw new RuntimeException("Closed"); | |
71 if( !isStarted ) { | |
72 start(); | |
73 isStarted = true; | |
74 } | |
75 try { | |
76 Node node = null; | |
77 if( currentMailingList != null ) | |
78 node = currentMailingList.getNodeFromMessageID(data.messageID); | |
79 | |
80 if (node != null) | |
81 return Jtp.url(node); | |
82 else | |
83 return createNode(data); | |
84 } catch(ModelException e) { | |
85 logger.error("node "+data.exportId + " / data.exportId=" + data.exportId, e); | |
86 throw new RuntimeException(e.toString()); | |
87 } catch(SQLRuntimeException e) { | |
88 logger.error("node "+data.exportId + " / data.exportId=" + data.exportId, e); | |
89 throw new RuntimeException(e.toString()); | |
90 } | |
91 } | |
92 | |
93 Node getNode(long nodeId) { | |
94 return site.getNode(nodeId); | |
95 } | |
96 | |
97 public void setMailingList(Long nodeId) { | |
98 if( nodeId==null ) { | |
99 currentMailingList = null; | |
100 } else { | |
101 if( currentMailingList != null ) | |
102 throw new RuntimeException("Already have mailing list "+currentMailingList); | |
103 currentMailingList = getNode(nodeId).getMailingList(); | |
104 if( currentMailingList == null ) | |
105 throw new RuntimeException("No mailing list for node "+nodeId); | |
106 } | |
107 } | |
108 | |
109 public void subscribe(long nodeId) { | |
110 getNode(nodeId).getMailingList().subscribe(); | |
111 } | |
112 | |
113 public void setExportOwner(long nodeId,String exportOwner) { | |
114 try { | |
115 MailingList ml = getNode(nodeId).getMailingList(); | |
116 ml.setExportOwner(exportOwner); | |
117 ml.update(); | |
118 } catch(ModelException e) { | |
119 logger.error(exportOwner,e); | |
120 throw new RuntimeException(e.toString()); | |
121 } | |
122 } | |
123 | |
124 public void close() { | |
125 isClosed = true; | |
126 if (pit != null) { | |
127 pit.stop(); | |
128 pit = null; | |
129 } | |
130 end(); | |
131 } | |
132 | |
133 @Override protected void finalize() throws Throwable { | |
134 try { | |
135 if( !isClosed ) { | |
136 logger.error("import not closed"); | |
137 close(); | |
138 } | |
139 } finally { | |
140 super.finalize(); | |
141 } | |
142 } | |
143 | |
144 } |