Menu

Listbox field and @DefaultValueCalculator

ch.
2021-05-25
2025-10-31
  • ch.

    ch. - 2021-05-25

    Hello,

    I have a problem my entity contains 2 fields (year and number) with @DefaultValueCalculator but the 2nd field (number) is not calculated.

    I have to select a value in the listbox "year" so that the value of the field "number" is calculated

    Do you have a solution for this?

    Thanks

    below the code

    
    

    package com.yourcompany.cgpa.model;

    import javax.persistence.*;

    import org.openxava.annotations.*;

    import com.yourcompany.cgpa.calculators.*;

    import lombok.*;

    @Entity @Getter @Setter

    public class Livres {

    @Id @Hidden
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(length=10)
    private Integer oid;
    
    // Année Licence en cours , on affiche l'année courante par défaut  
        @ManyToOne( fetch=FetchType.LAZY) 
        @DescriptionsList (descriptionProperties="libelle") @NoCreate @NoModify
        @DefaultValueCalculator(CurrentYearFdCalculator.class) 
        PrmYears annee;
    
        @Column(length=6)
        @ReadOnly
        @DefaultValueCalculator(value=NextNumberActForYearCalculator.class,
                    properties=@PropertyValue(name="ann",from="annee.oid") 
        )
        int number;
    
        @Column(length=80,unique=true,nullable=false)
        String description;
    

    }

    
    
    package com.yourcompany.cgpa.model;
    
    import java.time.*;
    
    import javax.persistence.*;
    
    import lombok.*;
    
    @Entity
    @Getter @Setter
    
    public class PrmYears extends OidClass {
    
        @Column(length=25, unique=true,nullable=false) 
        String libelle;
    
        LocalDate startDate;
    
        LocalDate endDate;
    
    }
    
    INSERT INTO `PrmYears` (`oid`, `endDate`, `libelle`, `startDate`) VALUES
    (1, '2019-08-30', '2018-2019', '2018-08-31'),
    (2, '2020-08-30', '2019-2020', '2019-08-31'),
    (3, '2021-08-30', '2020-2021', '2020-08-31');
    

    package com.yourcompany.cgpa.calculators;

    import org.openxava.calculators.;
    import org.openxava.jpa.
    ;

    import lombok.*;

    @Getter @Setter // To be publicly accessible

    public class CurrentYearFdCalculator implements ICalculator{

    public Object calculate() throws Exception { // It does the calculation
    
        return XPersistence.getManager()
                .createQuery("from PrmYears p where sysdate() BETWEEN p.startDate AND p.endDate")
                .getResultList().get(0);
    
    }
    

    }

    package com.yourcompany.cgpa.calculators;
    import javax.persistence.*;
    
    import org.openxava.calculators.*;
    import org.openxava.jpa.*;
    
    import lombok.*;
    
    @Getter @Setter // To be publicly accessible
    
    public class NextNumberActForYearCalculator implements ICalculator{
    
    int ann; // This value will be injected before calculating
    
    public Object calculate() throws Exception { // It does the calculation
    Query query = XPersistence.getManager() // A JPA query
    .createNativeQuery("select max(i.number) from Livres i" + " where i.annee_oid = :iDoid"); // The query returns
    // the max invoice number of the indicated year
    query.setParameter("iDoid", ann); // We use the injected year as a parameter for the query
    Integer lastNumber = (Integer) query.getSingleResult();
    return lastNumber == null ? 1 : lastNumber + 1; // Returns the last invoice number
    // of the year + 1 or 1 if there is no last number
    }
    
    }
    
     
  • Javier Paniza

    Javier Paniza - 2021-05-27

    Hi Ch,

    You're right. When you change annee number is recalculated, but not when you click on New. I think number should be recalculated also when the anno calculates its default value, just as you expect. So, add it as a bug with a link to this thread, and we'll fix this issue for future release.

    In the meantime, you can solve the issue with your new action that assign the value to anno just to fire the event. For that, add a controller to your controllers.xml like this one:

    <controller name="Livres">
    <extends controller="Typical">
    <action name="new" icon="library-plus" keystroke="Control N" class="">
    </action>
    </extends></controller>

    The write the next code for your new action:

    package com.yourcompany.cgpa.actions;
    
    import org.openxava.actions.*;
    
    public class NewLivresAction extends NewAction {
    
        @Override
        public void execute() throws Exception {
            super.execute();
            Object annee = getView().getValue("annee.oid");
            getView().setValueNotifying("annee.oid", annee);
        }
    
    }
    

    Help others in this forum as I help you.

     
  • Javier Paniza

    Javier Paniza - 2025-10-31

    Hi Ch,

    This bug is already fixed and the fix will be included in 7.6.2, available in November 2025.

    BTW, the bug only arose when the calculator returned the entity, so you can fix it without upgrading just in this way:

    PrmYears prm = (PrmYears) XPersistence.getManager()
            .createQuery("from PrmYears p where sysdate() BETWEEN p.startDate AND p.endDate")
            .getResultList().get(0);
     return prm.getOid(); // Instead of prm
    

    With 7.6.2 it work with both entity and oid, but with olders version you have to use the oid to avoid the bug.


    Help others in this forum as I help you.

     

Log in to post a comment.

MongoDB Logo MongoDB