// El número de factura para una factura nueva private String getCodigo() { // Usamos inicialización vaga if (codigo == null) { // Una consulta JPA para obtener el último número Query query = getManager().createQuery("select max(f.codigo) from Factura f where f.ano = :ano"); // Dates es una utilidad de OpenXava query.setParameter("ano", Dates.getYear(new Date())); Integer lastNumber = (Integer) query.getSingleResult(); if (lastNumber == null) lastNumber = 0; // Añadimos 1 al último número de factura codigo = Integer.toString(lastNumber + 1); } return codigo; }
public void testConverterWithMetaSets() throws Exception { // Creating new execute("CRUD.new"); setValue("invoice.year", "2002"); setValue("invoice.number", "1"); setValue("type.number", "1"); setValue("number", "66"); setValue("description", "JUNIT"); setValue("distance", usesAnnotatedPOJO() ? "1" : "2"); // National, in database 'N' execute("CRUD.save"); assertNoErrors(); assertValue("invoice.year", ""); assertValue("invoice.number", ""); assertValue("type.number", ""); assertValue("number", ""); assertValue("description", ""); assertValue("distance", usesAnnotatedPOJO() ? "" : "0"); // Search just created setValue("invoice.year", "2002"); setValue("invoice.number", "1"); setValue("type.number", "1"); setValue("number", "66"); execute("CRUD.refresh"); assertNoErrors(); assertValue("invoice.year", "2002"); assertValue("invoice.number", "1"); assertValue("type.number", "1"); assertValue("number", "66"); assertValue("description", "JUNIT"); assertValue("distance", usesAnnotatedPOJO() ? "1" : "2"); assertNoErrors(); // Verifying database value Query query = XPersistence.getManager() .createNativeQuery( "select d.distance from XAVATEST.Delivery as d where " + "invoice_year=2002 and invoice_number=1 and type=1 and number=66"); String distanceDB = (String) query.getSingleResult(); assertEquals("distance in database incorrect", "N", distanceDB); // Delete execute("CRUD.delete"); assertNoErrors(); assertMessage("Delivery deleted successfully"); }