diff src/nabble/model/export/ImportImpl.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/ImportImpl.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,89 @@
+package nabble.model.export;
+
+import fschmidt.db.DbDatabase;
+import nabble.model.ModelException;
+import nabble.model.ModelHome;
+import nabble.model.Node;
+import nabble.model.Site;
+import nabble.model.User;
+import nabble.view.lib.Jtp;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Date;
+
+
+final class ImportImpl extends AbstractImportImpl {
+
+	ImportImpl(Site site) {
+		super(site);
+	}
+
+	protected void start() {
+		ModelHome.beginImport();
+	}
+
+	protected void end() {
+		ModelHome.endImport();
+	}
+
+	protected String createNode(NodeData data) throws ModelException {
+		Node node;
+		DbDatabase db = site.getDb();
+		db.beginTransaction();
+		try {
+			node = site.getGoodCopy().newNode(data);
+			db.commitTransaction();
+		} finally {
+			db.endTransaction();
+		}
+		return Jtp.url(node.getGoodCopy());
+	}
+
+	public void addUser(String name, String email, String passwordDigest, Date registrationDate, String smallAvatarUrl, String bigAvatarUrl) {
+		final DbDatabase db = site.getDb();
+		db.beginTransaction();
+		try {
+			User user = site.getOrCreateUser(email, name);
+			if (!user.isRegistered()) {
+				user.setPasswordDigest(passwordDigest);
+				user.register(registrationDate);
+				user.update();
+				user = user.getGoodCopy();
+				if (!user.hasAvatar() && smallAvatarUrl != null && bigAvatarUrl != null) {
+					BufferedImage small = ImageIO.read(new URL(smallAvatarUrl));
+					BufferedImage big = ImageIO.read(new URL(bigAvatarUrl));
+					user.saveAvatar(small, big);
+				}
+			}
+			db.commitTransaction();
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		} catch (ModelException e) {
+			throw new RuntimeException(e);
+		} finally {
+			db.endTransaction();
+		}
+	}
+
+	/** FOR OLD NABBLE ONLY -- TO BE REMOVED SOON */
+	public void addUser0(String name, String email, String password, long registrationDate) {
+		final DbDatabase db = site.getDb();
+		db.beginTransaction();
+		try {
+			User user = site.getOrCreateUser(email, name);
+			if (!user.isRegistered()) {
+				user.setPassword(password);
+				user.register(new Date(registrationDate));
+				user.update();
+			}
+			db.commitTransaction();
+		} catch (ModelException e) {
+			// do nothing -- skip this user
+		} finally {
+			db.endTransaction();
+		}
+	}
+}