List of usage examples for org.joda.time YearMonth toLocalDate
public LocalDate toLocalDate(int dayOfMonth)
From source file:org.alexlg.bankit.controllers.AccountController.java
License:Open Source License
/** * Build categories summary for each month from startDate * to previous nbPrevMonth/*from w w w .j av a 2 s .co m*/ * @param startDate Start the summary for this month * @param endDate Stop the summary for this month * @return Map with the date of the month and a Map with Category * and amount for this category for this month */ protected Map<Date, Map<Category, BigDecimal>> buildCategories(LocalDate startDate, LocalDate endDate) { Map<Date, Map<Category, BigDecimal>> categories = new LinkedHashMap<Date, Map<Category, BigDecimal>>(); YearMonth curMonth = null; //month we start to retrieve YearMonth endMonth = null; //last month we have to retrieve if (startDate.isBefore(endDate)) { curMonth = new YearMonth(startDate.getYear(), startDate.getMonthOfYear()); endMonth = new YearMonth(endDate.getYear(), endDate.getMonthOfYear()); } else { curMonth = new YearMonth(endDate.getYear(), endDate.getMonthOfYear()); endMonth = new YearMonth(startDate.getYear(), startDate.getMonthOfYear()); } do { Map<Category, BigDecimal> monthSummary = categoryDao.getMonthSummary(curMonth); if (monthSummary.size() > 0) { categories.put(curMonth.toLocalDate(1).toDate(), monthSummary); } curMonth = curMonth.plusMonths(1); } while (curMonth.isBefore(endMonth) || curMonth.isEqual(endMonth)); return categories; }
From source file:org.alexlg.bankit.dao.CategoryDao.java
License:Open Source License
/** * Calculate the amount of operations for all categories on a specific month * @param yearMonth Year and month of the summary to calculate * @return Map containing the Category and the amount for the month *//*from w w w . j a v a 2s.c om*/ public Map<Category, BigDecimal> getMonthSummary(YearMonth yearMonth) { CriteriaBuilder b = getBuilder(); //SELECT PASSED OPERATION //create criteria and join CriteriaQuery<Tuple> q = b.createTupleQuery(); Root<Operation> operation = q.from(Operation.class); //we left join to get operation with no categories Join<Operation, Category> category = operation.join(Operation_.category, JoinType.LEFT); //select //sum all amount operation for operation imported from the bank Expression<BigDecimal> sum = b.sum(operation.get(Operation_.amount)); //sum only planned amount if the amount is not set (as we have a planned operation) //we use a sum(case when xx end) for that //in sql, it will be translated into : sum(case when o.amount is null then o.planned otherwise 0 end) Expression<BigDecimal> sumPlanned = b.sum(b.<BigDecimal>selectCase() .when(b.isNull(operation.get(Operation_.amount)), operation.get(Operation_.planned)) .otherwise(BigDecimal.ZERO)); //select the 3 fields into a tuple q.select(b.tuple(category, sum, sumPlanned)); //where clause : between the start/end date, and for operation with no category, only < 0 LocalDate startDate = yearMonth.toLocalDate(1); LocalDate endDate = startDate.withDayOfMonth(startDate.dayOfMonth().getMaximumValue()); q.where(b.between(operation.get(Operation_.operationDate), startDate.toDate(), endDate.toDate()), b.or(b.isNotNull(operation.get(Operation_.category)), b.lt(operation.get(Operation_.amount), 0), b.lt(operation.get(Operation_.planned), 0))); //group by q.groupBy(category.get(Category_.categoryId)); //order by q.orderBy(b.asc(category.get(Category_.name))); //execute query List<Tuple> results = getEm().createQuery(q).getResultList(); //put in map Map<Category, BigDecimal> resMap = new LinkedHashMap<Category, BigDecimal>(results.size()); //saving null category for adding at the end BigDecimal noCatAmount = null; for (Tuple res : results) { Category resCat = res.get(category); BigDecimal sumVal = res.get(sum); BigDecimal sumPlannedVal = res.get(sumPlanned); if (sumVal == null) sumVal = BigDecimal.ZERO; if (sumPlannedVal == null) sumPlannedVal = BigDecimal.ZERO; BigDecimal sumTotal = sumVal.add(sumPlannedVal); if (!sumTotal.equals(BigDecimal.ZERO)) { if (resCat != null) { resMap.put(resCat, sumTotal); } else { noCatAmount = sumTotal; } } } //adding operation with no categories at the end of the list if (noCatAmount != null) { Category noCat = new Category(); noCat.setCategoryId(-1); noCat.setName(""); resMap.put(noCat, noCatAmount); } return resMap; }