1

I am using Liferay to develop a module. A part of involves fetching only those records form the database where the leave status of employees is Pending. the code that I have to fetch "Pending" records is:

  @SuppressWarnings("unchecked")
   public static List<Employee> getEmployeeData() throws    PortalException, SystemException{

   List<Employee> employeeDetails;

   try{
    int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();
            for(Employee emp: employeeDetails   {
               if(emp.getEmpStatus.equals("Pending")  {
    employeeDetails=  EmployeeLocalServiceUtil.getEmployees(0,totalEmployees);
           }
    }           
                      }catch(SystemException se){
                          employeeDetails = Collections.emptyList();
                     }


                     return employeeDetails;
                }

The above code fetches all the details - Pending as well as non pending. This I know happens because of the statement in the above code:

employeeDetails= EmployeeLocalServiceUtil.getEmployees(0,totalEmployees);

since It fetches all the rows. So how should I structure and modify my code to get only the pending details?

Maximin
  • 1,655
  • 1
  • 14
  • 32
Seeya K
  • 1,221
  • 6
  • 27
  • 43

3 Answers3

2

As you are dealing with custom entity in Liferay, you can use finder tag in service.xml for such scenario. In service.xml define finder for empStatus field.

<finder name="EmpStatus" return-type="Collection">
  <finder-column name="empStatus"/>
</finder>

This will create finder[findByEmpStatus(String status)] method in **Persistence.java which will return specific rows based on status , now You need to manually add methods to your *(Local)ServiceImpl.java files. Those methods will call your *persitence.finderMethodName() methods.

HTH,

Pankaj Kathiriya
  • 4,210
  • 2
  • 19
  • 26
1

A quick but really bad practice is keeping your code with this change :

List<Employee> employeeDetails = new ArrayList<Employee>;
try{

List<Employees> allEmployees = EmployeeLocalServiceUtil.getAllEmployees();

        for(Employee emp: allEmployees {
           if(emp.getEmpStatus.equals("Pending")  {
employeeDetails.add(emp);
       }
}return employeeDetails;      

Now, the correct way to do this is :

  1. add a Finder, as @Pankaj Kathiriya already proposed. Then, build services
  2. go to EmployeeLocalServiceImpl, and add

    public List getAllEmployeesByEmpStatus (String status) { 
    try { 
        return employeePersistence.findByEmpStatus(status); 
    } catch (SystemException e) {
        e.printStackTrace(); return Collections.emptyList(); 
        }
    }
    

then build service again

  1. Replace your code with

        List employeeDetails = EmployeeLocalServiceUtil.getAllEmployeesByEmpStatus("Pending") ; 
    return employeeDetails;
    
yannicuLar
  • 3,083
  • 3
  • 32
  • 50
  • Thanks yet again!! Remember? You have helped me with other probs too !! :) Hey could you help me with: http://stackoverflow.com/questions/16171384/display-data-in-liferay-search-container-from-different-database-tables-lifera – Seeya K Apr 23 '13 at 14:07
  • Hey Seeya, I just got the "Tenacious" badge from this. It means that too many of my answer gets accepted but not upvoted. It usually happens if you tend to help new users who don't have enough rep to upvote. So, I'm just wandering.. Since you have the rep to vote, is there any reason for not upvoting this one ? – yannicuLar Apr 25 '13 at 08:09
  • I have accepted your answers in other questions too I think. I thought it would earn you reputation and increase your badges and stuff. But had no idea about this "Tenacious" badge. I have been here for yeah, two months :) but not all too familiar.. Apologies again to you. Do you want to get rid of the Tenacious badge? You tell me what I have to do for that. – Seeya K Apr 25 '13 at 08:35
  • You don't have to apologize for anything. You can upvote whatever you like and you don't have to explain anything. I asked just because I was curious. In my book, it's easier to upvote than accept an answer, but that's just me. Don't worry, you've done nothing wrong, and besides, I like the badge ;) – yannicuLar Apr 25 '13 at 08:38
  • What I mean is where we can stay connected so I can contact you for help with the coding stuff – Seeya K Apr 25 '13 at 08:54
  • http://meta.stackexchange.com/questions/431/any-way-to-send-a-personal-message-to-another-user – yannicuLar Apr 25 '13 at 09:05
  • Since you did not provide any blog or mail Id I was asking for a way to connect. :) Do you maintain a blog? – Seeya K Apr 25 '13 at 09:13
0

I have a solution for this. I got a suggestion in the forum to use Dyanamic Query in liferay.

And it is pretty simple.

Read the wiki on Dynamic Query on liferay.

Well to answer my own question, here is the solution I found:

  DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(LeaveApplication.class);

  dynamicQuery.add(PropertyFactoryUtil.forName("EmpStatus").eq("Pending"));

and Then I return of those people whose status is pending..

Hope it helps someone.. More suggestions are welcome :)

Seeya K
  • 1,221
  • 6
  • 27
  • 43
  • Seeya, results by DynamicQuery wont be cached, but by finder ,results would be cached. I believe , you had added finder tag for entity LeaveApplication. – Pankaj Kathiriya Apr 22 '13 at 10:48
  • @ Pankaj Kathiriya: Yes I have added finder tag for entity LeaveApplication . But I cannot understand why it isn't creating the method in the persistence class.. Pankja I have another query. I read that Dynamic queires are better than using finder tags. Of course since my query was just fetching only those entries that have Pending as status it wont matter much. – Seeya K Apr 22 '13 at 10:55