Liferay DXP Free Tier    |    From download to running in minutes    |    Get started →

Blogs

Sample scripts for the Ruby Console portlet [1]

Ray Augé
Ray Augé
Menys d'un minut de lectura

Here is a sample script for the Ruby Console portlet to test various aspects of the Dynamic Query API.

import com.liferay.portal.kernel.util.Time
import java.lang.System
import java.util.Date

module Orm
   include_package "com.liferay.portal.kernel.dao.orm"
end
module Model
   include_package "com.liferay.portal.model"
end
module Service
   include_package "com.liferay.portal.service"
end

$resourceResponse.setContentType "text/html"

out = $resourceResponse.getPortletOutputStream

date24HoursAgo = Date.new(System.currentTimeMillis - Time::HOUR * 24)

dq1 = Orm::DynamicQueryFactoryUtil.forClass(Model::User.java_class)

pl1 = Orm::ProjectionFactoryUtil.projectionList
pl1.add(Orm::ProjectionFactoryUtil.property("emailAddress"))
pl1.add(Orm::ProjectionFactoryUtil.property("lastLoginDate"))
pl1.add(Orm::ProjectionFactoryUtil.property("lastLoginIP"))

dq1.setProjection(pl1)
dq1.add(Orm::PropertyFactoryUtil.forName("lastLoginDate").gt(date24HoursAgo))
dq1.addOrder(Orm::OrderFactoryUtil.desc("emailAddress"))

result = Service::UserLocalServiceUtil.dynamicQuery(dq1, 0, 20)

out.println result.size
out.println "<br/>"
out.println "<table width='100%' border='1'>"
result.each do |row|
  out.println "<tr>"
  row.each do |field|
    out.println "<td>#{field}</td>"
  end
  out.println "</tr>"
end
out.println "</table>"
Comentaris de la pàgina

Related Assets...

No s'ha trobat cap resultat

More Blog Entries...

Ben Turner
de novembre 26, 2026
Himanshu Chauhan
de novembre 20, 2026
Tony Lim
de novembre 19, 2026