Foros de discusión

how to convert long type value to string type in velocity template?

thumbnail
maryam maryam masoudy, modificado hace 11 años.

how to convert long type value to string type in velocity template?

Regular Member Mensajes: 149 Fecha de incorporación: 30/10/11 Mensajes recientes
hi anyone
how to convert long type value to string type in velocity template?
thanks a lot
thumbnail
Paul ., modificado hace 11 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 522 Fecha de incorporación: 29/08/11 Mensajes recientes
You can use $getterUtil.getString(..)
thumbnail
maryam maryam masoudy, modificado hace 11 años.

RE: how to convert long type value to string type in velocity template?

Regular Member Mensajes: 149 Fecha de incorporación: 30/10/11 Mensajes recientes
paul thanks for reply

but i think $getterUtil.getString(..) get string and return string !!

Archivos adjuntos:

thumbnail
Paul ., modificado hace 11 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 522 Fecha de incorporación: 29/08/11 Mensajes recientes
In velocity template? You can use it directly without declaring a variable etc as indicated in the attached image. Also in you question you asked how to get a string value.
$getterUtil.getString(..) takes object and returns a string. The actual method is com.liferay.portal.kernel.util.GetterUtil.getString(Object value)
com.liferay.portal.kernel.util.GetterUtil_IW is wrong
com.liferay.portal.kernel.util.GetterUtil is right

Hope it's clear.
thumbnail
maryam maryam masoudy, modificado hace 11 años.

RE: how to convert long type value to string type in velocity template?

Regular Member Mensajes: 149 Fecha de incorporación: 30/10/11 Mensajes recientes
i know in velociti i can use $getterUtil directly i write in itellij IDE in on sample portlet because i want to certain from code but i think in velocity
getteruttil is in com.liferay.portal.kernel.util.GetterUtil_IW
however i have error when i write $getterUtil.getString(Longvariable)

Archivos adjuntos:

thumbnail
Paul ., modificado hace 11 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 522 Fecha de incorporación: 29/08/11 Mensajes recientes
Sorry my bad.
I wasn't aware that you were using Liferay 5.2. , I did a quick debug on my Liferay instance which is Liferay 6.1 (this class gets called com.liferay.portal.kernel.util.GetterUtil)
I checked further to get the conversion done and it seems even it's possible to pass any object to get it's string value, but the implementation only returns if the passed object is an instance of String . So I thought of a workaround.
$portal.getClass().forName("java.lang.String").valueOf($group_id)

It may not be the way to do it, but should work. here replace the variable $group_id with your long variable.
Let me know if it works fine.
thumbnail
maryam maryam masoudy, modificado hace 11 años.

RE: how to convert long type value to string type in velocity template?

Regular Member Mensajes: 149 Fecha de incorporación: 30/10/11 Mensajes recientes
hi
this code resolve my problem
$portal.getClass().forName("java.lang.String").valueOf($group_id)

thanks alot very much
after some days i am happy emoticon
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
maryam maryam masoudy:
hi
this code resolve my problem
$portal.getClass().forName("java.lang.String").valueOf($group_id)

thanks alot very much
after some days i am happy emoticon



How can I do this in Liferay 6.2? Above code doesn't work in LFR 6.2.

Thanks!
thumbnail
Jitendra Rajput, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
You can use $getterUtil for type conversion ..
use $getterUtil.getString(..)
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
Jitendra Rajput:
You can use $getterUtil for type conversion ..
use $getterUtil.getString(..)



This is not working for me.
This code
#set( $ricd = $getterUtil.getString($nav_item.getPrimaryKey()) )
	    		<span>x: $getterUtil.getString(10) :x</span>


prints x: :x in my web page.
$nav_item.getPrimaryKey() returns an integer.

Thanks
thumbnail
Jitendra Rajput, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
It should work . Which version of Liferay you are using . I saw in Liferay 6.2 this class is getting injected from TemplateContextHelper.java
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
Jitendra Rajput:
It should work . Which version of Liferay you are using . I saw in Liferay 6.2 this class is getting injected from TemplateContextHelper.java



I'm using Liferay 6.2.
I explain my problem: I'm trying to retrieve permissions for an organization's site pages (Layout model). In my Velocity template i use this code
#set ( $hasNotPermission =  $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", $portal.getClass().forName("com.liferay.portal.model.ResourceConstants").SCOPE_INDIVIDUAL, $getterUtil.getString($nav_item.getPrimaryKey()), $userRolesIds, "MY_ACTION_ID") )


In this code I have two problems:
  • $getterUtil.getString($nav_item.getPrimaryKey()): Convert the primary key of the page (layout model) from long to string. $nav_item.getPrimaryKey() correctly returns the id but the whole expression returns blank
  • $portal.getClass().forName("com.liferay.portal.model.ResourceConstants").SCOPE_INDIVIDUAL: I have to refer the static field SCOPE_INDIVIDUAL of the Liferay class ResourceConstants but the whole expression returns "$portal.getClass().forName("com.liferay.portal.model.ResourceConstants").SCOPE_INDIVIDUAL"


Thanks
thumbnail
Jitendra Rajput, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
I hope you are using findService to locate $resourcePermissionService.

#set ($resourcePermissionService= $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionService"))
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
Jitendra Rajput:
I hope you are using findService to locate $resourcePermissionService.

#set ($resourcePermissionService= $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionService"))



I'm locating the service with this code:
#set ($resourcePermissionService = $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionLocalService"))
thumbnail
Jitendra Rajput, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
Have you made any changes in below property pf portal-ext ? .

This is default value of property ..

#
# Set a comma delimited list of variables the Velocity engine cannot
# have access to. This will affect Dynamic Data List templates, Journal
# templates, and Portlet Display templates.
#
velocity.engine.restricted.variables=serviceLocator
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
My portal-ext doesn't contains the property velocity.engine.restricted.variables.
I tried to add the line velocity.engine.restricted.variables= and now the line velocity.engine.restricted.variables=serviceLocator but nothing change.

But I'm sure that serviceLocator works well because i use other Liferay service and all works well.
For example I successfully use
#set ($layoutLS = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
#set ($groupService = $serviceLocator.findService("com.liferay.portal.service.GroupService"))
#set ($orgService = $serviceLocator.findService("com.liferay.portal.service.OrganizationLocalService"))
thumbnail
Jitendra Rajput, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
$getterUtil is used in default init.vm also if every thing is ok then it should work in your case also.
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
Ok, and do you known how to solve my other problem? How to get static field com.liferay.portal.model.ResourceConstants.SCOPE_INDIVIDUAL in Velocity?
thumbnail
Jitendra Rajput, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
I never tried to load any static field in velocity template but easy way is the pass directly value of that static field in your case you can directly pass 4
public static final int SCOPE_INDIVIDUAL = 4;
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
Thank you, but I'm not able to get this damn code working:
#set ( $hasNotPermission = $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", 4, "103742", 1179813, "ACT_ID") )

103742 is a valid layout primary key, 1179813 is a valid role id, ACT_ID is a valid resource permission id.

Addictionally, I've built a servlet doing the same job in Java and this work! emoticon
// Some code to get site layout and user role...
boolean hasPermission = ResourcePermissionLocalServiceUtil.hasResourcePermission(user.getCompanyId(), "com.liferay.portal.model.Layout", ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(layout.getPrimaryKey()), userRolesIds, "ACT_ID");


emoticon
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
R DS:
Thank you, but I'm not able to get this damn code working:
#set ( $hasNotPermission = $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", 4, "103742", 1179813, "ACT_ID") )

103742 is a valid layout primary key, 1179813 is a valid role id, ACT_ID is a valid resource permission id.

Addictionally, I've built a servlet doing the same job in Java and this work! emoticon
// Some code to get site layout and user role...
boolean hasPermission = ResourcePermissionLocalServiceUtil.hasResourcePermission(user.getCompanyId(), "com.liferay.portal.model.Layout", ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(layout.getPrimaryKey()), userRolesIds, "ACT_ID");


emoticon


UPDATE:
This code works:
#set ($resourcePermissionService = $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionLocalService"))

I was missing Local in ResourcePermissionLocalService.
#set ( $hasNotPermission = $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", 4, "103742", $userRoleId, "ACT_ID") )


But i wont to set resource id parameter (103742) dinamically. So i inject $nav_item.getPrimaryKey() in above code, but this method returns a long, and the method accept String.

So the *last* question is: how can i convert long to String in Velocity?

Thanks
R DS, modificado hace 9 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 41 Fecha de incorporación: 15/10/13 Mensajes recientes
SOLVED.

I convert long to String with a custom velocity service as explained here: https://www.liferay.com/it/web/raymond.auge/blog/-/blogs/custom-velocity-tools-and-liferay-6-0

Note that for Liferay 6.2 the situation is a bit different as explained in Simon Göransson's post:
..To get this to work in Liferay 6.2 you can add this in you web.xml and remove the listener.
<context-param>
<param-name>portalContextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
thumbnail
Enrique Valdes Lacasa, modificado hace 7 años.

RE: how to convert long type value to string type in velocity template?

Junior Member Mensajes: 92 Fecha de incorporación: 29/07/14 Mensajes recientes
I accomplished this by doing:

#set($stringNumber= $longNumber.toString())