...
Create Your First Application (Hello World...)
Reference Tutorial
Create a Plugin in OFBiz
Introduction to Components
...
Before running our first component, let's say 'Hello to the World'
Reference document:
Understanding the OFBiz Widget Toolkit
...
Creating First Database Entity (Table)
Reference document:
Entity Engine Guide
Defining entity
...
Create a Service
Reference Document:
Service Engine Guide
Before preparing the form, let's write a service to create records in the database for the OfbizDemo entity in the service definition XML file ($OFBIZ_HOME/plugins/ofbizDemo/servicedef/services.xml)
...
Use of UI Labels (Introduction)
Reference Document:
Guide to OFBiz-i18n
How to localize Product and Categories descriptions
...
package com.companyname.ofbizdemo.services;
import java.util.Map;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.service.DispatchContext;
import org.apache.ofbiz.service.ServiceUtil;
public class OfbizDemoServices {
public static final String module MODULE = OfbizDemoServices. class .getName();
public static Map<String, Object> createOfbizDemo(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> result = ServiceUtil.returnSuccess();
Delegator delegator = dctx.getDelegator();
try {
GenericValue ofbizDemo = delegator.makeValue( "OfbizDemo" );
ofbizDemo.setNextSeqId();
ofbizDemo.setNonPKFields(context);
ofbizDemo = delegator.create(ofbizDemo);
result.put( "ofbizDemoId" , ofbizDemo.getString( "ofbizDemoId" ));
Debug.log( "==========This is my first Java Service implementation in Apache OFBiz. OfbizDemo record created successfully with ofbizDemoId:" + ofbizDemo.getString( "ofbizDemoId" ));
} catch (GenericEntityException e) {
Debug.logError(e, moduleMODULE);
return ServiceUtil.returnError( "Error in creating record in OfbizDemo entity ........" + moduleMODULE);
}
return result;
}
}
|
...
package com.companyname.ofbizdemo.events;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.service.GenericServiceException;
import org.apache.ofbiz.service.LocalDispatcher;
public class OfbizDemoEvents {
public static final String module MODULE = OfbizDemoEvents. class .getName();
public static String createOfbizDemoEvent(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute( "delegator" );
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute( "dispatcher" );
GenericValue userLogin = (GenericValue) request.getSession().getAttribute( "userLogin" );
String ofbizDemoTypeId = request.getParameter( "ofbizDemoTypeId" );
String firstName = request.getParameter( "firstName" );
String lastName = request.getParameter( "lastName" );
if (UtilValidate.isEmpty(firstName) || UtilValidate.isEmpty(lastName)) {
String errMsg = "First Name and Last Name are required fields on the form and can't be empty." ;
request.setAttribute( "_ERROR_MESSAGE_" , errMsg);
return "error" ;
}
String comments = request.getParameter( "comments" );
try {
Debug.logInfo( "=======Creating OfbizDemo record in event using service createOfbizDemoByGroovyService=========" , moduleMODULE);
dispatcher.runSync( "createOfbizDemoByGroovyService" , UtilMisc.toMap( "ofbizDemoTypeId" , ofbizDemoTypeId,
"firstName" , firstName, "lastName" , lastName, "comments" , comments, "userLogin" , userLogin));
} catch (GenericServiceException e) {
String errMsg = "Unable to create new records in OfbizDemo entity: " + e.toString();
request.setAttribute( "_ERROR_MESSAGE_" , errMsg);
return "error" ;
}
request.setAttribute( "_EVENT_MESSAGE_" , "OFBiz Demo created succesfully." );
return "success" ;
}
}
|
...