CouchDbTimesheetService extends AbstractCouchDbService implements TimesheetService, TimesheetContextConstants { @Override public List<Timesheet> listAllTimesheet(Map<String, Object> context) { String loginUser = (String) context.get(CONTEXT_LOGIN_USER); try { Pattern idPattern = Pattern.compile("ts_([a-zA-Z0-9]*)_(\\d{4})_(\\d{2})"); List<Timesheet> res = new ArrayList<Timesheet>(); JSONObject couchObj = getCouch("_all_docs?startkey=\"ts_"+loginUser+"_\"&endkey=\"ts_"+loginUser+"_zzz\"&include_docs=true"); JSONArray couchTsList = couchObj.getJSONArray("rows"); for (int i = 0; i < couchTsList.length(); i++) { JSONObject couchTs = couchTsList.getJSONObject(i); Timesheet ts = new TimesheetCouch(couchTs.getString("id"), couchTs.getJSONObject("value").getString("rev")); String id = couchTs.getString("id"); Matcher idMatch = idPattern.matcher(id); idMatch.find(); ts.setYear(Integer.parseInt(idMatch.group(2))); ts.setMonth(Integer.parseInt(idMatch.group(3))); int daysInMonth; if (ts.getMonth() == 1 || ts.getMonth() == 3 || ts.getMonth() == 5 || ts.getMonth() == 7 || ts.getMonth() == 8 || ts.getMonth() == 10 || ts.getMonth() == 12) { daysInMonth = 31; } else if (ts.getMonth() == 2 && (ts.getYear() % 4) == 0) { daysInMonth = 29; } else if (ts.getMonth() == 2 && (ts.getYear() % 4) != 0) { daysInMonth = 28; } else if (ts.getMonth() == 4 || ts.getMonth() == 6 || ts.getMonth() == 9 || ts.getMonth() == 11) { daysInMonth = 30; } else { throw new IllegalArgumentException("Wrong month in timesheet"); } Map<Integer, TimesheetDay> days = new HashMap<Integer, TimesheetDay>(); for (int j = 1; j <= daysInMonth; j++) { TimesheetDay day = new TimesheetDay(); day.setDay(j); ts.addDay(day); days.put(j, day); } JSONArray couchEntriesList = couchTs.getJSONObject("doc").getJSONArray("entries"); for (int j = 0; j < couchEntriesList.length(); j++) { JSONObject couchEntry = couchEntriesList.getJSONObject(j); TimesheetDayEntry dayEntry = new TimesheetDayEntry(); dayEntry.setHours(couchEntry.getString("hours")); Project project = new Project(); project.setName(couchEntry.getString("project")); dayEntry.setProject(project); days.get(couchEntry.getInt("dayInMonth")).addEntry(dayEntry); } res.add(ts); } return res; } catch (JSONException e) { throw new TechnicalException(e); } catch (MalformedURLException e) { throw new TechnicalException(e); } catch (IOException e) { throw new TechnicalException(e); } } CouchDbTimesheetService(); @Override List<Timesheet> listAllTimesheet(Map<String, Object> context); @Override Timesheet addNewTimesheet(Timesheet timesheet, Map<String, Object> context); @Override Timesheet updateExistingTimesheet(Timesheet timesheet, Map<String, Object> context); @Override void removeExistingTimesheet(Timesheet timesheet,
			Map<String, Object> context); @Override long countTimesheet(TimesheetQuery query, Map<String, Object> context); @Override List<Timesheet> listTimesheet(TimesheetQuery query, Map<String, Object> context);  }
@Test public void testListAllTimesheet() { getResults .put("_all_docs?startkey=\"ts_sebastian_\"&endkey=\"ts_sebastian_zzz\"&include_docs=true", "{\"total_rows\":9,\"offset\":8,\"rows\":[{\"id\":\"ts_sebastian_2014_07\",\"key\":\"ts_sebastian_2014_07\",\"value\":{\"rev\":\"2-1983e046a14377c433c08711fbb57b28\"},\"doc\":{\"_id\":\"ts_sebastian_2014_07\",\"_rev\":\"2-1983e046a14377c433c08711fbb57b28\",\"entries\":[{\"dayInMonth\":1,\"project\":\"P1\",\"hours\":\"2:31\"},{\"dayInMonth\":2,\"project\":\"P1\",\"hours\":\"1:11\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"2:22\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"3:33\"},{\"dayInMonth\":14,\"project\":\"P1\",\"hours\":\"1:00\"}]}}]}"); List<Timesheet> res = timesheetService.listAllTimesheet(context); assertEquals(1, getUrls.size()); assertEquals(0, putUrls.size()); assertEquals(1, res.size()); Timesheet ts = res.get(0); assertNotNull(ts); assertEquals(TimesheetCouch.class, ts.getClass()); assertEquals("ts_sebastian_2014_07", ((TimesheetCouch) ts).getCouchId()); assertEquals("2-1983e046a14377c433c08711fbb57b28", ((TimesheetCouch) ts).getCouchRev()); assertEquals(7, ts.getMonth()); assertEquals(2014, ts.getYear()); assertEquals(31, ts.getDays().size()); assertEquals(1, ts.getDays().get(0).getDay()); assertEquals(1, ts.getDays().get(0).getEntries().size()); assertEquals("P1", ts.getDays().get(0).getEntries().get(0).getProject().getName()); assertEquals("2:31", ts.getDays().get(0).getEntries().get(0).getHours()); assertEquals(2, ts.getDays().get(1).getDay()); assertEquals(3, ts.getDays().get(1).getEntries().size()); assertEquals(14, ts.getDays().get(13).getDay()); assertEquals(1, ts.getDays().get(13).getEntries().size()); assertEquals(15, ts.getDays().get(14).getDay()); assertEquals(0, ts.getDays().get(14).getEntries().size()); }