1 | SimpleMethod.runSimpleMethod( "component://scrum/script/org/ofbiz/scrum/ScrumEvents.xml" , "createProductBacklog" , formInput); |
จากตัวอย่างเป็นการเรียกใช้ method "createProductBacklog" ในไฟล์ ScrumEvents.xml ครับ ซึ่งขั้นตอนการเรียกใช้นั้นจะเหมือนกัน ทั้งใน ่java event และ java service แต่จะมีความแตกต่างกันตรงที่วิธีการเตรียมข้อมูล input ครับ เช่น หากเป็นใน java service ก็จะเป็นแบบนี้ครับ
1 2 3 4 5 6 7 | 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" , "" ); ... |
1 2 3 4 5 6 7 | 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" , "" ); ... |
ต่อไปนี้เป็นโคดตัวอย่างที่ใช้จริงครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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.<string object= "" >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</string> result = ServiceUtil.returnSuccess(); return result; } else { Map result = ServiceUtil.returnError( "A communication event id is required" ); return result; } } |
0 comments:
Post a Comment