Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
George Stafford
Filtering User Search
March 6, 2013 5:51 PM
Answer

George Stafford

Rank: Junior Member

Posts: 62

Join Date: October 1, 2012

Recent Posts

I am trying to implement a hook to filter the users that are shown. I want to take a few people out of the list, such as the administration users. So I tried several things and have come across this wiki post User Search using Custom Attributes which I have tried several different ways to implement. I can't seem to get it working.

I am overriding the template located at /html/portlet/directory/view_users.jsp. I am trying to work with my custom attribute and add have done this in my page:

userParams.put("show-in-directory", "true");

When it runs with the user_search_results_database.jspf option, I get the error in LPS-26742. If I use user_search_results_index.jspf it runs but the custom attribute isn't picked up. I've tried making the variable indexable and not. Neither worked.

I also tried filtering on last name using both userParams.put("lastName","blah"); and userParams.put("usersLastName","blah") and both through the same error as LPS-26742.

I also hardcoded an organizationID in and that properly filtered. So there are some things working, but I am just not able to figure out my next step. Any advice?

George
George Stafford
RE: Filtering User Search
March 11, 2013 7:57 AM
Answer

George Stafford

Rank: Junior Member

Posts: 62

Join Date: October 1, 2012

Recent Posts

Anyone have any thoughts on this for the hook I'm working on?

Thanks,
George
George Stafford
RE: Filtering User Search
March 12, 2013 7:52 AM
Answer

George Stafford

Rank: Junior Member

Posts: 62

Join Date: October 1, 2012

Recent Posts

So I changed up and went with this person's wiki instead and that one worked much better for me. I ended up using the code below:

 1<%
 2String viewUsersRedirect = ParamUtil.getString(request, "viewUsersRedirect");
 3
 4PortletURL portletURL = (PortletURL)request.getAttribute("view.jsp-portletURL");
 5
 6if (Validator.isNotNull(viewUsersRedirect)) {
 7    portletURL.setParameter("viewUsersRedirect", viewUsersRedirect);
 8}
 9%>
10
11<c:if test="<%= Validator.isNotNull(viewUsersRedirect) %>">
12    <aui:input name="viewUsersRedirect" type="hidden" value="<%= viewUsersRedirect %>" />
13</c:if>
14
15<%
16    PortalPreferences portalPrefs = PortletPreferencesFactoryUtil.getPortalPreferences(request);
17    String sortByCol = ParamUtil.getString(request, "orderByCol");
18    String sortByType = ParamUtil.getString(request, "orderByType");
19       
20    if (Validator.isNotNull(sortByCol ) && Validator.isNotNull(sortByType )) {
21    
22        portalPrefs.setValue("NAME_SPACE", "sort-by-col", sortByCol);
23        portalPrefs.setValue("NAME_SPACE", "sort-by-type", sortByCol);
24    
25    } else {
26    
27        sortByCol = portalPrefs.getValue("NAME_SPACE", "sort-by-col", "Last Name");
28        sortByType = portalPrefs.getValue("NAME_SPACE", "sort-by-type ", "asc");   
29    }
30%>
31
32<liferay-ui:search-container
33    searchContainer="<%= new UserSearch(renderRequest, portletURL) %>"
34    emptyResultsMessage="No Results Were found for the Selected Criteria"
35>
36    <aui:input disabled="<%= true %>" name="usersRedirect" type="hidden" value="<%= portletURL.toString() %>" />
37
38    <liferay-ui:search-container-results>
39        <%       
40            long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class);
41            long companyId = PortalUtil.getDefaultCompanyId();
42            
43            List<ExpandoValue> values = ExpandoValueLocalServiceUtil.getColumnValues(companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, "show-in-directory", "true", -1, -1);
44            
45            List<User> users = new ArrayList<User>();
46            User cppUser;
47            
48            for (int i = 0; i < values.size(); i++) {
49              long userId = values.get(i).getClassPK();
50              try {
51                  cppUser =  UserLocalServiceUtil.getUser(userId);
52                  users.add(cppUser);
53              } catch(NoSuchUserException e ){
54                 ////user with this primary key was not found in DB .....          
55              }
56            }
57
58            Collections.sort(users,new Comparator<User>(){
59                  public int compare(User u1, User u2) {
60                        return u1.getLastName().compareToIgnoreCase(u2.getLastName());
61                      }
62                    });
63
64            results = ListUtil.subList(users, searchContainer.getStart(), searchContainer.getEnd());
65       
66            total = users.size();
67            
68            pageContext.setAttribute("results", results);
69            pageContext.setAttribute("total", total);
70        %>
71    </liferay-ui:search-container-results>
72
73    <liferay-ui:search-container-row
74        className="com.liferay.portal.model.User"
75        escapedModel="<%= true %>"
76        keyProperty="userId"
77        modelVar="user2"
78        rowIdProperty="screenName"
79    >
80   
81        <liferay-portlet:renderURL varImpl="rowURL">
82            <portlet:param name="struts_action" value="/directory/view_user" />
83            <portlet:param name="tabs1" value="<%= HtmlUtil.escape(tabs1) %>" />
84            <portlet:param name="redirect" value="<%= searchContainer.getIteratorURL().toString() %>" />
85            <portlet:param name="p_u_i_d" value="<%= String.valueOf(user2.getUserId()) %>" />
86        </liferay-portlet:renderURL>
87       
88        <%@ include file="/html/portlet/directory/user/search_columns.jspf" %>
89    </liferay-ui:search-container-row>
90
91    <div class="separator"><!-- --></div>
92
93    <liferay-ui:search-iterator />
94
95</liferay-ui:search-container>


I found this by utilizing this wiki post: Search for an object using custom attributes.

The other trick in this was to re-achieve my sorting. I tried using the Custom Comparators I've seen out there but since I was doing this in a hook, I was unable to get them to work. Has to do, apparently, with the code ending up in a different place than the jsp page with a hook.

This is working and seems to have solved the problems. I'd love any feedback on ways to streamline though.

Thanks!
Rahul Pande
RE: Filtering User Search
March 15, 2013 6:03 AM
Answer

Rahul Pande

Rank: Expert

Posts: 308

Join Date: July 6, 2010

Recent Posts

Hi George,

I hope you are using Liferay 6.0.6 version.
When I debugged the code, I found that when you create an Indexable custom field, it is getting indexed as follows.

1. Code for indexing of custom attribute can be found in ExpandoBridgeIndexerImpl.java
2. Its addAttribute(Document document, ExpandoColumn expandoColumn, List<ExpandoValue> expandoValues) method actually add those custom attribute values in indexer
3. While calling document.addKeyword(fieldName, expandoValue.getBoolean()); method to add the value, field name is taken by calling following method

 1public String encodeFieldName(String columnName) {
 2        StringBundler sb = new StringBundler(3);
 3
 4        sb.append(_FIELD_NAMESPACE);
 5        sb.append(StringPool.FORWARD_SLASH);
 6        sb.append(ExpandoTableConstants.DEFAULT_TABLE_NAME.toLowerCase());
 7        sb.append(StringPool.FORWARD_SLASH);
 8        sb.append(columnName);
 9
10        return sb.toString();
11    }


so the final field name stored in the indexer will be "expando/CUSTOM_FIELDS/{column name}"

Can you try setting param name in userParams in this format instead of userParams.put("show-in-directory", "true");

Hope that helps.

Thanks
Rahul