r3550 - in trunk: lima-business/src/main/java/org/chorem/lima/business/ejb lima-business/src/main/java/org/chorem/lima/business/utils lima-business/src/test/java/org/chorem/lima/business lima-business/src/test/resources lima-callao/src/main/java/org/chorem/lima/entity
Author: mallon Date: 2012-07-24 16:25:42 +0200 (Tue, 24 Jul 2012) New Revision: 3550 Url: http://chorem.org/repositories/revision/lima/3550 Log: fixes #709 Correction de l incrementation du lettrage, permettant de depasser les 17000 possibilites; realisation d une classe de test pour le comparator ajoute, ainsi que pour la classe de service permettant d obtenir la lettre suivante; remplacement de la liste, contenant les resultats de la requete, par un hashset. Added: trunk/lima-business/src/main/java/org/chorem/lima/business/utils/LetteringComparator.java trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/LetteringComparatorTest.java trunk/lima-business/src/test/java/org/chorem/lima/business/utils/ Removed: trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java trunk/lima-business/src/test/resources/log4j.properties trunk/lima-callao/src/main/java/org/chorem/lima/entity/EntryDAOImpl.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java 2012-07-23 15:42:42 UTC (rev 3549) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java 2012-07-24 14:25:42 UTC (rev 3550) @@ -35,6 +35,7 @@ import org.chorem.lima.business.api.FinancialTransactionService; import org.chorem.lima.business.api.OptionsService; import org.chorem.lima.business.api.ReportService; +import org.chorem.lima.business.utils.LetteringComparator; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.AccountDAO; import org.chorem.lima.entity.Entry; @@ -52,6 +53,7 @@ import javax.ejb.TransactionAttribute; import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -124,39 +126,39 @@ @Override public String getNextLetters() throws LimaException { String lastActualLetters; - try { EntryDAO entryDAO = getDaoHelper().getEntryDAO(); - lastActualLetters = entryDAO.findLetters(); + + List<String> letters = new ArrayList<String>(entryDAO.findLetters()); + lastActualLetters = findLastLetter(letters); + } catch (Exception ex) { throw new LimaException("Can't get new letters", ex); } - String nextThirdLetter ="A"; - String nextSecondLetter = "A"; - String nextFirstLetter = "A"; + /**Increment letter alphabetically*/ + if (lastActualLetters.charAt(0) != 'Z') { + String finLastActualLetters = lastActualLetters.substring(1, lastActualLetters.length()); + return String.valueOf( (char) (lastActualLetters.charAt(0) + 1)) + finLastActualLetters; + } else { + return "A" + lastActualLetters; + } + } - //TODO:permettre de lettrer avec plus de trois lettres + public String findLastLetter(List<String> letters) { - if (lastActualLetters != null && !lastActualLetters.isEmpty()){ - if (lastActualLetters.length() > 2 && lastActualLetters.charAt(2) != 'Z'){ - nextThirdLetter = incrementLetter(2, lastActualLetters); - }else if (lastActualLetters.length() > 1 && lastActualLetters.charAt(1) != 'Z'){ - nextSecondLetter = incrementLetter(1, lastActualLetters); - }else if (lastActualLetters.charAt(0) != 'Z'){ - nextFirstLetter = incrementLetter(0, lastActualLetters); - } - } + String result; - String nextLetters = String.valueOf(nextFirstLetter + nextSecondLetter + nextThirdLetter); + Collections.sort(letters, new LetteringComparator()); + Collections.reverse(letters); - return nextLetters; - } + if (letters.isEmpty()) { + result = ""; - /**Increment letter alphabetically*/ - protected String incrementLetter(int indiceLetter, String lastActualLetters){ - int valueLetter = lastActualLetters.charAt(indiceLetter); - return String.valueOf( (char) (valueLetter + 1)); + } else { + result = letters.get(0); + } + return result; } /** Added: trunk/lima-business/src/main/java/org/chorem/lima/business/utils/LetteringComparator.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/utils/LetteringComparator.java (rev 0) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/utils/LetteringComparator.java 2012-07-24 14:25:42 UTC (rev 3550) @@ -0,0 +1,34 @@ +package org.chorem.lima.business.utils; + +import java.io.Serializable; +import java.util.Comparator; + +/** + * Created with IntelliJ IDEA. + * User: mallon + * Date: 24/07/12 + * Time: 15:39 + * To change this template use File | Settings | File Templates. + */ +public class LetteringComparator implements Serializable , Comparator<String> { + private static final long serialVersionUID = 1L; + + @Override + public int compare(String firstString, String secondString) { + + int lengthFirstString = firstString.length(); + int lengthSecondString = secondString.length(); + + if (lengthFirstString < lengthSecondString) { + return -1; + } else if (lengthFirstString > lengthSecondString) { + return 1; + } else { + if (firstString.charAt(0) < secondString.charAt(0)) { + return -1; + } else { + return 1; + } + } + } +} Deleted: trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java 2012-07-23 15:42:42 UTC (rev 3549) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java 2012-07-24 14:25:42 UTC (rev 3550) @@ -1,145 +0,0 @@ -/* - * #%L - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 Codelutin, Chatellier Eric - * %% - * 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 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>. - * #L% - */ - -package org.chorem.lima.business; - -import org.chorem.lima.entity.Account; -import org.chorem.lima.entity.Entry; -import org.chorem.lima.entity.EntryBook; -import org.chorem.lima.entity.EntryImpl; -import org.chorem.lima.entity.FinancialTransaction; -import org.chorem.lima.entity.FinancialTransactionImpl; -import org.chorem.lima.entity.FiscalPeriod; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.math.BigDecimal; -import java.text.ParseException; -import java.util.List; - -/** - * Test on financial transaction service. - * - * @author chatellier - * @version $Revision$ - * - * Last update : $Date$ - * By : $Author$ - */ -public class FinancialTransactionServiceImplTest extends AbstractLimaTest { - - @Before - public void initTest() throws LimaException, ParseException { - initTestDatabase(); - } - - /** - * Test to find all unbalanced transactions. - * Nothing wrong here. - * - * @throws ParseException - * @throws LimaException - */ - @Test - public void testGetInexactTransactionAllGood() throws ParseException, LimaException { - FiscalPeriod fiscalPeriod = fiscalPeriodService.getAllFiscalPeriods().get(0); - List<FinancialTransaction> transactions = financialTransactionService.getAllInexactFinancialTransactions(fiscalPeriod); - Assert.assertTrue(transactions.isEmpty()); - } - - /** - * Test to find all unbalanced transactions. - * - * wrong data. - * - * @throws ParseException - * @throws LimaException - */ - @Test - public void testGetInexactTransactionNotAllGood() throws ParseException, LimaException { - - EntryBook journalDesVentes = entryBookService.getEntryBookByCode("jdv"); - Account accountVmpVae = accountService.getAccountByNumber("511"); - - FinancialTransaction transaction1 = new FinancialTransactionImpl(); - transaction1.setTransactionDate(df.parse("April 5, 2012")); - transaction1.setEntryBook(journalDesVentes); - transaction1 = financialTransactionService.createFinancialTransaction(transaction1); - - Entry tr1Entry1 = new EntryImpl(); - tr1Entry1.setAmount(BigDecimal.valueOf(42.0)); - tr1Entry1.setAccount(accountVmpVae); - tr1Entry1.setFinancialTransaction(transaction1); - //tr1Entry1.setDescription("test desc"); - tr1Entry1.setVoucher("voucher"); - tr1Entry1 = financialTransactionService.createEntry(tr1Entry1); - - Entry tr1Entry2 = new EntryImpl(); - tr1Entry2.setAmount(BigDecimal.valueOf(42.0)); - tr1Entry2.setDebit(true); - tr1Entry2.setAccount(accountVmpVae); - tr1Entry2.setFinancialTransaction(transaction1); - tr1Entry2.setDescription("test desc"); - tr1Entry2.setVoucher("voucher"); - tr1Entry2 = financialTransactionService.createEntry(tr1Entry2); - - // one in period - FiscalPeriod fiscalPeriod = fiscalPeriodService.getAllFiscalPeriods().get(0); - List<FinancialTransaction> transactions = financialTransactionService.getAllInexactFinancialTransactions(fiscalPeriod); - Assert.assertEquals(1, transactions.size()); - } - - /** - * Test to find all unbalanced transactions. - * - * Test only unbalanced transactions (no data errors : fields). - * - * @throws ParseException - * @throws LimaException - */ - @Test - public void testGetUnbalancedTransactionNotAllGood() throws ParseException, LimaException { - - EntryBook journalDesVentes = entryBookService.getEntryBookByCode("jdv"); - Account accountVmpVae = accountService.getAccountByNumber("511"); - - FinancialTransaction transaction1 = new FinancialTransactionImpl(); - transaction1.setTransactionDate(df.parse("April 5, 2012")); - transaction1.setEntryBook(journalDesVentes); - transaction1 = financialTransactionService.createFinancialTransaction(transaction1); - - Entry tr1Entry1 = new EntryImpl(); - tr1Entry1.setAmount(BigDecimal.valueOf(54.0)); - tr1Entry1.setAccount(accountVmpVae); - tr1Entry1.setFinancialTransaction(transaction1); - tr1Entry1.setDescription("test desc"); - tr1Entry1.setVoucher("voucher"); - tr1Entry1 = financialTransactionService.createEntry(tr1Entry1); - - // one in period - FiscalPeriod fiscalPeriod = fiscalPeriodService.getAllFiscalPeriods().get(0); - List<FinancialTransaction> transactions = financialTransactionService.getAllInexactFinancialTransactions(fiscalPeriod); - Assert.assertEquals(1, transactions.size()); - } -} Added: trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java (rev 0) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/FinancialTransactionServiceImplTest.java 2012-07-24 14:25:42 UTC (rev 3550) @@ -0,0 +1,165 @@ +/* + * #%L + * $Id: FinancialTransactionServiceImplTest.java 3408 2012-05-09 15:37:59Z echatellier $ + * $HeadURL: http://svn.chorem.org/svn/lima/trunk/lima-business/src/test/java/org/chorem/... $ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * 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 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>. + * #L% + */ + +package org.chorem.lima.business; + +import org.chorem.lima.business.ejb.FinancialTransactionServiceImpl; +import org.chorem.lima.entity.Account; +import org.chorem.lima.entity.Entry; +import org.chorem.lima.entity.EntryBook; +import org.chorem.lima.entity.EntryImpl; +import org.chorem.lima.entity.FinancialTransaction; +import org.chorem.lima.entity.FinancialTransactionImpl; +import org.chorem.lima.entity.FiscalPeriod; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.math.BigDecimal; +import java.text.ParseException; +import java.util.Arrays; +import java.util.List; + +/** + * Test on financial transaction service. + * + * @author chatellier + * @version $Revision: 3408 $ + * + * Last update : $Date: 2012-05-09 17:37:59 +0200 (mer. 09 mai 2012) $ + * By : $Author: echatellier $ + */ +public class FinancialTransactionServiceImplTest extends AbstractLimaTest { + + @Before + public void initTest() throws LimaException, ParseException { + initTestDatabase(); + } + + /** + * Test to find all unbalanced transactions. + * Nothing wrong here. + * + * @throws ParseException + * @throws LimaException + */ + @Test + public void testGetInexactTransactionAllGood() throws ParseException, LimaException { + FiscalPeriod fiscalPeriod = fiscalPeriodService.getAllFiscalPeriods().get(0); + List<FinancialTransaction> transactions = financialTransactionService.getAllInexactFinancialTransactions(fiscalPeriod); + Assert.assertTrue(transactions.isEmpty()); + } + + /** + * Test to find all unbalanced transactions. + * + * wrong data. + * + * @throws ParseException + * @throws LimaException + */ + @Test + public void testGetInexactTransactionNotAllGood() throws ParseException, LimaException { + + EntryBook journalDesVentes = entryBookService.getEntryBookByCode("jdv"); + Account accountVmpVae = accountService.getAccountByNumber("511"); + + FinancialTransaction transaction1 = new FinancialTransactionImpl(); + transaction1.setTransactionDate(df.parse("April 5, 2012")); + transaction1.setEntryBook(journalDesVentes); + transaction1 = financialTransactionService.createFinancialTransaction(transaction1); + + Entry tr1Entry1 = new EntryImpl(); + tr1Entry1.setAmount(BigDecimal.valueOf(42.0)); + tr1Entry1.setAccount(accountVmpVae); + tr1Entry1.setFinancialTransaction(transaction1); + //tr1Entry1.setDescription("test desc"); + tr1Entry1.setVoucher("voucher"); + tr1Entry1 = financialTransactionService.createEntry(tr1Entry1); + + Entry tr1Entry2 = new EntryImpl(); + tr1Entry2.setAmount(BigDecimal.valueOf(42.0)); + tr1Entry2.setDebit(true); + tr1Entry2.setAccount(accountVmpVae); + tr1Entry2.setFinancialTransaction(transaction1); + tr1Entry2.setDescription("test desc"); + tr1Entry2.setVoucher("voucher"); + tr1Entry2 = financialTransactionService.createEntry(tr1Entry2); + + // one in period + FiscalPeriod fiscalPeriod = fiscalPeriodService.getAllFiscalPeriods().get(0); + List<FinancialTransaction> transactions = financialTransactionService.getAllInexactFinancialTransactions(fiscalPeriod); + Assert.assertEquals(1, transactions.size()); + } + + /** + * Test to find all unbalanced transactions. + * + * Test only unbalanced transactions (no data errors : fields). + * + * @throws ParseException + * @throws LimaException + */ + @Test + public void testGetUnbalancedTransactionNotAllGood() throws ParseException, LimaException { + + EntryBook journalDesVentes = entryBookService.getEntryBookByCode("jdv"); + Account accountVmpVae = accountService.getAccountByNumber("511"); + + FinancialTransaction transaction1 = new FinancialTransactionImpl(); + transaction1.setTransactionDate(df.parse("April 5, 2012")); + transaction1.setEntryBook(journalDesVentes); + transaction1 = financialTransactionService.createFinancialTransaction(transaction1); + + Entry tr1Entry1 = new EntryImpl(); + tr1Entry1.setAmount(BigDecimal.valueOf(54.0)); + tr1Entry1.setAccount(accountVmpVae); + tr1Entry1.setFinancialTransaction(transaction1); + tr1Entry1.setDescription("test desc"); + tr1Entry1.setVoucher("voucher"); + tr1Entry1 = financialTransactionService.createEntry(tr1Entry1); + + // one in period + FiscalPeriod fiscalPeriod = fiscalPeriodService.getAllFiscalPeriods().get(0); + List<FinancialTransaction> transactions = financialTransactionService.getAllInexactFinancialTransactions(fiscalPeriod); + Assert.assertEquals(1, transactions.size()); + } + + @Test + public void testFindLastLetter() { + + List<String> letters = Arrays.asList("A", "BZ", "ZZZ", "C", "Z", "E"); + + FinancialTransactionServiceImpl instance = new FinancialTransactionServiceImpl(); + + String nextLetter = instance.findLastLetter(letters); + Assert.assertEquals("ZZZ", nextLetter); + + Assert.assertEquals("A", letters.get(5)); + Assert.assertEquals("C", letters.get(4)); + Assert.assertEquals("E", letters.get(3)); + Assert.assertEquals("Z", letters.get(2)); + Assert.assertEquals("BZ", letters.get(1)); + Assert.assertEquals("ZZZ", letters.get(0)); + } +} Added: trunk/lima-business/src/test/java/org/chorem/lima/business/LetteringComparatorTest.java =================================================================== --- trunk/lima-business/src/test/java/org/chorem/lima/business/LetteringComparatorTest.java (rev 0) +++ trunk/lima-business/src/test/java/org/chorem/lima/business/LetteringComparatorTest.java 2012-07-24 14:25:42 UTC (rev 3550) @@ -0,0 +1,35 @@ +package org.chorem.lima.business; + +import org.chorem.lima.business.utils.LetteringComparator; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * User: mallon + * Date: 24/07/12 + * Time: 15:42 + * To change this template use File | Settings | File Templates. + */ +public class LetteringComparatorTest { + + @Test + public void testCompare() { + List<String> letters = Arrays.asList("A", "BZ", "ZZZ", "C", "Z", "E"); + + Collections.sort(letters, new LetteringComparator()); + Collections.reverse(letters); + + Assert.assertEquals("A", letters.get(5)); + Assert.assertEquals("C", letters.get(4)); + Assert.assertEquals("E", letters.get(3)); + Assert.assertEquals("Z", letters.get(2)); + Assert.assertEquals("BZ", letters.get(1)); + Assert.assertEquals("ZZZ", letters.get(0)); + } + +} Modified: trunk/lima-business/src/test/resources/log4j.properties =================================================================== --- trunk/lima-business/src/test/resources/log4j.properties 2012-07-23 15:42:42 UTC (rev 3549) +++ trunk/lima-business/src/test/resources/log4j.properties 2012-07-24 14:25:42 UTC (rev 3550) @@ -31,4 +31,6 @@ log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) %M - %m%n # package level -log4j.logger.org.chorem.lima=INFO \ No newline at end of file +log4j.logger.org.chorem.lima=INFO + +log4j.logger.org.chorem.lima.business.ejb.FinancialTransactionServiceImpl=DEBUG \ No newline at end of file Modified: trunk/lima-callao/src/main/java/org/chorem/lima/entity/EntryDAOImpl.java =================================================================== --- trunk/lima-callao/src/main/java/org/chorem/lima/entity/EntryDAOImpl.java 2012-07-23 15:42:42 UTC (rev 3549) +++ trunk/lima-callao/src/main/java/org/chorem/lima/entity/EntryDAOImpl.java 2012-07-24 14:25:42 UTC (rev 3550) @@ -31,7 +31,9 @@ import org.nuiton.topia.TopiaException; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class EntryDAOImpl<E extends Entry> extends EntryDAOAbstract<Entry> { @@ -122,15 +124,12 @@ return entries; } - public String findLetters() throws TopiaException{ - String result = ""; + public Set<String> findLetters() throws TopiaException{ String query = "Select E.lettering FROM " + Entry.class.getName() + " E" + + " where E.lettering <> null" + " order by E.lettering desc"; - List<String> letters = context.findAll(query); - if (!letters.isEmpty()) { - result = letters.get(0); - } + Set<String> result = new HashSet<String>(context.findAll(query)); return result; }
participants (1)
-
mallon@users.chorem.org