public static MapdeletePerson(DispatchContext dctx, Map context) {
MapresultMap = ServiceUtil.returnSuccess();
String id = (String) context.get("id");
try {
GenericValue personGV = delegator.findOne("Person", UtilMisc.toMap("id", id), false);
if (personGV != null && !personGV.isEmpty()) {
personGV.remove();
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError("Failed. " +e.getMessage());
}
return resultMap;
}
Deleting a Record in OFBiz using Java
This is an example on how to delete a record in OFBiz using Java. I am assuming that the you have created the service of this method in you service.xml.
Labels:
delegator,
DispatchContext,
findOne,
GenericEntityException,
GenericValue,
Java,
Map,
OFBiz,
remove(),
ServiceUtil
Updating a Record in OFBiz using Java
This is an example on how to update a record in OFBiz using Java as engine. I am assuming that the you have created the service of this method in you service.xml.
public static MapupdatePerson(DispatchContext dctx, Map context) {
MapresultMap = ServiceUtil.returnSuccess();
//this is how you fetch the values from the request
String id = (String) context.get("id");
String firstName = (String) context.get("firstName");
String lastName = (String) context.get("lastName");
String gender = (String) context.get("gender");
String email = (String) context.get("email");
try {
GenericValue personGV = delegator.findOne("Person", UtilMisc.toMap("id", id), false);
if (personGV != null && !personGV.isEmpty()) {
personGV.put("firstName", firstName);
personGV.put("lastName", lastName);
personGV.put("gender", gender);
personGV.put("email", email);
personGV.store();
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError("Failed. " +e.getMessage());
}
return resultMap;
}
Labels:
delegator,
DispatchContext,
findOne,
GenericEntityException,
GenericValue,
Java,
Map,
OFBiz,
ServiceUtil,
store()
Creating a New Record in OFBiz using Java
This is an example on how to create a record in OFBiz using Java as engine.
First, you have to create a service containing the attributes/values to be passed in your Java method. This service is invoked in your controller.xml.
Once you have the service in your xml. Create the method with the same name as the value of the invoke attribute in the xml.
First, you have to create a service containing the attributes/values to be passed in your Java method. This service is invoked in your controller.xml.
<service name=“addPerson” engine=“java” location=“org.ofbiz.person.PersonServices” invoke=“addPerson” auth=“true”>
<description>Add Person Service</description>
<attribute name=“firstName” mode=“IN” type=“String” optional=“true”></attribute>
<attribute name=“lastName” mode=“IN” type=“String” optional=“true”></attribute>
<attribute name=“gender” mode=“IN” type=“String” optional=“true”></attribute>
<attribute name=“email” mode=“IN” type=“String” optional=“true”></attribute>
</service>
Once you have the service in your xml. Create the method with the same name as the value of the invoke attribute in the xml.
public static MapaddPerson(DispatchContext dctx, Map context) {
MapresultMap = ServiceUtil.returnSuccess();
//this is how you fetch the values from the request
String firstName = (String) context.get("firstName");
String lastName = (String) context.get("lastName");
String gender = (String) context.get("gender");
String email = (String) context.get("email");
try {
//delegator.getNextSeqId(String EntityName) for auto-increment id
MappersonValue = UtilMisc.toMap("id", delegator.getNextSeqId("Person"),
"firstName", firstName,
"lastName", lastName,
"gender", gender,
"email", email);
GenericValue personGV = delegator.makeValue("Person", personValue);
personGV.create();
} catch (GenericEntityException e) {
return ServiceUtil.returnError("Failed. " +e.getMessage());
}
return resultMap;
}
Labels:
DispatchContext,
GenericEntityException,
GenericValue,
Java,
makeValue,
Map,
OFBiz,
Service,
ServiceUtil
Subscribe to:
Posts (Atom)