Liferay 7.3: Add service builder to the portlet

Previously we created the portlet and service builder inside the eclipse workspace instead of creating a liferay workspace project inside eclipse workspce. So I face the following issues to add the service builder to my portlet (both of them are reside in eclipse workspace)

Could not run phased build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-5.6.4-bin.zip'.
Build file '/home/abhilash/eclipse-workspace/register-emailbox/build.gradle' line: 32
A problem occurred evaluating root project 'register-emailbox'.
Project with path ':sitesService:sitesService-api' could not be found in root project 'register-emailbox'.

Many of them had this issue and you can search in this article of Liferay dev for creating service builder:

https://liferay.dev/blogs/-/blogs/creating-service-builder-mvc-portlet-in-liferay-7-with-liferay-ide-3-

People commented like:

But no solution mentioned here

After some research I came to know that solving this issue, we need to create a portlet and a service builder inside the liferay workspace not eclipse workspace (We should create a liferay workspace project inside eclipse workspace).

Lets do that this time.

Click File -> New -> Liferay Workspace Project

Provide a Project Name and click on Finish

Next right click on the da-workspace, New -> Liferay Module Project

Provide the Project Name, then it automatically changes the Location

Provide the class name and project name

Deploy this service by clicking on the gradle section of IDE and double click on deploy

Deployed successfully

You can see the module os created inside our new Liferay Workspace: da-workspace

Jar file created

Copy this jar file and paste into the liferay server folder path given below:

~/liferay-ce-portal-tomcat-7.3.0-ga1-20200127150653953/liferay-ce-portal-7.3.0-ga1/deploy

You can see the server log like this:

2020-04-14 09:16:09.299 INFO  [fileinstall-/home/abhilash/liferay-ce-portal-tomcat-7.3.0-ga1-20200127150653953/liferay-ce-portal-7.3.0-ga1/osgi/modules][BundleStartStopLogger:39] STARTED com.emailbox_1.0.0 [1117]

Status -> STARTED

Now delete our old services. Goto the Goshell and uninstal the bundles:

Now goto the liferay and check our newly created portlet

Now lets repeat the steps for creating the service-builder from the previous article. But this time create it from da-workspace

File -> New -> Liferay Module Project

Services are created – For details check the previous article

Folder structure for the portlet and the service builder

Add the details as shown in the below screenshots (If any doubt check the previous article).

Do builder service and deploy

Copy this jar files one by one to the server’s deploy folder. First *api.jar and then *service.jar

Server logs:

liferay-ce-portal-tomcat-7.3.0-ga1-20200127150653953/liferay-ce-portal-7.3.0-ga1/osgi/modules][BundleStartStopLogger:39] STARTED com.siteservice.api_1.0.0 [1118]

liferay-ce-portal-7.3.0-ga1/osgi/modules][BundleStartStopLogger:39] STARTED com.siteservice.service_1.0.0 [1119]


Check the database, you can see the Site_ Table and columns are created.

Now add the service builder dependancy to the portlet

Add this two lines in the build.gradle file

Right click on showEmailBox portlet and gradle -> refresh gradle project

DONE! You are successfully binded the service builder to your portlet.

now add the following to your portal class file above the doView function

@Reference
private SiteLocalService _siteLocalService;

now you can use the following default functions provided by liferay on the service.

_siteLocalService.fetchSite(23);
_siteLocalService.createSite(2344);
_siteLocalService.deleteSite(2233);
_siteLocalService.getSitesCount();
_siteLocalService.updateSite(site);

But what is we needed to fetch suppose some sites which has particular site_id Or fetch all sites which has registered after this time etc?

For all these custom query to mysql db, we needed to create a custom finder methods. So lets create one.

Open service.xml of `siteService-service`

Click on Finders and add Name and Type

Click on Finder column and add the db column to find

Click on Source, you can see the finder is added

Double click on the buildService to build the service

Now we can add custom finder findBySiteId to this service.

Open siteLocalServiceImpl.java

package com.siteservice.service.impl;

import com.liferay.portal.aop.AopService;
import com.siteservice.model.Site;
import com.siteservice.service.base.SiteLocalServiceBaseImpl;

import java.util.List;

import org.osgi.service.component.annotations.Component;


@Component(
	property = "model.class.name=com.siteservice.model.Site",
	service = AopService.class
)
public class SiteLocalServiceImpl extends SiteLocalServiceBaseImpl {

	public List<Site> findBySiteId(long site_id) {
		return sitePersistence.findBySiteId(site_id);
	}
}

Now do the buildService for siteService. Then Gradle -> Refresh and deploy the service. Copy this jar files one by one to the server’s deploy folder. First *api.jar and then *service.jar

Refresh Gradle project for the portlet – showEmailBox

Add the following to the doView function of the portlet

Site site = _siteLocalService.findBySiteId(2233).get(0);
		
System.out.println("We got the site: ---------");
System.out.println(site);

and don’t forget to create a site entry in the database with id: 2233

mysql> insert into Site_ (id_, site_id, name, register_from_date, register_to_date, created_at, updated_at) values (1, 2233, 'Site 2020', '2020-01-01', '2020-06-19', CURDATE(), CURDATE());

deploy the portlet and check you are getting the site in the server console.

that’s it for now, will see in the next article.

Liferay 7.3: Configure IDE

Step 1. Download it from

Download from here:
https://liferay.dev/project/-/asset_publisher/TyF2HQPLV1b5/content/ide-installation-instructions

Extract it to your /home directory

Step 2. run the installer

./LiferayProjectSDKwithDevStudioCommunityEdition-201910152009-linux-x64-installer.run

You can see the steps involving the process below.

Select java runtime

After install Open it. Double click on the following file:

~/LiferayProjectSDKwithDevStudioCommunityEdition/liferay-developer-studio/DeveloperStudio

you can see the welcome page. Close it.



Click on Workbench to start on

This is how Liferay Studio Workspace home page look like

You can create liferay plugins / projects etc from here.

Step 3. Click on right corner first button and open perspective.

select Liferay plugins

Open Perspective

Step 4. Left bottom corner there is ‘Servers’ Tab

right click on it and select ‘New’ -> ‘Server’

Select ‘Liferay inc’ -> Liferay 7.x and click next

Add the tomcat server to your SDK to control the server from SDK. This helps you to see the server logs and other live status in IDE

Select the tomcat server path from your downloaded liferay portal

Make sure you are using the timestamp folder in the path of the server, else it not gonna work. It will show you an error like this:

If you start the tomcat server and try to access it without the timestamp PATH

You can add an existing resources created if available like theme etc to the server. If you don’t have any resources don’t worry. We are going to cover this in next chapter.

Click on the server name on the bottom left corner and you can see the configurations of the server.

Server Started. You can see the server logs in the console

ThinkingSphinx::SphinxError (undefined method `next_result’ for Mysql2 (rails 3)

I got this error after my thinking sphinx updation to the newest version of ‘3.0.5’. The error shows in the exact line of the code Model.search in my Rails controller. I updated my mysql2 version to ‘0.3.13’, and the issue is solved.
So in you Gemfile update the mysql2 version:

gem 'mysql2', '0.3.13'

And do

$ bundle install

Rails mysql2 gem getting error checking for mysql_query() in -lmysqlclient… no

Make sure that you have installed mysql server and mysql client. Else install by

$ sudo apt-get install mysql-server
$ sudo apt-get install mysql-client

After that install the dependency library for mysql

$ sudo apt-get install libmysql-ruby libmysqlclient-dev

Can’t login to mysql as a root?

One day when I tried to login to mysql it is not permitting me to login as a root. Then I found the following solution to get rid of from this issue.

First you stop the server:

$ sudo /etc/init.d/mysql stop

Edit the cnf file:

$ sudo vim /etc/my.cnf

> Add the following line to section [mysqld]:
skip-grant-tables

Start Mysql:

$ sudo /etc/init.d/mysql start

Reset Root Password:

$ mysql -e "update user set password = old_password('newpassword') where user = 'root'" mysql

Kill MySQL:

$ sudo kill -9 PROCESS_ID

Create an init-file:

$ vim ~/mysql-init

> Add the following lines:

EOF
UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'@'localhost';
EOF

Run MySQL with the init file:

$ mysqld_safe --init-file=~/mysql-init &

Remove the mysql-init file:

$ rm ~/mysql-init

Restart MySQL:

$ sudo /etc/init.d/mysql restart

Login as root!
References:
1) http://www.ipreferjim.com/2011/06/cant-login-to-mysql-as-root/

Install Rails 3 on Ubuntu 10.04 Lucid Lynx Using RVM with Mysql and sphinx

First install ‘curl’ and ‘git’

  • step 1: install curl
    $ sudo apt-get install curl
  • step 2: install git
    $ sudo apt-get install git-core
  • Then install RVM

  • step 3: install rvm
    $ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
  • step 4: edit .bashrc file to load rvm into the shell session
    $ gedit .bashrc
    Add the following line to the end of the file:
    if [[ -s “$HOME/.rvm/scripts/rvm” ]] ; then source “$HOME/.rvm/scripts/rvm” ; fi
  • step 5 : restart linux
    $ sudo reboot
  • step 6 : type
    $ rvm notes
    In the notes RVM tells you what packages you’re gonna need to install for various flavors of Ruby. Since we’re going with 1.9.2 we want the packages it lists under the MRI & ree section. Let’s install those now.
    $ sudo aptitude install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf
  • Install Ruby 1.9.2

  • step 7 : install ruby 1.9.2 through rvm
    $ rvm list known
    $ rvm install 1.9.2-head
    for 64bit:
    $ rvm install 1.9.2
  • step 8 : make ruby 1.9.2 as default
    $ rvm –default ruby-1.9.2-head
    for 64bit:
    $ rvm –default ruby-1.9.2
    check ruby version
    $ ruby -v
  • Install Rails 3

  • step 9 : Install rails 3
    $ gem install i18n tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler
    $ gem install rails –pre
  • Select your database

  • step 10 : Install database
    For Sqlite3
    $ sudo apt-get install sqlite3 libsqlite3-dev
    $ gem install sqlite3-ruby
    (OR)
    $ sudo apt-get update
    For Mysql goto synaptic package manager and type
    mysql-server, mysql-client and install and set password.
    Install mysql client libraries :
    $ sudo aptitude install libmysqlclient16-dev
    or
    $ sudo apt-get install libmysql-ruby libmysqlclient-dev
  • Install Sphinx

  • If you want sphinx, install Sphinx
    $ wget http://sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz
    $ tar xzvf sphinx-0.9.8.1.tar.gz
    $ cd sphinx-0.9.8.1/
    $ ./configure
    $ make
    $ sudo make install
  • To know about sphinx type
    $ search

If you are getting error when starting sphinx make sure that the ‘indexer’,’searchd’,’search’ is under /usr/local/bin or under where sphinx specifies.
You are now ready to go…