12070

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

Question:

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

Answer1:

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)

Recommend

  • How to show one Imageview on top of that another ImageVIew in IOS using swift
  • i have an issue with checked textview
  • EntityFramework - One to many relationship
  • Yii2:how to get return value in view from model?
  • Trying to save my Django Model Formset, keep getting ManagementForm error?
  • Rails: Multiple databases, same schema
  • Create table with COUNT ms-access
  • Using Attendance Log file from fingerprint device. How to show present and absents?
  • Adding image to flextable using function
  • Export descriptive statistics row of values into an Excel Sheet from R
  • DateTime String to Date, In Time and Out Time
  • String error in SQL MS Access
  • How to set up multiple aliased joins between two models?
  • Java: JFileChooser getting international filenames
  • GXT 2.2 - MessageBox button constants
  • Creating UnfoldingMap instance with java.lang.NoSuchFieldError: quality error
  • Applet size (15 megabytes) too heavy to load?
  • java: PropertyChangeSupport for Map or EnumMap?
  • why am I stuck in an infinite loop after invoking poll() or remove() on a priority queue? Java
  • Writing Typescript interface for a Class
  • Help refreshing list with notifyDataSetChanged()
  • When an entity has more than one 0..1:n relationships to another enitity, how to define them using F
  • FullTextHibernateSessionProxy is not visible from class loader
  • Indexing through Android string resources
  • find specific file type from folder and its sub folder
  • Android MasterDetail layout inside ActivityTab avoiding nested Fragments
  • Struts2 validation is not working properly, validation error messages not shown
  • How can I convince powershell (run through task scheduler) to find my network drive?
  • Why can't I catch EJB exceptions?
  • Problem serializing DecimalFormatSymbols in 2.3.4
  • Issue with static code in Android
  • Problem with rejecting incoming call
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app