0

I have a module where I have to update attendance of each student. The code is as below:

public void updateDailyAttendance(ActionRequest areq, ActionResponse aRes) throws Exception{ 

int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();
String attendanceValue = getAttendanceValue(areq);
//long attPKey = CounterLocalServiceUtil.increment(Employee.class.getName());
for (int i = 0; i < totalEmployees; i++) {
// use attendanceValue to update employee entry
//String attendanceValue = getAttendanceValue(areq);
// parameterValue is value of radio button parameter


long attPKey = CounterLocalServiceUtil.increment(Employee.class.getName());

Attendance newAttendanceInstance = new AttendanceImpl();
newAttendanceInstance.setAttId(attPKey);
                            newAttendanceInstance.setAttStatus(attendanceValue);
                        AttendanceLocalServiceUtil.addAttendance(newAttendanceInstance);

} }

 private String getAttendanceValue(ActionRequest areq) {
 Enumeration parameters = areq.getParameterNames();

while (parameters.hasMoreElements()) {
   String parameterName = parameters.nextElement().toString();
if (parameterName.startsWith("updateattendance")) {
    return areq.getParameter(parameterName);
 }
 }
throw new IllegalStateException("Parameter updateattendance cannot be found!!");
}

When I use the above code my database gets updated but the attendance (present/absent entered for first employee is taken as the value for other employees even though I mark different values(Present/absent) for other employees

How should I modify the above code so that the radio button value entred for each employee is stored ?

Seeya K
  • 1,221
  • 6
  • 27
  • 43
  • this is a duplicate of http://stackoverflow.com/questions/15876090/passing-values-to-action-class-from-radio-button. – yannicuLar Apr 09 '13 at 11:22
  • I also suggest you accept an answer or at least upvote some. people are very helpful with you, and their answers are correct – yannicuLar Apr 09 '13 at 11:23

3 Answers3

1

You get the attendanceValue only once before the loop. getAttendanceValue returns only one string, so how can the attendanceValue can be different between two students?

String attendanceValue = getAttendanceValue(areq);

That's why all attendanceValues are the same. What you need to do is updating the value for every employee inside the for loop.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
  • Thanks for the reply. I had tried putting the above statement you mentioned inside the for loop but I still get the same results... – Seeya K Apr 09 '13 at 06:12
  • You need to change the code for `getAttendanceValue` also. You can either return the relevant string, or return an array and iterate over it inside the `for` loop. – BobTheBuilder Apr 09 '13 at 06:14
  • How should I change my above code? I mean the code for getAttendance Value.. Can you give me a hint? – Seeya K Apr 09 '13 at 06:14
  • If you'll debug it and check what getAttendanceValue returns, you may be able to answer this for yourself. You need to think before you debug it what do you **expect** it to return, and compare it to the result – BobTheBuilder Apr 09 '13 at 06:16
  • I'll give you another hint. `getAttendanceValue` returns the first parameter which is of value updateattendance. You dont need only the first one. – BobTheBuilder Apr 09 '13 at 06:18
  • Yeah I know I am trying to put a for loop inside getAttendanceValue and written an array. will that help? – Seeya K Apr 09 '13 at 06:22
  • Yes. Its a good solution if you are sure about the order. If not, you may want to use Map of some identifier -> attendanceValue – BobTheBuilder Apr 09 '13 at 06:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27820/discussion-between-cya-k-and-baraky) – Seeya K Apr 09 '13 at 06:29
1

It is because you are fetching only one value for attendence. In this line "String attendanceValue = getAttendanceValue(areq);" you get the attendnce for first employee. But in getAttendanceValue method

while (parameters.hasMoreElements()) {
   String parameterName = parameters.nextElement().toString();
if (parameterName.startsWith("updateattendance")) {
    return areq.getParameter(parameterName);
 }
 }

This code just check if its starts with updateattendance which is always satisfied for first record and that value is returned. You can Store record for different employees by checking the suffix you used for making "updateattendance" unique if it is some number then you can store it in the ArrayList with Index as the number after updateattendance. And instead of returning the Strig you can return that ArrayList

  • @Cya...can you tell the unique names of your updateattendance parameter and any other parameter of employee which is on the page like employee Number.. – Kishor Raskar Apr 09 '13 at 06:25
  • Thanks for the reply. Here is the snippet code from jsp page where I accept the user input. The unique value is updateattendance+employeeId for each employee. How to pass it with Arraylist?? – Seeya K Apr 10 '13 at 04:02
  • I have added another answer for this question.It is better for your code point of view. – Kishor Raskar Apr 10 '13 at 06:15
1

Here is the better way instead use Map with EmpId as Key and updateattendanc as value.

Map<String, String> mapEmpAttendance = new HashMap<String, String>(); 
while (parameters.hasMoreElements()) {
    String parameterName = parameters.nextElement().toString();
    String value = "";
    if (parameterName.startsWith("updateattendance")) {
            value = areq.getParameter(parameterName);
            map.put(parameterName.substring(16, parameterName.length), value)
    }
 } 
return mapEmpAttendance;