ofbiz

常见的方法有三种,第一种是mini-lang中的simple-method,使用该方式需在XXXServices.xml中配置相关的simple-method,如:<?xml version="1.0" encoding="UTF-8" ?>
<simple-methods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/simple-methods.xsd">
    <simple-method method-name="createHelloPersonHobby" short-description="Create a Hobby-Person relationship"
        login-required="false">
        <make-value entity-name="HelloPersonHobby" value-name="newEntity"/>
        <set-nonpk-fields map-name="parameters" value-name="newEntity"/>
        <set-pk-fields map-name="parameters" value-name="newEntity"/>
        <create-value value-name="newEntity"/>
    </simple-method>
<simple-method  method-name="removeHelloPersonHobby" short-description="delete HelloPersonHobby" login-required="true">
   <check-permission permission="FACILITY" action="_DELETE"><fail-message message="Security Error: to run removeFacilityFromGroup you must have the FACILITY_DELETE or FACILITY_ADMIN permission"/></check-permission>
        <check-errors/>
  <make-value entity-name="HelloPersonHobby" value-name="lookupPKMap"/>
  <set-pk-fields map-name="parameters" value-name="lookupPKMap"/>
  <find-by-and entity-name="HelloPersonHobby" map-name="lookupPKMap" list-name="lookedUpValue"/>
  <remove-by-and entity-name="HelloPersonHobby" map-name="lookupPKMap"/>
</simple-method>
<simple-method method-name="removeHelloPerson" short-description="delete HelloPerson">
  <make-value entity-name="HelloPerson" value-name="lookupPKMap"/>
  <make-value entity-name="HelloPersonHobby" value-name="lookupNonPKMap"/>
  <set-pk-fields map-name="parameters" value-name="lookupPKMap"/>
  <find-by-and entity-name="HelloPersonHobby" list-name="lookUpValue" map-name="lookupPKMap"/>
  <remove-by-and entity-name="HelloPersonHobby" map-name="lookupPKMap"/>
  <!--<find-by-and entity-name="HelloPerson" list-name="lookedUoValue" map-name="lookupPKMap"/>
  <remove-by-and entity-name="HelloPerson" map-name="lookupPKMap"/>-->
  <find-by-primary-key entity-name="HelloPerson" value-name="lookedUpValue" map-name="lookupPKMap"/>
  <remove-value value-name="lookedUpValue"/>
</simple-method>
</simple-methods>
其中method-name,和controller.xml中调用的方法相对应,make-value则对应相关的实体,set-pk-fields设置主键值,是从穿过来的参数中获取的,即所对应的map-name(名称一般为parameters),find-by-and是根据所传参数进行查询。该文件设置好后只需在controller.xml文件中进行如下设置即可 <request-map uri="createHelloPersonHobby">
  <security https="true" auth="true"/>
  <event type="service" invoke="createHelloPersonHobby"/>
  <response name="success" type="view" value="hobbies"/>
  <response name="error" type="view" value="hobbies"/>
</request-map>

第二种和第三种方式都是调用java类中的方法,一种是返回值是map的方式,而且参数必须是DispatchContext dctx, Map context,如public static Map removeHelloPerson(DispatchContext dctx, Map context){
   GenericDelegator delegator = dctx.getDelegator();
   Map result = new HashMap();
   try{
    Map map = new HashMap();
    String helloPersonId = (String)context.get("helloPersonId");
    map.put("helloPersonId", helloPersonId);
    GenericValue helloPersonHobby = delegator.makeValue("HelloPersonHobby", map);
    if(delegator.findByAnd("HelloPersonHobby", map).size() != 0){
     helloPersonHobby.setPKFields(map);
     delegator.findByAnd("HelloPersonHobby", map);
     delegator.removeByAnd("HelloPersonHobby", map);
    }
    GenericValue helloPerson = delegator.makeValue("HelloPerson",map);
    helloPerson.setPKFields(map);
    delegator.findByPrimaryKey("HelloPerson", map);
    delegator.removeValue(helloPerson);
   }catch(Exception e){
   }
   return result;
}

该方法的调用必须在services.xml文件中进行相关配置,如:

<?xml version="1.0" encoding="UTF-8"?>
<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/services.xsd">
    <description>Hello 3 Services</description>

    <!-- this will be implemented in Java -->
    <service name="createHelloPerson" engine="java" export="true"
        location="org.ofbiz.hello3.Hello3Services" invoke="createHelloPerson">
        <description>Create a HelloPerson</description>
        <auto-attributes mode="IN" entity-name="HelloPerson" include="nonpk" optional="true"/>
        <attribute name="helloPersonId" mode="OUT" type="String" optional="false"/>
    </service>
   
   
    <!-- this will be implemented in Java -->
    <service name="searchHelloPerson" engine="java" export="true"
        location="org.ofbiz.hello3.Hello3Services" invoke="searchHelloPerson">
        <description>Search a HelloPerson</description>
        <attribute name="helloPersonId" mode="IN" type="String" optional="true"/>
        <!--auto-attributes mode="OUT" entity-name="HelloPerson" include="nonpk" optional="true"/-->
        <attribute name="helloPersonIdOut" mode="OUT" type="String" optional="false"/>
        <attribute name="firstName" mode="OUT" type="String" optional="true"/>
        <attribute name="lastName" mode="OUT" type="String" optional="true"/>
    </service>
<service name="createHelloPersonHobby" engine="java" export="true" location="org.ofbiz.hello3.Hello3Services"  invoke="createHelloPersonHobby">
  <description>Create a HelloPersonHobby which linkes a person and a hobby</description>
  <auto-attributes mode="IN" entity-name="HelloPersonHobby" include="nonpk" optional="true"/>
  <attribute name="helloPersonId" mode="IN" type="String" optional="true"/>
  <attribute name="helloHobbyId" mode="IN" type="String" optional="true"/>
</service>
<service name="searchHelloPersonHobby" engine="java" export="true"
  location="org.ofbiz.hello3.Hello3Services" invoke="serchHelloPersonHobby">
  <description>Search a HelloPerson</description>
  <attribute name="helloPersonId" mode="IN" type="String" optional="true"/>
  <attribute name="helloPersonIdOut" mode="OUT" type="String" optional="false"/>
  <attribute name="helloPersonHobby" mode="OUT" type="String" optional="true"/>
</service>
<!--<service name="deletePerson" engine="simple" default-entity-name="HelloPerson"
  location="org/ofbiz/hello3/Hello3Services.xml" invoke="removeHelloPerson" auth="false">
  <description>Delete a Person</description>
  <attribute name="helloPersonId" mode="IN" type="String" optional="false"/>
</service>-->
<service engine="java" name="deletePerson" export="true"
  location="org.ofbiz.hello3.Hello3Services" invoke="removeHelloPerson" auth="true">
   <description>Delete Person</description>
  <auto-attributes mode="IN" entity-name="HelloPerson" include="nonpk" optional="true"/>
  <attribute type="String" mode="IN" name="helloPersonId" optional="true"/>
</service>
<service name="deleteHobby" engine="java" export="true"
  location="org.ofbiz.hello3.Hello3Services" invoke="removeHelloPersonHobby" auth="true">
  <description>Delete Person Hobby</description>
  <auto-attributes mode="IN" entity-name="HelloPersonHobby" include="nonpk" optional="true"/>
  <attribute name="helloPersonId" mode="IN" type="String" optional="true"/>
  <attribute name="helloHobbyId" mode="IN" type="String" optional="true"/>
</service>
</services>
然后在controller.xml文件中进行如下配置即可

<request-map uri="deleteHelloPerson">
  <security https="true" auth="true"/>
  <event type="service" invoke="deletePerson"/>
  <response name="success" type="view" value="guestbook"/>
  <response name="error" type="view" value="guestbook"/>
</request-map>
<request-map uri="deleteHobby">
  <security https="true" auth="true"/>
  <event type="service" invoke="deleteHobby"/>
  <response name="success" type="view" value="hobbies"/>
  <response name="error" type="view" value="hobbies"/>
</request-map>

另一种方式则是在controller.xml直接调用java类,如下所示:

<request-map uri="deleteAddress">
       <security https="false" auth="true"/>
       <event type="java" path="com.eastsun.itman.addressbook.AddressBookEvents" invoke="deleteAddressBook"/>
       <response name="success" type="view" value="findAddressBook"/>
       <response name="error" type="view" value="error"/>
    </request-map>

该方法也有返回值,但其中的参数必须是HttpServletRequest request,HttpServletResponse response,例如:

  public static String deleteAddressBook(HttpServletRequest request,HttpServletResponse response)
       {
              GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
              GenericValue userLogin = (GenericValue)request.getSession().getAttribute("userLogin");
              String partyId = ((String)request.getParameter("partyId")).trim();
              String mobile = ((String)request.getParameter("mobile")).trim();
              if(mobile == null || mobile.equals("")){
                  request.setAttribute("_ERROR_MESSAGE_",UtilProperties.getMessage(resource,"addressBook.createAddressBook_mobile_IsEmpty",UtilHttp.getLocale(request)));
                  return "error";  
              }
             
              if(partyId == null || partyId.equals(""))
                   partyId = (String)userLogin.get("partyId");
              try{    
                  GenericValue delValue = delegator.findByPrimaryKey("PhoneAddressBook",UtilMisc.toMap("partyId",partyId,"mobile",mobile));
                  delValue.remove();
              }catch(GenericEntityException e){
                  request.setAttribute("_ERROR_MESSAGE_",UtilProperties.getMessage(resource,"address.deleteAddressBook_no_find_entity",UtilHttp.getLocale(request)));
                  return "error";
              }
             
              return "success";            
       }

你可能感兴趣的:(ofbiz,mini)