1 19 20 package org.lucane.applications.todolist.io; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 25 import org.lucane.applications.todolist.Todolist; 26 import org.lucane.applications.todolist.TodolistAction; 27 import org.lucane.applications.todolist.TodolistItem; 28 import org.lucane.client.Client; 29 import org.lucane.client.Communicator; 30 import org.lucane.client.Plugin; 31 import org.lucane.client.widgets.DialogBox; 32 import org.lucane.common.ConnectInfo; 33 import org.lucane.common.acl.AclInfo; 34 import org.lucane.common.acl.AclProducer; 35 import org.lucane.common.net.ObjectConnection; 36 37 public class IO implements AclProducer 38 { 39 private ConnectInfo service; 40 private static IO singleInstance; 41 private static Plugin plugin; 42 43 private IO() { 44 service = Communicator.getInstance().getConnectInfo("org.lucane.applications.todolist"); 45 } 46 47 public static IO getInstance(Plugin plugin) { 48 IO.plugin = plugin; 50 51 if (singleInstance==null) { 52 singleInstance = new IO(); 53 } 54 55 return singleInstance; 56 } 57 58 public ConnectInfo getService() { 59 return service; 60 } 61 62 public String getUserName() { 63 return Client.getInstance().getMyInfos().getName(); 64 } 65 66 public ArrayList getTodolists() { 67 ArrayList todolists = null; 68 try { 69 TodolistAction action = new TodolistAction(TodolistAction.GET_TODOLISTS, getUserName()); 70 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 71 String ack = oc.readString(); 72 if (ack.startsWith("FAILED")) { 73 DialogBox.error(plugin.tr("IO.cantGetLists")); 74 return null; 75 } 76 todolists = (ArrayList ) oc.read(); 77 oc.close(); 78 } catch (Exception e) { 79 DialogBox.error(plugin.tr("IO.cantGetLists")); 80 return null; 81 } 82 83 return todolists; 84 } 85 86 public ArrayList getTodolistItems(int todolistId) { 87 ArrayList todolistitems = null; 88 try { 89 TodolistAction action = new TodolistAction(TodolistAction.GET_TODOLISTITEMS, new Integer (todolistId)); 90 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 91 String ack = oc.readString(); 92 if (ack.startsWith("FAILED")) { 93 DialogBox.error(plugin.tr("IO.cantGetItems")); 94 return null; 95 } 96 todolistitems = (ArrayList ) oc.read(); 97 oc.close(); 98 } catch (Exception e) { 99 DialogBox.error(plugin.tr("IO.cantGetItems")); 100 return null; 101 } 102 103 return todolistitems; 104 } 105 106 public ArrayList getTodolistItems(ArrayList lists) { 107 ArrayList todolistitems = new ArrayList (); 108 Iterator it = lists.iterator(); 109 110 while (it.hasNext()){ 111 Object obj = it.next(); 112 if (obj instanceof Todolist) { 113 int todolistId = ((Todolist)obj).getId(); 114 try { 115 TodolistAction action = new TodolistAction(TodolistAction.GET_TODOLISTITEMS, new Integer (todolistId)); 116 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 117 String ack = oc.readString(); 118 if (ack.startsWith("FAILED")) { 119 DialogBox.error(plugin.tr("IO.cantGetItems")); 120 return null; 121 } 122 todolistitems.addAll((ArrayList ) oc.read()); 123 oc.close(); 124 } catch (Exception e) { 125 DialogBox.error(plugin.tr("IO.cantGetItems")); 126 return null; 127 } 128 } 129 } 130 131 return todolistitems; 132 } 133 134 public int addTodolistItem(TodolistItem newTodolistItem) { 135 int id=-1; 136 try { 137 TodolistAction action = new TodolistAction(TodolistAction.ADD_TODOLISTITEM, newTodolistItem); 138 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 139 String ack = oc.readString(); 140 if (ack.startsWith("FAILED")) { 141 DialogBox.error(plugin.tr("IO.cantCreateItem")); 142 return -1; 143 } 144 id = ((Integer )oc.read()).intValue(); 145 oc.close(); 146 } catch (Exception e) { 147 DialogBox.error(plugin.tr("IO.cantCreateItem")); 148 return -1; 149 } 150 151 return id; 152 } 153 154 public boolean modifyTodolistItem(TodolistItem oldTodolistItem, TodolistItem newTodolistItem) { 155 try { 156 TodolistAction action = new TodolistAction(TodolistAction.MOD_TODOLISTITEM, new Object [] {new Integer (oldTodolistItem.getId()), newTodolistItem}); 157 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 158 String ack = oc.readString(); 159 if (ack.startsWith("FAILED")) { 160 DialogBox.error(plugin.tr("IO.cantModifyItem")); 161 return false; 162 } 163 oc.close(); 164 } catch (Exception e) { 165 DialogBox.error(plugin.tr("IO.cantModifyItem")); 166 return false; 167 } 168 169 return true; 170 } 171 172 public boolean deleteTodolistItem(TodolistItem defunctTodolistItem) { 173 try { 174 TodolistAction action = new TodolistAction(TodolistAction.DEL_TODOLISTITEM, new Integer (defunctTodolistItem.getId())); 175 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 176 String ack = oc.readString(); 177 if (ack.startsWith("FAILED")) { 178 DialogBox.error(plugin.tr("IO.cantDeleteItem")); 179 return false; 180 } 181 oc.close(); 182 } catch (Exception e) { 183 DialogBox.error(plugin.tr("IO.cantDeleteItem")); 184 return false; 185 } 186 187 return true; 188 } 189 190 public int addTodolist(Todolist newTodolist) { 191 int id=-1; 192 try { 193 TodolistAction action = new TodolistAction(TodolistAction.ADD_TODOLIST, newTodolist); 194 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 195 String ack = oc.readString(); 196 if (ack.startsWith("FAILED")) { 197 DialogBox.error(plugin.tr("IO.cantCreateList")); 198 return -1; 199 } 200 id = ((Integer )oc.read()).intValue(); 201 202 oc.close(); 203 } catch (Exception e) { 204 DialogBox.error(plugin.tr("IO.cantCreateList")); 205 return -1; 206 } 207 208 return id; 209 } 210 211 public boolean modifyTodolist(Todolist oldTodolist, Todolist newTodolist) { 212 try { 213 TodolistAction action = new TodolistAction(TodolistAction.MOD_TODOLIST, new Object [] {new Integer (oldTodolist.getId()), newTodolist}); 214 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 215 String ack = oc.readString(); 216 if (ack.startsWith("FAILED")) { 217 DialogBox.error(plugin.tr("IO.cantModifyList")); 218 return false; 219 } 220 oc.close(); 221 } catch (Exception e) { 222 DialogBox.error(plugin.tr("IO.cantModifyList")); 223 return false; 224 } 225 226 return true; 227 } 228 229 public boolean deleteTodolist(Todolist defunctTodolist) { 230 try { 231 TodolistAction action = new TodolistAction(TodolistAction.DEL_TODOLIST, new Integer (defunctTodolist.getId())); 232 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, service.getName(), action); 233 String ack = oc.readString(); 234 if (ack.startsWith("FAILED")) { 235 DialogBox.error(plugin.tr("IO.cantDeleteList")); 236 return false; 237 } 238 oc.close(); 239 } catch (Exception e) { 240 DialogBox.error(plugin.tr("IO.cantDeleteList")); 241 return false; 242 } 243 244 return true; 245 } 246 247 public AclInfo[] getAcls(String listId) 249 throws Exception 250 { 251 AclInfo[] acl = null; 252 253 TodolistAction action = new TodolistAction(TodolistAction.GET_ACLS, listId); 254 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, plugin.getName(), action); 255 String ack = (String )oc.read(); 256 257 if(ack.equals("OK")) 258 { 259 acl = (AclInfo[])oc.read(); 260 oc.close(); 261 262 return acl; 263 } 264 oc.close(); 265 throw new Exception (ack); 266 } 267 268 public ArrayList getAllGroups() 269 throws Exception 270 { 271 ArrayList groups = null; 272 273 TodolistAction action = new TodolistAction(TodolistAction.GET_GROUPS); 274 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, plugin.getName(), action); 275 String ack = (String )oc.read(); 276 277 if(ack.equals("OK")) 278 { 279 groups = (ArrayList )oc.read(); 280 oc.close(); 281 282 return groups; 283 } 284 oc.close(); 285 throw new Exception (ack); 286 } 287 288 public ArrayList getAllUsers() 289 throws Exception 290 { 291 ArrayList users = null; 292 293 TodolistAction action = new TodolistAction(TodolistAction.GET_USERS); 294 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, plugin.getName(), action); 295 String ack = (String )oc.read(); 296 297 if(ack.equals("OK")) 298 { 299 users = (ArrayList )oc.read(); 300 oc.close(); 301 302 return users; 303 } 304 oc.close(); 305 throw new Exception (ack); 306 } 307 308 public void addAcl(String listId, AclInfo info) 309 throws Exception 310 { 311 TodolistAction action = new TodolistAction(TodolistAction.ADD_ACL, listId, info); 312 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, plugin.getName(), action); 313 String ack = (String )oc.read(); 314 oc.close(); 315 316 if(!ack.equals("OK")) 317 throw new Exception (ack); 318 } 319 320 public void removeAcl(String listId, AclInfo info) 321 throws Exception 322 { 323 TodolistAction action = new TodolistAction(TodolistAction.REMOVE_ACL, listId, info); 324 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, plugin.getName(), action); 325 String ack = (String )oc.read(); 326 oc.close(); 327 328 if(!ack.equals("OK")) 329 throw new Exception (ack); 330 } 331 332 public String [] getAccesses() 334 { 335 return new String [] {"write", "read"}; 336 } 337 338 public String getItemName(String itemId) 339 throws Exception 340 { 341 Iterator lists = this.getTodolists().iterator(); 342 while(lists.hasNext()) 343 { 344 Todolist list = (Todolist)lists.next(); 345 if(list.getId() == Integer.parseInt(itemId)) 346 return list.getName(); 347 } 348 349 return itemId; 350 } 351 } 352 | Popular Tags |