Liferayは6年連続でGartner Magic Quadrantのリーダーに。 詳細はこちら

フォーラム

ホーム » Liferay Portal » English » 3. Development

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
Riccardo Rotondo
Generate static method with Liferay Service Builder
2013/04/19 8:04
答え

Riccardo Rotondo

ランク: Junior Member

投稿: 29

参加年月日: 2010/06/11

最近の投稿

Dear All,

I'm working with liferay service builder and following the documentation I'm writing all the methods for a VirtualPath table in the class:

sb.model.impl.VirtualPathImpl

Everything works fine until I didn't try to implement a static method. Once I write:

 1
 2public class VirtualPathImpl extends VirtualPathBaseImpl {
 3    /*
 4     * NOTE FOR DEVELOPERS:
 5     *
 6     * Never reference this class directly. All methods that expect a virtual path model instance should use the {@link it.infn.ct.einfrsrv.sb.model.VirtualPath} interface instead.
 7     */
 8    public VirtualPathImpl() {
 9    }
10       
11        public static VirtualPath exist (String path, long userId){
12            VirtualPath vp = null;
13            
14            Log _log =LogFactoryUtil.getLog(VirtualPathImpl.class);
15            try {
16                DynamicQuery query =
17                        DynamicQueryFactoryUtil.forClass(VirtualPath.class)
18                        .add(PropertyFactoryUtil.forName("virtualPath")
19                        .eq(path))
20                        .add(PropertyFactoryUtil.forName("userId")
21                        .eq(new Long (userId)));
22                List <VirtualPath> queryVP = null;
23                queryVP = VirtualPathLocalServiceUtil.dynamicQuery(query);
24               
25                if (queryVP.isEmpty()) {
26                        _log.debug("VirtualPath '"+path+
27                                "' not present for userId:"+
28                                Long.toString(userId));
29                }else{
30                    vp = queryVP.get(0);
31                    _log.debug("VirtualPath '"+vp.getVirtualPath()+"' found");
32                }
33               
34            } catch (SystemException ex) {
35                Logger.getLogger(VirtualPathImpl.class.getName()).
36                        log(Level.SEVERE, null, ex);
37            }
38            return vp;
39        }
40       
41}


The code for that static method exist is not being generated and I'm not able to reference it neither from sb.model.VirtualPath nor from sb.service.VirtualPathLocalServiceUtil

What am I doing wrong? Static method are supposed to be implemented somewhere else?

Thank you very much for you help.

Regards,

Riccardo
David H Nebinger
RE: Generate static method with Liferay Service Builder
2013/04/19 9:38
答え

David H Nebinger

Community Moderator

ランク: Liferay Legend

投稿: 10366

参加年月日: 2006/09/01

最近の投稿

Static methods are not supported.

Instead put the method into VirtualPathLocalServiceImpl and you can access it from VirtualPathLocalServiceUtil after you build services.

In general you should never put business logic into the entity implementations. Most of these types of methods would not be available in the generated VirtualPath interface and trying to call the method will result in an UnsupportedOperationException every time.
Riccardo Rotondo
RE: Generate static method with Liferay Service Builder
2013/04/22 1:34
答え

Riccardo Rotondo

ランク: Junior Member

投稿: 29

参加年月日: 2010/06/11

最近の投稿

Dear David,

thank you for your quick reply!

David H Nebinger:

Instead put the method into VirtualPathLocalServiceImpl and you can access it from VirtualPathLocalServiceUtil after you build services.


I already tried that and it didn't work. I tried again this morning, this time putting the method non-static, and in it generated the method correctly!

David H Nebinger:

In general you should never put business logic into the entity implementations. Most of these types of methods would not be available in the generated VirtualPath interface and trying to call the method will result in an UnsupportedOperationException every time.


With into entity implementations what do you mean? That I should not put method in VirtualPathImpl and it's more recommended to write them into VirtualPathLocalServiceImpl? But what if a method is needed both in local and remote interface? Should I implement the method twice?

Thank you very much for you help.

Regards,

Riccardo
Amit Doshi
RE: Generate static method with Liferay Service Builder
2013/04/22 2:49
答え

Amit Doshi

ランク: Liferay Master

投稿: 549

参加年月日: 2010/12/29

最近の投稿

Hi Riccardo,


With into entity implementations what do you mean? That I should not put method in VirtualPathImpl and it's more recommended to write them into VirtualPathLocalServiceImpl? But what if a method is needed both in local and remote interface? Should I implement the method twice?


VirtualPathImpl, VirtualPathLocalServiceImpl and VirtualPathServiceImpl are all different.

VirtualPathImpl - As david said, there should not be any bussiness logic in that class. It was the extended model implementation for the Virtual path service. Represents a row in the virtualpath service database table, with each column mapped to a property of this class.

VirtualPAthLocalServiceImpl - For local Service, In this class where you have to write your business logic & methods and re-run the service and access it using VirtualPathLocalServiceUtil.

VirtualPathServiceImpl - For remote Service, In this class where you have to write your business logic & methods and re-run the service and access it using VirtualPathServiceUtil.


But what if a method is needed both in local and remote interface? Should I implement the method twice?


Yes . you need to implement that in both.
David H Nebinger
RE: Generate static method with Liferay Service Builder
2013/04/22 7:04
答え

David H Nebinger

Community Moderator

ランク: Liferay Legend

投稿: 10366

参加年月日: 2006/09/01

最近の投稿

Riccardo Rotondo:
With into entity implementations what do you mean? That I should not put method in VirtualPathImpl and it's more recommended to write them into VirtualPathLocalServiceImpl? But what if a method is needed both in local and remote interface? Should I implement the method twice?


Nope, take a queue from how Liferay structures their remote service implementations. Most of them, you'll see, invoke the permission checker stuff and then pass through to the local service implementation.

This way you're only implementing once but reusing for both local and remote.