Sample scripts for the Ruby Console portlet [1]

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>"