Mercurial Hosting > nabble
comparison src/nabble/modules/workgroup/NodeNamespaceExt.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.modules.workgroup; | |
2 | |
3 import java.sql.Connection; | |
4 import java.sql.PreparedStatement; | |
5 import java.sql.ResultSet; | |
6 import java.sql.SQLException; | |
7 import java.util.List; | |
8 import java.util.ArrayList; | |
9 import java.util.Collections; | |
10 import java.util.NoSuchElementException; | |
11 import javax.servlet.ServletException; | |
12 import fschmidt.util.java.Filter; | |
13 import fschmidt.util.java.Stack; | |
14 import fschmidt.util.java.ArrayStack; | |
15 import nabble.model.Db; | |
16 import nabble.model.ModelHome; | |
17 import nabble.model.Node; | |
18 import nabble.model.NodeIterator; | |
19 import nabble.model.Site; | |
20 import nabble.model.DbParamSetter; | |
21 import nabble.model.User; | |
22 import nabble.naml.compiler.Command; | |
23 import nabble.naml.compiler.CommandSpec; | |
24 import nabble.naml.compiler.IPrintWriter; | |
25 import nabble.naml.compiler.Interpreter; | |
26 import nabble.naml.compiler.NamespaceExtension; | |
27 import nabble.naml.compiler.ScopedInterpreter; | |
28 import nabble.view.web.template.NodeList; | |
29 import nabble.view.web.template.NodeNamespace; | |
30 import nabble.view.web.template.ServletNamespace; | |
31 import nabble.view.web.template.UserNamespace; | |
32 | |
33 | |
34 @NamespaceExtension ( | |
35 name = "workgroup_node", | |
36 target = NodeNamespace.class | |
37 ) | |
38 public final class NodeNamespaceExt { | |
39 private final NodeNamespace ns; | |
40 private final Node node; | |
41 private final Site site; | |
42 | |
43 public NodeNamespaceExt(NodeNamespace ns) { | |
44 this.ns = ns; | |
45 this.node = ns.node(); | |
46 this.site = node.getSite(); | |
47 } | |
48 | |
49 private static Integer priority(Interpreter interp) { | |
50 String priorityS = interp.getArgString("priority"); | |
51 return priorityS == null ? null : Integer.valueOf(priorityS); | |
52 } | |
53 | |
54 private User assignee(Interpreter interp) { | |
55 String assigneeS = interp.getArgString("assignee"); | |
56 return assigneeS == null || "0".equals(assigneeS) ? null : node.getSite().getUser(Long.valueOf(assigneeS)); | |
57 } | |
58 | |
59 public static final CommandSpec assignment_filter = new CommandSpec.Builder() | |
60 .parameters("assignee","priority") | |
61 .build() | |
62 ; | |
63 | |
64 @Command public void assignment_filter(IPrintWriter out,Interpreter interp) { | |
65 Integer priority = priority(interp); | |
66 out.print( ModelHome.assignmentCondition(assignee(interp), priority == null? null : priority == 0? 5 : priority) ); | |
67 } | |
68 | |
69 @Command public void is_assigned(IPrintWriter out,Interpreter interp) { | |
70 out.print( Assignment.of(node) != null ); | |
71 } | |
72 | |
73 @Command public void priority(IPrintWriter out,Interpreter interp) { | |
74 out.print( Assignment.of(node).priority ); | |
75 } | |
76 | |
77 public static final CommandSpec assignee = CommandSpec.DO; | |
78 | |
79 @Command public void assignee(IPrintWriter out,ScopedInterpreter<UserNamespace> interp) { | |
80 out.print( interp.getArg(new UserNamespace(Assignment.of(node).assignee),"do") ); | |
81 } | |
82 | |
83 public static final CommandSpec unassign = CommandSpec.NO_OUTPUT; | |
84 | |
85 @Command public void unassign(IPrintWriter out,Interpreter interp) { | |
86 Assignment.unassign(node); | |
87 } | |
88 | |
89 public static final CommandSpec assign = CommandSpec.NO_OUTPUT() | |
90 .parameters("assignee","priority") | |
91 .build() | |
92 ; | |
93 | |
94 @Command public void assign(IPrintWriter out,Interpreter interp) { | |
95 UserNamespace userNs = interp.getArgAsNamespace(UserNamespace.class,"assignee"); | |
96 User assignee = userNs.user(); | |
97 int priority = interp.getArgAsInt("priority"); | |
98 new Assignment(node,assignee,priority).save(); | |
99 } | |
100 | |
101 public static final CommandSpec children_list_by_priority = CommandSpec.DO() | |
102 .parameters("length") | |
103 .optionalParameters("start", "filter") | |
104 .build() | |
105 ; | |
106 | |
107 @Command public void children_list_by_priority(IPrintWriter out,ScopedInterpreter<NodeList> interp) { | |
108 int start = NodeNamespace.getLoopStart(interp); | |
109 int length = NodeNamespace.getLoopLength(interp); | |
110 String filter = interp.getArgString("filter"); | |
111 NodeIterator<? extends Node> nodeIter = getChildrenByPriority(filter); | |
112 NodeList.children(out,interp,node,nodeIter,start,length); | |
113 } | |
114 | |
115 public static final CommandSpec topics_list_by_priority = CommandSpec.DO() | |
116 .parameters("length") | |
117 .optionalParameters("start","filter") | |
118 .requiredInStack(ServletNamespace.class) | |
119 .build() | |
120 ; | |
121 | |
122 @Command public void topics_list_by_priority(IPrintWriter out,ScopedInterpreter<NodeList> interp) | |
123 throws ServletException | |
124 { | |
125 int start = NodeNamespace.getLoopStart(interp); | |
126 int length = NodeNamespace.getLoopLength(interp); | |
127 String filter = interp.getArgString("filter"); | |
128 NodeIterator<Node> nodeIter = getTopicsByPriority(filter,ns.filter(interp)); | |
129 NodeList.topics(out,interp,node,nodeIter,start,length); | |
130 } | |
131 | |
132 | |
133 private static final class PriorityNode implements Comparable<PriorityNode> { | |
134 final long nodeId; | |
135 final int priority; | |
136 final long lastNodeDate; | |
137 | |
138 PriorityNode(ResultSet rs) throws SQLException { | |
139 nodeId = rs.getLong("node_id"); | |
140 priority = rs.getInt("assigned_priority"); | |
141 lastNodeDate = rs.getTimestamp("last_node_date").getTime(); | |
142 } | |
143 | |
144 public int compareTo(PriorityNode pn) { | |
145 int diff = priority - pn.priority; | |
146 return diff != 0 ? diff | |
147 : lastNodeDate < pn.lastNodeDate ? -1 | |
148 : lastNodeDate > pn.lastNodeDate ? 1 | |
149 : 0 | |
150 ; | |
151 } | |
152 } | |
153 | |
154 private static class NodePriorityIterator extends NodeIterator<Node> { | |
155 private final Site site; | |
156 private final List<PriorityNode> list; | |
157 private int next = 0; | |
158 | |
159 NodePriorityIterator(Site site,List<PriorityNode> list) { | |
160 this.site = site; | |
161 this.list = list; | |
162 } | |
163 | |
164 public void skip(int n) { | |
165 next += n; | |
166 } | |
167 | |
168 public boolean hasNext() { | |
169 return next < list.size(); | |
170 } | |
171 | |
172 public Node next() throws NoSuchElementException { | |
173 return site.getNode(list.get(next++).nodeId); | |
174 } | |
175 | |
176 public void close() {} | |
177 } | |
178 | |
179 private NodeIterator<Node> getTopicsByPriority(String cnd,Filter<Node> filter) { | |
180 if( node.getKind() != Node.Kind.APP ) | |
181 return NodeIterator.empty(); | |
182 List<PriorityNode> list = new ArrayList<PriorityNode>(); | |
183 try { | |
184 Connection con = site.getDb().getConnection(); | |
185 PreparedStatement pstmtTopics = con.prepareStatement( | |
186 "select node_id, assigned_priority, last_node_date from node where parent_id = ? and is_app is null" + (cnd==null ? "" : " and " + cnd) | |
187 ); | |
188 PreparedStatement pstmtApps = con.prepareStatement( | |
189 "select * from node where parent_id = ? and is_app" | |
190 ); | |
191 Stack<Long> stack = new ArrayStack<Long>(); | |
192 stack.push(node.getId()); | |
193 do { | |
194 long nodeId = stack.pop(); | |
195 pstmtTopics.setLong(1,nodeId); | |
196 ResultSet rs = pstmtTopics.executeQuery(); | |
197 while( rs.next() ) { | |
198 list.add( new PriorityNode(rs) ); | |
199 } | |
200 rs.close(); | |
201 pstmtApps.setLong(1,nodeId); | |
202 rs = pstmtApps.executeQuery(); | |
203 while( rs.next() ) { | |
204 Node node = site.getNode(rs); | |
205 if( filter.ok(node) ) | |
206 stack.push( node.getId() ); | |
207 } | |
208 rs.close(); | |
209 } while( !stack.isEmpty() ); | |
210 con.close(); | |
211 } catch(SQLException e) { | |
212 throw new RuntimeException(e); | |
213 } | |
214 Collections.sort(list); | |
215 return new NodePriorityIterator(site,list); | |
216 } | |
217 | |
218 | |
219 public NodeIterator<? extends Node> getChildrenByPriority(String cnd) { | |
220 if( !node.isInDb() ) { | |
221 /* | |
222 List<Node> children = dummyChildMap.get(this); | |
223 if( children == null ) | |
224 return NodeIterator.empty(); | |
225 return NodeIterator.nodeIterator( children.iterator() ); | |
226 */ | |
227 throw new RuntimeException(); | |
228 } | |
229 return site.getNodeIterator( | |
230 "select *" | |
231 +" from node" | |
232 +" where parent_id = ?" | |
233 +(cnd==null?"":"and " + cnd) | |
234 +" order by assigned_priority, last_node_date, node_id" | |
235 , | |
236 new DbParamSetter() { | |
237 public void setParams(PreparedStatement stmt) throws SQLException { | |
238 stmt.setLong(1, node.getId()); | |
239 } | |
240 } | |
241 ); | |
242 } | |
243 | |
244 } |