Exemple #1
0
 // 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");
  }
  public void testDeleteSelectedOnesAndOrderBy() throws Exception {
    // Creating new
    execute("CRUD.new");
    setValue("invoice.year", "2009");
    setValue("invoice.number", "1");
    setValue("type.number", "1");
    setValue("number", "1");
    setValue("description", "JUNIT");
    execute("CRUD.save");
    assertNoErrors();

    // To list mode and order
    execute("Mode.list");
    assertActions(listActions);
    execute("List.orderBy", "property=invoice.year"); // ascending
    execute("List.orderBy", "property=invoice.year"); // descending
    assertNoErrors();

    // Delete
    assertValueInList(0, "invoice.year", "2009");
    assertValueInList(0, "invoice.number", "1");
    assertValueInList(0, "type.number", "1");
    assertValueInList(0, "number", "1");

    checkRow(0);

    execute("CRUD.deleteSelected");
    assertNoErrors();
    assertRowUnchecked(0);

    // Verifying that it is deleted
    Query query =
        XPersistence.getManager()
            .createQuery(
                "from Delivery d where "
                    + "d.invoice.year=2009 and d.invoice.number=1 and d.type.number=1 and d.number=1");
    if (!query.getResultList().isEmpty()) {
      fail("Delivery would be deleted and it is not the case");
    }
  }