Liferay and relationships in it - liferay

I have a portlet, which can add/update/delete books and add authors. Moreover, you can choose existing authors when you try to add book.
And now I need to show how many books were written by each author in "author" table. How can I do it? Im a newbie in liferay and I even have no idea.
It's my service.xml
<entity name="Book" local-service="true" remote-service="true" cache-enabled="false">
<column name="bookId" type="long" primary="true" />
<column name="bookName" type="String" />
<column name="bookDescription" type="String" />
<column name="authors" type="Collection" entity="Author" mapping-table="Books_Authors" />
<finder return-type="Collection" name="bookName">
<finder-column name="bookName"></finder-column>
</finder>
</entity>
<entity name="Author" local-service="true" remote-service="true" cache-enabled="false">
<column name="authorId" type="long" primary="true" />
<column name="authorName" type="String" />
<column name="books" type="Collection" entity="Book" mapping-table="Books_Authors" />
</entity>

Service Builder is your friend.
You just need to add a finder in your book entity in service.xml. If your entity has a field named author:
<finder name="Author" return-type="Collection">
<finder-column name="author" />
</finder>
The execution of build-service will generate the methods BookUtil.findByAuthor() and BookUtil.countByAuthor().
You can implement now the corresponding methods in BookLocalServiceImpl, calling the previous, and after another run of build-serivce, they're available in your Util class. Something like
public List<Book> getByAuthor(String author) {
return getPersistence().findByAuthor(author);
}
public int countByAuthor(String author) {
return getPersistence().countByAuthor(author);
}
After the last call to build-service you can call them from your BookLocalServiceUtil.
If you just want the count, don't retrieve all the collection. If there are many records, it's a bad idea. Invoke the count instead.

Related

Liferay JSF service builder configuration error

Am newbie to Liferay. I have created one JSF portlet and added the service builder to it. Using this link Generate a Persistence Framework. After configure a service builder i have generated the source using service builder. But am getting following exception when build service of project.
Exception in thread "main" java.lang.IllegalArgumentException: No entity column exist with column database name guestbookId for entity Guestbook
[echo] at com.liferay.portal.tools.service.builder.ServiceBuilder._getEntityColumnByColumnDBName(ServiceBuilder.java:4242)
[echo] at com.liferay.portal.tools.service.builder.ServiceBuilder._getColumnLengths(ServiceBuilder.java:3857)
[echo] at com.liferay.portal.tools.service.builder.ServiceBuilder._createSQLIndexes(ServiceBuilder.java:3355)
[echo] at com.liferay.portal.tools.service.builder.ServiceBuilder.<init>(ServiceBuilder.java:796)
[echo] at com.liferay.portal.tools.service.builder.ServiceBuilder.main(ServiceBuilder.java:216)
[echo] -Dservice.tpl.spring_xml_session=com/liferay/portal/tools/service/builder/dependencies/spring_xml_session.ftl
[echo] Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512m; support was removed in 8.0
[mkdir] Created dir: D:\liferay\liferay-plugins-sdk-7.0\portlets\jsfguestbook-portlet\docroot\WEB-INF\service-classes
[copy] Copied 9 empty directories to 9 empty directories under D:\liferay\liferay-plugins-sdk-7.0\portlets\jsfguestbook-portlet\docroot\WEB-INF\service-classes
[javac] Compiling 38 source files to D:\liferay\liferay-plugins-sdk-7.0\portlets\jsfguestbook-portlet\docroot\WEB-INF\service-classes
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[jar] Building jar: D:\liferay\liferay-plugins-sdk-7.0\portlets\jsfguestbook-portlet\docroot\WEB-INF\lib\jsfguestbook-portlet-service.jar
[delete] Deleting directory D:\liferay\liferay-plugins-sdk-7.0\portlets\jsfguestbook-portlet\docroot\WEB-INF\service-classes
BUILD SUCCESSFUL
Total time: 8 seconds
Also after built the service i have added the code as per article. I got no method is configured for following code.
public Guestbook getFirstGuestbookByName(long groupId, String name) throws SystemException {
Guestbook guestbook = null;
List<Guestbook> guestbooks = guestbookPersistence.findByName(groupId, name); // This method is not exist.
if (guestbooks != null && guestbooks.size() > 0) {
guestbook = guestbooks.get(0);
}
return guestbook;
}
My service.xml configuration is follows
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd">
<service-builder package-path="com.liferay.docs.guestbook">
<author>muthuvignesh.k</author>
<namespace>GB</namespace>
<entity name="Guestbook" local-service="true" uuid="true">
<!-- PK fields -->
<column name="guestbookId" type="long" primary="true"></column>
<!-- Group instance -->
<column name="groupId" type="long"></column>
<!-- Audit fields -->
<column name="companyId" type="long"></column>
<column name="userId" type="long"></column>
<column name="userName" type="String"></column>
<column name="createDate" type="Date"></column>
<column name="modifiedDate" type="Date"></column>
<column name="name" type="String"></column>
<finder name="GroupId" return-type="Collection">
<finder-column name="groupId"></finder-column>
</finder>
</entity>
<entity name="Entry" local-service="true" uuid="true">
<!-- PK fields -->
<column name="entryId" type="long" primary="true"></column>
<!-- Group instance -->
<column name="groupId" type="long"></column>
<!-- Audit fields -->
<column name="companyId" type="long"></column>
<column name="userId" type="long"></column>
<column name="userName" type="String"></column>
<column name="createDate" type="Date"></column>
<column name="modifiedDate" type="Date"></column>
<column name="name" type="String"></column>
<column name="email" type="String"></column>
<column name="message" type="String"></column>
<column name="guestbookId" type="long"></column>
<finder name="G_G" return-type="Collection">
<finder-column name="groupId"></finder-column>
<finder-column name="guestbookId"></finder-column>
</finder>
</entity>
<exceptions>
<exception>GuestbookName</exception>
<exception>EntryName</exception>
<exception>EntryMessage</exception>
<exception>EntryEmail</exception>
</exceptions>
Since i have using Liferay 7.0 for this. How to overcome this.
I had a same error in 6.2. It seems that the error was in the WEB-INF/sql/indexes.sql file. An index entry was using the field that the service builder could not find (but existed in the service.xml).
I think (but I couldn't verify it) the problem was occurred by an older version of liferay maven plugin (6.2.0-ga1). By deleting the file and upgrading to version 6.2.5, service builder generated a different indexes.sql and the build was successful.
Service Builder
The issue appears to be in your service builder.
1.What stands out the most is your malformed XML. You need to close the tag.
2.Secondly in your Java code you are trying to access a finder that doesn't exist. The only finder you have defined in your hibernate file is groupId. It would look something like
return guestbookPersistence.findByGroupId(groupId);
3.To create a groupid and name find it would look similar to your entry finder.
<finder name="G_N" return-type="Collection">
<finder-column name="groupId"></finder-column>
<finder-column name="name"></finder-column>
</finder>
and your java code
return guestbookPersistence.findByG_N(groupId, name);
service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd">
<service-builder package-path="com.latham.data">
<author>muthuvignesh.k</author>
<namespace>GB</namespace>
<entity name="Guestbook" local-service="true" uuid="true">
<!-- PK fields -->
<column name="guestbookId" type="long" primary="true"></column>
<!-- Group instance -->
<column name="groupId" type="long"></column>
<!-- Audit fields -->
<column name="companyId" type="long"></column>
<column name="userId" type="long"></column>
<column name="userName" type="String"></column>
<column name="createDate" type="Date"></column>
<column name="modifiedDate" type="Date"></column>
<column name="name" type="String"></column>
<finder name="GroupId" return-type="Collection">
<finder-column name="groupId"></finder-column>
</finder>
<finder name="G_N" return-type="Collection">
<finder-column name="groupId"></finder-column>
<finder-column name="name"></finder-column>
</finder>
</entity>
<entity name="Entry" local-service="true" uuid="true">
<!-- PK fields -->
<column name="entryId" type="long" primary="true"></column>
<!-- Group instance -->
<column name="groupId" type="long"></column>
<!-- Audit fields -->
<column name="companyId" type="long"></column>
<column name="userId" type="long"></column>
<column name="userName" type="String"></column>
<column name="createDate" type="Date"></column>
<column name="modifiedDate" type="Date"></column>
<column name="name" type="String"></column>
<column name="email" type="String"></column>
<column name="message" type="String"></column>
<column name="guestbookId" type="long"></column>
<finder name="G_G" return-type="Collection">
<finder-column name="groupId"></finder-column>
<finder-column name="guestbookId"></finder-column>
</finder>
</entity>
<exceptions>
<exception>GuestbookName</exception>
<exception>EntryName</exception>
<exception>EntryMessage</exception>
<exception>EntryEmail</exception>
</exceptions>
</service-builder>
The tutorial you linked to is for Liferay 6.2 but your document definition is for Liferay 7. There were some very big changes between 6.2 and 7. Your code seems to be compatible with both however you should still keep that in mind.

Finder in Liferay's ServiceBuilder

I know I had already asked this question, but I have misunderstandings yet.
My previous question:
Liferay and relationships in it
In two words:
I have a portlet, which can add/update/delete books and add authors. Moreover, you can choose existing authors when you try to add book.
http://i.stack.imgur.com/vzUjn.png
And now I need to show how many books were written by each author in "author" table.
My service.xml:
<entity name="Book" local-service="true" remote-service="true" cache-enabled="false">
<column name="bookId" type="long" primary="true" />
<column name="bookName" type="String" />
<column name="bookDescription" type="String" />
<column name="authors" type="Collection" entity="Author" mapping-table="Books_Authors" />
<finder return-type="Collection" name="bookName">
<finder-column name="bookName"></finder-column>
</finder>
</entity>
<entity name="Author" local-service="true" remote-service="true" cache-enabled="false">
<column name="authorId" type="long" primary="true" />
<column name="authorName" type="String" />
<column name="books" type="Collection" entity="Book" mapping-table="Books_Authors" />
</entity>
What finder should I create to achieve my goal? If I create bookName finder Im able to count how many different books I have. If I create authorName finder Im able to count how many authors I have. Im a little lost.
Thank you for your help, but I still have some questions:
How and where can I get authorName with authorId?
How can I use my count variable in my table in view.jsp?
long count = BookLocalServiceUtil.countByAuthor(authorId);
public void addBook(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
String bookName = ParamUtil.getString(actionRequest,"bookName");
String bookDescription = ParamUtil.getString(actionRequest, "bookDescription");
Long authorId = ParamUtil.getLong(actionRequest, "author");
try {
Book book = BookLocalServiceUtil.createBook(CounterLocalServiceUtil.increment());
book.setBookName(bookName);
book.setBookDescription(bookDescription);
book.setAuthorId(authorId);
book=BookLocalServiceUtil.addBook(book);
String author = ParamUtil.getString(actionRequest, "authorId");
} catch (Exception e) {
log.info(ADD_BOOK_ERROR, e);
SessionErrors.add(actionRequest, "PortalExceptionError");
}
}
In-case, if your Book can have only one Author (many-to-one), then following entity structure will work for you:
service.xml
<entity name="Book" local-service="true" remote-service="true" cache-enabled="false">
<column name="bookId" type="long" primary="true"></column>
<column name="bookName" type="String"></column>
<column name="bookDescription" type="String"></column>
<column name="authorId" type="long"></column>
<finder return-type="Collection" name="Author">
<finder-column name="authorId"></finder-column>
</finder>
</entity>
<entity name="Author" local-service="true" remote-service="true" cache-enabled="false">
<column name="authorId" type="long" primary="true"></column>
<column name="authorName" type="String"></column>
</entity>
On successful build, above finder will generate two methods findByAuthor(long authorId) and countByAuthor(long authorId) in BookUtil. Then, you can implement these methods in BookLocalServiceImpl as following:
BookLocalServiceImpl:
public List<Book> findByAuthor(long authorId) {
try {
return BookUtil.findByAuthor(authorId);
} catch (Exception ex) {}
return null;
}
public int countByAuthor(long authorId) {
try {
return BookUtil.countByAuthor(authorId);
} catch (Exception ex) {}
return 0;
}
And on building service once again, you can use these methods in your action class and on view from BookLocalServiceUtil.
Also, you are using JSTL on your views, you need to add dependencies of the jars in liferay-plugin-package.properties as following:
liferay-plugin-package.properties:
portal-dependency-jars=\
jstl-api.jar,\
jstl-impl.jar
Your Questions:
How and where can I get authorName with authorId?
<liferay-ui:search-container>
<liferay-ui:search-container-results results="${bookListArray}" />
<liferay-ui:search-container-row className="builder.model.Book"
keyProperty="bookId" modelVar="aBook">
<liferay-ui:search-container-column-text property="bookName"
name="book-Name" />
<liferay-ui:search-container-column-text property="bookDescription"
name="description" />
<%
Author bookAuthor = AuthorLocalServiceUtil.getAuthor(aBook.getAuthorId());
%>
<liferay-ui:search-container-column-text name="Author"
value="<%=bookAuthor.getAuthorName() %>" />
<liferay-ui:search-container-column-jsp path="/html/actionBook.jsp" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
How can I use my count variable in my table in view.jsp?
<liferay-ui:search-container>
<liferay-ui:search-container-results results="${authorListArray}" />
<liferay-ui:search-container-row className="builder.model.Author"
keyProperty="authorId" modelVar="aAuthor">
<liferay-ui:search-container-column-text property="authorName"
name="author-Name" />
<%
int count = BookLocalServiceUtil.countByAuthor(aAuthor.getAuthorId());
%>
<liferay-ui:search-container-column-text name="count"
value="<%=String.valueOf(count) %>" />
<liferay-ui:search-container-column-jsp path="/html/actionAuthor.jsp" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>

How to create listerners for Custom entity in liferay?

I have created custom entity employee. Now I want to add listeners for this entity so that I can track add/edit/delete employee operations.
In Liferay for portal enitites like Blogs, Group, User etc we can add properties like
value.object.listener.com.liferay.portal.model.Group=com.smb.test.hook.listeners.GroupListener
in portal.properties via hook-plugin.
But for custom entity this approach does not seem to work.
Any help would be appreciated.
For custom entity, we need to add the listener property in service-ext.properties instead of the portal.properties file.
For my employee entity I have added following property in service-ext.properties file:
value.object.listener.com.smb.employee.model.Employee=com.smb.employee.hook.listeners.EmployeeListener
Note: We need to manually create service-ext.properties file in src folder besides the service.properties file. We could have updated service.properties but since it auto-generates our changes would be lost and hence service-ext.properties is the correct liferay approach.
Here is the location of the service-ext.properties file:
Here is my service.xml:
<service-builder package-path="com.smb.employee">
<author>Suyash</author>
<namespace>smb</namespace>
<entity name="Employee" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="fooId" type="long" primary="true" />
<!-- Audit fields -->
<column name="companyId" type="long" />
<column name="userId" type="long" />
<column name="userName" type="String" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />
<!-- Other fields -->
<column name="field1" type="String" />
<column name="field2" type="boolean" />
<column name="field3" type="int" />
<column name="field4" type="Date" />
<column name="field5" type="String" />
<!-- Order -->
<order by="asc">
<order-column name="field1" />
</order>
<!-- Finder methods -->
<finder name="Field2" return-type="Collection">
<finder-column name="field2" />
</finder>
</entity>
</entity>
</service-builder>

NHibernate: Two foreign keys in the same table against same column

I am working on a consignment booking software.
The consignment object has the following structure:
public class Consignment
{
//Other properties
public virtual Station FromStation{get;set;}
public virtual Station ToStation{get;set;}
}
and here is the station object:
public class Station
{
public virtual int StationId{get;set;}
public virtual string StationName{get;set;}
//I don't want this property but I have to keep it.
public virtual Iesi.Collections.Generic.ISet<Consginment> ConsginmentFrom
{
get;
set;
}
//I don't want this property but I have to keep it here.
public virtual Iesi.Collections.Generic.ISet<Consginment> ConsginmentTo
{
get;
set;
}
}
In the database side, there would be a Station table, and the Consignment table would have two int columns (called FromStation and ToStation), storing the ids of the station.
I am not much of an NHibernate guy, after googling and reading for half a day I came up with the following Mapping Files:
Station.hbm.xml
<class name="Station" table="Station">
<id name="StationId" >
<column name="STATION_ID" not-null="true" />
<generator class="identity" />
</id>
<property name="StationName" >
<column name="STATION_NAME" not-null="true" />
</property>
<set name="ConsginmentFrom" inverse="true" lazy="true" generic="true">
<key>
<column name="StationId" />
</key>
<one-to-many class="Consignment" />
</set>
<set name="ConsignmentTo" inverse="true" lazy="false" generic="true">
<key>
<column name="StationId" />
</key>
<one-to-many class="Consignment" />
</set>
</class>
Consignment.hbm.xml
<class name="Consignment" abstract="true"
table="Consignment" lazy="false">
<id name="ConsignmentId">
<generator class="hilo"/>
</id>
<!--Column mapping for other properties-->
<many-to-one name="FromStation" class="Station">
<column name="STATION_ID" not-null="true" />
</many-to-one>
<many-to-one name="ToStation" class="Station">
<column name="STATION_ID" not-null="true" />
</many-to-one>
</class>
But the above is generating strange DB structure.
I have to do it xml mapping files as a lot has already been written in xml.
Am I doing it correctly? Can there be a better way?
Thanks for reading this.
There are a few things I can see wrong with the mappings.
The FromStation and ToStation properties map to the same column in the Consignment table. They should map to different columns such as FROM_STATION_ID and TO_STATION_ID:
<many-to-one name="FromStation" class="Station">
<column name="FROM_STATION_ID" not-null="true" />
</many-to-one>
<many-to-one name="ToStation" class="Station">
<column name="TO_STATION_ID" not-null="true" />
</many-to-one>
The Set for ConsignmentFrom and ConsignmentTo in Station maps to StationID instead of Station_Id. Also you need to use the FROM_STATION_ID AND TO_STATION_ID as the key columns.
<set name="ConsignmentFrom" inverse="true" lazy="true" generic="true">
<key column="FROM_STATION_ID" />
<one-to-many class="Consignment" />
</set>
<set name="ConsignmentTo" inverse="true" lazy="false" generic="true">
<key colum="TO_STATION_ID" />
<one-to-many class="Consignment" />
</set>
Also consignment is misspelt in some places which may also cause some confusion.

NLog code line logging

I am using NLog to log error messages into csv:
<target name="csv" xsi:type="File"
fileName="${basedir}/Logs/log.csv"
archiveFileName="${basedir}/Logs/Archives/log.{#####}.csv"
archiveAboveSize="10240"
archiveNumbering="Sequence"
concurrentWrites="true"
keepFileOpen="false">
<layout xsi:type="CSVLayout" delimiter="Comma" withHeader="false">
<column name="time" layout="${longdate}" />
<column name="message" layout="${message}" />
<column name="logger" layout="${logger}"/>
<column name="level" layout="${level}"/>
</layout>
</target>
How can I add a column to track the code line number?
Thank you.
Try to add ${callsite} layout renderer.
<column name="source" layout="${callsite:fileName=true}" />

Resources