liferay display connected user in jsp - liferay

I work with liferay 5.2
in my jsp I can get the connected user with this code :
<%#page import="com.nbs.fw.portal.PortalUtil"%>
String id_employe=PortalUtil.getConnectedUserID(request).toUpperCase();
but now I want to know the role of connected user
Updated :
<%#page import="com.liferay.portal.service.RoleServiceUtil"%>
<%# page import="com.liferay.portal.model.User" %>
<%# page import="com.liferay.portal.model.Role" %>
<%#page import="com.liferay.portal.theme.ThemeDisplay"%>
<%#page import="com.liferay.portal.util.WebKeys"%>
<%
User user = ((ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY)).getUser();
List<Role> roles = (List<Role>) RoleServiceUtil.getUserRoles(user.getUserId());
for (Role role : roles) {
out.println( role.getRoleId() );
}
%>
but when I test I have an error :
Caused by: org.apache.jasper.JasperException: Impossible de compiler la classe pour la JSP:
An error occurred at line: 13 in the generated java file
Only a type can be imported. com.liferay.portal.util.WebKeys resolves to a package
Une erreur s'est produite � la ligne: 1 208 dans le fichier jsp: /jsp/_correspondencelist/html/correspondenceList.jsp
WebKeys.THEME_DISPLAY cannot be resolved to a type
1205: %>
1206:
1207: <%
1208: User user = ((ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY)).getUser();
1209: List<Role> roles = (List<Role>) RoleServiceUtil.getUserRoles(user.getUserId());
1210: for (Role role : roles) {
1211: out.println( role.getRoleId() );
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)

It seems that you didn't import all classes like WebKeys or ThemeDisplay
<%# page import="com.liferay.portal.theme.ThemeDisplay" %>
<%# page import="com.liferay.portal.util.WebKeys" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<liferay-theme:defineObjects />

Change the import to com.liferay.portal.PortalUtil
<%
User user = PortalUtil.getUser(request);
List<Role> roleList = user.getRoles();
%>

Related

SweetAlert2 is not working in node.js

I'm making website for db study.
I wanted to alert to user if user didn't input id and password in register page.
So I installed sweetalert2 by npm. But sweetalert2 doesn't work in route.js which connect user's ask and server.
var swal = require('sweetalert2');
router.post('/register', function(req, res){
if(!req.body.id && !req.body.password){
swal('WARNING','You must input both id and password!','error')
}
else{
swal('COMPLETE','You registered your information!','success');
res.redirect('/');
}
})
How can I fix it?
I used .ejs file to use sweetalert2.
<!DOCTYPE html>
<html>
<head>
<% include ../pages/head %>
</head>
<body>
<% if(fail) { %>
<% include ../partials/alert_login %>
<% } %>
<% include ../pages/login %>
</body>
</html>
in login_connect.ejs includes when user fail login alert_login.ejs file and alert_login.ejs includes script tag of using swal of sweetalert2 module.
But It isn't work. What is the problem?
alert_login.ejs is below.
<script type="text/javascript">
alert("You are not our member!");
</script>

How to place an EJS tag within another

I am using EJS as my templating engine. I am passing variables from node js to ejs like...
router.get("/AdminDatabase", function(req, res) {
res.render('Pages/AdminDatabase', {title: 'WebFormsAdmin', role: 'System Admin' });
});
I am building a role base control and for this I want to change the header of the page base on the role of user.
<% include ../partials/Common/header_<%= role %> %>
The problem is with the above segment. How can I place the variable role inside this EJS code segment?
My header files are
header_System Admin.ejs,
header_Survey Admin.ejs,
header_Survey Taker.ejs
A workaround would be to do a conditional render like so:
<% switch (role) {
case 'System Admin': %>
<% include ./partials/header_System_Admin %>
<% break; %>
<% case 'Survey Admin': %>
<% include ./partials/header_Survey_Admin %>
<% break; %>
<% default: %>
<% include ./partials/header_Survey_Taker %>
<% break; %>
<% } %>
Note that the first case must be grouped with the switch declaration. Make sure the paths are correct for your partials.
You can concatenate the path and the variable.
<%- include('../partials/Common/header_'+role) %>

Liferay 6.2 How to display images stored in Documents and Media

I would like to know all possible ways of displaying a DLFileEntry image in a jsp of a custom portlet.
More specifically, I currently use the following way but I have some issues with DLFileEntry objects that have zero values for 'largeimageid'
DLFileEntry image = DLFileEntryLocalServiceUtil.getFileEntry(long_id);
String imageUrl = themeDisplay.getPathImage() + "/image_gallery?img_id=" + image.getLargeImageId() + "&t=" + WebServerServletTokenUtil.getToken(image.getLargeImageId());
Which are the alternatives of getting the image url without use of the large image id?
Following is the pattern similar to the one that is used by Liferay Documents and Media portlet:
DLFileEntry image = DLFileEntryLocalServiceUtil.getFileEntry(long_id);
String imageUrl = "";
if (image != null) {
imageUrl =
PortalUtil.getPortalURL(request) + "/documents/" + image.getGroupId() + "/" +
image.getFolderId() + "/" + image.getTitle() + "/" + image.getUuid() + "?t=" +
System.currentTimeMillis();
}
Where PortalUtil.getPortalURL(request) will return you base URL of your portal based on httpServletRequest, System.currentTimeMillis() will give you current time (miliseconds), and rest of the parameters are all available through DLFileEntry object.
I think this can help you
<%# page import="com.liferay.portlet.documentlibrary.model.DLFolder" %>
<%# page import="com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil" %>
<%# page import="com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil" %>
<%# page import="com.liferay.portlet.documentlibrary.model.DLFileEntry" %>
<%# page import="java.util.List" %>
<%# page import="com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil" %>
<%# page import="com.liferay.portlet.imagegallery.model.IGImage" %>
<%# include file="init.jsp" %>
<%
String igFolderId = portletPreferences.getValue("igFolderId", "0");
String cycleSpeed = portletPreferences.getValue("cycleSpeed", "1000");
String fxSpeed = portletPreferences.getValue("fxSpeed", "1000");
String type = portletPreferences.getValue("type", "fade");
String height = portletPreferences.getValue("height", "480");
String width = portletPreferences.getValue("width", "640");
List<IGImage> images = IGImageLocalServiceUtil.getImages(Long.valueOf(igFolderId));
%>
<c:choose>
<c:when test="<%= Long.valueOf(igFolderId) != 0%>">
<div id="<portlet:namespace />images">
<%
for (int i = 0; i < images.size(); i++) {
IGImage image = images.get(i);
%>
<img width="<%= width %>" height="<%= height %>" src="/image/image_gallery?img_id=<%=image.getLargeImageId()%>" alt="<%=image.getDescription()%>" <%= i == 0 ? "" : "style=\"display:none;\""%>/>
<%
}
%>
</div>
</c:when>
<c:otherwise>
<span class="portlet-msg-info">
Please configure this portlet.
</span>
</c:otherwise>
</c:choose>
<script type="text/javascript">
jQuery(
function() {
jQuery("#<portlet:namespace />images").cycle({
fx: '<%= type %>',
speed: <%= fxSpeed %>,
timeout: <%= cycleSpeed %>
});
}
);
</script>
Greetings!

Liferay.PortletURL is undefined

Want to implement:
I have two pages with one portlet each. On click of link i want to move from first page portlet to another page portlet.
For that i have written:
<aui:script>
function openCompanyPage(companyId) {
AUI().use(
'liferay-portlet-url',
'aui-resize-iframe',
function(A) {
var navigationURL;
var portletURL = Liferay.PortletURL.createRenderURL();
var url = themeDisplay.getLayoutURL();
portletURL.setParameter("employerId", companyId);
portletURL.setPortletId(A.one('#custSupportPortletId'));
navigationURL = portletURL.toString();
window.location = navigationURL;
}
);
</aui:script>
but i am getting error as Liferay.PortletURL is undefined on bold line.
I have already provided :
<%# taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%# taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%# taglib uri="http://liferay.com/tld/util" prefix="liferay-util"%>
Please let me know the possible reason for this.
OR
What is other way to create the Render portlet URL.
Update:
I resolved the issue by brute force.
Before:
<a href="" onclick="openCDPPage('${individual.individualId}')">${individual.individualName}
After :
<a onclick="openCDPPage('${individual.individualId}')">${individual.individualName}</a>
Changes in the script:
<script>
function openCompanyPage(companyId) {
AUI().use(**'liferay-portlet-url'**,
function(A) {
var navigationURL;
var portletURL = Liferay.PortletURL.createRenderURL();
var url = themeDisplay.getLayoutURL();
portletURL.setParameter("employerId", companyId);
portletURL.setPortletId(A.one('#custSupportPortletId'));
navigationURL = portletURL.toString();
window.location = navigationURL;
}
);
</script>
This solved my issue.
I am not sure whether this is the perfect solution for the problem or not.
Expert please let us know.

How to Include AlloyUI

I have some troubles including AlloyUI in my Liferay Portlet.
Following this article, I have generated the following jsp:
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<input type="text" id="some-input" />
<span id="counter"></span> character(s) remaining
<aui:script>
YUI().use(
'aui-char-counter',
function(Y) {
new Y.CharCounter(
{
counter: '#counter',
input: '#some-input',
maxLength: 10
}
);
}
);
</aui:script>
But the rendered page looks like this:
I made sure that the taglib is correctly defined in the web.xml:
<taglib>
<taglib-uri>http://liferay.com/tld/aui</taglib-uri>
<taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
</taglib>
AUI does work, when I include it in the jsp as follows:
<script src="http://cdn.alloyui.com/2.0.0/aui/aui-min.js"></script>
<link href="http://cdn.alloyui.com/2.0.0/aui/aui-min.js" rel="stylesheet"></link>
<input type="text" id="some-input" />
<span id="counter"></span> character(s) remaining
<script>
YUI().use(
'aui-char-counter',
function(Y) {
new Y.CharCounter(
{
counter: '#counter',
input: '#some-input',
maxLength: 10
}
);
}
);
</script>
I'm using Liferay 6.1.20 EE GA2
Liferay uses alloy-ui (also referred to asAUI) library developed on top of Yahoo UI (also referred to as yui) library.
The instance terms for both these libraries are different i.e. AUI for Alloy-UI and YUI for the other.
Replacing these terms in your code will resolve your issue i.e. have AUI instead of YUI.
The seed file declaration is all you need to access AlloyUI for this char counter code.
<script src="http://cdn.alloyui.com/2.0.0/aui/aui-min.js"></script>
You shouldn't need the taglib reference in your web.xml. In fact, you're inhibiting access to the seed file. The taglib you're referencing may be inconsistent with the version of AlloyUI that is expected.
Also, accessing YUI's CharCounter is fine. See the API example at http://alloyui.com/api/classes/A.CharCounter.html.

Resources