1

In my xhtml there are 3 input field which calculate remaining day of two <p:calendar> dates. In next step I want to store calculated remaining day to MY DB.

<p:dataTable styleClass="vtable" editable="true" var="user"
editMode="cell" value="#{userBean.employeeList}">
<p:column styleClass="columntd" headerText="#{text['user.startedDate']}">
    <p:calendar widgetVar="fromCal" value="#{vacationBean.vacation.beginDate}">
 <p:ajax event="dateSelect" listener="#{dayDiffBean.fromSelected}"
        update="diff" />
  </p:calendar>
</p:column>
<p:column styleClass="columntd"
headerText="#{text['user.finishedDate']}">
 <p:calendar widgetVar="toCal" value="#{vacationBean.vacation.endDate}">
    <p:ajax event="dateSelect" listener="#{dayDiffBean.toSelected}"
        update="diff" />
  </p:calendar>
  </p:column>
  <p:column styleClass="columntd"
headerText="#{text['employee.remainingdays']}">

<p:inputText id="diff" styleClass="daysNumber"
    value="#{dayDiffBean.diff}" />
        </p:column>
</p:dataTable>
   <h:commandButton styleClass="sndbutton1"
value="#{text['employee.send']}" action="#{vacationBean.addVac}"/>

I used value="#{dayDiffBean.diff} to get remaining day and now I also want to use my vacationbean to store remaingday to my db using like this : value="#{vacationBean.vacation.balanceDay}"

But I cant use 2 value in inputtext field like this:

<p:inputText value="dayDiffBean.diff" value1="vacationBean.vacation.balanceDay">

How can i solve this problem?

This is my vacation bean code:

@ManagedBean(name="vacationBean")
@ViewScoped
public class VacationBean implements Serializable{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Date vEndDate;
    private boolean selected;
    private Date vStartDate;
    private Date createdDate;
    private String isNobody;
    Requestrelax vacation;
    Employee e;
    Calendar javaCalendar = null;
    private short balanceDay;
    @EJB
    VacationLocal vacations;
    @ManagedProperty(value="#{loginBean.userId}")
    Integer userId;

    @EJB
    EmployeesLocal employees;
    @PostConstruct
    public void init(){
        System.out.println("0");
        //System.out.println("STATrtsg >> . "+ diff.getDiff());
        vacation=new Requestrelax();
        e=employees.getEmployee(userId);
        vacation.setEmployee(e);
        System.out.println("balanday is:"+balanceDay);
    }
    public void addVac(){
        System.out.println("1");
        javaCalendar = Calendar.getInstance();
        Date currenDate=Calendar.getInstance().getTime();
        vacation.setCreatedDate(currenDate);
        vacation.setBalanceDay(balanceDay);
        vacations.addEmployeeVacation(vacation);
    }
    public Integer getUserId() {
        return userId;
    }
    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    public Employee getE() {
        return e;
    }
    public void setE(Employee e) {
        this.e = e;
    }
    public Requestrelax getVacation() {
        return vacation;
    }
    public void setVacation(Requestrelax vacation) {
        this.vacation = vacation;
    }
    public Date getvEndDate() {
        return vEndDate;
    }
    public void setvEndDate(Date vEndDate) {
        this.vEndDate = vEndDate;
    }
    public Date getvStartDate() {
        return vStartDate;
    }
    public void setvStartDate(Date vStartDate) {
        this.vStartDate = vStartDate;
    }
    public short getBalanceDay() {
        return balanceDay;
    }
    public void setBalanceDay(short balanceDay) {
        this.balanceDay = balanceDay;
    }
    public Date getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getIsNobody() {
        return isNobody;
    }

    public void setIsNobody(String isNobody) {
        this.isNobody = isNobody;
    }

}

And daydiffbean code :

      @ManagedBean(name="dayDiffBean")

    @SessionScoped
     public class DayDiffBean implements Serializable {

/**
 * 
 */



private static final long serialVersionUID = 1L;
private Date from;

private Date to;
private String diff="";
private final long oneDay=1000*60*60*24;



public void fromSelected(SelectEvent event){
    from=(Date) event.getObject();
    calDiff();

}

public void toSelected(SelectEvent event){
    to=(Date) event.getObject();
    calDiff();

}


public void calDiff(){
    if(from==null||to==null){
        diff="N/A";
        return;
    }
    diff=(to.getTime()-from.getTime())/oneDay+"";
} 
public String getDiff() {
    return diff;
}
public void setDiff(String diff) {
    this.diff = diff;
}
public void setFrom(Date from) {
    this.from = from;
}
public Date getFrom() {
    return from;
}
public Date getTo() {
    return to;
}
public void setTo(Date to) {
    this.to = to;
}



}
4
  • kindly post the code here of ur bean classes Commented Jul 2, 2014 at 4:03
  • ok iposted my bean classes anas Commented Jul 2, 2014 at 4:16
  • have you tried using inheritance? if not then extend daydiffBean from vacationBean then in your callDiff function use the object of vacationBean and set its vacation.balanceDay attribute tothe difference you want . i hope you understand Commented Jul 2, 2014 at 5:11
  • can u give me some examples?im kinda confusing Commented Jul 2, 2014 at 5:16

1 Answer 1

1

From the code, one way to add balanceDay to your vacationBean is by passing the diff string as a parameter to addVac() method (notice action in the second line):

<h:commandButton styleClass="sndbutton1" value="#{text['employee.send']}" 
action="#{vacationBean.addVac(dayDiffBean.diff)}"/>

Then, for your VacationBean.addVac():

// 'diff' is now being passed in as a parameter
public void addVac(String diff) {
    System.out.println("1");
    javaCalendar = Calendar.getInstance();
    Date currenDate=Calendar.getInstance().getTime();
    vacation.setCreatedDate(currenDate);
    vacation.setBalanceDay(balanceDay);

    // UPDATED
    // so now you can set balanceDay
    setBalanceDay(Short.parseShort(diff));

    vacations.addEmployeeVacation(vacation);
}
Sign up to request clarification or add additional context in comments.

7 Comments

I got 0 value in my DB
@user3724426 So you need to set diff to vacationBean.balanceDay? In the question, you said you want vacationBean.vacation.balanceDay so I coded it that way. See updated addVac().
Still i got 0 value do i need to change <p:inputText value="#{dayDiffBean.diff}" ></p:inputText> to alue="#{vacationBean.vacation.balanceDay}" ???
@user3724426 You only need to change h:commandButton to use #{vacationBean.addVac(dayDiffBean.diff)}. There's no need to change p:inputText. Can you try System.out.println(diff); inside addVac()? What do you get from that?
Hey @b0nyb0y its printing remaining day So how can i now put it to in db
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.