Xman

Suggest that you first have a look at:
http://ofbiz.apache.org/docs/services.html

With this, you can gain an understanding of what a service actually is in
Ofbiz.  In a nutshell, it is basically only a piece of public static code
that takes the arguments DispatchContext dctx and Map context.  The engine
supplies the DispatchContext and you supply the Map.  The contents of the
context map are whatever you specified in the service definition and
prepared before hand before making the call.  Here is a sample of a service
definition from accounting/servicedef/services_payment.xml:

    <service name="setPaymentStatus" engine="simple"
default-entity-name="Payment"
        location="org/ofbiz/accounting/payment/PaymentServices.xml"
invoke="setPaymentStatus" auth="true">
        <description>Change the status of a Payment</description>
        <auto-attributes include="pk" mode="IN" optional="false"/>
        <attribute name="statusId" type="String" mode="IN"
optional="false"/>
        <attribute name="oldStatusId" type="String" mode="OUT"
optional="true"/>
    </service>

Note the <attribute> tag and the mode="IN" and optional="false".  This sez
that the attribute MUST be supplied in the context map.  Failure to do so
results in an error.  I can call this service in a java program like this:

        Map results = dispatcher.runSync("setPaymentStatus",
UtilMisc.toMap("userLogin", demofinadmin, "paymentId",
paycheck.get("paymentId"), "statusId", "PMNT_SENT"));

I can call it from a simple method like this:

...
        <if-not-empty field-name="parameters.StatusId">
            <if-compare-field field-name="parameters.statusId"
operator="not-equals" to-field-name="statusIdSave">
                <set-service-fields service-name="setPaymentSatus"
map-name="parameters" to-map-name="param"/>
                <call-service service-name="setPaymentStatus"
in-map-name="param"/>
                <check-errors/>
            </if-compare-field>
        </if-not-empty>

I can call it in my controller like this:

    <request-map uri="setPaycheckStatus">
        <security https="true" auth="true"/>
        <event type="service" invoke="setPaymentStatus"/>
        <response name="success" type="request-redirect"
value="viewPaycheck"/>
        <response name="error" type="view" value="viewPaycheck"/>
    </request-map>

If I call it from the controller as above, I MUST make sure the required
attributes are available as parameters of the request.

Note that "setPaymentStatus" has the java signiture:
public static Map setPaymentStatus(DispatchContext dctx, Map context)
(although this particular one is a simple method).

If you enumerate all the items in the context map, nowhere will you find
HttpServletRequest or HttpServletResponse in any that I know of and for sure
not in this example.  You could however require these like this:

    <service name="myHTTPResponseReader" engine="java"
        location="org/ofbiz/mystuff/httpStuff" invoke="myHTTPResponseReader"
auth="true">
        <description>My HTTP stuff</description>
        <attribute name="request"
type="javax.servlet.http.HttpServletRequest" mode="IN" optional="false"/>
    </service>

Have a look here: https://localhost:8443/webtools/control/availableServices
and you can see all the services that Ofbiz knows about.

If you are trying to manipulate web page content, I don't think a service is
what you really want (unless you want to perform some action as the result
of some operator action).

If you want to do something servlet style, you can create a method with this
signature:

    public static String myServlet(HttpServletRequest request,
HttpServletResponse response)

and call it like this:

    <request-map uri="myURI">
       <event type="java" path="org.ofbiz.myServlet" invoke="myServlet"/>
       <response name="fail" ..."/>
       <response name="success" ..."/>
    </request-map>

You would NOT call it with dispatcher.runSync() because it is NOT a service
in the Ofbiz sense.


Note the type="java" here and for a service as show above, type="service".

The servlet code like this does not need to be defined in any service.xml
file and in fact doesn't really want to be.

Hope this helps.

Skip



-----Original Message-----
From: X Gylee [mailto:[EMAIL PROTECTED]
Sent: Monday, November 19, 2007 7:33 PM
To: [email protected]
Subject: RE: How to access session and request from Java service


Skip,
Thank you for your explanation. That explains better the design
paradigm in OFBiz. I might need to re-think my application design
then.

However, to understand better, can you elaborate what you said in your
response? how can I do the following?

You wrote:
As a general rule, services do not have a request or response in their
context (although you can certainly put them in if you need to).
...
In your services.xml file, you can require that it be called with a request
and a response in the context if that is what your needs are.

Reply via email to