Search code examples
mysqlliferayliferay-6

Display data from Custom Query(Joined tables) in liferay search container


I have followed this wiki and have successfully built a custom query.

It works fine. I have used a join between tables.

My question is how do I display it on a jsp using liferay search container since className in search container requires one model class.

EDIT:
What I have tried till now is this:

<%

getAttendanceData attName = new getAttendanceData();
List<Object[]> displayAttListName = AttendanceLocalServiceUtil.findAttendance();

ArrayList name = new ArrayList();
ArrayList title = new ArrayList();
ArrayList status = new ArrayList();
ArrayList remarks = new ArrayList();

for(Object[] att:displayAttListName) {

    name.add(att[0]);
    title.add(att[1]);
    status.add(att[2]);
    remarks.add(att[3]);
}

%>

<liferay-ui:search-container delta="20" emptyResultsMessage="No Results Found">
    <liferay-ui:search-container-results
            total="<%= displayAttListName.size() %>"
            results="<%= ListUtil.subList(displayAttListName , searchContainer.getStart(), searchContainer.getEnd()) %>"
        />

    <liferay-ui:search-container-row modelVar="search"
        className="java.lang.Object">


    <%
    for(Object displayName:name) {
    %>

        <liferay-ui:search-container-column-text name='studName' value = '<%=String.valueOf(displayName)%>' href="">

        </liferay-ui:search-container-column-text>
    <%
    }
    %> 

    </liferay-ui:search-container-row>
    <liferay-ui:search-iterator/>
</liferay-ui:search-container>  

What I have done above displays each of the 10 names 10 times in a column.
I want the names to appear on each new row. How shoould I modify the above code?

EDIT 2
Considering the name array defines earlier I did the following:

<liferay-ui:search-container-column-text name="employee name" href = "">

    <%=name.getClass().getDeclaredFields().toString() %>

</liferay-ui:search-container-column-text>

With the above, I am getting the result something like: [Ljava.lang.reflect.Field;@195f1af on each row.


Solution

  • I see that the name, title, status and remarks field are all String (as per your comment) so in the for loop you should cast the Object as a String and you don't need the four ArrayList for this.

    Here is how the row tag would look like:

    <liferay-ui:search-container-row className="java.lang.Object" modelVar="search">
    
        <%--
            Since an "Object[]" is nothing but an "Object", we first cast the "search"
            instance to an "Object[]" and then to a "String"
        --%>
        <liferay-ui:search-container-column-text name='name' value='<%= (String) ((Object[])search)[0] %>' /> 
        <liferay-ui:search-container-column-text name='title' value='<%= (String) ((Object[])search)[1] %>' /> 
        <liferay-ui:search-container-column-text name='status' value='<%= (String) ((Object[])search)[2] %>' /> 
        <liferay-ui:search-container-column-text name='remarks' value='<%= (String) ((Object[])search)[3] %>' /> 
    
    </liferay-ui:search-container-row>
    

    There you go, this should work.

    A more cleaner way I think would be to have a POJO defined that would store these values and then the POJO's list can be returned. I have not tried the second approach though.

    Another standard approach is to include extra fields in any one of the entity's *Impl and then returning the list of that entity, in your case I would assume you have Student and Attendance entities, so you can put the fields status & remarks in StudentImpl and then return a List<Student> or put fname in AttendanceImpl and return List<Attendance> from the finder method. (updated after this comment)