Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Nico Schreiber
How to write data from a form to a database
December 1, 2011 4:13 AM
Answer

Nico Schreiber

Rank: New Member

Posts: 17

Join Date: November 28, 2011

Recent Posts

Hi,

I have created a simple Liferay portlet with a form. In this form are displayed some userdata. By clicking the submit button I want to save the displayed userdata of
the current user in a database. To creat a database table I used the Liferay Service Builder. Everything works fine.
I read that I have to modify a file called ...LocalServiceImpl.java, which was created during the build process of the Service Builder.
What I have to do to save the data in the database?

Here ist my service.xml
 1
 2<?xml version="1.0" encoding="UTF-8"?>
 3<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
 4<service-builder package-path="de.test">
 5    <author>Nico Schreiber</author>
 6    <namespace>Test</namespace>
 7     <entity name="Antraege" local-service="true" remote-service="false">
 8        <column name="userId" type="String" primary="true"></column>
 9        <column name="firstName" type="String"></column>
10        <column name="lastName" type="String"></column>
11        <column name="email" type="String"></column>
12        <column name="telephone" type="String"></column>
13        <column name="department" type="String"></column>
14        <column name="costCenter" type="String"></column>
15        <column name="costCenterChief" type="String"></column>
16        <column name="date" type="Date"></column>
17       
18        <order by="asc">
19            <order-column name="userId" />
20        </order>
21       
22        <finder name="FirstName" return-type="Collection">
23            <finder-column name="firstName"></finder-column>
24        </finder>
25        <finder name="LastName" return-type="Collection">
26            <finder-column name="lastName"></finder-column>
27        </finder>
28        <finder name="EMail" return-type="Collection">
29            <finder-column name="email"></finder-column>
30        </finder>
31        <finder name="Telephone" return-type="Collection">
32            <finder-column name="telephone"></finder-column>
33        </finder>
34        <finder name="Department" return-type="Collection">
35            <finder-column name="department"></finder-column>
36        </finder>
37        <finder name="CostCenter" return-type="Collection">
38            <finder-column name="costCenter"></finder-column>
39        </finder>
40        <finder name="CostCenterChief" return-type="Collection">
41            <finder-column name="costCenterChief"></finder-column>
42        </finder>
43    </entity>
44</service-builder>


Here is my bean to display the userdata of the current usere
  1
  2package de.test.beans;
  3
  4import java.io.Serializable;
  5
  6import javax.faces.bean.ManagedBean;
  7import javax.faces.bean.SessionScoped;
  8import javax.faces.context.FacesContext;
  9
 10import com.liferay.portal.kernel.exception.PortalException;
 11import com.liferay.portal.kernel.exception.SystemException;
 12import com.liferay.portal.model.User;
 13import com.liferay.portal.service.UserLocalServiceUtil;
 14
 15@ManagedBean(name = "userdata")
 16@SessionScoped
 17public class Userdata implements Serializable{
 18   
 19    private static final long serialVersionUID = 1L;
 20   
 21    private String userId;
 22    private String firstName;
 23    private String lastName;
 24    private String eMail;
 25    private String telephone;
 26    private String department;
 27    private String costCenter;
 28    private String costCenterChief;
 29   
 30    public Userdata() throws PortalException, SystemException {
 31        FacesContext ctx = FacesContext.getCurrentInstance();
 32        String remoteUser = ctx.getExternalContext().getRemoteUser();
 33        User user = UserLocalServiceUtil.getUserById(Long.parseLong(remoteUser));
 34        userId = user.getScreenName();
 35        firstName = user.getFirstName();
 36        lastName= user.getLastName();
 37        eMail = user.getEmailAddress();   
 38        telephone = (String) user.getExpandoBridge().getAttribute("telephonenumber");
 39        department = (String) user.getExpandoBridge().getAttribute("department");
 40        costCenter = (String) user.getExpandoBridge().getAttribute("costCenter");
 41        costCenterChief = (String) user.getExpandoBridge().getAttribute("costCenterChief");       
 42    }
 43   
 44    public String getUserId() {
 45        return userId;
 46    }
 47
 48    public void setUserId(String userId) {
 49    }
 50
 51    public void setFirstName(String firstName) {
 52        this.firstName = firstName;
 53    }
 54
 55    public String getFirstName() {
 56        return firstName;
 57    }
 58
 59    public void setLastName(String lastName) {
 60        this.lastName = lastName;
 61    }
 62
 63    public String getLastName() {
 64        return lastName;
 65    }
 66
 67    public void seteMail(String eMail) {
 68        this.eMail = eMail;
 69    }
 70
 71    public String geteMail() {
 72        return eMail;
 73    }
 74
 75    public void setTelephone(String telephone) {
 76        this.telephone = telephone;
 77    }
 78
 79    public String getTelephone() {
 80        return telephone;
 81    }
 82
 83    public void setDepartment(String department) {
 84        this.department = department;
 85    }
 86
 87    public String getDepartment() {
 88        return department;
 89    }
 90
 91    public void setCostCenter(String costCenter) {
 92        this.costCenter = costCenter;
 93    }
 94
 95    public String getCostCenter() {
 96        return costCenter;
 97    }
 98   
 99    public void setCostCenterChief(String costCenterChief) {
100        this.costCenterChief = costCenterChief;
101    }
102
103    public String getCostCenterChief() {
104        return costCenterChief;
105    }
106
107}


Here is my view.xhtml
 1
 2[...]
 3
 4    <h:form>
 5    <p:fieldset legend="#{msg.userdata_head}" ><br/>
 6        <h:panelGrid columns="2" cellpadding="10" columnClasses="userdata, userdata_output"  styleClass="userdata" >
 7            <h:outputLabel for="userID" value="#{msg.userId}" />
 8            <h:outputText  id="userId" value="#{userdata.userId}"  />
 9            <h:outputLabel for="firstName" value="#{msg.firstName}" />
10            <h:outputText id="firstName" value="#{userdata.firstName}" />               
11            <h:outputLabel for="lastName" value="#{msg.lastName}" />
12            <h:outputText id="lastName" value="#{userdata.lastName}" />
13            <h:outputLabel for="eMail" value="#{msg.eMail}" />
14            <h:outputText id="eMail" value="#{userdata.eMail}" />               
15            <h:outputLabel for="telephone" value="#{msg.telephone}" />
16            <h:outputText id="telephone" value="#{userdata.telephone}" />
17            <h:outputLabel for="dapartment" value="#{msg.department}" />
18            <h:outputText id="department" value="#{userdata.department}" />
19            <h:outputLabel for="costCenter" value="#{msg.costCenter}" />
20            <h:outputText id="costCenter" value="#{userdata.costCenter}" />
21            <h:outputLabel for="costCenterChief" value="#{msg.costCenterChief}" />
22            <h:outputText id="costCenterChief" value="#{userdata.costCenterChief}" />   
23        </h:panelGrid>
24    </p:fieldset>
25    <br/>
26
27    [...]
28
29    <h:commandButton class="appointment_button_send" id="sendAppointment" value="#{msg.send_button}" action="view2.xhtml" />
30    </h:form>


I hope anybody can help me.

Regards,
Nico
Rajesh Chaurasia
RE: How to write data from a form to a database
December 1, 2011 8:41 AM
Answer

Rajesh Chaurasia

Rank: Regular Member

Posts: 119

Join Date: August 18, 2011

Recent Posts

In portlet class of your sample portlet add code on similar lines as given below:
package com.sify.npp.portlet;

import java.rmi.RemoteException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.sify.npp.businesslogic.model.Metadata;
import com.sify.npp.businesslogic.model.impl.MetadataImpl;
import com.sify.npp.businesslogic.service.MetadataLocalServiceUtil;

/**
* Portlet implementation class Metadata
*/
public class MetadataPortlet extends MVCPortlet {

public Metadata createMetadata(ActionRequest actionRequest,ActionResponse actionResponse) throws PortalException, SystemException, RemoteException {
long metadataId = 0L;
metadataId = CounterLocalServiceUtil.increment(this.getClass().getName());
Metadata metadata = new MetadataImpl();
metadata.setListtypeid(metadataId);
metadata.setName("tintin");
metadata.setType_("com.sify.npp.businesslogic.model.Metadata");
MetadataLocalServiceUtil.addMetadata(metadata);
return metadata;
}

public List<Metadata> displayMetadata(ActionRequest actionRequest,ActionResponse actionResponse) throws PortalException, SystemException, RemoteException {
int start=0;
int end=20;
List<com.sify.npp.businesslogic.model.Metadata> listMetadata = MetadataLocalServiceUtil.getMetadatas(start, end);
actionRequest.setAttribute("metadataList", listMetadata);
return listMetadata;
}
}


write jsp code whcih takes calls from UI to portlet class given above as:
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ include file="/html/metadata/init.jsp" %>
<h1>Create Metadata Form</h1>
<%
PortletURL updateMetadataURL = renderResponse.createActionURL();
updateMetadataURL.setParameter(ActionRequest.ACTION_NAME, "createMetadata");
%>
<aui:form name="fm" method="POST" action="<%=updateMetadataURL.toString() %>">
<aui:input name="name" label="Metadata Name:"/>
<aui:button type="submit" value="Save"/>
</aui:form>


Hope this helps emoticon