Liferay update services.xml file
Liferay example
===============================================================
Generating finder methods using finder tags in service.xml file
===============================================================
1. update the service.xml file with order by clause
< !-- order by -->
< order by=" asc" >
< order-column name=" title" />
< /order>
More on order by (sorting). Taken from DTD file.
< !--
The attributes of the order-column element allows you to fine tune the ordering
of the entity.
For example:
< order by=" asc" >
< order-column name=" parentLayoutId" />
< order-column name=" priority" />
< /order>
The above settings will order by parentLayoutId and then by priority in an
ascending manner.
For example:
< order by=" asc" >
< order-column name=" name" case-sensitive=" false" />
< /order>
The above settings will order by name and will not be case sensitive.
For example:
< order>
< order-column name=" articleId" order-by=" asc" />
< order-column name=" version" order-by=" desc" />
< /order>
The above settings will order by articleId in an ascending manner and then by
version in a descending manner.
-->
2. update the service layer with new finder methods.
< !-- Finder methods -->
< finder name=" BookId" return-type=" Book" >
< finder-column name=" bookId" />
< /finder>
< finder name=" Title" return-type=" Collection" >
< finder-column name=" title" />
< /finder>
< finder name=" Title_Author" return-type=" Collection" >
< finder-column name=" title" />
< finder-column name=" author" />
< /finder>
2. Run " ant build-service" to re-generate the service layer.
You'll find the new finder methods generated.
difference fetchBy and findBy methods.
3. Writing a new method in BookLocalServiceImpl.java to invoke a finder method.
public List< Book> findBooks(String title)
throws PortalException, SystemException, RemoteException {
//return bookPersistence.findByTitle(title);
return BookUtil.findByTitle(title);
}
When to use the persistence object and the interface.
4. update view.jsp
Add a new button " Find Book" and a hidden variable 'cmd'
< input type=" hidden" name=" < portlet:namespace /> cmd" />
< input type=" button" value=" Find Book" onClick=" < portlet:namespace /> findBook();" />
Write new JS function,
function < portlet:namespace /> findBook() {
var frm = document.< portlet:namespace /> fm;
frm.< portlet:namespace /> cmd.value = 'find';
frm.submit();
}
5. updating the AddBookAction.java to invoke the new API.
Add the line,
String cmd = ParamUtil.getString(req, " cmd" );
replace,
BookLocalServiceUtil.create(bookTitle);
with,
if (Validator.isNull(cmd)) {
BookLocalServiceUtil.create(bookTitle);
} else {
List< Book> results = BookLocalServiceUtil.findBooks(bookTitle);
req.setAttribute(" results" , results);
req.setAttribute(" cmd" , cmd);
}
un-comment setForward and comment out res.sendRedirect
6. updating the success.jsp
replace,
List< Book> books = BookLocalServiceUtil.getAllBooks();
with,
List< Book> books = null;
String cmd = ParamUtil.getString(request, " cmd" );
if (Validator.isNotNull(cmd)) {
books = (List)request.getAttribute(" results" );
} else {
books = BookLocalServiceUtil.getAllBooks();
}
7. deploy
verify we've done all the steps properly
ant deploy-fast from ext-web (we ve only modified files view.jsp and success.jsp)
ant deploy from ext-impl
restart the server and confirm the find feature is working perfectely fine.
Congratulations !!!
8. Advantages and dis-advantages of using finder tags.
a. it generated the indices for the database.
b. can only do " EQ" finds
c. " where" attribute can be used.
eg. where=" dateAdded is not NULL"
Liferay example
===============================================================
Generating finder methods using finder tags in service.xml file
===============================================================
1. update the service.xml file with order by clause
< !-- order by -->
< order by=" asc" >
< order-column name=" title" />
< /order>
More on order by (sorting). Taken from DTD file.
< !--
The attributes of the order-column element allows you to fine tune the ordering
of the entity.
For example:
< order by=" asc" >
< order-column name=" parentLayoutId" />
< order-column name=" priority" />
< /order>
The above settings will order by parentLayoutId and then by priority in an
ascending manner.
For example:
< order by=" asc" >
< order-column name=" name" case-sensitive=" false" />
< /order>
The above settings will order by name and will not be case sensitive.
For example:
< order>
< order-column name=" articleId" order-by=" asc" />
< order-column name=" version" order-by=" desc" />
< /order>
The above settings will order by articleId in an ascending manner and then by
version in a descending manner.
-->
2. update the service layer with new finder methods.
< !-- Finder methods -->
< finder name=" BookId" return-type=" Book" >
< finder-column name=" bookId" />
< /finder>
< finder name=" Title" return-type=" Collection" >
< finder-column name=" title" />
< /finder>
< finder name=" Title_Author" return-type=" Collection" >
< finder-column name=" title" />
< finder-column name=" author" />
< /finder>
2. Run " ant build-service" to re-generate the service layer.
You'll find the new finder methods generated.
difference fetchBy and findBy methods.
3. Writing a new method in BookLocalServiceImpl.java to invoke a finder method.
public List< Book> findBooks(String title)
throws PortalException, SystemException, RemoteException {
//return bookPersistence.findByTitle(title);
return BookUtil.findByTitle(title);
}
When to use the persistence object and the interface.
4. update view.jsp
Add a new button " Find Book" and a hidden variable 'cmd'
< input type=" hidden" name=" < portlet:namespace /> cmd" />
< input type=" button" value=" Find Book" onClick=" < portlet:namespace /> findBook();" />
Write new JS function,
function < portlet:namespace /> findBook() {
var frm = document.< portlet:namespace /> fm;
frm.< portlet:namespace /> cmd.value = 'find';
frm.submit();
}
5. updating the AddBookAction.java to invoke the new API.
Add the line,
String cmd = ParamUtil.getString(req, " cmd" );
replace,
BookLocalServiceUtil.create(bookTitle);
with,
if (Validator.isNull(cmd)) {
BookLocalServiceUtil.create(bookTitle);
} else {
List< Book> results = BookLocalServiceUtil.findBooks(bookTitle);
req.setAttribute(" results" , results);
req.setAttribute(" cmd" , cmd);
}
un-comment setForward and comment out res.sendRedirect
6. updating the success.jsp
replace,
List< Book> books = BookLocalServiceUtil.getAllBooks();
with,
List< Book> books = null;
String cmd = ParamUtil.getString(request, " cmd" );
if (Validator.isNotNull(cmd)) {
books = (List)request.getAttribute(" results" );
} else {
books = BookLocalServiceUtil.getAllBooks();
}
7. deploy
verify we've done all the steps properly
ant deploy-fast from ext-web (we ve only modified files view.jsp and success.jsp)
ant deploy from ext-impl
restart the server and confirm the find feature is working perfectely fine.
Congratulations !!!
8. Advantages and dis-advantages of using finder tags.
a. it generated the indices for the database.
b. can only do " EQ" finds
c. " where" attribute can be used.
eg. where=" dateAdded is not NULL"
No comments:
Post a Comment