@ApiMethod(name = "storeTask") public void storeTask(TaskBean taskBean) { DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService(); Transaction txn = datastoreService.beginTransaction(); try { Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt"); Entity taskEntity = new Entity("TaskBean", taskBean.getId(), taskBeanParentKey); taskEntity.setProperty("data", taskBean.getData()); datastoreService.put(taskEntity); txn.commit(); } finally { if (txn.isActive()) { txn.rollback(); } } }
@ApiMethod(name = "getTasks") public List<TaskBean> getTasks() { DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService(); Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt"); Query query = new Query(taskBeanParentKey); List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults()); ArrayList<TaskBean> taskBeans = new ArrayList<TaskBean>(); for (Entity result : results) { TaskBean taskBean = new TaskBean(); taskBean.setId(result.getKey().getId()); taskBean.setData((String) result.getProperty("data")); taskBeans.add(taskBean); } return taskBeans; }
public static void main(String[] args) { boolean autoGoBack = false; String str; Scanner sc1 = new Scanner(System.in); Scanner sc2 = new Scanner(System.in); int ch1 = 0; int ch2 = 0; int ch3 = 0; File categoryList[] = new File[100]; String driver = "org.hsqldb.jdbcDriver"; String url = "jdbc:hsqldb:hsql://localhost/"; String uid = "SA"; String pwd = ""; Connection con = null; try { TaskModel model = new TaskModel(); String catName; Class.forName(driver); con = DriverManager.getConnection(url, uid, pwd); Statement stmt = con.createStatement(); while (ch1 < 6) { System.out.println(""); System.out.println("Press 1 to create category"); System.out.println("Press 2 to load category"); System.out.println("Press 3 to list pending tasks"); System.out.println("Press 4 to search a category"); System.out.println("Press 5 to remove a category"); System.out.println("Press 6 to exit"); ch1 = sc1.nextInt(); switch (ch1) { case 1: System.out.println("Enter category name"); catName = sc1.next(); // input validations! while (!validateString(catName)) { System.out.println("No special characters and spaces"); catName = sc1.next(); } if (model.isCatPresent(catName)) { System.out.println("category already present. try loading it instead!!!"); autoGoBack = true; } if (autoGoBack == false) { while (ch2 != 2) { System.out.println("Press 1 to add a task"); System.out.println("Press 2 to go back"); ch2 = sc1.nextInt(); switch (ch2) { case 1: System.out.println("adding task..."); System.out.println("..."); System.out.println("..."); System.out.println("..."); System.out.println("Enter task name"); String tName = sc2.nextLine(); while (!validateString(tName)) { System.out.println("cannot remain blank and cannot contain only spaces"); tName = sc2.nextLine(); } System.out.println("Enter task description"); String tDesc = sc2.nextLine(); System.out.println("Enter task end date"); String endDate = sc2.nextLine(); SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy"); Date dt = sf.parse(endDate); TaskBean obj = new TaskBean(tName, tDesc, dt); model.add(obj); String msg = model.addTask(obj, catName); if (msg.equals("success")) { System.out.println("Task added successfully!"); break; // not sure if this is required } else System.out.println("Message:" + msg); break; // adding task ends here default: System.out.println("enter correct input!"); break; case 2: System.out.println("going back..."); break; } // switch ch2 ends here } // while loop for ch2 ends here } // autoGoBack ends here autoGoBack = false; break; case 4: System.out.println("enter the name of category to search"); catName = sc1.next(); str = model.search(catName); System.out.println(str); break; case 2: System.out.println("list of categories already created"); categoryList = model.loadCat(); for (int i = 0; i < categoryList.length; i++) System.out.println(categoryList[i]); System.out.println(""); System.out.println(""); System.out.println("Enter a name of the category listed above"); catName = sc1.next(); if (!model.isCatPresent(catName)) { System.out.println("category not present"); break; } else { label: while (ch3 <= 7) { System.out.println("Press 1 to add a task"); System.out.println("Press 2 to list tasks"); System.out.println("Press 3 to search"); System.out.println("Press 4 to get details of the task"); System.out.println("Press 5 to update a task"); System.out.println("Press 6 to delete a task"); System.out.println("Press 7 to go back"); int ch4 = sc1.nextInt(); ArrayList<TaskBean> al; int flag; switch (ch4) { case 1: System.out.println("adding task..."); System.out.println("..."); System.out.println("..."); System.out.println("..."); System.out.println("Enter task name"); String tName = sc2.nextLine(); while (!validateString(tName)) { System.out.println("cannot remain blank and cannot contain only spaces"); tName = sc2.nextLine(); } System.out.println("Enter task description"); String tDesc = sc2.nextLine(); System.out.println("Enter task end date"); String endDate = sc2.nextLine(); SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy"); Date dt = sf.parse(endDate); TaskBean obj = new TaskBean(tName, tDesc, dt); model.add(obj); String msg = model.addTask(obj, catName); if (msg.equals("success")) { System.out.println("Task added successfully!"); } else System.out.println("Message:" + msg); break; // adding task ends here case 2: System.out.println(""); System.out.println(""); System.out.println("list of tasks in the category..."); al = model.listTasks(catName); /*for(int i=0;i<al.size();i++) { TaskBean t = al.get(i); System.out.println(t.getName()); }*/ System.out.println( "total number of tasks in the category............" + al.size()); for (TaskBean tt : al) { System.out.println(tt.getName()); } break; // listing of all tasks within the category ends here case 3: System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("enter the name of task to search"); String ch5 = sc2.nextLine(); al = model.listTasks(catName); int flag2 = 0; for (TaskBean tt : al) { if (!ch5.equals(tt.getName())) continue; else flag2 = 1; break; } if (flag2 == 0) System.out.println("task not present"); else System.out.println("task present"); break; // searching a task ends here case 4: System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("enter the name of task"); String ch6 = sc2.nextLine(); al = model.listTasks(catName); flag = 0; TaskBean t = null; for (TaskBean tt : al) { if (!ch6.equals(tt.getName())) continue; else flag = 1; t = tt; break; } if (flag == 0) System.out.println("task not present"); else { System.out.println("task name"); System.out.println(t.getName()); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("task description"); System.out.println(t.getDesc()); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("end date for the task"); System.out.println(t.getEndDt()); } break; case 5: System.out.println("Enter name of task to be updated"); String ch8 = sc2.nextLine(); al = model.listTasks(catName); int flag1 = 0; for (TaskBean tt : al) { if (!ch8.equals(tt.getName())) continue; else flag1 = 1; break; } if (flag1 == 0) System.out.println("task not present"); else { System.out.println("Enter new name of task"); String ch9 = sc2.nextLine(); while (!validateString(ch9)) { System.out.println("cannot remain blank and cannot contain only spaces"); ch9 = sc2.nextLine(); } System.out.println("Enter new description"); String ch10 = sc2.nextLine(); System.out.println("Enter End Date"); String ch11 = sc2.nextLine(); SimpleDateFormat sf1 = new SimpleDateFormat("dd/MM/yyyy"); Date dt1 = sf1.parse(ch11); ArrayList<String> xyz = model.updateTask(catName, ch8, ch9, ch10, dt1); model.reWrite(catName, xyz); System.out.println("successfully updated"); System.out.println(""); System.out.println(""); System.out.println(""); } break; // end of getting details case 6: System.out.println("Enter name of task to be deleted"); String ch7 = sc2.next(); al = model.listTasks(catName); int flag3 = 0; for (TaskBean tt : al) { if (!ch7.equals(tt.getName())) continue; else flag3 = 1; break; } if (flag3 == 0) System.out.println("task not present"); else { ArrayList<String> abc = model.deleteTask(catName, ch7); model.reWrite(catName, abc); System.out.println("Task successfully removed"); System.out.println(""); System.out.println(""); System.out.println(""); } break; case 7: System.out.println("going back..."); break label; default: System.out.println("option not supported yet!"); break; } // switch for ch4 ends here } // while for ch3!6 ends here } break; // end of loading category default: System.out.println("Option not supported...yet!"); break; case 6: System.out.println("closing task manager..."); break; case 5: System.out.println("enter the name of category"); String ch6 = sc2.nextLine(); str = model.search(ch6); System.out.println(str); if (str.equals("Not found")) System.out.println(str); else model.deleteFile(ch6); System.out.println("category removed!"); break; case 3: System.out.println("list of pending tasks"); ArrayList<TaskBean> al = model.listPendingTasks(); /*for(int i=0;i<al.size();i++) { TaskBean t = al.get(i); System.out.println(t.getName()); }*/ System.out.println("total number of pending tasks............" + al.size()); for (TaskBean tt : al) { System.out.println(tt.getName()); } } } } catch (Exception e) { System.out.println("oops! something went wrong!"); e.printStackTrace(); } }