diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/model/export/AbstractImportImpl.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,144 @@
+package nabble.model.export;
+
+import fschmidt.db.SQLRuntimeException;
+import fschmidt.util.java.ProxyIntoThread;
+import nabble.model.MailingList;
+import nabble.model.ModelException;
+import nabble.model.Node;
+import nabble.model.Site;
+import nabble.view.lib.Jtp;
+import nabble.view.web.template.UrlMapperNamespace;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+
+public abstract class AbstractImportImpl implements Import {
+
+	private static final Logger logger = LoggerFactory.getLogger(AbstractImportImpl.class);
+
+	final Site site;
+	private ProxyIntoThread pit = null;
+	private MailingList currentMailingList = null;
+	private boolean isStarted = false;
+	private boolean isClosed = false;
+
+	AbstractImportImpl(Site site) {
+		this.site = site;
+	}
+
+	protected abstract String createNode(NodeData data) throws ModelException;
+	protected abstract void start();
+	protected abstract void end();
+
+	void setProxy(ProxyIntoThread<Import> pit) {
+		this.pit = pit;
+	}
+/*
+	public Pattern[] getRedirectUrlPatterns() {
+		Pattern rootPtn = Pattern.compile("http://.+\\.(\\d+)\\..+/");
+		Pattern appPtn = Pattern.compile("-f(\\d+)\\.html$");
+		Pattern postPtn = Pattern.compile("-td(\\d+)\\.html$");
+		Pattern postPermalinkPtn = Pattern.compile("-tp\\d+p(\\d+)\\.html$");
+		return new Pattern[] { appPtn, postPtn, postPermalinkPtn, rootPtn };
+	}
+*/
+	private static final Pattern[] redirectUrlPatterns =  new Pattern[]{
+		Pattern.compile("-td(\\d+)\\.html$"),  // post
+		Pattern.compile("-tp(\\d+)\\.html$"),  // topic permalink
+		Pattern.compile("-tp\\d+p(\\d+)\\.html$"),  // post permalink
+	};
+
+	public long getNodeId(String permalink) throws BadLink {
+		Node node = UrlMapperNamespace.getNodeFromUrl(permalink);
+		if( node != null )
+			return node.getId();
+		for (Pattern p : redirectUrlPatterns) {
+			Matcher m = p.matcher(permalink);
+			if (m.find()) {
+				String s = m.group(m.groupCount());
+				return Long.parseLong(s);
+			}
+		}
+		throw new BadLink();
+	}
+
+	public String importNode(NodeData data) {
+		if( isClosed )
+			throw new RuntimeException("Closed");
+		if( !isStarted ) {
+			start();
+			isStarted = true;
+		}
+		try {
+			Node node = null;
+			if( currentMailingList != null )
+				node = currentMailingList.getNodeFromMessageID(data.messageID);
+
+			if (node != null)
+				return Jtp.url(node);
+			else
+				return createNode(data);
+		} catch(ModelException e) {
+			logger.error("node "+data.exportId + " / data.exportId=" + data.exportId, e);
+			throw new RuntimeException(e.toString());
+		} catch(SQLRuntimeException e) {
+			logger.error("node "+data.exportId + " / data.exportId=" + data.exportId, e);
+			throw new RuntimeException(e.toString());
+		}
+	}
+
+	Node getNode(long nodeId) {
+		return site.getNode(nodeId);
+	}
+
+	public void setMailingList(Long nodeId) {
+		if( nodeId==null ) {
+			currentMailingList = null;
+		} else {
+			if( currentMailingList != null )
+				throw new RuntimeException("Already have mailing list "+currentMailingList);
+			currentMailingList = getNode(nodeId).getMailingList();
+			if( currentMailingList == null )
+				throw new RuntimeException("No mailing list for node "+nodeId);
+		}
+	}
+
+	public void subscribe(long nodeId) {
+		getNode(nodeId).getMailingList().subscribe();
+	}
+
+	public void setExportOwner(long nodeId,String exportOwner) {
+		try {
+			MailingList ml = getNode(nodeId).getMailingList();
+			ml.setExportOwner(exportOwner);
+			ml.update();
+		} catch(ModelException e) {
+			logger.error(exportOwner,e);
+			throw new RuntimeException(e.toString());
+		}
+	}
+
+	public void close() {
+		isClosed = true;
+		if (pit != null) {
+			pit.stop();
+			pit = null;
+		}
+		end();
+	}
+
+	@Override protected void finalize() throws Throwable {
+		try {
+			if( !isClosed ) {
+				logger.error("import not closed");
+				close();
+			}
+		} finally {
+			super.finalize();
+		}
+	}
+
+}