Hello Avijit,
You have shared following code snippet in your previous email:
// Setting up all non primary key field values from context map
smsSubjectMaster.setNonPKFields(context);
"context" is just a map object from which you can get the parameters passed
to the service definition in OFBiz.
You can do similar thing in events by using similar code snippet as shown
below:
Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
String dataResourceId;
GenericValue dataResource = delegator.makeValue("DataResource");
dataResource.setPKFields(paramMap);
dataResource.setNonPKFields(paramMap);
All the 4 points which you have shared in this email can be taken care of
either in Events or Services. You might have noticed that "delegator"(Used
to interact with entities) and "dispatcher"(call services synchronously or
asynchronously) objects are available in Events as well as in Services. So
it is our personal choice where we wish to send form data either to Events
or to Services. As I have said in the previous email that Services in OFBiz
are session less.
There are some best practices/conventions which are set in OFBiz framework:
- "context" is a map object which is generally used in the *Services.java
to read the parameters passed to the service definition.
- The other place where you will see a "context" map is the groovy files.
In groovy files you can prepare the data and put everything in the
"context" map.
This data will be used in the form which is included in the screen
definition.
Hope this helps!
--
Kind Regards,
Ashish Vijaywargiya
Vice President of Operations
*HotWax Systems*
*Enterprise open source experts*
http://www.hotwaxsystems.com
On Tue, Jul 12, 2022 at 6:22 AM Avijit Bose <[email protected]> wrote:
> Hi Ashish,
>
> I completely understood your explanation. I also have a workable
> understanding about service and events.
>
> Firstly ... I wanted to handle and manipulate request data before storing
> it in the database.
> Secondly... It is required to insert request data into 10 different
> entities.
> Thirdly... I wanted ofbiz to create auto increment of all the primary
> fields in the database of all 10 tables and not to create them myself.
> Fourthly... It is required to get primary key of the first table and insert
> into 9 other tables as foreign keys.
>
> This is why I was trying to get the "context" in a java event.
>
> Anyway I have done it through Servlet and I am able to satisfy all the
> above 4 conditions.
>
> NOTE: If in future ofbiz release, the ofbiz core can be updated, where
> "context" can be passed from the screen itself as like other parameters,
> and it can be called in the method ...
> public static String updateStudent(HttpServletRequest request,
> HttpServletResponse response) {}, then I think it will be helpful to all
> users and the coding will be a bit easier.
>
> thank you for your help
> Avijit
>
>
> On Mon, Jul 11, 2022 at 10:09 AM Ashish Vijaywargiya <
> [email protected]> wrote:
>
> > Hello Avijit,
> >
> > It looks like you are getting confused from the concept of Events and
> > Services in java. It is very well explained in the OFBiz tutorial:
> >
> >
> https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guide+for+18.12
> >
> > Now talking about your code snippet:
> >
> > You are calling an event from a controller so you need to get all the
> > parameters from the method
> request.getParameter("html_form_elements_name").
> >
> >
> > And once you have read all the values in events then from here you should
> > pass these values to the service where you can take care of business
> logic
> > and CRUD operations. Basically events are used to do the validations and
> > extra manipulations with the form parameters.
> >
> > If you don't want to go with the Events route then you should change your
> > controller.xml entry as shown below:
> >
> > <request-map uri="createStudent">
> > <security https="true" auth="true"/>
> > <event type="service" invoke="createStudent"/>
> > <response name="success" type="view" value="newStudent"/>
> > <response name="error" type="view" value="errorPage"/>
> > </request-map>
> >
> > Here you will notice that I have used the event type as "service" where
> you
> > don't need to specify the path of the service. You can specify the
> service
> > definition and implement your service either in
> Java/Groovy/XML(Minilang).
> > Services in OFBiz are session-less, so if you wish to do handling of
> > HttpSession object then you need to do it in the Events.
> >
> > All these things are very well explained in the tutorial link which I
> have
> > shared above. There is also a very good reference available for the
> > comparison between Events and Services. Please take a look at it.
> >
> > Thank you.
> >
> > --
> > Kind Regards,
> > Ashish Vijaywargiya
> > Vice President of Operations
> > *HotWax Systems*
> > *Enterprise open source experts*
> > http://www.hotwaxsystems.com
> >
> >
> >
> > On Sat, Jul 9, 2022 at 7:32 AM Avijit Bose <[email protected]>
> wrote:
> >
> > > Hi Nicolas,
> > >
> > > First... I have a ftl file like this. Here I am calling "createStudent"
> > in
> > > the controller through "<@ofbizUrl>createStudent</@ofbizUrl>"
> > > *************************
> > > <form id="regForm" method="post"
> > > action="<@ofbizUrl>createStudent</@ofbizUrl>" name="regForm"
> > > enctype="multipart/form-data">
> > > <body>
> > > ..... blah blah blah
> > > </body>
> > > </form>
> > > *************************
> > >
> > > Second.... my controller as shown below....
> > > *************************
> > > <request-map uri="createStudent">
> > > <security https="true" auth="true"/>
> > > <event type="java" path="com.sms.events.StudentServices"
> > > invoke="createStudent"/>
> > > <response name="success" type="view" value="newStudent"/>
> > > <response name="error" type="view" value="errorPage"/>
> > > </request-map>
> > > *************************
> > > Third.... from the controller above I am going to a java file called
> > > "StudentServices" and invoking a method in the java file called
> > > "createStudent".
> > >
> > >
> > > Fourth... In the "createStudent" method of the java file... I have the
> > > following...
> > > **************************
> > > public static String createStudent(HttpServletRequest request,
> > > HttpServletResponse response) {
> > > Delegator delegator = (Delegator) request.getAttribute("delegator");
> > > LocalDispatcher dispatcher = (LocalDispatcher)
> > > request.getAttribute("dispatcher");
> > > GenericValue userLogin = (GenericValue)
> > > request.getSession().getAttribute("userLogin");
> > > Map<String, Object> result = ServiceUtil.returnSuccess();
> > >
> > > try {
> > > GenericValue smsSubjectMaster =
> > > delegator.makeValue("smsSubjectMaster");
> > > // Auto generating next sequence of studentId primary key
> > > smsSubjectMaster.setNextSeqId();
> > > // Setting up all non primary key field values from context map
> > > smsSubjectMaster.setNonPKFields(context);
> > > // Creating record in database for smsStudentMaster entity for
> > > prepared value
> > > smsSubjectMaster = delegator.create(smsSubjectMaster);
> > > //subjectId = smsSubjectMaster.getString("studentId");
> > > //context.putIfAbsent("subjectId", subjectId);
> > > context.put("subjectId", subjectId);
> > > result.put("subjectId",
> smsSubjectMaster.getString("subjectId"));
> > > Debug.log("==========smsSubjectMaster record created
> successfully
> > > with subjectId: "+smsSubjectMaster.getString("subjectId"));
> > >
> > > } catch (GenericEntityException e) {
> > > Debug.logError(e, module);
> > > return ServiceUtil.returnError("Error in creating record in
> > > smsSubjectMaster entity ........" +module);
> > > }
> > >
> > > }
> > > **************************
> > > In the try/catch block... pls notice this...
> > > "smsSubjectMaster.setNonPKFields(context);". This 'context' I am
> talking
> > > about. I want the context in this method ...
> > >
> > > public static String createStudent(HttpServletRequest request,
> > > HttpServletResponse response) {
> > > // I NEED CONTEXT HERE
> > > }
> > >
> > > AND NOT IN THIS METHOD
> > >
> > > public static Map<String, Object> createStudent(DispatchContext dctx,
> > > Map<String, Object> context){
> > > ?? HERE I AM ALREADY PASSING CONTEXT AND GETTING IT
> > > }
> > >
> > > Now...
> > >
> > > I see a 'globalContext' in this link...
> > >
> > >
> >
> https://cwiki.apache.org/confluence/display/OFBIZ/Variables+always+available+in+screen+context
> > >
> > > But I think globalcontext is not the same as context. From the link
> > above I
> > > could pass the variables mentioned in the link from ftl to java by
> > setting
> > > screen parameters, but no parameter named 'context' in the link above.
> > >
> > > Question: How do I get context in a method where I am passing
> > > "HttpServletRequest request, HttpServletResponse response" as shown
> > above.
> > >
> > > regards
> > > Avijit
> > >
> > >
> > > On Fri, Jul 8, 2022 at 1:05 PM Nicolas Malin <[email protected]
> >
> > > wrote:
> > >
> > > > Hello Avijit,
> > > >
> > > > Can you share some peace of code, I didn't understand your ftl to
> java
> > ?
> > > >
> > > > You have a ftl template and go to java service through request call
> or
> > > > you want to call a java code on a ftl code ?
> > > >
> > > > Nicolas
> > > >
> > > > On 28/06/2022 12:45, Avijit Bose wrote:
> > > > > Dear Sir,
> > > > >
> > > > > Can the 'context' be passed from a ftl file to java?
> > > > >
> > > > > I see a 'globalContext' in this link...
> > > > >
> > > >
> > >
> >
> https://cwiki.apache.org/confluence/display/OFBIZ/Variables+always+available+in+screen+context
> > > > >
> > > > > My goal is to use context here..
> > > > > -----------------------
> > > > > dbName.setNonPKFields(context);
> > > > > -------------------------
> > > > > in a method...
> > > > > ------------------------
> > > > > public static String createSubscriber(HttpServletRequest request,
> > > > > HttpServletResponse response) { }
> > > > > -----------------------
> > > > > can I use 'globalContext' in place of 'context'?
> > > > >
> > > > > regards
> > > > > Avijit
> > > > >
> > > >
> > > >
> > >
> >
>