Foren

RE: Portlet with Web Service, store data in DB

Rafa Quinonero, geändert vor 13 Jahren.

Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
Hi all,

I'm new to liferay and exploring web services in portlets by myself. With some tutorials and help, I've been able to make a portlet with a working web service. What I'd like to do now is to add some data in the DB via HTTP POST from my portlet, but I'm a little bit lost due to my little concept knowledge of portlets and liferay API.

I'd like to show a text field in my portlet so that the user can write there a name, and when the click button (with aui) is pushed, we call the HTTP POST and create a new row in the DB. I created my portlet with an entity Employee with 2 columns: id (PK) and name.

Trying to build that, I have some questions I'd like to ask and hope you can help me:

1st) Where and how should I write the code to send the HTTP POST after getting the name that the user entered? I did something like that:

<portlet:actionURL var="post_url" />

<aui:form method="post" action="<%=post_url%>">
<aui:input label="New Employee: " name="name" type="text" value="<%=employee%>" />
<aui:button type="submit" />
</aui:form>

But it seems that the post_url is never really filled.

2nd) Looking at my EmployeeServiceUtil.java, I can see that the create method I need to call, has a Model as an argument, how can I send a Model via HTTP to store a new row in the DB? I put that code in my processAction() method without succes:

public void processAction (ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException
{
PortletPreferences prefs = actionRequest.getPreferences();
String name = prefs.getValue("name", "Noname"); /* This should be the name inserted by the user to be stored as a new Employee */

/* URL to be sent as a POST call to store the new row in DB */
String post = "http://localhost:8080/c/portal/json_service?serviceClassName:com.liferay.test.service.EmployeeServiceUtil" +
"&serviceMethodName:create&servletContextName:EmployeePlugin-portlet&serviceParameters=[var]&var=" + name;

actionResponse.setRenderParameter("post_url", post);

super.processAction(actionRequest, actionResponse);
}

Please correct me if I'm wrong with my code or with the next concepts: First we render the portlet by reading the view.jsp or the init parameters at portlet.xml, maybe with the init() method too. Then, when the user writes a name in the text field and clicks on submit, we are calling the processAction method, that should take the name, concatenate it to the needed URL and send it as POST to store it in database. Then, we render our portlet again, after the action has been finished. So the action is called and then we render the portlet again, but I don't understand much well how must the view.jsp structured to achieve that.

Thank you very much, hope you can help to improve my skills with liferay and portlets concepts.

Cheers,

Rafa
Bharani Ravi Kanth, geändert vor 13 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 63 Beitrittsdatum: 20.01.12 Neueste Beiträge
HEY Rafa

I think this blog will help you out.
http://michi-path.blogspot.in/2012/03/create-form-and-submit-values-to.html

and i am also attaching a sample code in this reply..please find and go through it it may help you

Regards bharani
Rafa Quinonero, geändert vor 13 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
Hi! Thanks a lot! emoticon
Rafa Quinonero, geändert vor 13 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
Bharani Ravi Kanth:
HEY Rafa

I think this blog will help you out.
http://michi-path.blogspot.in/2012/03/create-form-and-submit-values-to.html

and i am also attaching a sample code in this reply..please find and go through it it may help you

Regards bharani


How can I take a quick look at the database to see if data has been stored correctly? is it possible via http GET?

Thanks a lot ;)
Bharani Ravi Kanth, geändert vor 13 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 63 Beitrittsdatum: 20.01.12 Neueste Beiträge
Hey Rafa

by default Liferay comes with its own database.HSQL so you can configure to your existing database by doing some small manipulations

i am explaining for MYSQL

1. first create a database withname lportal which is having a chartype utf8 then go to this location
2. liferay-portal-6.0.6\tomcat-6.0.29\webapps\ROOT\WEB-INF\classes
3. then create a file naming portlet-ext.properties
4. add the following content there
5. save the portal-ext.properties file

jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost:3306/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=enter your database username
jdbc.default.password=enter your database password
schema.run.enabled=true
schema.run.minimal=true

and restart your liferay server you see the tables added in your lportal and from now what ever tables you add by using service.xml and service builder classes it will store in this lportal database so you can you see the values in tables easily.

try this and let me know.

regards
bharani
thumbnail
Danial Mustofa Habibi, geändert vor 13 Jahren.

RE: Portlet with Web Service, store data in DB

Regular Member Beiträge: 141 Beitrittsdatum: 01.11.11 Neueste Beiträge
hi there if you want to use your own database than lportal database u can use this on your service.xml on entity tag
data-source="myAppDataSource" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager"

sample from my entity
	<entity name="Jadwal" local-service="true" remote-service="true" table="jadwal" data-source="myAppDataSource" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager">
	<column name="kode_jadwal" type="int" primary="true"></column>
	<column name="stasiun_asal" type="String"></column>
	<column name="stasiun_tujuan" type="String"></column>
        <column name="kode_kereta" type="String"></column>
	</entity>
	

then modify/create ext-spring.xml on your meta-inf folder
<!--?xml version="1.0"?-->

<beans>
  <bean class="
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  </bean>
  <bean id="myAppDataSourceTarget" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db_kereta" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>
  <bean id="myAppDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource">
      <ref bean="myAppDataSourceTarget" />
    </property>
  </bean>
  <bean id="myAppHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
    <property name="dataSource">
      <ref bean="myAppDataSource" />
    </property>
  </bean>
  <bean id="myAppSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl"> 
    <property name="sessionFactoryImplementor">
      <ref bean="myAppHibernateSessionFactory" />
    </property>
  </bean>
  <bean id="myAppTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">          
    <property name="dataSource">
      <ref bean="myAppDataSource" />
    </property>
    <property name="sessionFactory">
      <ref bean="myAppHibernateSessionFactory" />
    </property>
  </bean>
</beans>

run service builder again.

Regards

Danial
Rafa Quinonero, geändert vor 13 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
Hi guys!! Thanks for your answers!!

Finally I found the way, I just needed to learn how to use <form> in the right way to make a call to the web service to store/retrieve info into/from the Liferay database. Anyway, you informatoin is also good for me, I didn't know about that you wrote. emoticon

To make this thread usefull, I paste here my solution and a little explanation of what I did.

After being able to get a response from the web service with a GET request (although I was getting an exception, it was a good signal that things were going in the right way), I wanted to store new rows in the database so that the GET request had a real response. What I didn't know was how to make that call to GET/POST from code, as I was doing it via browser, and the answer was just the <form> tag. I needed to make the following GETcall for looking for a user in the DB:

http://localhost:8080/c/portal/json_service?serviceClassName=com.liferay.test.service.TrabajadorServiceUtil&serviceMethodName=findByName&servletContextName=TrabajadorPlugin-portlet&serviceParameters=[param]&param=Name

and that's how your <form> should look:

<form method="get" action="http://localhost:8080/c/portal/json_service">
	<input name="serviceClassName" type="hidden" value="com.liferay.test.service.TrabajadorServiceUtil">
	<input name="serviceMethodName" type="hidden" value="findByName">
	<input name="servletContextName" type="hidden" value="TrabajadorPlugin-portlet">
	<input name="serviceParameters" type="hidden" value="[param]">
	<input name="param" type="text" value="<%=searching%>">
	<input type="submit" value="Search">
</form>


And the same for the POST call, to store data in the database:

http://localhost:8080/c/portal/json_service?serviceClassName=com.liferay.test.service.TrabajadorServiceUtil&serviceMethodName=create&servletContextName=TrabajadorPlugin-portlet&serviceParameters=[param]&param=Name

<form method="post" action="http://localhost:8080/c/portal/json_service">
	<input name="serviceClassName" type="hidden" value="com.liferay.test.service.TrabajadorServiceUtil">
	<input name="serviceMethodName" type="hidden" value="create">
	<input name="servletContextName" type="hidden" value="TrabajadorPlugin-portlet">
	<input name="serviceParameters" type="hidden" value="[param]">
	<input name="param" type="text" value="<%=employee%>"> 
	<input type="submit" value="Submit">
</form> 


NOTE that I use my methods created from the Liferay Build Service: findByName() and create() in the URLs.

value="&lt;%=searching%&gt;"
AND
value="&lt;%=employee%&gt;" 
is used to get what user inputs in the fields created by <input name="param" type="text" ... /> lines.

and

PortletPreferences prefs = renderRequest.getPreferences(); 
String employee = (String)prefs.getValue("name", "New Employee");  

PortletPreferences prefs2 = renderRequest.getPreferences(); 
String searching = (String)prefs2.getValue("search", "Search an Employee"); 


to get the user input and concatenate it to the end of the URL; it is in fact, almost the most important thing of these URLs, since it is the parameter that we're really looking for.

Once with that in my view.jsp (without any java class) I was able to store rows in my database and to retrieve the information.

I hope to help anybody with the same problem that me!! And thanks again for the help given to me!! emoticon
thumbnail
rozuan mat adam, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

New Member Beiträge: 8 Beitrittsdatum: 12.04.12 Neueste Beiträge
Danial Mustofa Habibi:
hi there if you want to use your own database than lportal database u can use this on your service.xml on entity tag
data-source="myAppDataSource" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager"

sample from my entity
	<entity name="Jadwal" local-service="true" remote-service="true" table="jadwal" data-source="myAppDataSource" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager">
	<column name="kode_jadwal" type="int" primary="true"></column>
	<column name="stasiun_asal" type="String"></column>
	<column name="stasiun_tujuan" type="String"></column>
        <column name="kode_kereta" type="String"></column>
	</entity>
	

then modify/create ext-spring.xml on your meta-inf folder
<!--?xml version="1.0"?-->

<beans>
  <bean class="
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  </bean>
  <bean id="myAppDataSourceTarget" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db_kereta" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>
  <bean id="myAppDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource">
      <ref bean="myAppDataSourceTarget" />
    </property>
  </bean>
  <bean id="myAppHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
    <property name="dataSource">
      <ref bean="myAppDataSource" />
    </property>
  </bean>
  <bean id="myAppSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl"> 
    <property name="sessionFactoryImplementor">
      <ref bean="myAppHibernateSessionFactory" />
    </property>
  </bean>
  <bean id="myAppTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">          
    <property name="dataSource">
      <ref bean="myAppDataSource" />
    </property>
    <property name="sessionFactory">
      <ref bean="myAppHibernateSessionFactory" />
    </property>
  </bean>
</beans>

run service builder again.

Regards

Danial


Hello Danial,

Can u provide full tutorial for using external database for portlet?
Really appreciate if U can help.
Rafa Quinonero, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
Hello Danial,

Can u provide full tutorial for using external database for portlet?
Really appreciate if U can help.

External DB like MySQL or external DB in a different server?
thumbnail
rzn adm, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

New Member Beiträge: 8 Beitrittsdatum: 12.04.12 Neueste Beiträge
Rafa Quinonero:
Hello Danial,

Can u provide full tutorial for using external database for portlet?
Really appreciate if U can help.

External DB like MySQL or external DB in a different server?


External DB like MySQL in different server. the settings itself in the portlet not in the Liferay.

Thanks.
Rafa Quinonero, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
So what you need is a "Server Connection String". I can't hel you more because I have never used it, but it seems an interesting thing to know. Possibly with that you can research a little bit more about your issue.

Hope that helps.

Rafa
thumbnail
rzn adm, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

New Member Beiträge: 8 Beitrittsdatum: 12.04.12 Neueste Beiträge
Rafa Quinonero:
So what you need is a "Server Connection String". I can't hel you more because I have never used it, but it seems an interesting thing to know. Possibly with that you can research a little bit more about your issue.

Hope that helps.

Rafa


Yes, I know that but I dunno how to do it.

Can you share with me about your web services in portlet tutorial?

Thanks.
Rafa Quinonero, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
rzn adm:
Rafa Quinonero:
So what you need is a "Server Connection String". I can't hel you more because I have never used it, but it seems an interesting thing to know. Possibly with that you can research a little bit more about your issue.

Hope that helps.

Rafa


Yes, I know that but I dunno how to do it.

Can you share with me about your web services in portlet tutorial?

Thanks.


Take a look at this page. I think that's what you are looking for to beginn with the portlet implementation.

Try it, if that's not what you are looking for, specify it a bit more, or ask here if you get issues while following that page.

http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/service-build-5
thumbnail
rzn adm, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

New Member Beiträge: 8 Beitrittsdatum: 12.04.12 Neueste Beiträge
Rafa Quinonero:
rzn adm:
Rafa Quinonero:
So what you need is a "Server Connection String". I can't hel you more because I have never used it, but it seems an interesting thing to know. Possibly with that you can research a little bit more about your issue.

Hope that helps.

Rafa


Yes, I know that but I dunno how to do it.

Can you share with me about your web services in portlet tutorial?

Thanks.


Take a look at this page. I think that's what you are looking for to beginn with the portlet implementation.

Try it, if that's not what you are looking for, specify it a bit more, or ask here if you get issues while following that page.

http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/service-build-5


I'm currently lost here. The page explain about web services and not a full tutorial which I can follow.
Rafa Quinonero, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Junior Member Beiträge: 37 Beitrittsdatum: 22.03.12 Neueste Beiträge
rzn adm:
Rafa Quinonero:
rzn adm:
Rafa Quinonero:
So what you need is a "Server Connection String". I can't hel you more because I have never used it, but it seems an interesting thing to know. Possibly with that you can research a little bit more about your issue.

Hope that helps.

Rafa


Yes, I know that but I dunno how to do it.

Can you share with me about your web services in portlet tutorial?

Thanks.


Take a look at this page. I think that's what you are looking for to beginn with the portlet implementation.

Try it, if that's not what you are looking for, specify it a bit more, or ask here if you get issues while following that page.

http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/service-build-5


I'm currently lost here. The page explain about web services and not a full tutorial which I can follow.


So, you need a tutorial for Service Builder or for connecting to your external database server?
I'm sorry, if you don't specify exactly what you need, I can't help you.
thumbnail
Riccardo Ferrari, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Regular Member Beiträge: 139 Beitrittsdatum: 13.11.10 Neueste Beiträge
Hi,

I think this link should help you. I tested and is working nicely.

http://bairdblog.wordpress.com/2011/02/11/using-liferay-6-0-x-service-builder-with-external-databases/

Regards,

Riccardo
thumbnail
Danial Mustofa Habibi, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Regular Member Beiträge: 141 Beitrittsdatum: 01.11.11 Neueste Beiträge
rzn adm:
Danial Mustofa Habibi:
hi there if you want to use your own database than lportal database u can use this on your service.xml on entity tag
data-source="myAppDataSource" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager"

sample from my entity
	<entity name="Jadwal" local-service="true" remote-service="true" table="jadwal" data-source="myAppDataSource" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager">
	<column name="kode_jadwal" type="int" primary="true"></column>
	<column name="stasiun_asal" type="String"></column>
	<column name="stasiun_tujuan" type="String"></column>
        <column name="kode_kereta" type="String"></column>
	</entity>
	

then modify/create ext-spring.xml on your meta-inf folder
<!--?xml version="1.0"?-->

<beans>
  <bean class="
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  </bean>
  [code][code]</beans>
<bean id="myAppDataSourceTarget"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_kereta" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="myAppDataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<ref bean="myAppDataSourceTarget" />
</property>
</bean>
<bean id="myAppHibernateSessionFactory"
class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
<property name="dataSource">
<ref bean="myAppDataSource" />
</property>
</bean>
<bean id="myAppSessionFactory"
class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
<property name="sessionFactoryImplementor">
<ref bean="myAppHibernateSessionFactory" />
</property>
</bean>
<bean id="myAppTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource">
<ref bean="myAppDataSource" />
</property>
<property name="sessionFactory">
<ref bean="myAppHibernateSessionFactory" />
</property>
</bean>
</beans>
run service builder again.

Regards

Danial


Hello Danial,

Can u provide full tutorial for using external database for portlet?
Really appreciate if U can help.

hi there from my last post..that's more than enough if you want to use your own database,different server,...so what exactly what do you want to achieve??
just change this to meet your own requirement
<bean id="myAppDataSourceTarget" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db_kereta" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>

and if you use oracle,ibm or other database, just put the driver on the bin folder

Regards

Danial
thumbnail
rzn adm, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

New Member Beiträge: 8 Beitrittsdatum: 12.04.12 Neueste Beiträge
Thanks guys.

I managed to make the portlet using external database using service.xml and ext-spring.xml.
Before run the portlet, I need to create the external database first.


p/s: anyone know how to implement web services in portlet? I need web services to retrieve data from other database server.
thumbnail
Danial Mustofa Habibi, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Regular Member Beiträge: 141 Beitrittsdatum: 01.11.11 Neueste Beiträge
rzn adm:
Thanks guys.

I managed to make the portlet using external database using service.xml and ext-spring.xml.
Before run the portlet, I need to create the external database first.


p/s: anyone know how to implement web services in portlet? I need web services to retrieve data from other database server.

Glad To hear that !!.....
what do you want to do?implementing your own service?or call other web service??
thumbnail
rzn adm, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

New Member Beiträge: 8 Beitrittsdatum: 12.04.12 Neueste Beiträge
Danial Mustofa Habibi:
rzn adm:
Thanks guys.

I managed to make the portlet using external database using service.xml and ext-spring.xml.
Before run the portlet, I need to create the external database first.


p/s: anyone know how to implement web services in portlet? I need web services to retrieve data from other database server.

Glad To hear that !!.....
what do you want to do?implementing your own service?or call other web service??


Thanks Danial, Its help a lot.

I want to create my own web services and the portlet is the web service's client. Any tutorial how to do this?
thumbnail
Danial Mustofa Habibi, geändert vor 12 Jahren.

RE: Portlet with Web Service, store data in DB

Regular Member Beiträge: 141 Beitrittsdatum: 01.11.11 Neueste Beiträge
Usually if you want to implement your own webservice, the things u need to do is enable the remote-service in your entity tag to true
<entity name="Jadwal" local-service="true" remote-service="true" < code></entity>
<br>and just implement your code in your entityImpl. when you ant build-service ,its automatically web service, for your reference may be u can check this<br><a href="http://arvindm.com/2010/03/23/web-services-in-liferay/">WebService Liferay</a><br>Hope this help<br><br><br>Regards<br>Danial