0
|
1 package nabble.modules.workgroup;
|
|
2
|
|
3 import java.io.Serializable;
|
|
4 import java.sql.ResultSet;
|
|
5 import java.sql.SQLException;
|
|
6 import fschmidt.db.DbDatabase;
|
|
7 import fschmidt.db.DbNull;
|
|
8 import fschmidt.db.DbRecord;
|
|
9 import fschmidt.db.LongKey;
|
|
10 import nabble.model.Db;
|
|
11 import nabble.model.ExtensionFactory;
|
|
12 import nabble.model.ModelHome;
|
|
13 import nabble.model.Node;
|
|
14 import nabble.model.User;
|
|
15
|
|
16
|
|
17 final class Assignment {
|
|
18
|
|
19 /** Must be public because XML backups use reflection to create extension objects. */
|
|
20 public static final class ExportData implements Serializable {
|
|
21 String assigneeEmail;
|
|
22 String assigneeName;
|
|
23 Integer priority;
|
|
24 }
|
|
25
|
|
26 private static final ExtensionFactory<Node,Assignment> FACTORY = new ExtensionFactory<Node,Assignment>() {
|
|
27
|
|
28 public String getName() {
|
|
29 return WorkgroupModule.INSTANCE.getName();
|
|
30 }
|
|
31
|
|
32 public Class<Assignment> extensionClass() {
|
|
33 return Assignment.class;
|
|
34 }
|
|
35
|
|
36 public Assignment construct(Node node) {
|
|
37 return null;
|
|
38 }
|
|
39
|
|
40 public Assignment construct(Node node,ResultSet rs)
|
|
41 throws SQLException
|
|
42 {
|
|
43 long assignedUserId = rs.getLong("assigned_user");
|
|
44 if( rs.wasNull() )
|
|
45 return null;
|
|
46 User user = node.getSite().getUser(assignedUserId);
|
|
47 int priority = rs.getInt("assigned_priority");
|
|
48 return new Assignment(node,user,priority);
|
|
49 }
|
|
50
|
|
51 public Serializable getExportData(Node node) {
|
|
52 Assignment assignment = node.getExtension(this);
|
|
53 if( assignment == null )
|
|
54 return null;
|
|
55 ExportData data = new ExportData();
|
|
56 data.assigneeEmail = assignment.assignee.getEmail();
|
|
57 data.assigneeName = assignment.assignee.getName();
|
|
58 data.priority = assignment.priority;
|
|
59 return data;
|
|
60 }
|
|
61
|
|
62 public void saveExportData(Node node,Serializable s) {
|
|
63 ExportData data = (ExportData)s;
|
|
64 User user = node.getSite().getOrCreateUser(data.assigneeEmail,data.assigneeName);
|
|
65 Assignment assignment = new Assignment(node,user,data.priority);
|
|
66 assignment.save();
|
|
67 }
|
|
68 };
|
|
69
|
|
70 static {
|
|
71 ModelHome.addNodeExtensionFactory(FACTORY);
|
|
72 }
|
|
73
|
|
74 static void init() {}
|
|
75
|
|
76 public static Assignment of(Node node) {
|
|
77 return node.getExtension(FACTORY);
|
|
78 }
|
|
79
|
|
80
|
|
81
|
|
82 private Node node;
|
|
83 final User assignee;
|
|
84 final int priority;
|
|
85
|
|
86 Assignment(Node node,User assignee,int priority) {
|
|
87 if( priority < 1 || priority > 5 || assignee==null )
|
|
88 throw new IllegalArgumentException();
|
|
89 this.node = node;
|
|
90 this.assignee = assignee;
|
|
91 this.priority = priority;
|
|
92 }
|
|
93
|
|
94 void save() {
|
|
95 DbDatabase db = node.getSite().getDb();
|
|
96 if( !db.isInTransaction() ) {
|
|
97 db.beginTransaction();
|
|
98 try {
|
|
99 node = node.getGoodCopy();
|
|
100 save();
|
|
101 db.commitTransaction();
|
|
102 } finally {
|
|
103 db.endTransaction();
|
|
104 }
|
|
105 return;
|
|
106 }
|
|
107 if( !assignee.getSite().equals(node.getSite()) )
|
|
108 throw new RuntimeException();
|
|
109 DbRecord<LongKey,?> record = node.getDbRecord();
|
|
110 record.fields().put("assigned_user", assignee.getId());
|
|
111 record.fields().put("assigned_priority", priority);
|
|
112 node.update();
|
|
113 }
|
|
114
|
|
115 static void unassign(Node node) {
|
|
116 if( Assignment.of(node) != null ) {
|
|
117 DbRecord<LongKey,?> record = node.getDbRecord();
|
|
118 record.fields().put("assigned_user",DbNull.INTEGER);
|
|
119 record.fields().put("assigned_priority",DbNull.INTEGER);
|
|
120 node.update();
|
|
121 }
|
|
122 }
|
|
123 }
|