Foros de discusión

Retrieving data through portlet

thumbnail
Hery Sutanto, modificado hace 11 años.

Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Hi all,

I want to ask on how to retrieve data from database table through portlet..
Can anyone give me the detail on how to retrieve the data...

Thanks,

Hery
thumbnail
Amit Doshi, modificado hace 11 años.

RE: Retrieving data through portlet

Liferay Master Mensajes: 550 Fecha de incorporación: 29/12/10 Mensajes recientes
Hery Sutanto:
Hi all,

I want to ask on how to retrieve data from database table through portlet..
Can anyone give me the detail on how to retrieve the data...

Thanks,

Hery



Hey Hery,

You had not mentioned from which table you want to retrive data from DB.
In general scenario you can retrive data using the *LocalServiceUtil Class and refer there appropriate method which is appropriate for you.

Thanks & Regards,
Amit Doshi
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Hi Amit,

I am creating custom portlet... using eclipse, the table is generated by service builder itself.... but I don't ave any idea on how to retrieve the data from view.jsp of my custom portlet... do you know how to do that...? can you include a snipet of code here...?

Regards,

Hery
thumbnail
Pinkesh Gandhi, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 99 Fecha de incorporación: 27/01/12 Mensajes recientes
Hi Hery,

If you have used service-builder and you have already built your service through it then, you can notice in your source folder, there are so many auto generated service classes has been created.

So now you only have to use XXXLocalServiceUtil class to retrieve data from table.
For e.g., If you have created service for Employee entity, then your Util class having name 'EmployeeLocalServiceUtil' is created in your source folder and you can use it's methods to retrieve data from table like


int total = EmployeeLocalServiceUtil.getEmployeesCount();

List<employee> emplyeeList = EmployeeLocalServiceUtil.getEmployees(0, total);
</employee>


I hope this will helps you.
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Pinkesh Gandhi:
Hi Hery,

If you have used service-builder and you have already built your service through it then, you can notice in your source folder, there are so many auto generated service classes has been created.

So now you only have to use XXXLocalServiceUtil class to retrieve data from table.
For e.g., If you have created service for Employee entity, then your Util class having name 'EmployeeLocalServiceUtil' is created in your source folder and you can use it's methods to retrieve data from table like


int total = EmployeeLocalServiceUtil.getEmployeesCount();

List<employee> emplyeeList = EmployeeLocalServiceUtil.getEmployees(0, total);
</employee>


I hope this will helps you.


Hi Pinkesh,

Yeah I have seen lots of classes generated... I can see lots of method within it.... But it is created under XXXLocalServiceBaseImpl... is that different?? I also got error for all the class generated for me saying that :
the return types are incompatible for the inherited method... why is that?

Regards,

Hery
thumbnail
Pinkesh Gandhi, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 99 Fecha de incorporación: 27/01/12 Mensajes recientes
Hery,

Yes, all classes having names like *Impl.java are called implementation classes of relevant interfaces. And as per the best practices, we should not use this implementation classes directly, instead we can use its relevant Util classes as I mentioned in my previous reply.

And regarding errors you are getting, are those error comes at run-time or you are facing it on compile time?
thumbnail
Jitendra Rajput, modificado hace 11 años.

RE: Retrieving data through portlet

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
If you are using service builder, then you can get data from XXXLocalServiceUtil.java or XXXServiceUtil.java(this will need permission check as well).
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Jitendra Rajput:
If you are using service builder, then you can get data from XXXLocalServiceUtil.java or XXXServiceUtil.java(this will need permission check as well).


Hi jitendra,

Can you explain to me in details on how to implement this..?

Thks,

Hery
thumbnail
Prakash Khanchandani, modificado hace 11 años.

RE: Retrieving data through portlet

Expert Mensajes: 329 Fecha de incorporación: 10/02/11 Mensajes recientes
You need to go though a short tutorial on service builder, so I guess this wiki will help.

Still, I would give a little start:
1) if in your service.xml you have remote-service=true then it will generate YourEntityServiceImpl and YourEntityServiceUtil classes
2) if in your service.xml you have local-service=true then it will generate YourEntityLocalServiceImpl and YourEntityLocalServiceUtil classes.
3) The *Util classes are updated when you run build-service. So you don't need to change these classes. These Util classes provide static methods of the *Impl classes to access from outside the service layer.
4) Now you can create methods inside the YourEntityServiceImpl and YourEntityLocalServiceImpl to access the database. Normally YourEntityLocalServiceImpl is the place where the code to access database will go. And YourEntityServiceImpl can have permission code or remote code.
5) There are other advance stuff you can do with service-builder like build finder methods to run your Custom-queries.

So in short your database code would go inside the YourEntityLocalServiceImpl and then run build-service and you will get a static method generated inside YourEntityLocalServiceUtil class which you can call from your portlet or JSP.

Hope this helps.
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Hi Pinkesh and Prakash,

After I build the service... the classes are generated but with lots of errors as I mentioned
and somehow I don't see any EntityLocalServiceUtil classes
I put both local-service and remote-service to true....

Regards,

Hery
thumbnail
Jitendra Rajput, modificado hace 11 años.

RE: Retrieving data through portlet

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
Hery Sutanto:

I put both local-service and remote-service to true....


If you specify remote service as true then you are creating remote web services that can we accessed by other application.
make it to false if its not required . And refer to below development guide for step by step process for building services with service.xml

http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/service-build-2
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Jitendra Rajput:
Hery Sutanto:

I put both local-service and remote-service to true....


If you specify remote service as true then you are creating remote web services that can we accessed by other application.
make it to false if its not required . And refer to below development guide for step by step process for building services with service.xml

http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/service-build-2



Hi Jitendra,

I have follow the step and still there are errors while generating the classes...

Regards,


Hery
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
EntityLocalServiceUtil are not generated somehow I create the portlet couple of times and still not generated..
and one of the error is could not locate the EntityLocalServiceUtil.... and always get the error of the return types are incompatible for the inherited method.

Regards,

Hery
thumbnail
Pinkesh Gandhi, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 99 Fecha de incorporación: 27/01/12 Mensajes recientes
Hi Hery,

Are you getting these errors run-time or it is compile time errors?

If you are getting it at run-time then could you please attach log file here?
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Pinkesh Gandhi:
Hi Hery,

Are you getting these errors run-time or it is compile time errors?

If you are getting it at run-time then could you please attach log file here?



Hi Pinkesh,

There are error while compiling stating that I have duplicate in parameter... How can that possible?
I state unique column name within the table, these are the errors :

[javac] Compiling 2 source files to F:\Liferay - plugins - SDK\liferay-plugins-sdk-6.1.0-ce-ga1-20120106155615760\portlets\PhoneExtensionListPort-portlet\docroot\WEB-INF\service-classes
[javac] ----------
[javac] 1. ERROR in F:\Liferay - plugins - SDK\liferay-plugins-sdk-6.1.0-ce-ga1-20120106155615760\portlets\PhoneExtensionListPort-portlet\docroot\WEB-INF\service\generate\phoneList\service\persistence\PhoneExtensionList_EntityPersistence.java (at line 207)
[javac] java.lang.String EmployeeN, java.lang.String EmployeeN,
[javac] ^^^^^^^^^
[javac] Duplicate parameter EmployeeN
[javac] ----------
[javac] ----------
[javac] 2. ERROR in F:\Liferay - plugins - SDK\liferay-plugins-sdk-6.1.0-ce-ga1-20120106155615760\portlets\PhoneExtensionListPort-portlet\docroot\WEB-INF\service\generate\phoneList\service\persistence\PhoneExtensionList_EntityUtil.java (at line 311)
[javac] java.lang.String EmployeeN, java.lang.String EmployeeN,
[javac] ^^^^^^^^^
[javac] Duplicate parameter EmployeeN
[javac] ----------
[javac] 2 problems (2 errors)

BUILD FAILED
F:\Liferay - plugins - SDK\liferay-plugins-sdk-6.1.0-ce-ga1-20120106155615760\build-common-plugin.xml:212: The following error occurred while executing this line:
F:\Liferay - plugins - SDK\liferay-plugins-sdk-6.1.0-ce-ga1-20120106155615760\build-common.xml:94: Compile failed; see the compiler error output for details.

Regards,

Hery
thumbnail
Pinkesh Gandhi, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 99 Fecha de incorporación: 27/01/12 Mensajes recientes
Have you tried with clean and build service?

If so then can you please attach your service.xml here?
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Pinkesh Gandhi:
Have you tried with clean and build service?

If so then can you please attach your service.xml here?


These are my code :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="generate.phoneList">
<author>zelion.lee</author>
<namespace>PhoneListportlet</namespace>

<entity name="PhoneListExt" local-service="true" remote-service="true">

<!-- PK fields -->

<column name="EmployeeName" type="String" primary="true" />

<!-- Audit fields -->

<column name="Division" type="String" />
<column name="Department" type="String" />
<column name="PhoneExtension" type="String" />

<order by="asc">
<order-column name="EmployeeName" />
</order>

<!-- Finder methods -->

<finder name="EmployeeName" return-type="Collection">
<finder-column name="EmployeeName" />
</finder>
</entity>
</service-builder>

almost all the generated classes having errors...

Can you tell if there are any errors from the above code?

Thanks,

Hery
thumbnail
Hitoshi Ozawa, modificado hace 11 años.

RE: Retrieving data through portlet

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
Did you execute Service Builder several times? If so, you may have to delete already generated files before executing Service Builder again.
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Hitoshi Ozawa:
Did you execute Service Builder several times? If so, you may have to delete already generated files before executing Service Builder again.


Yes I have executed several times.... It still generated the same error again and again...


Regards,

Hery
thumbnail
Hitoshi Ozawa, modificado hace 11 años.

RE: Retrieving data through portlet

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
So, have you tried deleting the tomcat's work, temp directories and service.model.impl, service.base, service.persitence, docroot\serivce, docroot\sql directories?
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Hitoshi Ozawa:
So, have you tried deleting the tomcat's work, temp directories and service.model.impl, service.base, service.persitence, docroot\serivce, docroot\sql directories?


I have tried so... it doesn't create a new one... after I rebuild it again...


Regards,

Hery.
thumbnail
Prakash Khanchandani, modificado hace 11 años.

RE: Retrieving data through portlet

Expert Mensajes: 329 Fecha de incorporación: 10/02/11 Mensajes recientes
Can you provide your service.xml, I guess what might be happening is you must have explicitly created a Finder with a primary key which liferay automatically creates.

I am not sure though. But if you can provide your service.xml it would help.

Thanks
thumbnail
Joseph Hobbs, modificado hace 11 años.

RE: Retrieving data through portlet

New Member Mensajes: 14 Fecha de incorporación: 10/07/12 Mensajes recientes
Prakash Khanchandani:
Can you provide your service.xml, I guess what might be happening is you must have explicitly created a Finder with a primary key which liferay automatically creates.


I was thinking the same thing...

What if you remove the EmployeeName finder declaration from the service.xml and try executing another Services build?
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Joseph Hobbs:
Prakash Khanchandani:
Can you provide your service.xml, I guess what might be happening is you must have explicitly created a Finder with a primary key which liferay automatically creates.


I was thinking the same thing...

What if you remove the EmployeeName finder declaration from the service.xml and try executing another Services build?
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Prakash Khanchandani:
Can you provide your service.xml, I guess what might be happening is you must have explicitly created a Finder with a primary key which liferay automatically creates.

I am not sure though. But if you can provide your service.xml it would help.

Thanks


Hi Prakash,

this is my service.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="generate.phoneList">
<author>zelion.lee</author>
<namespace>PhoneListportlet</namespace>

<entity name="PhoneListExt" local-service="true" remote-service="true">

<!-- PK fields -->

<column name="EmployeeName" type="String" primary="true" />

<!-- Audit fields -->

<column name="Division" type="String" />
<column name="Department" type="String" />
<column name="PhoneExtension" type="String" />

<order by="asc">
<order-column name="EmployeeName" />
</order>

<!-- Finder methods -->

<finder name="EmployeeName" return-type="Collection">
<finder-column name="EmployeeName" />
</finder>
</entity>
</service-builder>

almost all the generated classes having errors...

Can you tell if there are any errors from the above code?

Thanks,

Hery
thumbnail
Hitoshi Ozawa, modificado hace 11 años.

RE: Retrieving data through portlet

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
Can you attach your project?
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Hitoshi Ozawa:
So, have you tried deleting the tomcat's work, temp directories and service.model.impl, service.base, service.persitence, docroot\serivce, docroot\sql directories?


Can you tell me how can I attach my project here??

Regards,

Hery
thumbnail
Prakash Khanchandani, modificado hace 11 años.

RE: Retrieving data through portlet

Expert Mensajes: 329 Fecha de incorporación: 10/02/11 Mensajes recientes
As I suspected, remove this:

<!-- Finder methods -->

<finder name="EmployeeName" return-type="Collection">
<finder-column name="EmployeeName" />
</finder>


and your code should work fine, the finder for primarykey is by default generated by Liferay since EmployeeName is the primary key.

Cheers!
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Prakash Khanchandani:
As I suspected, remove this:

<!-- Finder methods -->

<finder name="EmployeeName" return-type="Collection">
<finder-column name="EmployeeName" />
</finder>


and your code should work fine, the finder for primarykey is by default generated by Liferay since EmployeeName is the primary key.

Cheers!


The errors now gone, but I still cannot see nay XXXLocalServiceUtil generated in the package...

Regards,

Hery
thumbnail
Prakash Khanchandani, modificado hace 11 años.

RE: Retrieving data through portlet

Expert Mensajes: 329 Fecha de incorporación: 10/02/11 Mensajes recientes
Do one thing delete everything and build the service again or better still create a new plugin project and copy the service.xml and build the service again. It should work.
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Prakash Khanchandani:
Do one thing delete everything and build the service again or better still create a new plugin project and copy the service.xml and build the service again. It should work.



I have created a new pluign remove the finder.... and build the service... I get 5 packages generated :
1. Entity
2. Entity.model.impl
3. Entity.service.base
4. Entity.service.impl
5. Entity.service.persistence

Is that correct? I still cannot see any EntityLocalServiceUtil generated there...

Regards,

Hery
thumbnail
Hitoshi Ozawa, modificado hace 11 años.

RE: Retrieving data through portlet

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
Is that correct? I still cannot see any EntityLocalServiceUtil generated there...


That's correct. The file to edit is the EntityLocalServiceImpl.java. After adding your "add" method there and re-executing service builder, you'll be able to call EntityLocalServiceUtil.
thumbnail
Hery Sutanto, modificado hace 11 años.

RE: Retrieving data through portlet

Junior Member Mensajes: 81 Fecha de incorporación: 4/06/12 Mensajes recientes
Hi Hitoshi... can you be more specific on how to add the "add" method...
can you show me step by step...

Thanks,

Hery