0

I am having a jsp form where I have to mark attendance for each employee and store the results in the database. My jsp snippet for marking attendance is as follows:

<portlet:defineObjects />
<%
   List<Employee> EmployeeAttendanceDetails = MISPortalActionUtil.getEmployeeData();
 %>



 <portlet:renderURL  var="viewMarkAttendanceURL"/>
 <!DOCTYPE HTML>
 <html>
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>mark attendance</title>
 </head>

 <body>
Mark Attendance for Today:   
<%= new java.util.Date() %>
<portlet:actionURL name="updateDailyAttendance" var="updateDailyAttendanceURL" />


**<aui:form name="updateDailyAttendance" action="<%=updateDailyAttendanceURL.toString()%>" method="post" >
<portlet:renderURL var="viewEmployeeDataURL"/>
<liferay-ui:search-container delta="20" emptyResultsMessage="No Results Found">
<liferay-ui:search-container-results total="<%= EmployeeAttendanceDetails .size() %>"
results="<%= ListUtil.subList(EmployeeAttendanceDetails , searchContainer.getStart(),     searchContainer.getEnd()) %>" />
<liferay-ui:search-container-row modelVar="search"
 className="com.test.mis.portal.model.Employee">
<liferay-ui:search-container-column-text name='Employee Name'     value='<%=String.valueOf(search.getEmpFname()) + " " +    String.valueOf(search.getEmpLname())%>' href="" />
<liferay-ui:search-container-column-text name='Employee Id' value='<%=String.valueOf(search.getEmpId())%>' href="" />
<liferay-ui:search-container-column-text name = "Attendance Status" >
 <label>Present</label><input type = "radio" name ='updateattendance +     <%=String.valueOf(search.getEmpId())%>' value = "present" />
<label>Absent</label><input type = "radio" name= 'updateattendance +     <%=String.valueOf(search.getEmpId())%>' value = "absent"/>
</liferay-ui:search-container-column-text>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator searchContainer="<%=searchContainer %>" paginate="<%=true %>" />
</liferay-ui:search-container> 
<input type = "submit" value = "Update"/>
</aui:form>**

And I use the following functions to mark the attendance: public void updateDailyAttendance(ActionRequest areq, ActionResponse aRes) throws Exception{

int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();
String attendanceValue = getAttendanceValue(areq);
***for (int i = 0; i < totalEmployees; i++) {
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 is not found");
}

The problem that I am facing is that whatever attendance I mark for the first employee (Present/Absent) the same is stored for the other employees. The error I think is in the above for loop which I have italicized. How should I rectify this code such that for each employee the correct attendance status is stored?

Seeya K
  • 1,221
  • 6
  • 27
  • 43

1 Answers1

1

Considering your jsp is correct, the code

<input type = "radio" name ='updateattendance +     <%=String.valueOf(search.getEmpId())%>' value = "present" />

will create an array of properties named like updateattendance101, updateattendance102, updateattendance201, updateattendance301 etc

The code

if (parameterName.startsWith("updateattendance")) { return areq.getParameter(parameterName);

gets the first matching property, therefore you get the same value always. So what you need to do, is use the same array you used to feed the search-container (array 'EmployeeAttendanceDetails' ), iterate through all it's objects and use the 'getEmpId' id to exact match the property.

Secondly, I see some bad practices on the usage of the ServiceBuilder.

  1. Are you sure you want to create a new Employee, instead of updating the existing ones ?
  2. Even if you want to create/update an Employee Entry, you should make a wrapping function in AttendanceLocalServiceImpl, instead of manually editing all attributes/ increase persistence counters etc inside your client's code

Edit : you can replace your posted java code with this, I hope you can understand to do

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

    List<Employee> employeeAttendanceDetails = MISPortalActionUtil.getEmployeeData();

    for (Employee emp: employeeAttendanceDetails) {


    String name = "updateattendance" + Long.toString(emp.getEmpId());


                    String value = getAttendanceValue(areq, name);
                    // You don't really need to call call getAttendanceValue, except if you're going to handle the IllegalStateException. 
                    //If this is the case, you can just call :
                    //String value = areq.getParameter(name);



        // Do your stuff with the employee object


    }
}

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

 while (parameters.hasMoreElements()) {
     String parameterName = parameters.nextElement().toString();
     if (parameterName.equals(paramName)) {
         return areq.getParameter(parameterName);
     }
 }
 throw new IllegalStateException("Parameter updateattendance is not found");
}

Edit 2: replace

name ='updateattendance +     <%=String.valueOf(search.getEmpId())%>'

with

name ='updateattendance<%=String.valueOf(search.getEmpId())%>'
yannicuLar
  • 3,083
  • 3
  • 32
  • 50
  • Thanks for the reply. :) Well I am updating the attendance table. i will make the changes you suggested while using service builder. I am new to liferay and doing this project for my internship.. I didnt understand how should I iterate over the same EmployeeAttendanceDetails array in the action class – Seeya K Apr 09 '13 at 07:31
  • 1
    in jsp, you're using a variable named 'EmployeeAttendanceDetails'. I don't know how you're getting it (missing from your jsp code). The point is that you should use the same array on your action class – yannicuLar Apr 09 '13 at 09:44
  • I have updated my code and EmployeeAttendanceDetails is a variable. Just check my code above. – Seeya K Apr 09 '13 at 09:54
  • And how do I call in my action class EmployeeAttendanceDetails which is a variable of type list. Till no I have only been using getparameter to get the values.. – Seeya K Apr 09 '13 at 10:05
  • 1
    I've updated my answer, to help you iterate through the Employee values. Note that I've deleted on purpose the Employee update -related code, as it's another issue. – yannicuLar Apr 09 '13 at 10:22
  • I will try and modify this code you gave me according to my module needs. i will let you know the results here.. – Seeya K Apr 09 '13 at 10:36
  • I tried the code but it alwasy displays the exception message. that it cannot find the parameter – Seeya K Apr 10 '13 at 05:28
  • It would take a simple debug, even a printf to see the name of the parameters. Anyway, see my 2nd Edit, it should work now – yannicuLar Apr 10 '13 at 06:35
  • I have problem with this ques. I have posted the query in new forum.. I have implemented the solution u mentioned above.. The problem that I'm facing I have addressed here: http://stackoverflow.com/questions/16891117/not-able-to-update-database-due-to-pre-marked-attendancevalues-liferay Please help me.. – Seeya K Jun 03 '13 at 09:30