view src/nabble/modules/workgroup/Assignment.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line source

package nabble.modules.workgroup;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import fschmidt.db.DbDatabase;
import fschmidt.db.DbNull;
import fschmidt.db.DbRecord;
import fschmidt.db.LongKey;
import nabble.model.Db;
import nabble.model.ExtensionFactory;
import nabble.model.ModelHome;
import nabble.model.Node;
import nabble.model.User;


final class Assignment {

	/** Must be public because XML backups use reflection to create extension objects. */
	public static final class ExportData implements Serializable {
		String assigneeEmail;
		String assigneeName;
		Integer priority;
	}

	private static final ExtensionFactory<Node,Assignment> FACTORY = new ExtensionFactory<Node,Assignment>() {

		public String getName() {
			return WorkgroupModule.INSTANCE.getName();
		}

		public Class<Assignment> extensionClass() {
			return Assignment.class;
		}

		public Assignment construct(Node node) {
			return null;
		}

		public Assignment construct(Node node,ResultSet rs)
			throws SQLException
		{
			long assignedUserId = rs.getLong("assigned_user");
			if( rs.wasNull() )
				return null;
			User user = node.getSite().getUser(assignedUserId);
			int priority = rs.getInt("assigned_priority");
			return new Assignment(node,user,priority);
		}

		public Serializable getExportData(Node node) {
			Assignment assignment = node.getExtension(this);
			if( assignment == null )
				return null;
			ExportData data = new ExportData();
			data.assigneeEmail = assignment.assignee.getEmail();
			data.assigneeName = assignment.assignee.getName();
			data.priority = assignment.priority;
			return data;
		}

		public void saveExportData(Node node,Serializable s) {
			ExportData data = (ExportData)s; 
			User user = node.getSite().getOrCreateUser(data.assigneeEmail,data.assigneeName);
			Assignment assignment = new Assignment(node,user,data.priority);
			assignment.save();
		}
	};

	static {
		ModelHome.addNodeExtensionFactory(FACTORY);
	}

	static void init() {}

	public static Assignment of(Node node) {
		return node.getExtension(FACTORY);
	}



	private Node node;
	final User assignee;
	final int priority;

	Assignment(Node node,User assignee,int priority) {
		if( priority < 1 || priority > 5 || assignee==null )
			throw new IllegalArgumentException();
		this.node = node;
		this.assignee = assignee;
		this.priority = priority;
	}

	void save() {
		DbDatabase db = node.getSite().getDb();
		if( !db.isInTransaction() ) {
			db.beginTransaction();
			try {
				node = node.getGoodCopy();
				save();
				db.commitTransaction();
			} finally {
				db.endTransaction();
			}
			return;
		}
		if( !assignee.getSite().equals(node.getSite()) )
			throw new RuntimeException();
		DbRecord<LongKey,?> record = node.getDbRecord();
		record.fields().put("assigned_user", assignee.getId());
		record.fields().put("assigned_priority", priority);
		node.update();
	}

	static void unassign(Node node) {
		if( Assignment.of(node) != null ) {
			DbRecord<LongKey,?> record = node.getDbRecord();
			record.fields().put("assigned_user",DbNull.INTEGER);
			record.fields().put("assigned_priority",DbNull.INTEGER);
			node.update();
		}
	}
}