r17 - in trunk/cash-business/src: main/java/org/chorem/cash/business main/java/org/chorem/cash/impl main/java/org/chorem/cash/persistence main/xmi test/java/org/chorem/cash/impl
Author: fdesbois Date: 2009-08-20 18:14:29 +0200 (Thu, 20 Aug 2009) New Revision: 17 Added: trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelper.java trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelperByMonth.java trunk/cash-business/src/main/java/org/chorem/cash/persistence/EntryEntityDAOImpl.java Modified: trunk/cash-business/src/main/java/org/chorem/cash/business/EntryHelper.java trunk/cash-business/src/main/java/org/chorem/cash/impl/ServiceEntryImpl.java trunk/cash-business/src/main/xmi/cash.zargo trunk/cash-business/src/test/java/org/chorem/cash/impl/ServiceEntryImplTest.java Log: - Implementation for frequency - get for tree frequency types (unlimited, untilDate, nbTimes) - createUpdate for particular case modify only current occurence (unlimited) Modified: trunk/cash-business/src/main/java/org/chorem/cash/business/EntryHelper.java =================================================================== --- trunk/cash-business/src/main/java/org/chorem/cash/business/EntryHelper.java 2009-08-19 10:12:28 UTC (rev 16) +++ trunk/cash-business/src/main/java/org/chorem/cash/business/EntryHelper.java 2009-08-20 16:14:29 UTC (rev 17) @@ -20,17 +20,18 @@ package org.chorem.cash.business; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.chorem.cash.CashModelDAOHelper; import org.chorem.cash.ContextUtilCash; +import org.chorem.cash.dto.Category; import org.chorem.cash.dto.Entry; import org.chorem.cash.dto.Reference; import org.chorem.cash.persistence.CategoryEntity; +import org.chorem.cash.persistence.CategoryEntityDAO; import org.chorem.cash.persistence.EntryEntity; import org.chorem.cash.persistence.EntryEntityDAO; import org.chorem.cash.persistence.EntryFrequencyEntity; @@ -57,7 +58,7 @@ */ public class EntryHelper { - public static Entry getEntry(EntryEntity entity) throws ConvertException { + public static Entry getEntry(EntryEntity entity, Date current) throws ConvertException { Entry entry = Convert.toDTO(entity, EntryEntity.class, Entry.class); entry.setId(ContextUtilCash.convertId(entity.getTopiaId())); CategoryEntity category = entity.getCategoryEntity(); @@ -79,13 +80,62 @@ entry.setUnlimited(frequency.getUnlimited()); entry.setUntilDate(frequency.getUntilDate()); entry.setPeriodicity(frequency.getPeriodicity()); - entry.setMonths(frequency.getMonths()); + entry.setCurrentDate(current); + + //entry.setMonths(frequency.getMonths()); } return entry; } + + public static EntryEntity createUpdateEntry(Entry entry, TopiaContext transaction, Log log) + throws TopiaException, IllegalArgumentException, EntityException { + for (Reference reference : entry.getReferences()) { + ReferenceHelper.createUpdateReference(reference, transaction, log); + } + + EntryEntityDAO dao = CashModelDAOHelper.getEntryEntityDAO(transaction); + + Map<String, Object> saveParams = new HashMap<String, Object>(); + + saveParams.put(EntryEntity.LIBELLE, entry.getLibelle()); + + Category entryCategory = entry.getCategory(); + if (entryCategory != null) { + CategoryEntityDAO categDAO = CashModelDAOHelper.getCategoryEntityDAO(transaction); + CategoryEntity category = ServiceHelper.getEntity(categDAO, entryCategory.getId(), log); + saveParams.put(EntryEntity.CATEGORY_ENTITY, category); + } else { + throw new IllegalArgumentException("Null parameter Category"); + } + + // Modify amount sign if credit is false + if (!entry.getCategory().getCredit()) { + double amount = entry.getAmount(); + if (amount > 0) { // no modification if already negatif + entry.setAmount(-amount); + } + } + + saveParams.put(EntryEntity.AMOUNT, entry.getAmount()); + saveParams.put(EntryEntity.COMMENT, entry.getComment()); + saveParams.put(EntryEntity.ENTRY_DATE, entry.getEntryDate()); + + if (entry.getPeriodicity() != null && !entry.getPeriodicity().isEmpty()) { + EntryFrequencyEntity frequency = EntryHelper.createUpdateFrequency(entry, transaction, log); + + saveParams.put(EntryEntity.ENTRY_FREQUENCY_ENTITY, frequency); + } + + EntryEntity entity = ServiceHelper.createUpdateEntity(dao, entry.getId(), null, saveParams, log); + + entry.setId(ContextUtilCash.convertId(entity.getTopiaId())); + + return entity; + } + public static EntryFrequencyEntity createUpdateFrequency(Entry entry, TopiaContext transaction, Log log) throws IllegalArgumentException, TopiaException, EntityException { @@ -98,15 +148,6 @@ saveParams.put(EntryFrequencyEntity.NB_TIMES, entry.getNbTimes()); } else if (entry.getUnlimited()) { saveParams.put(EntryFrequencyEntity.UNLIMITED, entry.getUnlimited()); - - int firstMonth = DateUtils.getMonth(entry.getEntryDate()); - Collection<Integer> months = getMonthsIntervals(entry.getInterval(), firstMonth); - - if (log.isDebugEnabled()) { - log.debug(months); - } - - saveParams.put(EntryFrequencyEntity.MONTHS, months); } else if (entry.getUntilDate() != null) { saveParams.put(EntryFrequencyEntity.UNTIL_DATE, entry.getUntilDate()); } @@ -121,23 +162,37 @@ return entity; } - private static Collection<Integer> getMonthsIntervals(int interval, int firstMonth) { + public static int[] getMonthsIntervals(int interval, int firstMonth) { int max = 12 / interval; - //int[] results = new int[max]; - List<Integer> results = new ArrayList<Integer>(); + int[] results = new int[max]; + //List<Integer> results = new ArrayList<Integer>(); - //results[0] = firstMonth; - results.add(new Integer(firstMonth)); + results[0] = firstMonth; + //results.add(new Integer(firstMonth)); //results.set(0, new Integer(firstMonth)); for (int i = 1; i < max; i++) { - int curr = results.get(i-1).intValue(); - int res = ( curr + interval ) % 12; - results.add(new Integer(res)); - //results[i] = ( results[i-1] + interval ) % 12; + //int curr = results.get(i-1).intValue(); + //int res = ( curr + interval ) % 12; + //results.add(new Integer(res)); + results[i] = ( results[i-1] + interval ) % 12; } return results; } + + public static void addEntryForMonth(EntryEntity entity, Date currentDate, Map<String, List<Entry>> results, String pattern) throws ConvertException { + /*if (log.isDebugEnabled() && entity.getEntryFrequencyEntity() != null) { + log.debug("PERIODIC ENTRY : beginDate = " + entity.getEntryDate() + " _ current = " + currentDate); + }*/ + Entry result = EntryHelper.getEntry(entity, currentDate); + String month = DateUtils.formatDate(currentDate, pattern); + List<Entry> current = results.get(month); + if (current == null) { + current = new ArrayList<Entry>(); + results.put(month, current); + } + current.add(result); + } } Added: trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelper.java =================================================================== --- trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelper.java (rev 0) +++ trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelper.java 2009-08-20 16:14:29 UTC (rev 17) @@ -0,0 +1,112 @@ +/** + * *##% Ca$h Business + * Copyright (C) 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. ##%* + */ + +package org.chorem.cash.business; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.cash.persistence.EntryEntity; +import org.chorem.cash.persistence.EntryFrequencyEntity; +import org.chorem.utils.PeriodDates; + +/** + * PeriodicHelper.java + * + * Created on 2009-08-20 + * + * @author fdesbois + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public abstract class PeriodicHelper<T> { + + protected static final Log log = LogFactory.getLog(PeriodicHelper.class); + + protected PeriodDates period; + + private Calendar calendarEnd; + + protected Calendar calendar; + + protected EntryEntity entity; + + private int interval; + + private int calendarField; + + protected T results; + + public PeriodicHelper(EntryEntity entity, PeriodDates period) { + init(entity); + this.period = period; + } + + public PeriodicHelper(EntryEntity entity, Date endDate) { + init(entity); + this.calendarEnd = new GregorianCalendar(); + this.calendarEnd.setTime(endDate); + } + + private void init(EntryEntity entity) { + this.entity = entity; + this.calendar = new GregorianCalendar(); + this.calendar.setTime(entity.getEntryDate()); + EntryFrequencyEntity frequency = entity.getEntryFrequencyEntity(); + Periodicity periodicity = Periodicity.valueOf(frequency.getPeriodicity()); + this.interval = frequency.getInterval(); + this.calendarField = getField(periodicity); + } + + private int getField(Periodicity rule) { + switch (rule) { + case MONTHLY: return Calendar.MONTH; + } + return Calendar.MONTH; + } + + public T execute(T start) throws Exception { + + calendar.add(calendarField, interval); + boolean beforeEnd = period != null ? period.beforeEnd(calendar) : calendar.before(calendarEnd); + + while (beforeEnd && predicat()) { + if (log.isDebugEnabled()) { + log.debug("PREDICAT : beforeEnd = " + beforeEnd + " _ predicat = " + predicat()); + log.debug("PERIODIC ENTRY : current = " + calendar.getTime()); + } + boolean afterBegin = period != null ? period.afterBegin(calendar) : true; + if (afterBegin) { + start = action(start); //addEntryForMonth(periodicEntry, calendar.getTime(), results, period.getPattern()); + } + calendar.add(calendarField, interval); + beforeEnd = period != null ? period.beforeEnd(calendar) : calendar.before(calendarEnd); + } + return start; + } + + public abstract boolean predicat(); + + public abstract T action(T start) throws Exception; + +} Property changes on: trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelper.java ___________________________________________________________________ Added: svn:keywords + Author Date Revision Added: trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelperByMonth.java =================================================================== --- trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelperByMonth.java (rev 0) +++ trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelperByMonth.java 2009-08-20 16:14:29 UTC (rev 17) @@ -0,0 +1,51 @@ +/** + * *##% Ca$h Business + * Copyright (C) 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. ##%* + */ + +package org.chorem.cash.business; + +import java.util.List; +import java.util.Map; +import org.chorem.cash.dto.Entry; +import org.chorem.cash.persistence.EntryEntity; +import org.chorem.utils.PeriodDates; + +/** + * PeriodicHelperByMonth.java + * + * Created on 2009-08-20 + * + * @author fdesbois + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public abstract class PeriodicHelperByMonth extends PeriodicHelper<Map<String, List<Entry>>> { + + public PeriodicHelperByMonth(EntryEntity entity, PeriodDates period) { + super(entity, period); + } + + @Override + public Map<String, List<Entry>> action(Map<String, List<Entry>> start) throws Exception { + EntryHelper.addEntryForMonth(entity, calendar.getTime(), start, period.getPattern()); + return start; + } + +} Property changes on: trunk/cash-business/src/main/java/org/chorem/cash/business/PeriodicHelperByMonth.java ___________________________________________________________________ Added: svn:keywords + Author Date Revision Modified: trunk/cash-business/src/main/java/org/chorem/cash/impl/ServiceEntryImpl.java =================================================================== --- trunk/cash-business/src/main/java/org/chorem/cash/impl/ServiceEntryImpl.java 2009-08-19 10:12:28 UTC (rev 16) +++ trunk/cash-business/src/main/java/org/chorem/cash/impl/ServiceEntryImpl.java 2009-08-20 16:14:29 UTC (rev 17) @@ -26,12 +26,16 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.cash.CashException; import org.chorem.cash.CashModelDAOHelper; import org.chorem.cash.ContextUtilCash; import org.chorem.cash.business.EntryHelper; +import org.chorem.cash.business.PeriodicHelper; +import org.chorem.cash.business.PeriodicHelperByMonth; import org.chorem.cash.business.Periodicity; import org.chorem.cash.business.ReferenceHelper; import org.chorem.cash.dto.Category; @@ -44,6 +48,7 @@ import org.chorem.cash.persistence.EntryFrequencyEntity; import org.chorem.cash.services.ServiceEntry; import org.chorem.cash.services.ServiceEntryAbstract; +import org.chorem.exceptions.ConvertException; import org.chorem.utils.DateUtils; import org.chorem.utils.PeriodDates; import org.chorem.utils.ServiceHelper; @@ -76,54 +81,51 @@ try { transaction = context.beginTransaction(); - for (Reference reference : entry.getReferences()) { - ReferenceHelper.createUpdateReference(reference, transaction, log); - } - EntryEntityDAO dao = CashModelDAOHelper.getEntryEntityDAO(transaction); - Map<String, Object> saveParams = new HashMap<String, Object>(); - - saveParams.put(EntryEntity.LIBELLE, entry.getLibelle()); + if (entry.getId() != null && !entry.getId().isEmpty()) { // L'entree existe - Category entryCategory = entry.getCategory(); - if (entryCategory != null) { - CategoryEntityDAO categDAO = CashModelDAOHelper.getCategoryEntityDAO(transaction); - CategoryEntity category = ServiceHelper.getEntity(categDAO, entryCategory.getId(), log); - saveParams.put(EntryEntity.CATEGORY_ENTITY, category); - } else { - ContextUtilCash.serviceException(transaction, "Category must be specified to create or update an entry", - new IllegalArgumentException("Null parameter Category")); - } + EntryEntity oldEntry = ServiceHelper.getEntity(dao, entry.getId(), log); - // Modify amount sign if credit is false - if (!entry.getCategory().getCredit()) { - double amount = entry.getAmount(); - if (amount > 0) { // no modification if already negatif - entry.setAmount(-amount); - } - } + EntryFrequencyEntity frequency = oldEntry.getEntryFrequencyEntity(); - saveParams.put(EntryEntity.AMOUNT, entry.getAmount()); - saveParams.put(EntryEntity.COMMENT, entry.getComment()); - saveParams.put(EntryEntity.ENTRY_DATE, entry.getEntryDate()); + if (frequency != null && (entry.getPeriodicity() == null || "".equals(entry.getPeriodicity()))) { // Entite anciennement periodique mais l'entree courante devient indépendante - if (entry.getPeriodicity() != null && !entry.getPeriodicity().isEmpty()) { + if (frequency.getUnlimited()) { // Cas illimite : une seule entite en base - String periodicity = entry.getPeriodicity(); - if (Periodicity.valueOf(periodicity).equals(Periodicity.MONTHLY)) { + // On recupere l'ancienne version de l'entite (avec sa frequence) + Calendar calendar = new GregorianCalendar(); - EntryFrequencyEntity frequency = EntryHelper.createUpdateFrequency(entry, transaction, log); + // On cree une nouvelle entree pour la periodicite avant l'occurence courante + Entry newBeforeEntry = EntryHelper.getEntry(oldEntry, null); + newBeforeEntry.setId(""); + // On va modifier la frequence en changeant le unlimited par une date de fin (untilDate) + // La date de fin est calcule en fonction de la date de l'occurence passe en parametre du service + calendar.setTime(entry.getCurrentDate()); + calendar.add(Calendar.MONTH, -entry.getInterval()); // PERIODICITY = MONTHLY + newBeforeEntry.setUntilDate(calendar.getTime()); + newBeforeEntry.setUnlimited(false); - saveParams.put(EntryEntity.ENTRY_FREQUENCY_ENTITY, frequency); + EntryHelper.createUpdateEntry(newBeforeEntry, transaction, log); + + // On cree une nouvelle entree pour la periodicite apres l'occurence courante + Entry newAfterEntry = EntryHelper.getEntry(oldEntry, null); + newAfterEntry.setId(""); + // On va modifier la nouvelle entree en changeant la date de debut (entryDate) + calendar.setTime(entry.getCurrentDate()); + calendar.add(Calendar.MONTH, entry.getInterval()); // PERIODICITY = MONTHLY + newAfterEntry.setEntryDate(calendar.getTime()); + + EntryHelper.createUpdateEntry(newAfterEntry, transaction, log); + } else if (frequency.getNbTimes() > 0) { + + } } } - EntryEntity entity = ServiceHelper.createUpdateEntity(dao, entry.getId(), null, saveParams, log); + EntryHelper.createUpdateEntry(entry, transaction, log); - entry.setId(ContextUtilCash.convertId(entity.getTopiaId())); - transaction.commitTransaction(); transaction.closeContext(); } catch (Exception eee) { @@ -141,7 +143,7 @@ EntryEntityDAO dao = CashModelDAOHelper.getEntryEntityDAO(transaction); EntryEntity entity = ServiceHelper.getEntity(dao, entryId, log); - result = EntryHelper.getEntry(entity); + result = EntryHelper.getEntry(entity, null); transaction.closeContext(); } catch (Exception eee) { @@ -160,7 +162,7 @@ EntryEntityDAO dao = CashModelDAOHelper.getEntryEntityDAO(transaction); for (EntryEntity entity : dao.findAllWithOrder(EntryEntity.ENTRY_DATE)) { - Entry result = EntryHelper.getEntry(entity); + Entry result = EntryHelper.getEntry(entity, null); results.add(result); } @@ -204,44 +206,87 @@ EntryEntityDAO dao = CashModelDAOHelper.getEntryEntityDAO(transaction); - List<EntryEntity> entries = transaction.find( - "FROM " + EntryEntity.class.getName() + - " WHERE " + EntryEntity.ENTRY_DATE + " BETWEEN :dateFrom AND :dateThru" + - " AND " + EntryEntity.ENTRY_FREQUENCY_ENTITY + " IS NULL " + - " ORDER BY " + EntryEntity.ENTRY_DATE, - "dateFrom", period.getFromDate(), - "dateThru", period.getThruDate()); + List<EntryEntity> entries = dao.findAllBetweenDates(period.getFromDate(), period.getThruDate()); - /*List<EntryEntity> periodicEntries = new ArrayList<EntryEntity>(); - for (Date monthDate : period.getMonths()) { - List<EntryEntity> queryResults = transaction.find( - "FROM " + EntryEntity.class.getName() + - " WHERE " + EntryEntity.ENTRY_FREQUENCY_ENTITY + " IS NOT NULL " + - " AND " + EntryEntity.ENTRY_FREQUENCY_ENTITY + "." + EntryFrequencyEntity.UNLIMITED + " = :ok" + - " AND :month IN " + EntryEntity.ENTRY_FREQUENCY_ENTITY + "." + EntryFrequencyEntity.MONTHS + - " ORDER BY " + EntryEntity.ENTRY_DATE, - "ok", true, - "month", DateUtils.getMonth(monthDate)); - periodicEntries.addAll(queryResults); - }*/ + for (EntryEntity entity : entries) { + EntryHelper.addEntryForMonth(entity, entity.getEntryDate(), results, period.getPattern()); + } + List<EntryEntity> periodicEntries = dao.findUnlimitedPeriodicEntries(); + + for (EntryEntity periodicEntry : periodicEntries) { + EntryFrequencyEntity frequency = periodicEntry.getEntryFrequencyEntity(); + + /*if (frequency.getUnlimited()) { + PeriodicHelperByMonth helper = new PeriodicHelperByMonth(periodicEntry, period) { + @Override + public boolean predicat() { + return true; + } + }; + results = helper.execute(results); + } else if (frequency.getNbTimes() > 0) { + PeriodicHelperByMonth helper = new PeriodicHelperByMonth(periodicEntry, period) { + private int count = 0; + + @Override + public boolean predicat() { + return count < entity.getEntryFrequencyEntity().getNbTimes(); + } + }; + results = helper.execute(results); + } else if (frequency.getUntilDate() != null) { + PeriodicHelperByMonth helper = new PeriodicHelperByMonth(periodicEntry, period) { + @Override + public boolean predicat() { + Date untilDate = entity.getEntryFrequencyEntity().getUntilDate(); + return calendar.getTime().before(untilDate) || untilDate.equals(calendar.getTime()); + } + }; + results = helper.execute(results); + }*/ + + + Calendar calendar = new GregorianCalendar(); + calendar.setTime(periodicEntry.getEntryDate()); + // Si entryDate est contenu dans la periode, l'entree sera deja dans la liste des entries + calendar.add(Calendar.MONTH, frequency.getInterval()); + + Periodicity periodicity = Periodicity.valueOf(frequency.getPeriodicity()); + + if (frequency.getUnlimited()) { + while (period.beforeEnd(calendar)) { + if (period.afterBegin(calendar)) { + EntryHelper.addEntryForMonth(periodicEntry, calendar.getTime(), results, period.getPattern()); + } + calendar.add(Calendar.MONTH, frequency.getInterval()); + } + } else if (frequency.getNbTimes() > 0) { + int count = 0; + while (period.beforeEnd(calendar) && count < frequency.getNbTimes()) { + calendar.add(Calendar.MONTH, frequency.getInterval()); + if (period.afterBegin(calendar)) { + EntryHelper.addEntryForMonth(periodicEntry, calendar.getTime(), results, period.getPattern()); + } + count++; + } + } else if (frequency.getUntilDate() != null) { + Date untilDate = frequency.getUntilDate(); + while (period.beforeEnd(calendar) && (calendar.getTime().before(untilDate) || untilDate.equals(calendar.getTime()))) { + calendar.add(Calendar.MONTH, frequency.getInterval()); + if (period.afterBegin(calendar)) { + EntryHelper.addEntryForMonth(periodicEntry, calendar.getTime(), results, period.getPattern()); + } + } + } + } + if (log.isDebugEnabled()) { log.debug("NbEntries find : " + entries.size()); - //log.debug("NbPeriodicEntries find : " + periodicEntries.size()); + log.debug("NbPeriodicEntries find : " + periodicEntries.size()); + log.debug("Results added : " + results.size() + " _ keys = " + results.keySet()); } - for (EntryEntity entity : entries) { - Entry result = EntryHelper.getEntry(entity); - - String entityDate = DateUtils.formatDate(entity.getEntryDate(), period.getPattern()); - List<Entry> current = results.get(entityDate); - if (current == null) { - current = new ArrayList<Entry>(); - results.put(entityDate, current); - } - current.add(result); - } - transaction.closeContext(); } catch (Exception eee) { ContextUtilCash.serviceException(transaction, "Unable to get entries between dates " + period.getFromDate() + " and " + @@ -250,6 +295,8 @@ return results; } + + @Override public double getSumBeforeDate(Date date) throws CashException { TopiaContext transaction = null; @@ -257,16 +304,50 @@ try { transaction = context.beginTransaction(); - List results = transaction.find( - "SELECT SUM(amount) FROM " + EntryEntity.class.getName() + - " WHERE " + EntryEntity.ENTRY_DATE + " < :date", - "date", date); - - // le find renvoie une liste obligatoirement non null mais non vide (1 seul element potentiellement = null) - if (results.get(0) != null) { - total = (Double)results.get(0); + EntryEntityDAO dao = CashModelDAOHelper.getEntryEntityDAO(transaction); + + total = dao.sumAmountsBeforeDate(date); + + if (log.isDebugEnabled()) { + log.debug("TOTAL before UnlimitedPeriodicEntries : " + total); } + // Recherche sur les entites periodiques sans limite + for (EntryEntity periodicEntry : dao.findUnlimitedPeriodicEntries()) { + + /*PeriodicHelper<Double> helper = new PeriodicHelper<Double>(periodicEntry, date) { + @Override + public boolean predicat() { + return true; + } + + @Override + public Double action(Double start) throws Exception { + Double dd = new Double(entity.getAmount()); + start += dd; + return start; + } + }; + + total = helper.execute(total);*/ + + EntryFrequencyEntity frequency = periodicEntry.getEntryFrequencyEntity(); + Calendar calendar = new GregorianCalendar(); + calendar.setTime(periodicEntry.getEntryDate()); + // Si entryDate est avant la date, la somme sera deja compte dans le total + calendar.add(Calendar.MONTH, frequency.getInterval()); + int count = 0; + while (calendar.getTime().before(date)) { + count++; + total += periodicEntry.getAmount(); + calendar.add(Calendar.MONTH, frequency.getInterval()); + } + } + + if (log.isDebugEnabled()) { + log.debug("TOTAL after UnlimitedPeriodicEntries : " + total); + } + transaction.closeContext(); } catch (Exception eee) { ContextUtilCash.serviceException(transaction, "Unable to get sum before " + date, eee); Added: trunk/cash-business/src/main/java/org/chorem/cash/persistence/EntryEntityDAOImpl.java =================================================================== --- trunk/cash-business/src/main/java/org/chorem/cash/persistence/EntryEntityDAOImpl.java (rev 0) +++ trunk/cash-business/src/main/java/org/chorem/cash/persistence/EntryEntityDAOImpl.java 2009-08-20 16:14:29 UTC (rev 17) @@ -0,0 +1,81 @@ +/** + * *##% Ca$h Business + * Copyright (C) 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. ##%* + */ + +package org.chorem.cash.persistence; + +import java.util.Date; +import java.util.List; +import org.nuiton.topia.TopiaException; + +/** + * ServiceCategoryImpl.java + * + * Created on 2009-08-20 + * + * @author fdesbois + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class EntryEntityDAOImpl<E extends EntryEntity> extends EntryEntityDAOAbstract<E> { + + @Override + public List<EntryEntity> findUnlimitedPeriodicEntries() throws TopiaException { + List<EntryEntity> periodicEntries = getContext().find( + "FROM " + EntryEntity.class.getName() + + " WHERE " + EntryEntity.ENTRY_FREQUENCY_ENTITY + " IS NOT NULL " + + " AND " + EntryEntity.ENTRY_FREQUENCY_ENTITY + "." + EntryFrequencyEntity.UNLIMITED + " = :bool" + + " ORDER BY " + EntryEntity.ENTRY_DATE, + "bool", true); + return periodicEntries; + } + + @Override + public List<EntryEntity> findAllBetweenDates(Date dateFrom, Date dateThru) throws TopiaException { + List<EntryEntity> entries = getContext().find( + "FROM " + EntryEntity.class.getName() + + " WHERE " + EntryEntity.ENTRY_DATE + " BETWEEN :dateFrom AND :dateThru" + + /*" AND (" + + "(" + EntryEntity.ENTRY_FREQUENCY_ENTITY + " IS NULL) " + + " OR ( " + EntryEntity.ENTRY_FREQUENCY_ENTITY + "." + EntryFrequencyEntity.UNLIMITED + " = :bool " + + ")" + + ")" +*/ + " ORDER BY " + EntryEntity.ENTRY_DATE, + "dateFrom", dateFrom, + "dateThru", dateThru); + return entries; + } + + @Override + public double sumAmountsBeforeDate(Date date) throws TopiaException { + double total = 0; + List results = getContext().find( + "SELECT SUM(amount) FROM " + EntryEntity.class.getName() + + " WHERE " + EntryEntity.ENTRY_DATE + " < :date", + "date", date); + + // le find renvoie une liste obligatoirement non null mais non vide (1 seul element potentiellement = null) + if (results.get(0) != null) { + total = (Double)results.get(0); + } + return total; + } + +} Property changes on: trunk/cash-business/src/main/java/org/chorem/cash/persistence/EntryEntityDAOImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Revision Modified: trunk/cash-business/src/main/xmi/cash.zargo =================================================================== (Binary files differ) Modified: trunk/cash-business/src/test/java/org/chorem/cash/impl/ServiceEntryImplTest.java =================================================================== --- trunk/cash-business/src/test/java/org/chorem/cash/impl/ServiceEntryImplTest.java 2009-08-19 10:12:28 UTC (rev 16) +++ trunk/cash-business/src/test/java/org/chorem/cash/impl/ServiceEntryImplTest.java 2009-08-20 16:14:29 UTC (rev 17) @@ -88,11 +88,11 @@ Category categ = ServiceCategoryImplTest.addParentCategory(serviceCateg, "CATEG", 1, true); - Entry entry1 = createEntry(instance, "FIRST", DateUtils.createDate(5, 9, 2009), 15.00, categ); + Entry entry1 = createEntry(instance, "FIRST", DateUtils.createDate(5, 9, 2009), 15.00, categ, 0); - Entry entry2 = createEntry(instance, "SECOND", DateUtils.createDate(5, 12, 2009), 78.24, categ); + Entry entry2 = createEntry(instance, "SECOND", DateUtils.createDate(5, 12, 2009), 78.24, categ, 0); - Entry entry3 = createEntry(instance, "THIRD", DateUtils.createDate(15, 12, 2009), 258.44, categ); + Entry entry3 = createEntry(instance, "THIRD", DateUtils.createDate(15, 12, 2009), 258.44, categ, 0); try { List<Entry> list = instance.getEntries(); @@ -105,11 +105,12 @@ fail("CashException"); } - Date today = new Date(); - Date afterSixMonth = DateUtils.createDateAfterToday(0, 6, 0); - log.debug("DATES : " + today + " _ " + afterSixMonth); + Date date1 = DateUtils.createDate(20, 8, 2009); + //Date afterSixMonth = DateUtils.createDateAfterToday(0, 6, 0); + Date date2 = DateUtils.createDate(20, 2, 2010); + log.debug("DATES : " + date1 + " _ " + date2); - PeriodDates period = new PeriodDates(today, afterSixMonth); + PeriodDates period = new PeriodDates(date1, date2); period.setPattern(DateUtils.MONTH_PATTERN); log.info("RANGETIME : " + period + " _ pattern [" + period.getPattern() + "]"); @@ -140,7 +141,7 @@ } - /*@Test + @Test public void testCreateUpdateEntry() throws Exception { log.info("createUpdateEntry"); @@ -149,35 +150,43 @@ ServiceCategoryImpl serviceCateg = new ServiceCategoryImpl(); Category categ = ServiceCategoryImplTest.addParentCategory(serviceCateg, "CATEG", 1, true); - Entry entry1 = createEntry(instance, "FIRST", DateUtils.createDate(5, 9, 2009), 15.00, categ); + Entry entry1 = createEntry(instance, "FIRST", DateUtils.createDate(5, 9, 2009), 15.00, categ, 0); - log.debug("PERIODIC ENTRY : MONTHLY -> unlimited, interval = " + 3 + ", date = 12/07/2009"); + Entry entry2 = createEntry(instance, "SECOND", DateUtils.createDate(12,9,2008), 152.14, categ, 5); // PERIODIC interval = 5 + /*Date date2 = DateUtils.createDate(12,9,2008); + int interval = 5; + log.debug("PERIODIC ENTRY : MONTHLY -> unlimited, interval = " + interval + ", date = " + date2); Entry entry2 = instance.getNewEntry(); entry2.setLibelle("SECOND"); entry2.setAmount(152.14); - entry2.setEntryDate(DateUtils.createDate(12,7,2009)); + entry2.setEntryDate(date2); entry2.setCategory(categ); - entry2.setPeriodicity(Periodicity.MONTHLY.toString()); - entry2.setUnlimited(true); - entry2.setInterval(3); - try { instance.createUpdateEntry(entry2); assertNotNull(entry2.getId()); } catch (CashException eee) { log.error("Erreur createUpdateEntry : " + eee.getMessage()); fail("CashException"); - } + }*/ + Date from = DateUtils.createDate(12, 8, 2008); + Date thru = DateUtils.createDate(15, 10, 2009); + PeriodDates period = new PeriodDates(from, thru); + period.setPattern(DateUtils.MONTH_PATTERN); + + log.debug("GET ENTRIES BETWEEN : " + period); + try { - Entry result = instance.getEntry(entry2.getId()); - assertNotNull(result); - assertNotNull(result.getMonths()); - assertEquals(result.getMonths().size(), 4); + Map<String, List<Entry>> results = instance.getEntriesBetweenDates(period); + assertNotNull(results); + assertEquals(results.size(), 4); + List<Entry> list = results.get("02/2009"); + assertNotNull(list); + assertEquals(list.size(), 1); } catch (CashException eee) { - log.error("Erreur getEntry : " + eee.getMessage()); + log.error("Erreur getEntriesBetweenDates : " + eee.getMessage()); fail("CashException"); } @@ -189,7 +198,7 @@ fail("CashException"); } - }*/ + } @Test public void testGetSumBeforeDate() throws Exception { @@ -200,31 +209,33 @@ Category categ = ServiceCategoryImplTest.addParentCategory(serviceCateg, "CATEG", 1, true); Category categ2 = ServiceCategoryImplTest.addParentCategory(serviceCateg, "CATEG2", 2, false); - Entry entry1 = createEntry(instance, "FIRST", DateUtils.createDate(5, 9, 2009), 15.00, categ); + Entry entry1 = createEntry(instance, "FIRST", DateUtils.createDate(5, 9, 2009), 15.00, categ, 1); - Entry entry2 = createEntry(instance, "SECOND", DateUtils.createDate(5, 12, 2009), 78.24, categ); + Entry entry2 = createEntry(instance, "SECOND", DateUtils.createDate(5, 12, 2009), -78.24, categ2, 0); - Entry entry3 = createEntry(instance, "THIRD", DateUtils.createDate(15, 12, 2009), 258.44, categ); + Entry entry3 = createEntry(instance, "THIRD", DateUtils.createDate(15, 12, 2009), 258.44, categ, 0); - Entry entry4 = createEntry(instance, "FOURTH", DateUtils.createDate(2, 1, 2010), -68.12, categ2); + Entry entry4 = createEntry(instance, "FOURTH", DateUtils.createDate(2, 1, 2010), 68.12, categ, 0); Date date1 = DateUtils.createDate(31,12,2009); try { double total1 = instance.getSumBeforeDate(date1); + log.debug("CALCUL : 15 * 4 - 78.24 + 258.44"); log.debug("TOTAL1 : " + total1); - assertEquals(total1, 351.68, 0.01); + assertEquals(total1, 240.2, 0.01); } catch (CashException eee) { log.error("Erreur getSumBeforeDate : " + eee.getMessage()); fail("CashException"); } - Date date2 = DateUtils.createDate(8,12,2009); + Date date2 = DateUtils.createDate(8,11,2009); try { double total2 = instance.getSumBeforeDate(date2); + log.debug("CALCUL : 15 * 3"); log.debug("TOTAL2 : " + total2); - assertEquals(total2, 93.24, 0.01); + assertEquals(total2, 45, 0.01); } catch (CashException eee) { log.error("Erreur getSumBeforeDate : " + eee.getMessage()); fail("CashException"); @@ -234,8 +245,9 @@ try { double total3 = instance.getSumBeforeDate(date3); + log.debug("CALCUL : 15 * 5 - 78.24 + 258.44 + 68.12"); log.debug("TOTAL3 : " + total3); - assertEquals(total3, 283.56, 0.01); + assertEquals(total3, 323.32, 0.01); } catch (CashException eee) { log.error("Erreur getSumBeforeDate : " + eee.getMessage()); fail("CashException"); @@ -264,14 +276,30 @@ } - private Entry createEntry(ServiceEntry service, String libelle, Date entryDate, double amount, Category categ) { - log.debug("CREATE ENTRY : " + libelle + " _ " + entryDate + " _ " + amount); + /** + * Helper to create Entry for test. If interval > 0, entry is MONTHLY periodic unlimited. + * @param service ServiceEntry instance to use to create the entry + * @param libelle For the entry name + * @param entryDate begin date + * @param amount amount + * @param categ category (must be created before) + * @param interval for periodicity (MONTHLY _ UNLIMITED) + * @return the new Entry created + */ + private Entry createEntry(ServiceEntry service, String libelle, Date entryDate, double amount, Category categ, int interval) { + log.debug("CREATE ENTRY : " + libelle + " _ " + entryDate + " _ " + amount + " _ periodic : " + (interval > 0) + "(" + interval +")"); Entry entry = service.getNewEntry(); entry.setLibelle(libelle); entry.setAmount(amount); entry.setEntryDate(entryDate); entry.setCategory(categ); + if (interval > 0) { + entry.setPeriodicity(Periodicity.MONTHLY.toString()); + entry.setUnlimited(true); + entry.setInterval(interval); + } + try { service.createUpdateEntry(entry); assertNotNull(entry.getId());
participants (1)
-
fdesbois@users.chorem.org