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