SimpleMethod.runSimpleMethod("component://scrum/script/org/ofbiz/scrum/ScrumEvents.xml", "createProductBacklog", formInput);
จากตัวอย่างเป็นการเรียกใช้ method "createProductBacklog" ในไฟล์ ScrumEvents.xml ครับ ซึ่งขั้นตอนการเรียกใช้นั้นจะเหมือนกัน ทั้งใน ่java event และ java service แต่จะมีความแตกต่างกันตรงที่วิธีการเตรียมข้อมูล input ครับ เช่น หากเป็นใน java service ก็จะเป็นแบบนี้ครับ
MethodContext formInput = new MethodContext(ctx, context, ScrumServices.class.getClassLoader());
formInput.putParameter("backStatusId", "CRQ_ACCEPTED");
formInput.putParameter("billed", "Y");
formInput.putParameter("custEstimatedMilliSeconds", 0);
formInput.putParameter("custRequestTypeId", "RF_UNPLAN_BACKLOG");
formInput.putParameter("custSequenceNum", "");
...
แต่หากเป็นใน java event ก็จะอีกแบบหนึ่งครับMethodContext formInput = new MethodContext(request, response, ScrumEvents.class.getClassLoader());
formInput.putParameter("backStatusId", "CRQ_ACCEPTED");
formInput.putParameter("billed", "Y");
formInput.putParameter("custEstimatedMilliSeconds", 0);
formInput.putParameter("custRequestTypeId", "RF_UNPLAN_BACKLOG");
formInput.putParameter("custSequenceNum", "");
...
จะสังเกตว่าสิ่งที่ต่างคือ input ของ method ครับ เพียงเท่านี้เราก็สามารถเรียก method ต่างๆจาก simple method ได้แล้วครับ.
ต่อไปนี้เป็นโคดตัวอย่างที่ใช้จริงครับ
public static Map autoToUnplannedBacklog(DispatchContext ctx, Map context) {
Delegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
String communicationEventId = (String) context.get("communicationEventId");
if (UtilValidate.isNotEmpty(communicationEventId)) {
try {
GenericValue communicationEvent = delegator.findByPrimaryKey("CommunicationEvent", UtilMisc.toMap("communicationEventId", communicationEventId));
if (UtilValidate.isNotEmpty(communicationEvent)) {
String subject = communicationEvent.getString("subject");
if (UtilValidate.isNotEmpty(subject)) {
int pdLocation = subject.indexOf("JK#");
if (pdLocation >= 0) {
// scan until the first non digit character
int nonDigitLocation = pdLocation + 3;
while (nonDigitLocation < subject.length() && Character.isDigit(subject.charAt(nonDigitLocation))) {
nonDigitLocation++;
}
String productId = subject.substring(pdLocation + 3, nonDigitLocation);
GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId));
if (UtilValidate.isNotEmpty(product)) {
List productRoleList = product.getRelated("ProductRole", UtilMisc.toMap("roleTypeId", "PRODUCT_OWNER"), UtilMisc.toList("roleTypeId"));
GenericValue productRoleMap = EntityUtil.getFirst(productRoleList);
//เตรียม พารามิเตอร์ input
MethodContext formInput = new MethodContext(ctx, context, ScrumServices.class.getClassLoader());
try {
// prepare parameters
formInput.putParameter("backStatusId", "CRQ_ACCEPTED");
formInput.putParameter("billed", "Y");
formInput.putParameter("custEstimatedMilliSeconds", 0);
formInput.putParameter("custRequestTypeId", "RF_UNPLAN_BACKLOG");
formInput.putParameter("custSequenceNum", "");
formInput.putParameter("description", subject.substring(nonDigitLocation, subject.length()));
formInput.putParameter("noteId", "");
formInput.putParameter("noteInfo", "");
formInput.putParameter("parentCustRequestId", "");
formInput.putParameter("partyIdTo", productRoleMap.getString("partyId"));
formInput.putParameter("productId", productId);
formInput.putParameter("sequence", "");
formInput.putParameter("story", communicationEvent.getString("content")); //การเรียกใช้
SimpleMethod.runSimpleMethod("component://scrum/script/org/ofbiz/scrum/ScrumEvents.xml", "createProductBacklog", formInput);
String custRequestId = formInput.getEnv("custRequestId");
if (UtilValidate.isNotEmpty(custRequestId)) {
GenericValue custRequestMap = delegator.findByPrimaryKey("CustRequest", UtilMisc.toMap("custRequestId", custRequestId));
GenericValue userLogin = (GenericValue) context.get("userLogin");
// also close the incoming communication event
if (UtilValidate.isNotEmpty(custRequestMap)) {
dispatcher.runSync("setCommunicationEventStatus", UtilMisc.toMap("communicationEventId", communicationEvent.getString("communicationEventId"), "statusId", "COM_COMPLETE", "userLogin", userLogin));
}
}
} catch (Exception e) {
e.printStackTrace();
return ServiceUtil.returnError("Error calling auto create unplnned backlog from jenkins:" + e.getMessage());
}
} else {
Debug.logInfo("Product id " + productId + " found in subject but not in database", module);
}
}
}
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError("find by primary key error:" + e.toString());
}
Map result = ServiceUtil.returnSuccess();
return result;
} else {
Map result = ServiceUtil.returnError("A communication event id is required");
return result;
}
}
0 comments:
Post a Comment