1
|
1 package jp.ac.u_ryukyu.ie.cr.bbs.local;
|
|
2
|
|
3
|
|
4 import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle;
|
|
5 import jp.ac.u_ryukyu.ie.cr.jungle.Jungle;
|
|
6 import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree;
|
|
7 import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
|
|
8 import jp.ac.u_ryukyu.ie.cr.jungle.core.Children;
|
|
9 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;
|
|
10 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor;
|
|
11 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
|
4
|
12 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode;
|
1
|
13 import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser;
|
|
14 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
|
15 import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
|
|
16
|
|
17 import java.nio.ByteBuffer;
|
|
18 import java.util.concurrent.atomic.AtomicInteger;
|
|
19
|
|
20 public class JungleBulletinBoard implements
|
|
21 BulletinBoard {
|
|
22 private final Jungle jungle;
|
|
23
|
|
24 public JungleBulletinBoard() {
|
|
25 jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(
|
|
26 new DefaultTraverser()));
|
|
27 jungle.createNewTree("boards");
|
|
28 }
|
|
29
|
|
30 public Iterable<String> getBoards() {
|
|
31 JungleTree tree = jungle.getTreeByName("boards");
|
|
32 TreeNode node = tree.getRootNode();
|
|
33 Children chs = node.getChildren();
|
|
34
|
|
35 IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() {
|
|
36 public String conv(TreeNode _b) {
|
|
37 ByteBuffer e = _b.getAttributes().get("name");
|
|
38 return new String(e.array());
|
|
39 }
|
|
40 };
|
|
41
|
|
42 return new IterableConverter<String, TreeNode>(chs, converter);
|
|
43 }
|
|
44
|
|
45 public void createBoards(final String _name, final String _author,
|
|
46 final String _initMessage, final String _editKey) {
|
|
47 if (null == jungle.createNewTree(_name)) {
|
|
48 throw new IllegalStateException();
|
|
49 }
|
|
50 JungleTree tree = jungle.getTreeByName("boards");
|
|
51 JungleTreeEditor editor = tree.getTreeEditor();
|
|
52 DefaultNodePath root = new DefaultNodePath();
|
|
53 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0);
|
|
54 if (either.isA()) {
|
|
55 throw new IllegalStateException();
|
|
56 }
|
|
57 editor = either.b();
|
|
58
|
|
59 either = editor.putAttribute(root.add(0), "name", ByteBuffer.wrap(_name.getBytes()));
|
|
60 if (either.isA()) {
|
|
61 throw new IllegalStateException();
|
|
62 }
|
|
63 editor = either.b();
|
|
64 Either<Error, JungleTreeEditor> result = editor.success();
|
|
65 if (result.isA()) {
|
|
66 throw new IllegalStateException();
|
|
67 }
|
|
68
|
|
69 tree = jungle.getTreeByName(_name);
|
|
70 editor = tree.getTreeEditor();
|
|
71 editor = editor.putAttribute(root,"author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
72 editor = editor.putAttribute(root,"key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
73 either = editor.putAttribute(root,"mes", ByteBuffer.wrap(_initMessage.getBytes()));
|
4
|
74
|
1
|
75 if (either.isA()) {
|
|
76 throw new IllegalStateException();
|
|
77 }
|
|
78 editor = either.b();
|
|
79 editor.success();
|
|
80 }
|
|
81
|
|
82 public void createBoardMessage(final String _board, final String _author,
|
|
83 final String _message, final String _editKey) {
|
|
84 JungleTree tree = jungle.getTreeByName(_board);
|
|
85 if (tree == null) {
|
|
86 throw new IllegalStateException();
|
|
87 }
|
|
88
|
|
89 JungleTreeEditor editor;
|
|
90 do {
|
|
91 TreeNode node = tree.getRootNode();
|
|
92 int size = node.getChildren().size();
|
|
93 DefaultNodePath path = new DefaultNodePath();
|
|
94
|
|
95 editor = tree.getTreeEditor();
|
|
96 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path,
|
|
97 size);
|
|
98 if (either.isA()) {
|
|
99 throw new IllegalStateException();
|
|
100 }
|
|
101 editor = either.b();
|
|
102 editor = editor.putAttribute(path.add(size),"author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
103 editor = editor.putAttribute(path.add(size),"key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
104 either = editor.putAttribute(path.add(size),"mes", ByteBuffer.wrap(_message.getBytes()));
|
4
|
105
|
1
|
106 if (either.isA()) {
|
|
107 throw new IllegalStateException();
|
|
108 }
|
|
109 editor = either.b();
|
|
110 }while (editor.success().isA());
|
|
111 }
|
|
112
|
|
113 public void editMessage(String _board, String _uuid, final String _author,
|
|
114 final String _message, final String _editKey) {
|
|
115 JungleTreeEditor editor = null;
|
|
116 do {
|
|
117 DefaultNodePath path = new DefaultNodePath();
|
|
118 path = path.add(Integer.parseInt(_uuid));
|
|
119
|
|
120 JungleTree tree = jungle.getTreeByName(_board);
|
|
121 editor = tree.getTreeEditor();
|
|
122
|
|
123 editor = editor.putAttribute(path,"author", ByteBuffer.wrap(_author.getBytes())).b();
|
|
124 editor = editor.putAttribute(path,"key", ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
125 Either<Error, JungleTreeEditor> either = editor.putAttribute(path,"mes", ByteBuffer.wrap(_message.getBytes()));
|
4
|
126
|
1
|
127 if (either.isA()) {
|
|
128 throw new IllegalStateException();
|
|
129 }
|
|
130 editor = either.b();
|
|
131 }while (editor.success().isA());
|
|
132 }
|
|
133
|
|
134 public Iterable<BoardMessage> getMessages(String _boardName) {
|
|
135 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
136 TreeNode node = tree.getRootNode();
|
|
137 Children chs = node.getChildren();
|
|
138
|
|
139 final AtomicInteger counter = new AtomicInteger(0);
|
|
140 IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() {
|
|
141 public BoardMessage conv(TreeNode _b) {
|
|
142 String uuid = Integer.toString(counter.get());
|
|
143 String author = new String(_b.getAttributes().get("author").array());
|
|
144 String message = new String(_b.getAttributes().get("mes").array());
|
|
145 counter.incrementAndGet();
|
|
146 return new BoardMessageImpl(author, message, uuid);
|
|
147 }
|
|
148 };
|
|
149
|
|
150 return new IterableConverter<BoardMessage, TreeNode>(chs, converter);
|
|
151 }
|
|
152
|
|
153 private static class BoardMessageImpl implements BoardMessage {
|
|
154 private final String author;
|
|
155 private final String message;
|
|
156 private final String uuid;
|
|
157
|
|
158 public BoardMessageImpl(String _author, String _message, String _uuid) {
|
|
159 author = _author;
|
|
160 message = _message;
|
|
161 uuid = _uuid;
|
|
162 }
|
|
163
|
|
164 public String getAuthor() {
|
|
165 return author;
|
|
166 }
|
|
167
|
|
168 public String getMessage() {
|
|
169 return message;
|
|
170 }
|
|
171
|
|
172 public String getUUID() {
|
|
173 return uuid;
|
|
174 }
|
|
175 }
|
|
176
|
|
177 public String sanitize(String str) {
|
|
178 if (str == null) {
|
|
179 return str;
|
|
180 }
|
|
181 str = str.replaceAll("&", "&");
|
|
182 str = str.replaceAll("<", "<");
|
|
183 str = str.replaceAll(">", ">");
|
|
184 str = str.replaceAll("\"", """);
|
|
185 str = str.replaceAll("'", "'");
|
|
186 return str;
|
|
187 }
|
|
188
|
4
|
189 public GetAttributeImp getAttribute(String _bname, String nodePath,String revisionStr) {
|
|
190 Long revision = Long.parseLong(revisionStr);
|
|
191 DefaultNodePath path = createNodePath(nodePath);
|
|
192 JungleTree tree = jungle.getTreeByName(_bname);
|
|
193 JungleTree oldTree = tree.getOldTree(revision).b();
|
|
194 Either<Error, TreeNode> either = tree.getNodeOfPath(path);
|
|
195 if (either.isA())
|
|
196 return new GetAttributeImp(new DefaultTreeNode());
|
|
197 TreeNode node = either.b();
|
|
198 return new GetAttributeImp(node);
|
1
|
199 }
|
|
200
|
4
|
201 private DefaultNodePath createNodePath(String nodePath) {
|
|
202 DefaultNodePath path = new DefaultNodePath();
|
|
203 try {
|
|
204 for (int count = 0; nodePath.substring(count, count + 1) != null; count++) {
|
|
205 if (!nodePath.substring(count, count + 1).equals("/"))
|
|
206 path = path.add(Integer.parseInt(nodePath.substring(count, count + 1)));
|
|
207 }
|
|
208 } catch (Exception _e) {
|
|
209 }
|
|
210 return path;
|
|
211 }
|
1
|
212 }
|