论坛

主页 » Liferay Portal » English » 3. Development

组合视图 统一视图 树状图
讨论主题 [ 上一个 | 下一个 ]
Nico Schreiber
How to write data from a form to a database
2011年12月1日 上午5:15
答复

Nico Schreiber

等级: New Member

帖子: 17

加入日期: 2011年11月28日

最近的帖子

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>


Regards,
Nico
Dave Weitzel
RE: How to write data from a form to a database
2011年12月1日 下午1:16
答复

Dave Weitzel

等级: Regular Member

帖子: 186

加入日期: 2009年11月18日

最近的帖子

The LocalServiceUtil class has to have the main methods that will add delete modify your entities, you will then call these from your portlet.java code processing the form request data.

have you read the Liferay In Action book at all it has good explanation and examples.

You will need to create typically a method
public addAntraege (Antraege newAntraege, userId){
Antraege antraege= antraegePersinstence.create(
counterLocalService.increment(Antraege .class.getName()));
// gives a unique id to item

...
atnraege.setObject= newAntraege.getObject();

return antraegePersitence.update(antraege,false);
}

have left out details and replace with all your objects

In your portlet code you will need to create a new Antraege object
Antraege antraege = new AntraegeImpl();

and then set its values from the form before calling:

AntraegeLocalServiceUtil.addAntraege(antraege, userId);


hope that helps
Dave
Nico Schreiber
RE: How to write data from a form to a database
2011年12月2日 上午1:39
答复

Nico Schreiber

等级: New Member

帖子: 17

加入日期: 2011年11月28日

最近的帖子

Hi Dave,

thanks for your help. I have modified the AntraegeLocalServiceImpl, but I get the error "The method create(String) in the type AntraegePersistence is not applicable for the arguments (long)". How can I fix this error?

Here is my AntraegeLocalServiceImpl.java
 1
 2public class AntraegeLocalServiceImpl extends AntraegeLocalServiceBaseImpl {
 3   
 4    public  Antraege addAntraege(Antraege newAntraege, String userId){
 5        Antraege antrag = antraegePersistence.create(counterLocalService.increment(Antraege.class.getName()));
 6
 7        antrag.setUserId(newAntraege.getUserId());
 8        antrag.setFirstName(newAntraege.getFirstName());
 9        antrag.setLastName(newAntraege.getLastName());
10        antrag.setEmail(newAntraege.getEmail());
11        antrag.setTelephone(newAntraege.getTelephone());
12        antrag.setDepartment(newAntraege.getDepartment());
13        antrag.setCostCenter(newAntraege.getCostCenter());
14        antrag.setCostCenterChief(newAntraege.getCostCenterChief());
15        antrag.setBrandId(newAntraege.getBrandId());
16        antrag.setStatement(newAntraege.getStatement());
17        antrag.setDate(newAntraege.getDate());
18       
19        return antraegePersistence.update(antrag, false);
20    }       
21}


In my first post I had added the code of my Userdata.java. How can I integrate the commands

Antraege antraege = new AntraegeImpl();

and

AntraegeLocalServiceUtil.addAntraege(antraege, userId);

in this class to save the data like userId, firstName, ... in the database?

Regards,
Nico
Sagar A Vyas
RE: How to write data from a form to a database
2011年12月5日 上午2:27
答复

Sagar A Vyas

等级: Liferay Master

帖子: 655

加入日期: 2009年4月17日

最近的帖子

Hi Nico,

. How can I fix this error?


I guess
public de.test.model.Antraege create(java.lang.String userId);


Create method expect String and counterImpl will return long.

Thanks,
Sagar Vyas
Nico Schreiber
RE: How to write data from a form to a database
2011年12月5日 上午3:07
答复

Nico Schreiber

等级: New Member

帖子: 17

加入日期: 2011年11月28日

最近的帖子

Hi Sagar,

I modified my AntraegeLocalServiceImpl and fixed the create error. Now the code looks like that:

 1
 2public class AntraegeLocalServiceImpl extends AntraegeLocalServiceBaseImpl {
 3   
 4    public  Antraege addAntraege(Antraege newAntraege, String userId) throws SystemException{
 5        Antraege antraege = antraegePersistence.create(userId);
 6
 7        antraege.setUserId(newAntraege.getUserId());
 8        antraege.setFirstName(newAntraege.getFirstName());
 9        antraege.setLastName(newAntraege.getLastName());
10        antraege.setEmail(newAntraege.getEmail());
11        antraege.setTelephone(newAntraege.getTelephone());
12        antraege.setDepartment(newAntraege.getDepartment());
13        antraege.setCostCenter(newAntraege.getCostCenter());
14        antraege.setCostCenterChief(newAntraege.getCostCenterChief());
15        antraege.setBrandId(newAntraege.getBrandId());
16        antraege.setStatement(newAntraege.getStatement());
17        antraege.setDate(newAntraege.getDate());
18       
19        return antraegePersistence.update(antraege, false);
20    }       
21}


Is this correct?

Regards,
Nico
Sandeep Nair
RE: How to write data from a form to a database
2011年12月5日 上午3:20
答复

Sandeep Nair

等级: Liferay Legend

帖子: 1692

加入日期: 2008年11月5日

最近的帖子

try the following

1return addAntraege(antraege);


Regards,
Sandeep
Nico Schreiber
RE: How to write data from a form to a database
2011年12月5日 上午4:19
答复

Nico Schreiber

等级: New Member

帖子: 17

加入日期: 2011年11月28日

最近的帖子

Hi,

I modified the return how it was recommended by Sandeep. Now I want to save the entries of my form in the database. Therefore I generated following Bean:

 1
 2@ManagedBean (name="transfer")
 3@SessionScoped
 4public class TransferBean implements Serializable{
 5
 6    private static final long serialVersionUID = 1L;
 7   
 8    private String userId;
 9    private String firstName;
10    private String lastName;
11    private String eMail;
12    private String telephone;
13    private String department;
14    private String costCenter;
15    private String costCenterChief;
16    private String statement;
17   
18    public void addAntrag()throws PortalException, SystemException{
19        Antraege antraege = new AntraegeImpl();
20        FacesContext ctx = FacesContext.getCurrentInstance();
21        String remoteUser = ctx.getExternalContext().getRemoteUser();
22        User user = UserLocalServiceUtil.getUserById(Long.parseLong(remoteUser));
23        userId = user.getScreenName();
24        firstName = user.getFirstName();
25        lastName= user.getLastName();
26        eMail = user.getEmailAddress();   
27        telephone = (String) user.getExpandoBridge().getAttribute("telephonenumber");
28        department = (String) user.getExpandoBridge().getAttribute("department");
29        costCenter = (String) user.getExpandoBridge().getAttribute("costCenter");
30        costCenterChief = (String) user.getExpandoBridge().getAttribute("costCenterChief");       
31       
32        AntraegeLocalServiceUtil.addAntraege(antraege, userId);
33    }
34
35                      [...] (getter and setter)


I get an error in line AntraegeLocalServiceUtil.addAntraege(antraege, userId); -->"The method addAntraege(Antraege) in the type AntraegeLocalServiceUtil is not applicable for the arguments (Antraege, String)"

When I change it to AntraegeLocalServiceUtil.addAntraege(antraege); the error is away but from my point of view I have to commit the userId. Or isn`t it necessary?

Is the way I set the attributes like userId, firstName, ... correct?

Regards,
Nico
Sandeep Nair
RE: How to write data from a form to a database
2011年12月5日 上午4:28
答复

Sandeep Nair

等级: Liferay Legend

帖子: 1692

加入日期: 2008年11月5日

最近的帖子

I believe in your antraege model you have userId. Just set it using the setter as you were doing before and call addAntraege. You were doing that before werent just replace return *persistence.update with addAntreage as i said.

Regards,
Sandeep