Finder in Liferay's ServiceBuilder - liferay

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>

Related

UI to manage one to many relationship between classes in custom module in Kentico

I have created a Clinic module within Kentico V12 with two classes: Doctor and Appointment. There is a 1 to many relationship between Doctor and Appointment. How could I display the name and surname of the doctors in the Appointment grid?
<?xml version="1.0" encoding="utf-8" ?>
<grid>
<actions>
<action name="edit" caption="$General.Edit$" fonticonclass="icon-edit" fonticonstyle="allow" />
<action name="#delete" caption="$General.Delete$" fonticonclass="icon-bin" fonticonstyle="critical" confirmation="$General.ConfirmDelete$" />
</actions>
<columns>
<column source="AppointmentPatientFirstName" caption="First name" wrap="false" localize="true">
<filter type="text" size="200" />
</column>
<column source="AppointmentPatientLastName" caption="Last name" wrap="false" localize="true">
<filter type="text" size="200" />
</column>
<column source="AppointmentPatientEmail" caption="Email" wrap="false" localize="true">
<filter type="text" size="200" />
</column>
<column source="AppointmentPatientPhoneNumber" caption="Phone Number" wrap="false" localize="true">
<filter type="text" size="15" />
</column>
<column source="Date" caption="Date" wrap="false" localize="true">
<filter type="text" size="200" />
</column>
<column source="AppointmentPatientBirthDate" caption="BirthDate" wrap="false" localize="true">
<filter type="text" size="200" />
</column>
<column source="AppointmentDoctorID" caption="Doctor ID" wrap="false" localize="true">
<filter type="text" size="200" />
</column>
</columns>
<options>
<key name="DisplayFilter" value="true" />
</options>
</grid>
Kind regards.
Appointment List: https://i.stack.imgur.com/Ft1pE.png
Using this column AppointmentDoctorID modify the XML to use a transformation like so:
<column source="AppointmentDoctorID" caption="Doctor" externalsourcename="#transform: yournamespace.doctor: {%FirstName%} {%LastName%}" wrap="false" localize="true"></column>
Check out more documentation on unigrids in the Kentico Documentation.

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.

Liferay and relationships in it

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.

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>

Sharepoint Custom Field Render pattern: loop through Sub Columns of Multiple Column field

I have a Custom Sharepoint field that inherits from SPFieldMultiColumn. the values of the field appear like this:
";#column one value;#column two
value;#column three value;#"
I want to display each item in a separate line and remove the
;#
I checked this link http://msdn.microsoft.com/en-us/library/ms411957(office.12).aspx
and found that this can be achieved by overriding the render pattern of the field like this
<RenderPattern Name="DisplayPattern" DisplayName="DisplayPattern">
<Switch>
<Expr>
<GetVar Name="FreeForm"/>
</Expr>
<Case Value="TRUE">
<Column/>
</Case>
<Default>
<HTML>
<![CDATA[ <DIV ALIGN=RIGHT> ]]>
</HTML>
<Column/>
<HTML>
<![CDATA[ </DIV> ]]>
</HTML>
</Default>
</Switch>
</RenderPattern>
bu this requires that the number of SubColumns to be predefined, in my case it can be any number.
so I need a for loop to loop through each subcolumn in my multiple column field?
how can this be done ?
thanks
Mina,
I am running out of gas on helping you, here is the full content of my Xml and related class headers for your reference, good luck.
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">CountryStateCityAddress1Field</Field>
<Field Name="TypeDisplayName">CountryStateCityAddress1Field</Field>
<Field Name="TypeShortDescription">CountryStateCityAddress1Field</Field>
<Field Name="ParentType">MultiColumn</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">c0d4e972-cae4-4320-b13b-83ed0bf8cedc</Field>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr>
<Column />
</Expr>
<Case Value="" />
<Default>
<Column SubColumnNumber="0" HTMLEncode="TRUE" />
<HTML><![CDATA[<br/>]]></HTML>
<Column SubColumnNumber="1" HTMLEncode="TRUE" />
<HTML><![CDATA[<br/>]]></HTML>
<Column SubColumnNumber="2" HTMLEncode="TRUE" />
<HTML><![CDATA[<br/>]]></HTML>
</Default>
</Switch>
</RenderPattern>
</FieldType>
<FieldType>
<Field Name="TypeName">EMailFieldTypeField</Field>
<Field Name="TypeDisplayName">EMailFieldTypeField</Field>
<Field Name="TypeShortDescription">EMailFieldTypeField</Field>
<Field Name="ParentType">MultiColumn</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">0e8a370a-0388-4b78-9e5c-ffbb8a481391</Field>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr>
<Column />
</Expr>
<Case Value="" />
<Default>
<Column SubColumnNumber="0" HTMLEncode="TRUE" />
<HTML><![CDATA[<br/>]]></HTML>
<Column SubColumnNumber="1" HTMLEncode="TRUE" />
<HTML><![CDATA[<br/>]]></HTML>
<Column SubColumnNumber="2" HTMLEncode="TRUE" />
<HTML><![CDATA[<br/>]]></HTML>
</Default>
</Switch>
</RenderPattern>
</FieldType>
</FieldTypes>
Headers:
namespace CustomFieldTypeAddress1
{
[CLSCompliant(false)]
[Guid("0e8a370a-0388-4b78-9e5c-ffbb8a481391")]
public class EMailFieldTypeField : SPFieldMultiColumn
...
namespace CustomFieldTypeAddress1
{
[CLSCompliant(false)]
[Guid("dc20d765-2d36-4396-83aa-f063166d8fcc")]
public class EMailFieldTypeFieldControl : BaseFieldControl
...
You could create a custom field control.
See here and here for starting info on how to do this.
This gives you great flexibility in displaying the field, however it does involve more code.
I got the same problem which was caused by ParentType in the Xml file
Before fixing:
<Field Name="ParentType">Text</Field>"
After fixing it:
<Field Name="ParentType">MultiColumn</Field>"
Please refer to http://msdn.microsoft.com/en-us/magazine/dd727508.aspx.
whan SPFieldMultiColumn object use the method override BaseFieldControl FieldRenderingControl it load the object which inherit from the BaseFieldControl,
So, the object which inherit the BaseFieldControl has a method that call
protected override void RenderFieldForDisplay(HtmlTextWriter output){}
in this method you can render you controls that fits you.
You can use the example below
namespace SharePointTestApplication
{
public class UserChoiceColumn:SPFieldMultiColumn
{
public UserChoiceColumn(SPFieldCollection fields, string fname)
: base(fields, fname) { }
public UserChoiceColumn(SPFieldCollection fields, string tname, string dname)
: base(fields, tname, dname) { }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl ctr = new UserChoiceColumnControlType();
ctr.FieldName = this.InternalName;
return ctr;
}
}
public override string GetFieldValueAsHtml(object value)
{
SPFieldMultiColumnValue mcv = new SPFieldMultiColumnValue(value.ToString());
return string.Format("{0} , {1}",mcv[0],mcv[1]);
}
}
public class UserChoiceColumnControlType : BaseFieldControl
{
#region Protected Members
protected TextBox TextBox1;
protected TextBox TextBox2;
#endregion
protected override string DefaultTemplateName
{
get
{
return "UserChoiceColumnTemplate";
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
TextBox1 = (TextBox)this.TemplateContainer.FindControl("TextBox1");
TextBox2 = (TextBox)this.TemplateContainer.FindControl("TextBox2");
}
public override object Value
{
get
{
this.EnsureChildControls();
SPFieldMultiColumnValue mcv = new SPFieldMultiColumnValue(2);
mcv[0] = TextBox1.Text;
mcv[1] = TextBox2.Text;
return mcv;
}
set
{
this.EnsureChildControls();
SPFieldMultiColumnValue mcv = (SPFieldMultiColumnValue)this.ItemFieldValue;
TextBox1.Text = mcv[0];
TextBox2.Text = mcv[1];
}
}
**protected override void RenderFieldForDisplay(HtmlTextWriter output)
{
output.Write("hello world");
// base.RenderFieldForDisplay(output);
}**
}
}

Resources