custom-sql 在 liferay 中的现有表上

custom-sql on existing tables in liferay

我需要对 liferay 数据库进行自定义查询,包括 JOIN 等

我的研究让我使用了 Service Builder 策略。 但是当我阅读有关服务构建器的信息时,您需要在 service.xml 文件中定义实体。我不需要实体,我愿意使用现有实体,例如 JournalArcticle 等

如果有人能告诉我正确的方法,我将不胜感激。

我需要使用的查询:

SELECT ja.* 
FROM liferay.journalarticle ja
INNER JOIN liferay.assetentry ae
  ON ja.resourcePrimKey = ae.classPK
INNER JOIN liferay.assetentries_assettags ae_at
  ON ae.entryId = ae_at.entryId
INNER JOIN liferay.assettag tags
  ON ae_at.tagId = tags.tagId
where tags.name = "actualités de recherche"            
;

JournalArticle, assetentries_assettags, AssetTag, AssetEntry 都是遗留的 liferay 实体。

我正在使用 liferay 6.1.1-GA2

我读到: http://www.liferay.com/fr/documentation/liferay-portal/6.1/development/-/ai/define-object-relational-maps-liferay-portal-6-1-dev-guide-en

而且: http://www.liferay.com/fr/web/sten.martinez/blog/-/blogs/using-a-legacy-db-with-service-builder

基本上,我遵循服务构建器规则并使其适配maven环境。

Colored version of the post: http://blog.melard.fr/post/123556374437/liferay-implementing-your-custom-service-using

 Environnement

------------------------------------
liferay: liferay-portal-6.1.1-ce-ga2
------------------------------------
PAY ATTENTION

If you define a variable hot.deploy.listeners in your portal-ext.properties file, be sur to add the com.liferay.portal.deploy.hot.SpringHotDeployListener class or you might get a BeanLocator is not set error.

I’ve been studying the way to implement a custom service using custom sql based on existing entities.

Case study

I need to retrieve the last version of JournalArticles using pagination adn tag name.

References

blog

Get last version of last journal articles

The answer from that blog entry is to retrieve all the articles and then for each article check if it is the last version of the article using that method:

if(JournalArticleLocalServiceUtil.isLatestVersion(groupId,journalobj.getArticleId(),journalobj.getVersion())){
// here logic will come or you can create another list to add those articles with latest version
}
Determine latest version of Web Content in Liferay

This is another solution, worse than the one mentionned before as it retrieve all the articles and then for each article, check if the articleId has been already loaded.

List articles = JournalArticleLocalServiceUtil.getStructureArticles(GROUPID, STRUCTUREID);
ListIterator it = articles.listIterator();
List checkedArticleIds = new ArrayList();

while (it.hasNext()) {
  JournalArticle article = it.next();

  if (checkedArticleIds.contains(article.getArticleId())) {
    continue; // previous article version already checked
  }

  JournalArticle articleLastVersion = JournalArticleLocalServiceUtil.getLatestArticle(GROUPID, article.getArticleId());

  checkedArticleIds.add(article.getArticleId());

  System.out.println("Added articleId " + article.getArticleId() + " with version " + article.getVersion());
}
Find Liferay Latest Journal Atticles By Tag Name

    public List getArticlesByTags(long groupId, String tagName)
            throws PortalException, SystemException {
        AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
        long[] anyTagIds = AssetTagLocalServiceUtil.getTagIds(groupId, new String[]{"alllocation", tagName});
        assetEntryQuery.setAnyTagIds(anyTagIds);
        List assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);
        List journalArticleList = new ArrayList();
        for (AssetEntry ae : assetEntryList) {
            JournalArticleResource journalArticleResourceObj = JournalArticleResourceLocalServiceUtil
                    .getJournalArticleResource(ae.getClassPK());
            JournalArticle journalArticleObj = JournalArticleLocalServiceUtil.getArticle(groupId, 
                    journalArticleResourceObj.getArticleId());
            journalArticleList.add(journalArticleObj);
        }
        return journalArticleList;
    }
The first article entries are not possible because they retrieve all the articles in the database and then process them in order to get the latest versions of each article. It is possible for small entry sets. But when you get to thousands of articles, it will get very slow.

I like better the third article because you only get a limited amount or articles

Implement a custom service using Service Builder

I wanted to use a single method to get last article version by tag name using an SQL Query and let the database handle the joins

SELECT ja.* 
FROM JournalArticle as ja 
JOIN ( 
SELECT SUBSTRING_INDEX(GROUP_CONCAT(uuid_ ORDER BY version DESC),',',1) AS uuid_LastVersion 
FROM JournalArticle ja
INNER JOIN assetentry ae
  ON ja.resourcePrimKey = ae.classPK
INNER JOIN AssetEntries_AssetTags ae_at
  ON ae.entryId = ae_at.entryId
INNER JOIN AssetTag tags
  ON ae_at.tagId = tags.tagId
where tags.name = ?            
GROUP BY articleId
ORDER BY articleId DESC
) as uuids
    ON ja.uuid_ = uuids.uuid_LastVersion
After doing a lot of research, I found out that Service Builder was the way to go. I read the manual.

Why all that fuss around ORM, I did not want to create a new table, what I need is to access existing entities, remember? And I am not using Ant, who is using Ant nowadays? I am using Maven. So I had to understand how to create the sevice using maven and then where do I needed to put my classes in order to get my service up and running.

Build a service

In order to build a service, you can use a maven archetype.

mvn archetype:generate
then choose the option: com.liferay.maven.archetypes:liferay-servicebuilder-archetype (Provides an archetype to create Liferay Service Builder portlets.)

The Lifferay version: 2: 6.1.1

Define value for property 'groupId': : com.my.cms
Define value for property 'artifactId': : service
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  com.my.cms: :
The archetype then generate some code and modify the pom.xml of the current directory if any

directory:

pom.xml
service
├── pom.xml
├── service-portlet
│   ├── pom.xml
│   └── src
│       └── main
│           ├── resources
│           │   └── portlet.properties
│           └── webapp
│               ├── css
│               │   └── main.css
│               ├── icon.png
│               ├── js
│               │   └── main.js
│               ├── view.jsp
│               └── WEB-INF
│                   ├── liferay-display.xml
│                   ├── liferay-plugin-package.properties
│                   ├── liferay-portlet.xml
│                   ├── portlet.xml
│                   ├── service.xml
│                   └── web.xml
└── service-portlet-service
    └── pom.xml The pom.xml on the parent directory is modified to add the “service” module. Be carreful to modify it if you already have a “modules” tag in order to add the “service” module in that tag.
  <modules>
    <module>service
  </modules> As you can see, the Service Builder generated two portlets:
service-portlet
service-portlet-service
The service-portlet is a WAR containing the service to be deployed on you application server.

The service-portlet-service is a JAR containing the classes to be deployed in your portlet on the application server.

Implementation

service.xml

The first thing is to modify the service.xml file in order to have Service Builder to create all the stubs you need to register your service in liferay.

<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<!-- service/service-portlet/src/main/webapp/WEB-INF/service.xml -->
<service-builder package-path="com.my.cms">
        <namespace>service
        <entity name="MyService" local-service="true" remote-service="true">
        </entity>
</service-builder>
As you can see, I removed all the content of the entity tag, changed the name attribute to “MyService” instead of “Foo” and removed the uuid attribute.

Now, run service builder

 cd service
 mvn liferay:build-service
[INFO] Reactor Summary:
[INFO]
[INFO] service ........................................... SUCCESS [0.538s]
[INFO] service Portlet Service ........................... SUCCESS [0.299s]
[INFO] service Portlet ................................... SUCCESS [0.322s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
you can see the directory structure as some new files:

├── pom.xml
├── service-portlet
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── my
│           │           └── cms
│           │               └── service
│           │                   ├── base
│           │                   │   ├── MyServiceLocalServiceBaseImpl.java
│           │                   │   ├── MyServiceLocalServiceClpInvoker.java
│           │                   │   ├── MyServiceServiceBaseImpl.java
│           │                   │   └── MyServiceServiceClpInvoker.java
│           │                   ├── http
│           │                   │   └── MyServiceServiceSoap.java
│           │                   └── impl
│           │                       ├── MyServiceLocalServiceImpl.java
│           │                       └── MyServiceServiceImpl.java
│           ├── resources
│           │   ├── META-INF
│           │   │   ├── base-spring.xml
│           │   │   ├── cluster-spring.xml
│           │   │   ├── dynamic-data-source-spring.xml
│           │   │   ├── hibernate-spring.xml
│           │   │   ├── infrastructure-spring.xml
│           │   │   ├── portlet-hbm.xml
│           │   │   ├── portlet-model-hints.xml
│           │   │   ├── portlet-orm.xml
│           │   │   ├── portlet-spring.xml
│           │   │   └── shard-data-source-spring.xml
│           │   ├── portlet.properties
│           │   └── service.properties
│           └── webapp
│               ├── css
│               │   └── main.css
│               ├── icon.png
│               ├── js
│               │   ├── main.js
│               │   └── service.js
│               ├── view.jsp
│               └── WEB-INF
│                   ├── liferay-display.xml
│                   ├── liferay-plugin-package.properties
│                   ├── liferay-portlet.xml
│                   ├── portlet.xml
│                   ├── service.xml
│                   ├── service.xml~
│                   ├── sql
│                   │   ├── indexes.properties
│                   │   ├── indexes.sql
│                   │   ├── sequences.sql
│                   │   └── tables.sql
│                   └── web.xml
└── service-portlet-service
    ├── pom.xml
    └── src
        └── main
            └── java
                └── com
                    └── my
                        └── cms
                            └── service
                                ├── ClpSerializer.java
                                ├── messaging
                                │   └── ClpMessageListener.java
                                ├── MyServiceLocalService.java
                                ├── MyServiceLocalServiceClp.java
                                ├── MyServiceLocalServiceUtil.java
                                ├── MyServiceLocalServiceWrapper.java
                                ├── MyServiceService.java
                                ├── MyServiceServiceClp.java
                                ├── MyServiceServiceUtil.java
                                └── MyServiceServiceWrapper.java

27 directories, 48 files
Now, everything is in place to add our custom sql. you can read about custom-sql in the manual

Custom-SQL

In order to use custom SQL you need to add a file containing your SQL queries, then to add a persistence class that will call execute that query. And call the method in a sevice util class.

SQL File

The file must be called custom-sql/default.xml and contain the SQL in a CDATA section:

<custom-sql>
    <sql id="fr.meteo.cms.service.persistence.MeteoJournalArticleFinder.findLatestArticleByTagName">
        <![CDATA[
SELECT ja.*
FROM mf3_liferay.journalarticle AS ja
JOIN (
  SELECT SUBSTRING_INDEX(GROUP_CONCAT(uuid_ ORDER BY version DESC),',',1) AS uuid_LastVersion
  FROM mf3_liferay.journalarticle ja
  INNER JOIN mf3_liferay.assetentry ae
    ON ja.resourcePrimKey = ae.classPK
  INNER JOIN mf3_liferay.assetentries_assettags ae_at
    ON ae.entryId = ae_at.entryId
  INNER JOIN mf3_liferay.assettag tags
    ON ae_at.tagId = tags.tagId
  WHERE tags.name = ?
  GROUP BY articleId
  ORDER BY articleId DESC
) AS uuids
ON ja.uuid_ = uuids.uuid_LastVersion
        ]]>
    </sql>
</custom-sql> Two points of attention here :
The file needs to be put in the portlet resource dir: service/service-portlet/src/main/resources/custom-sql/default.xml
The id attribute must contain the correct package name fr.meteo.cms.service.persistence.MeteoJournalArticleFinder + a unique name for the query: findLatestArticleByTagName
Persistence class

You need to add an implementation of the persistence class, the class that will implement the execution of the SQL query. The class need to be in that package: com.my.cms.service.persistence and in that path: service\service-portlet\src\main\java\com\my\cms\service\persistence\MyServiceFinderImpl.java

package com.my.cms.service.persistence;

import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.portlet.journal.model.JournalArticle;

public class MyServiceFinderImpl extends BasePersistenceImpl {
}
You need to pay attention to the name of the class, it must be MyServiceFinderImpl or liferay will not recognize it! and it must be in the right package.
When you have put the file in the persistence forlder in the portlet package, you need to run Service Builder again to get the stubs file generated.

mvn liferay:build-service
service
├── pom.xml
├── service.iml
├── service-portlet
│   ├── pom.xml
│   ├── service-portlet.iml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── my
│           │           └── cms
│           │               └── service
│           │                   ├── base
│           │                   │   ├── MyServiceLocalServiceBaseImpl.java
│           │                   │   ├── MyServiceLocalServiceClpInvoker.java
│           │                   │   ├── MyServiceServiceBaseImpl.java
│           │                   │   └── MyServiceServiceClpInvoker.java
│           │                   ├── http
│           │                   │   └── MyServiceServiceSoap.java
│           │                   ├── impl
│           │                   │   ├── MyServiceLocalServiceImpl.java
│           │                   │   └── MyServiceServiceImpl.java
│           │                   └── persistence
│           │                       └── MyServiceFinderImpl.java
│           ├── resources
│           │   ├── custom-sql
│           │   │   └── default.xml
│           │   ├── META-INF
│           │   │   ├── base-spring.xml
│           │   │   ├── cluster-spring.xml
│           │   │   ├── dynamic-data-source-spring.xml
│           │   │   ├── hibernate-spring.xml
│           │   │   ├── infrastructure-spring.xml
│           │   │   ├── portlet-hbm.xml
│           │   │   ├── portlet-model-hints.xml
│           │   │   ├── portlet-orm.xml
│           │   │   ├── portlet-spring.xml
│           │   │   └── shard-data-source-spring.xml
│           │   ├── portlet.properties
│           │   └── service.properties
│           └── webapp
│               ├── css
│               │   └── main.css
│               ├── icon.png
│               ├── js
│               │   ├── main.js
│               │   └── service.js
│               ├── view.jsp
│               └── WEB-INF
│                   ├── liferay-display.xml
│                   ├── liferay-plugin-package.properties
│                   ├── liferay-portlet.xml
│                   ├── portlet.xml
│                   ├── service.xml
│                   ├── service.xml~
│                   ├── sql
│                   │   ├── indexes.properties
│                   │   ├── indexes.sql
│                   │   ├── sequences.sql
│                   │   └── tables.sql
│                   └── web.xml
└── service-portlet-service
    ├── pom.xml
    ├── service-portlet-service.iml
    └── src
        └── main
            └── java
                └── com
                    └── my
                        └── cms
                            └── service
                                ├── ClpSerializer.java
                                ├── messaging
                                │   └── ClpMessageListener.java
                                ├── MyServiceLocalService.java
                                ├── MyServiceLocalServiceClp.java
                                ├── MyServiceLocalServiceUtil.java
                                ├── MyServiceLocalServiceWrapper.java
                                ├── MyServiceService.java
                                ├── MyServiceServiceClp.java
                                ├── MyServiceServiceUtil.java
                                ├── MyServiceServiceWrapper.java
                                └── persistence
                                    ├── MyServiceFinder.java
                                    └── MyServiceFinderUtil.java

30 directories, 55 files liferay added or modified those files:
service/service-portlet/src/main/webapp/WEB-INF/sql/tables.sql
service/service-portlet/src/main/resources/service.properties
service/service-portlet/src/main/java/com/my/cms/service/base/MyServiceLocalServiceClpInvoker.java
service/service-portlet/src/main/java/com/my/cms/service/base/MyServiceServiceBaseImpl.java
service/service-portlet/src/main/java/com/my/cms/service/base/MyServiceServiceClpInvoker.java
service/service-portlet/src/main/resources/META-INF/portlet-spring.xml
service/service-portlet/src/main/java/com/my/cms/service/base/MyServiceLocalServiceBaseImpl.java
service/service-portlet-service/src/main/java/com/my/cms/service/persistence/MyServiceFinder.java
service/service-portlet-service/src/main/java/com/my/cms/service/persistence/MyServiceFinderUtil.java
You can now implements the method findLatestArticleByTagName in the MyServiceFinderImpl file
package com.my.cms.service.persistence;

import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
import com.liferay.portal.kernel.dao.orm.*;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.portlet.journal.model.JournalArticle;
import com.liferay.util.dao.orm.CustomSQLUtil;

import java.util.List;

public class MyServiceFinderImpl extends BasePersistenceImpl implements MyServiceFinder {

    public List findLatestArticleByTagName(
            String tagName,
            int begin, int end) {

        Session session = null;
        try {
            SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
            session = sessionFactory.openSession();

            String sql = CustomSQLUtil.get(
                    FIND_BY_LATEST_ARTICLES_BY_TAGNAME);

            SQLQuery q = session.createSQLQuery(sql);
            q.setCacheable(false);

            q.addEntity("JournalArticle_JournalArticle", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portlet.journal.model.impl.JournalArticleImpl"));

            QueryPos qPos = QueryPos.getInstance(q);
            qPos.add(tagName);

            return (List) QueryUtil.list(q, getDialect(), begin, end);
        } catch (Exception e) {
            try {
                throw new SystemException(e);
            } catch (SystemException se) {
                se.printStackTrace();
            }
        } finally {
            closeSession(session);
        }

        return null;
    }

    public static final String FIND_BY_LATEST_ARTICLES_BY_TAGNAME =
            MyServiceFinder.class.getName() +
                    ".findLatestArticleByTagName";
}
I you want to use an existing session and connect your service to it, you can use that line of code:
    SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory"); You can find the entity you are looking for by using the class loader:
    PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portlet.journal.model.impl.JournalArticleImpl") Then launch again the Service Builder in order to propagate the findLatestArticleByTagName method
mvn liferay:build-service You need now to add the method in the com.my.cms.service.impl.MyServiceLocalServiceImpl class in order to use it.
    public List findLatestArticleByTagName(
            String tagName,
            int begin, int end) {
        return MyServiceFinderUtil.findLatestArticleByTagName(tagName, begin, end);
    }
Then launch again the Service Builder in order to propagate the findLatestArticleByTagName method in different classes.
mvn liferay:build-service
Use the service in a portlet

In order to use the service in the portlet, you need to do some configuration first.

Add the dependency to the Service’s portlet in liferay(service/service-portlet/src/main/webapp/WEB-INF/liferay-plugin-package.properties), this will ensure that the portlet using the service will not be deployed before the service:
required-deployment-contexts=service-portlet
Add the dependency to the Service’s portlet in maven in order to get the JAR file:
        <dependency>
            <groupId>com.my.cms</groupId>
            <artifactId>service-portlet-service</artifactId>
            <version>${pom.version}</version>
        </dependency> Rebuild and redeploy the service in liferay application server.
You can test your service in your portlet using the Util class:

            List journalArticles = MyServiceLocalServiceUtil.findLatestArticleByTagName("actualités de recherche", currentPage, currentPage + 8);