How to implement a method of your own definition in Liferay

Source: Internet
Author: User

We see this article is not very happy ah, I feel is very happy, we can finally follow their own wishes to write a code, in Liferay some basic additions and deletions of the code is automatically generated, but we want to achieve their own method, I am afraid that a little effort, You have to know that every technology is necessary to study and learning, not always feel that the automatic generation of things do not have any good research, in fact, you think of this problem is not, why this framework can automatically generate these methods, this is the technology, as long as you are willing to think, you will have a harvest. All right, that's a lot of crap. Let's start with how we're going to implement our own defined methods today.

1: First of all, in the service of the package under the persistence this package to define the method, this method is not a random definition he has a certain way, first we need to define a class Xxxfinderimpl, we need to write this class (XXX) to represent the entity This class requires extends basepersistenceimpl<xxx> implements Xxxfinder. This time xxxfinder will be wrong, do not let him directly build, Liferay will automatically generate a Xxxfinder interface for you. There will be no error. Then start writing the method you want to

For example

public class Activityfinderimpl extends basepersistenceimpl<activity> implements activityfinder{

@SuppressWarnings ("Unchecked")
Public list<long> findbyquerystring (String queryString, int start, int end) throws SystemException {

list<long> list = null;

Session session = NULL;
try {
Session = Opensession ();

Query q = session.createsqlquery (queryString);
List = (list<long>) queryutil.list (q, Getdialect (), start, end);
} catch (Exception e) {
throw new SystemException (e);
} finally {
if (list = = null) {
List = new arraylist<long> ();
}
CloseSession (session);
}
return list;
}

public void Cacheresult (activity activity) {
Entitycacheutil.putresult (activitymodelimpl.entity_cache_enabled,
Activityimpl.class, Activity.getprimarykey (), activity);
}

public void Cacheresult (list<activity> activitys) {
for (Activity Activity:activitys) {
if (Entitycacheutil.getresult (
Activitymodelimpl.entity_cache_enabled, Activityimpl.class,
Activity.getprimarykey ()) = = null) {
Cacheresult (activity);
}
}
}
/**
* @author Xiongxiao
* @param queryString
* @param start
* @param end
* @return
* @throws SystemException
*/
@SuppressWarnings ("Unchecked")
Public list<activity> findbysqlquerystring (String queryString, int start, int end) throws SystemException {

list<activity> list = null;

Session session = NULL;
try {
Session = Opensession ();
Query q = session.createquery (queryString);
List = (list<activity>) queryutil.list (q, Getdialect (), start, end);
} catch (Exception e) {
throw new SystemException (e);
} finally {
if (list = = null) {
List = new arraylist<activity> ();
}
CloseSession (session);
}
return list;
}
}

The above code: is your own way of writing. Well, where did you write this method? This estimate is a headache for us, so tell you what you need in Service.impl this package will generate Xxxlocalserviceimpl (XXX represents the entity class) After you establish the entity. Recall the method you just wrote in this class. Like what:

public class Activitylocalserviceimpl extends Activitylocalservicebaseimpl {

@Override
Public Activity getactivity (long ActivityID) {
Activity activity = NULL;
try {
Activity = super.getactivity (ActivityID);
} catch (Portalexception e) {
_log.error (e);
} catch (SystemException e) {
_log.error (e);
}
return activity;
}

Public list<long> searchbyquerystring (String queryString, int start, int end)
Throws SystemException {
Return activityfinder.findbyquerystring (queryString, start, end);
}

Public list<activity> searchbysqlquerystring (String queryString, int start,
int end) throws SystemException {
Return activityfinder.findbysqlquerystring (queryString, start, end);
}

A good bottom-up method has been written, so how do we invoke AH in the logical layer. This time the call is also a regular, not you directly find the reference to this class, but through the package Docroot/web-inf/service, In finding Com.xxx.xxx.service This package finds the entity corresponding to the xxxlocalserviceutil of this class (XXX represents the entity) directly calling the name of the method.

Like what:

private void Listactivityfordetails (Portletrequest portletrequest, String relation)
Throws SystemException, NumberFormatException, Portalexception, IOException {
Boolean ishistory = false;
String query = getsqlqueryforactivity (Portletrequest, ishistory, relation);
String Portletid = portletids.activity_list;
list<long> activities = activitylocalserviceutil.searchbyquerystring (query, Queryutil.all_pos, Queryutil.all_pos);
Getdefaultcolumn (Portletrequest, Portletid);
Linkedhashmap<long, map<string, string>> entities = null;
if (activities! = NULL && activities.size () > 0) {
entities = new Linkedhashmap<long, map<string, string>> ();
for (int i = 0; i < activities.size (); i++) {
Long ActivityID = long.valueof (string.valueof (Activities.get (i)));
map<string, string> dynamicvalue = Dynamicdetailentity.getactivityvalue (Portletrequest, activityId);
Entities.put (ActivityID, Dynamicvalue);
}
}
Portletrequest.setattribute ("entities", entities);
}

Well, everyone has learned how to define their own methods, we can try to write.

How to implement a method of your own definition in Liferay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.