留言板

RE: How to write data from a form to a database

Nico Schreiber,修改在12 年前。

How to write data from a form to a database

New Member 帖子: 16 加入日期: 11-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

<!--?xml version="1.0" encoding="UTF-8"?-->

<service-builder package-path="de.test">
	<author>Nico Schreiber</author>
	<namespace>Test</namespace>
 	<entity name="Antraege" local-service="true" remote-service="false">
        <column name="userId" type="String" primary="true"></column>
        <column name="firstName" type="String"></column>
        <column name="lastName" type="String"></column>
        <column name="email" type="String"></column>
        <column name="telephone" type="String"></column>
        <column name="department" type="String"></column>
        <column name="costCenter" type="String"></column>
        <column name="costCenterChief" type="String"></column>
        <column name="date" type="Date"></column>
        
        <order by="asc"> 
			<order-column name="userId" />
		</order>
        
        <finder name="FirstName" return-type="Collection">
            <finder-column name="firstName"></finder-column>
        </finder>
        <finder name="LastName" return-type="Collection">
            <finder-column name="lastName"></finder-column>
        </finder>
        <finder name="EMail" return-type="Collection">
            <finder-column name="email"></finder-column>
        </finder>
        <finder name="Telephone" return-type="Collection">
            <finder-column name="telephone"></finder-column>
        </finder>
        <finder name="Department" return-type="Collection">
            <finder-column name="department"></finder-column>
        </finder>
        <finder name="CostCenter" return-type="Collection">
            <finder-column name="costCenter"></finder-column>
        </finder>
        <finder name="CostCenterChief" return-type="Collection">
            <finder-column name="costCenterChief"></finder-column>
        </finder>
    </entity>
</service-builder>


Here is my bean to display the userdata of the current usere

package de.test.beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

@ManagedBean(name = "userdata")
@SessionScoped
public class Userdata implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private String userId;
	private String firstName;
	private String lastName;
	private String eMail;
	private String telephone;
	private String department;
	private String costCenter;
	private String costCenterChief;
	
	public Userdata() throws PortalException, SystemException {
		FacesContext ctx = FacesContext.getCurrentInstance();
		String remoteUser = ctx.getExternalContext().getRemoteUser();
		User user = UserLocalServiceUtil.getUserById(Long.parseLong(remoteUser));
		userId = user.getScreenName(); 
		firstName = user.getFirstName();
		lastName= user.getLastName();
		eMail = user.getEmailAddress();	
		telephone = (String) user.getExpandoBridge().getAttribute("telephonenumber");
		department = (String) user.getExpandoBridge().getAttribute("department");
		costCenter = (String) user.getExpandoBridge().getAttribute("costCenter");
		costCenterChief = (String) user.getExpandoBridge().getAttribute("costCenterChief");		
	}
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}

	public void seteMail(String eMail) {
		this.eMail = eMail;
	}

	public String geteMail() {
		return eMail;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getDepartment() {
		return department;
	}

	public void setCostCenter(String costCenter) {
		this.costCenter = costCenter;
	}

	public String getCostCenter() {
		return costCenter;
	}
	
	public void setCostCenterChief(String costCenterChief) {
		this.costCenterChief = costCenterChief;
	}

	public String getCostCenterChief() {
		return costCenterChief;
	}

}



Here is my view.xhtml

[...]

	<h:form>
	<p:fieldset legend="#{msg.userdata_head}"><br>
		<h:panelgrid columns="2" cellpadding="10" columnclasses="userdata, userdata_output" styleclass="userdata">
			<h:outputlabel for="userID" value="#{msg.userId}" />
			<h:outputtext id="userId" value="#{userdata.userId}" />
			<h:outputlabel for="firstName" value="#{msg.firstName}" />
			<h:outputtext id="firstName" value="#{userdata.firstName}" />				
			<h:outputlabel for="lastName" value="#{msg.lastName}" />
			<h:outputtext id="lastName" value="#{userdata.lastName}" />
			<h:outputlabel for="eMail" value="#{msg.eMail}" />
			<h:outputtext id="eMail" value="#{userdata.eMail}" />				
			<h:outputlabel for="telephone" value="#{msg.telephone}" />
			<h:outputtext id="telephone" value="#{userdata.telephone}" />
			<h:outputlabel for="dapartment" value="#{msg.department}" />
			<h:outputtext id="department" value="#{userdata.department}" />
			<h:outputlabel for="costCenter" value="#{msg.costCenter}" />
			<h:outputtext id="costCenter" value="#{userdata.costCenter}" />
			<h:outputlabel for="costCenterChief" value="#{msg.costCenterChief}" />
			<h:outputtext id="costCenterChief" value="#{userdata.costCenterChief}" />	
		</h:panelgrid>
	</p:fieldset>
	<br>

	[...]

	<h:commandbutton class="appointment_button_send" id="sendAppointment" value="#{msg.send_button}" action="view2.xhtml" />
	</h:form>


Regards,
Nico
thumbnail
Dave Weitzel,修改在12 年前。

RE: How to write data from a form to a database

Regular Member 帖子: 208 加入日期: 09-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){
[indent]Antraege antraege= antraegePersinstence.create(
counterLocalService.increment(Antraege .class.getName()));
// gives a unique id to item

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

return antraegePersitence.update(antraege,false);[/indent]
}

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,修改在12 年前。

RE: How to write data from a form to a database

New Member 帖子: 16 加入日期: 11-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

public class AntraegeLocalServiceImpl extends AntraegeLocalServiceBaseImpl {
	
	public  Antraege addAntraege(Antraege newAntraege, String userId){
		Antraege antrag = antraegePersistence.create(counterLocalService.increment(Antraege.class.getName()));

		antrag.setUserId(newAntraege.getUserId());
		antrag.setFirstName(newAntraege.getFirstName());
		antrag.setLastName(newAntraege.getLastName());
		antrag.setEmail(newAntraege.getEmail());
		antrag.setTelephone(newAntraege.getTelephone());
		antrag.setDepartment(newAntraege.getDepartment());
		antrag.setCostCenter(newAntraege.getCostCenter());
		antrag.setCostCenterChief(newAntraege.getCostCenterChief());
		antrag.setBrandId(newAntraege.getBrandId());
		antrag.setStatement(newAntraege.getStatement());
		antrag.setDate(newAntraege.getDate());
		
		return antraegePersistence.update(antrag, false);
	}		
}


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
thumbnail
Sagar A Vyas,修改在12 年前。

RE: How to write data from a form to a database

Liferay Master 帖子: 679 加入日期: 09-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,修改在12 年前。

RE: How to write data from a form to a database

New Member 帖子: 16 加入日期: 11-11-28 最近的帖子
Hi Sagar,

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


public class AntraegeLocalServiceImpl extends AntraegeLocalServiceBaseImpl {
	
	public  Antraege addAntraege(Antraege newAntraege, String userId) throws SystemException{
		Antraege antraege = antraegePersistence.create(userId);

		antraege.setUserId(newAntraege.getUserId());
		antraege.setFirstName(newAntraege.getFirstName());
		antraege.setLastName(newAntraege.getLastName());
		antraege.setEmail(newAntraege.getEmail());
		antraege.setTelephone(newAntraege.getTelephone());
		antraege.setDepartment(newAntraege.getDepartment());
		antraege.setCostCenter(newAntraege.getCostCenter());
		antraege.setCostCenterChief(newAntraege.getCostCenterChief());
		antraege.setBrandId(newAntraege.getBrandId());
		antraege.setStatement(newAntraege.getStatement());
		antraege.setDate(newAntraege.getDate());
		
		return antraegePersistence.update(antraege, false);
	}		
}


Is this correct?

Regards,
Nico
thumbnail
Sandeep Nair,修改在12 年前。

RE: How to write data from a form to a database

Liferay Legend 帖子: 1744 加入日期: 08-11-6 最近的帖子
try the following

return addAntraege(antraege);


Regards,
Sandeep
Nico Schreiber,修改在12 年前。

RE: How to write data from a form to a database

New Member 帖子: 16 加入日期: 11-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:


@ManagedBean (name="transfer")
@SessionScoped
public class TransferBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private String userId;
	private String firstName;
	private String lastName;
	private String eMail;
	private String telephone;
	private String department;
	private String costCenter;
	private String costCenterChief;
	private String statement;
	
	public void addAntrag()throws PortalException, SystemException{
		Antraege antraege = new AntraegeImpl();
		FacesContext ctx = FacesContext.getCurrentInstance();
		String remoteUser = ctx.getExternalContext().getRemoteUser();
		User user = UserLocalServiceUtil.getUserById(Long.parseLong(remoteUser));
		userId = user.getScreenName(); 
		firstName = user.getFirstName();
		lastName= user.getLastName();
		eMail = user.getEmailAddress();	
		telephone = (String) user.getExpandoBridge().getAttribute("telephonenumber");
		department = (String) user.getExpandoBridge().getAttribute("department");
		costCenter = (String) user.getExpandoBridge().getAttribute("costCenter");
		costCenterChief = (String) user.getExpandoBridge().getAttribute("costCenterChief");		
		
		AntraegeLocalServiceUtil.addAntraege(antraege, userId);
	}

                      [...] (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
thumbnail
Sandeep Nair,修改在12 年前。

RE: How to write data from a form to a database

Liferay Legend 帖子: 1744 加入日期: 08-11-6 最近的帖子
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