Show all users liferay - liferay

I need to make a portlet which would show all registred users in liferay.
Im not asking you to write all code here, but I would aprreciate if u could present an step-by-step plan of actions, cause I really don't understand how to get info from database.
UPD:
1. I can`t solve what should I import in java file.
import java.io.IOException;
import java.util.List;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.transaction.SystemException;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class ShowUsers extends MVCPortlet {
public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException{
Log log = LogFactoryUtil.getLog(ShowUsers.class);
List<User> users = null;
try {
users = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
} catch (com.liferay.portal.kernel.exception.SystemException e) {
log.info("Exception happened");
}
renderRequest.setAttribute("allUsers", users );
super.render(renderRequest, renderResponse);
}
}
===================
My jsp file:
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%# page import="java.util.List" %>
<%# page import="com.liferay.portal.model.User" %>
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%
List<User> thatusers = renderRequest.getAttribute("allUsers");
%>
<ul>
<% for (User user : thatusers) { %>
<li><%= user %></li>
<% } %>
</ul>
And after this code I have strange info about all users and I need only it's name! this:
{uuid=fb7224c0-2488-45c1-97b8-5608450435a6, userId=20199, companyId=20155, createDate=2016-06-06 08:14:14.0, modifiedDate=2016-06-06 08:14:14.0, defaultUser=false, contactId=20200,

To get all users take a look at UserLocalServiceUtil.getUsers()
In your portlet class you need to pass this list to the jsp you are serving:
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
List<User> users = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
renderRequest.setAttribute("allUsers", users );
super.doView(renderRequest, renderResponse);
}
And than in your jsp iterate the allUsers list using JSTL to get a user object.
getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS); would retrieve all the users, instead of QueryUtil.ALL_POS you can specifie start and end if you need to paginate the result.

Related

Edit message portlet in liferay

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class GreetingMessage extends MVCPortlet {
public static final String GREETING = "greeting";
public static final String DEFAULT_GREETING = "Hello! It's my default greeting message";
#Override
public void render(RenderRequest request, RenderResponse response)
throws IOException, PortletException {
PortletPreferences preferences = request.getPreferences();
request.setAttribute(GREETING,
preferences.getValue(GREETING, DEFAULT_GREETING));
super.render(request, response);
}
public void updateGreeting(ActionRequest request, ActionResponse response)
throws ValidatorException, IOException, ReadOnlyException {
String greeting = request.getParameter("greeting");
PortletPreferences prefs = request.getPreferences();
if (greeting != null) {
prefs.setValue(GREETING, greeting);
prefs.store();
request.setAttribute(GREETING, greeting);
}
}
}
view.jsp:
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p>${greeting}</p>
<portlet:renderURL var="editGreetingURL">
<portlet:param name="mvcPath" value="/html/greetingmessage/edit.jsp"/>
</portlet:renderURL>
<p>Edit greeting</p>
edit.jsp
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%# page import="javax.portlet.PortletPreferences" %>
<portlet:defineObjects />
<portlet:actionURL name="updateGreeting" var="updateGreetingURL">
</portlet:actionURL>
<aui:form action="<%= updateGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
<aui:button type="submit" />
</aui:form>
<portlet:renderURL var="viewGreetingURL">
<portlet:param name="mvcPath" value="/html/greetingmessage/view.jsp" />
</portlet:renderURL>
<p>← AND NOW IT'S BACK</p>
It's tested code of my "edit-greeting" portlet. The question is, how can I make localization??? I've read a lot of docs but it's all for nothing. I created in WEB-INF folder src/language.properties and src/language_es.properties. What should I do next? Help me please. #Shivam
To Answer your question
1)You can handle your attributes and portlet preferences in render method and set them as attributes in render request,which can be subsequently read in your jsp via some scripting language like jstl
2)There is no need to make changes in portlet.xml file.
The init params as the name suggests are added to provide some params needed for initializing a portlet view.
You need to make the below changes to the render method
public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
{
String greeting = req.getParameter("greeting");
PortletPreferences prefs = req.getPreferences();
String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
if(prefs.getValue("greeting","true")==null)
{
prefs.setValue("greeting", defaultGreeting);
}
if (greeting != null)
{
prefs.setValue("greeting", greeting);
prefs.store();
req.setAttribute("greeting", prefs.getValue("greeting","true"));
}
else
{
req.setAttribute("greeting", prefs.getValue("greeting","true"));
}
super.render(req,res);
}
There will not be any changes required in view.jsp and edit.jsp(apart from removing code),Hence I forgot to mention the same.
As for the render method,the best approach would be defintely to use action url and use action method,but since it seems you are looking to try out some approach and to make mininmum changes to your,I have kept it render only.
As for the code,the prefs.getValue("greeting","true") checks whether a certain attribute is present in portlet preferences or not.
Updated with process action
public class NewPortlet extends MVCPortlet {
public static final String GREETING="greeting";
#Override
public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
{
PortletPreferences prefs = req.getPreferences();
String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
if(prefs.getValue(GREETING,"true")==null)
{
prefs.setValue(GREETING, defaultGreeting);
prefs.store();
}
req.setAttribute(GREETING, prefs.getValue(GREETING,"true"));
super.render(req,res);
}
public void updateGreeting(ActionRequest req,ActionResponse res) throws ValidatorException, IOException, ReadOnlyException
{
String greeting = req.getParameter("greeting");
PortletPreferences prefs = req.getPreferences();
if (greeting != null)
{
prefs.setValue(GREETING, greeting);
prefs.store();
req.setAttribute(GREETING, greeting);
}
}
}
Updates in edit jsp
<portlet:actionURL name="updateGreeting" var="updateGreetingURL">
</portlet:actionURL>
<aui:form action="<%= updateGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
<aui:button type="submit" />
</aui:form>

invoke a method in Liferay

I use Liferay Tomcat bundle 6.2, and I work with Liferay IDE(eclipse)
How I can invoke a method in Liferay?
I create one Liferay Plugin Project and write in java class this following code. But I don't know, How I can invoke this method?
I can't create a main class in Liferay. But, I think, I can invoke this method in view.jsp with creating an action URL, it this right?
Can you give me a sample example?
Thank you
public class TestLoggerPortlet extends MVCPortlet {
private static final Log logger = LogFactoryUtil.getLog(TestLoggerPortlet.class);
public void addEntry() {
logger.info("This is my message.");
if (logger.isDebugEnabled()) logger.debug("Not always printed.");
}
}
I think you are confusing desktop/mobile application with web application.
In my mind you have to study Java EE basics (how a browser performs a request to an application server and, how to server proceed to understand the request and dispatch it to the right method of the righte class, etc... it is called "servlet lifecycle").
Then should be easy to understand the differences with a portlet lifeycle (and how Liferay MVC portlets work for managing what you need).
I can suggest some interesting reading (in order of learning path):
http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html (see lifecycle part); basics to have a global idea about what happens behind the stages;
http://www.opensource-techblog.com/2014/12/introduction-to-portlet-phases-and.html an easy comparation to portlet lifecycle;
https://www.liferay.com/it/documentation/liferay-portal/6.0/development/-/ai/portlet-development - Liferay official general introduction to development
https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/creating-a-liferay-mvc-portlet-project - THE TUTORIAL YOU NEED to understand how to create a simple LR mvc portlet;
https://www.liferay.com/it/web/meera.success/blog/-/blogs/liferay-mvc-portlet-development-introduction another interesting tutorial.
Hope it helps
See the below code.
view.jsp
<portlet:defineObjects />
<portlet:actionURL var="myAction" name="myAction">
<portlet:param name="someTxt" value="Text Message" />
</portlet:actionURL>
<form action="<%= myAction %> method="post" name="fm">
<input name="<portlet:namespace/>txtName" type="text" />
<input type="submit" name="<portlet:namespace/>submit" value="submit"/> 
</form>
Controller Class. TestLoggerPortlet.java
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class TestLoggerPortlet
*/
public class TestLoggerPortlet extends MVCPortlet {
private static Log _log = LogFactoryUtil.getLog(TestLoggerPortlet.class);
#ProcessAction
public void myAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
String var1 = actionRequest.getAttribute("someText");//output -> "Text Message"
String var2 = actionRequest.getAttribute("txtName");//output -> Value of Text field.
sendRedirect(actionRequest, actionResponse);
}
}

Get list of users in liferay

I want to display users list on liferay. and I am having problem with it.
Here is my action class.
public void userList(ActionRequest actionRequest, ActionResponse actionResponse) throws SystemException {
// Todo Logic for user code
try {
int countUser = UserLocalServiceUtil.getUsersCount();
log.info("User Present In DB" + countUser);
List < User > users = UserLocalServiceUtil.getUsers(0, countUser);
PortletSession sessions = actionRequest.getPortletSession();
sessions.setAttribute("users", users);
log.info("Session set from My Portlet" + sessions.getAttribute("users"));
for (User user: users) {
if (user != null) {
log.info("UserID--:" + user.getUserId() + "UserCompanyID-:" + user.getCompanyId() + "UserEmail-:" + user.getEmailAddress() +
"UserScreenName--:" + user.getScreenName());
}
}
and how I am trying to get the users list on jsp.
<%#page import="com.test.UserList.userList"%>
<%#page import="java.util.ArrayList"%>
<%#page import="com.liferay.portal.model.User"%>
<%#page import="java.util.List"%>
<%#page import="javax.portlet.PortletSession"%>
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%#taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<%
PortletSession session2 = renderRequest.getPortletSession();
ArrayList<User> users = (ArrayList) session2.getAttribute("users");
if(users!=null){
%>
<b>Name: </b><%=users.get(users) %>
<%} %>
and i am getting the value is null
i want to display all the users name in list
Another option to get list of all users is
UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
and then iterate it to fetch each user.
There is no issue with your logic in putting user-list in session, the list is getting populated and set in session accurately.
However, on JSP, there are couple of issues:
renderRequest is undefined.
Cast exception (UnmodifiableList cannot be cast to java.util.ArrayList) for user-list.
You are not iterating list and getting user objects properly.
So, you need to do as following on your JSP:
<%#page import="java.util.ArrayList"%>
<%#page import="javax.portlet.RenderRequest"%>
<%#page import="com.liferay.portal.model.User"%>
<%#page import="javax.portlet.PortletSession"%>
<%
RenderRequest renderRequest =
(RenderRequest) request.getAttribute("javax.portlet.request");
PortletSession session = renderRequest.getPortletSession();
List<User> users = (List<User>) session.getAttribute("users");
if(users != null){
for(User user : users){
%>
<b>Name: </b><%=user.getLastName() + ", " + user.getFirstName() %><br />
<%}
}%>
(Tested Code)

creating simple reusable component in Apache Tapestry5

I am starting an adventure with Apache Tapestry5. I am trying to make simple component (for tests), consisting of pair of Textfields. Component is named "TestComp". I have following elements:
testComp.tml
<t:container
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
<p>
<input t:type="TextField" t:id="testOne" t:value="testOne.input"/><br/>
<input t:type="TextField" t:id="testTwo" t:value="testTwo.input"/><br/>
</p>
</t:container>
TestComp.java
public class TestComp {
private DataContainer testOne;
private DataContainer testTwo;
#SetupRender
public void setup(){
testOne = new DataContainer();
testTwo = new DataContainer();
}
public String getContentOfTestOne() {
return testOne.getInput();
}
public String getContentOfTestTwo() {
return testTwo.getInput();
}
public DataContainer getTestOne() {
return testOne;
}
public void setTestOne(DataContainer testOne) {
this.testOne = testOne;
}
public DataContainer getTestTwo() {
return testTwo;
}
public void setTestTwo(DataContainer testTwo) {
this.testTwo = testTwo;
}
}
And then I am trying to use it in other place, for example in index.tml:
<form t:type="form" t:id="out">
<t:testComp />
<br/><input type="submit" value="Component"/>
</form>
According to dozens of materials and examples I've found (to be honest non of it refereed to case similar to mine) such implementation should result of showing testComp element in the form, but unfotrunately there is nothing rendered above the button (though tapestry is not crashing). What am I missing? And will I be able to put in Index.java property of TestComp type and bind it with my
<t:testComp />
in Index.tml by id (or it requires something more to implement in my custom component?)
Did you provide the full index.tml file? If so, you are missing the tapestry namespace as well as a correctly setup html document. Try the following:
Index.tml
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
<head>
<title>My page</title>
</head>
<body>
<form t:type="form" t:id="out">
<div t:id="testComp" />
<br/><input type="submit" value="Component"/>
</form>
</body>
</html>
In your Index.java you can use this to access your component.
#component(id="testComp")
private TestComp testComp;
If this does not work there is probably something wrong in your configuration or setup and you might just be looking at a static tml file not handled by tapestry at all. In this case follow the step-by-step guide on the Getting Started page.

new to Liferay , i am unable to navigate through Pages using renderURL

I am new to Liferay , i am unable to navigate through Pages using renderURL , please tell me where i am doing a mistake
I am struck here , i am not able to navigate to the Second Page during on click of an hyper link as shown below
This is my First Page , where i am showing the First Page ( view.jsp ) , But from view.jsp , i am unable to show view2.jsp
public class TestPortlet extends GenericPortlet {
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
renderResponse.setContentType("text/html");
PortletRequestDispatcher rd = getPortletConfig().getPortletContext()
.getRequestDispatcher("/html/test/view.jsp");
if (rd != null) {
rd.include(renderRequest, renderResponse);
}
}
}
This is my view.jsp
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
This is the <b>Sai Test Portlet</b> portlet in View mode.
<portlet:renderURL var="clickRenderURL">
<portlet:param name="jspPage" value="/html/test/view2.jsp" />
</portlet:renderURL>
Click here
This is my view2.jsp
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
This is the <b>View 2 </b> portlet in View mode.
There are no errros in console mode , and i am uisng Liferay 6.1 version .
Can you check the same using
"liferay-portlet:renderURL" tag.
Instead of using "portlet:renderURL" tag.
Rest all looks fine to me.

Resources