Not able to update database due to pre-marked attendance(values) Liferay

≡放荡痞女 提交于 2019-12-12 04:38:45

问题


I have the following functions to mark attendance of an employee:

            public void updateDailyAttendance(ActionRequest areq, ActionResponse aRes) throws Exception {
                int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();
                List<Employee> employeeAttendanceDetails = MISPortalActionUtil.getEmployeeData();

                String datt = areq.getParameter("datt");
                String Imatt = areq.getParameter("matt");
                String yatt = areq.getParameter("yatt");

                int Lmatt = Integer.parseInt(Imatt);
                String matt = Integer.toString(Lmatt +1);


                String dateOfAttendance = datt +"/"+ matt +"/"+ yatt;


                SimpleDateFormat dateOfAttendanceFormat = new SimpleDateFormat("dd/MM/yyyy"); 
                java.util.Date date_Of_Attendance = dateOfAttendanceFormat.parse(dateOfAttendance);

                System.out.println("Today's attendance date is: " + date_Of_Attendance);

                ArrayList<String> attNames = new ArrayList<String>();

                for (Employee emp: employeeAttendanceDetails) {

                    long empId = emp.getEmpId();
                    String name = "updateattendance" + " " +Long.toString(emp.getEmpId());
                    System.out.println("updateattendance name :  " + name);
                    String value = getAttendanceValue(areq,name);
                    System.out.println("updateattendance value :  " + value);
                    long attPKey = CounterLocalServiceUtil.increment(Employee.class.getName());


                    Attendance newAttendanceInstance = new AttendanceImpl();

                    String checkAttMarkStatus = newAttendanceInstance.getAttStatus();
                    System.out.println("checkAttMarkStatus: " + checkAttMarkStatus);


                    //loop to mark the attendance if it has not been pre marked
                    if(checkAttMarkStatus != "Absent" || checkAttMarkStatus != "Half Day" ) {
                    newAttendanceInstance.setAttId(attPKey);
                    newAttendanceInstance.setAttDate(date_Of_Attendance);
                    newAttendanceInstance.setAttStatus(value);
                    newAttendanceInstance.setAttANStatus(value);
                    newAttendanceInstance.setAttFNStatus(value);
                    newAttendanceInstance.setEmpId(empId);
                    AttendanceLocalServiceUtil.addAttendance(newAttendanceInstance);
                    }//loop to mark the attendance if it has not been pre marked



                }
            }
            /**
             * The getAttendanceValue() is used to fetch parameter values and pass the values to updateDailyAttendance function
             * @param areq
             * @return
             * @throws SystemException 
             */

            private String getAttendanceValue(ActionRequest areq, String paramName) {
                 Enumeration parameters = areq.getParameterNames();
                 System.out.println("updateattendance paramName :  " + paramName);
                 while (parameters.hasMoreElements()) {
                     System.out.println("updateattendance paramName inside while :  " + paramName);
                     String parameterName = parameters.nextElement().toString();
                     System.out.println("updateattendance paramName new :  " + paramName);
                     System.out.println("the paramName " + paramName + " parameterName " + parameterName);

                     if (paramName.equals(parameterName)) {

                         return areq.getParameter(parameterName);

                     }




                 }
                 throw new IllegalStateException("Parameter updateattendance is not found");
                }

In my jsp the list of employees is populated and user is allowed to mark attendance through radio button. This approach works well when I am marking attendance for all the employees. But problem comes when I have pre marked attendance status. Whenever a user applies for leave his attendance status is premarked and the attendance form for marking attendance for this employee is shown as marked and disabled.. So when I try to mark attendance when pre marked attendance exists, it doesnt mark attendance for other employees. ex. Suppose if the 4th entry is pre marked as absent, and I mark attendance for other employees, then only first three entries are added in the database and then it doesnt find the fourth entry and throws the illegal exception: Parameter updateattendance is not found

How should I change my getAttendanceValue() function to suit my purpose?

EDIT:

The JSP part where I am fetching the values:

 <label>Present</label><input type = "radio" name ='updateattendance <%=((Object[])search)[5]%>' value = "Present" />
        <label>Absent</label><input type = "radio" name= 'updateattendance <%=((Object[])search)[5]%>' value = "Absent"  />

IN the above code I have kept a check to see if it is pre marked. I have put the above code fragment in if-else block for pre marked attendance check


回答1:


You're doing this:

  Attendance newAttendanceInstance = new AttendanceImpl();
  String checkAttMarkStatus = newAttendanceInstance.getAttStatus(); // most likely null or ""
  System.out.println("checkAttMarkStatus: " + checkAttMarkStatus);

So I don't expect the correct status to be held by the object that you just created without any reference to previous state. My expectation is that checkAddMarkStatus is now "" (empty string) or null

Further you check for identity of strings, not equality (this is a huge difference in java:

  if(checkAttMarkStatus != "Absent" || checkAttMarkStatus != "Half Day" ) {

You should rather use String.equal (and be aware of null values), but due to the issue described above, this will not help you without sorting out both issues. There might be more, but this is what I found on first sight.

Following the comments and your question update, I'm still missing to see the actual intent in the code. However, I'd advise to not use an exception like you do for a case that doesn't seem exceptional - rather use proper return values and check for these values - e.g. if someone never attended, have a value to signal this and react accordingly. If you throw an exception and don't catch it, you must expect things like you mention (e.g. half-executed methods)



来源:https://stackoverflow.com/questions/16891117/not-able-to-update-database-due-to-pre-marked-attendancevalues-liferay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!