Groovy là ngôn ngữ lập trình chạy theo mô hình lập trình chức năng (functional programming) thực thi các câu lệnh trên JVM (Java Virtual Machine – máy ảo Java). Để tự động hóa các nghiệp vụ kinh doanh trong phần mềm ERP, như trong Apache Ofbiz, có rất nhiều đoạn chương trình viết bằng *.groovy để thực hiện các nghiệp vụ một cách tự động theo nhiều bước. Cú pháp viết file build.gradle là sử dụng cú pháp của Apache Groovy. Môi trường phát triển
Ví dụ: File \ofbiz-framework\applications\order\groovyScripts\allocationplan\CreateAllocationPlan.groovy (Apache Ofbiz version 17.12.04, bản quyền Apache Software Foundation) import org.apache.ofbiz.entity.condition.EntityOperator import org.apache.ofbiz.entity.condition.EntityCondition import org.apache.ofbiz.order.order.OrderReadHelper import org.apache.ofbiz.party.party.PartyHelper allocationPlanInfo = [:] itemList = [] isPlanAlreadyExists = false productId = parameters.productId planName = parameters.planName if (productId) { orderedQuantityTotal = 0.0 orderedValueTotal = 0.0 reservedQuantityTotal = 0.0 ecl = EntityCondition.makeCondition([ EntityCondition.makeCondition("productId", EntityOperator.EQUALS, productId), EntityCondition.makeCondition("statusId", EntityOperator.IN, ["ALLOC_PLAN_CREATED", "ALLOC_PLAN_APPROVED"]), EntityCondition.makeCondition("planTypeId", EntityOperator.EQUALS, "SALES_ORD_ALLOCATION")], EntityOperator.AND) allocationPlanHeader = from("AllocationPlanHeader").where(ecl).queryFirst() if (allocationPlanHeader == null) { ecl = EntityCondition.makeCondition([ EntityCondition.makeCondition("productId", EntityOperator.EQUALS, productId), EntityCondition.makeCondition("orderStatusId", EntityOperator.EQUALS, "ORDER_APPROVED"), EntityCondition.makeCondition("orderTypeId", EntityOperator.EQUALS, "SALES_ORDER")], EntityOperator.AND) orderAndItemList = from("OrderHeaderAndItems").where(ecl).queryList() orderAndItemList.each { orderAndItem -> itemMap = [:] salesChannelEnumId = orderAndItem.salesChannelEnumId itemMap.salesChannelEnumId = salesChannelEnumId salesChannel = from("Enumeration").where("enumId", salesChannelEnumId).queryOne() if (salesChannel) { itemMap.salesChannel = salesChannel.description } orh = new OrderReadHelper(delegator, orderAndItem.orderId) placingParty = orh.getPlacingParty() if (placingParty != null) { itemMap.partyId = placingParty.partyId itemMap.partyName = PartyHelper.getPartyName(placingParty) } itemMap.orderId = orderAndItem.orderId itemMap.orderItemSeqId = orderAndItem.orderItemSeqId itemMap.estimatedShipDate = orderAndItem.estimatedShipDate unitPrice = orderAndItem.unitPrice cancelQuantity = orderAndItem.cancelQuantity quantity = orderAndItem.quantity if (cancelQuantity != null) { orderedQuantity = quantity.subtract(cancelQuantity) } else { orderedQuantity = quantity } orderedValue = orderedQuantity.multiply(unitPrice) orderedQuantityTotal = orderedQuantityTotal.add(orderedQuantity) orderedValueTotal = orderedValueTotal.add(orderedValue) itemMap.orderedQuantity = orderedQuantity itemMap.orderedValue = orderedValue // Reserved quantity reservedQuantity = 0.0 reservations = from("OrderItemShipGrpInvRes").where("orderId", orderAndItem.orderId, "orderItemSeqId", orderAndItem.orderItemSeqId).queryList() reservations.each { reservation -> if (reservation.quantity) { reservedQuantity += reservation.quantity } } reservedQuantityTotal = reservedQuantityTotal.add(reservedQuantity) itemMap.reservedQuantity = reservedQuantity itemList.add(itemMap) } } else { isPlanAlreadyExists = true } allocationPlanInfo.orderedQuantityTotal = orderedQuantityTotal allocationPlanInfo.orderedValueTotal = orderedValueTotal allocationPlanInfo.reservedQuantityTotal = reservedQuantityTotal } allocationPlanInfo.isPlanAlreadyExists = isPlanAlreadyExists allocationPlanInfo.itemList = itemList context.allocationPlanInfo = allocationPlanInfoĐoạn code trên tạo kế hoạch phan bổ dựa trên Mã sản phẩm, Mã trạng thái, Mã kế hoạch; phân bổ đơn hàng vào các kênh bán hàng với số lượng cụ thể. Liên kết tải về https://dl.bintray.com/groovy/maven/apache-groovy-sdk-3.0.6.zip (71,2 MB), giải nén ra (thành khoảng 242 MB). Source code: https://github.com/apache/groovy Groovy 3.0.6 yêu cầu phải có JDK phiên bản từ 1.8 trở lên. Trong thư mục sau giải nén, tìm thư mục bin , giả sử D:\tools\groovy-3.0.6\bin , khai báo đường dẫn này trong biến môi trường của Windows. Chạy lệnh kiểm tra phiên bản của Groovy groovy --versionKết quả là Picked up _JAVA_OPTIONS: -Xmx512M Groovy Version: 3.0.6 JVM: 1.8.0_261 Vendor: Oracle Corporation OS: Windows 10Khai báo Groovy SDK trong IntelliJ IDEA 2020 Viết chương trình Groovy đầu tiên, file Hello.groovy println("Hello, Hanoi!");Kết quả: Hello, Hanoi!Xin chúc mừng, vậy là bạn đã viết chương trình Groovy đầu tiên. Các từ khóa (keyword) trong Groovy là as, assert, break, case, catch, class, const, continue, def, default, do, else, enum, extends, false, finally, for, goto, if, implements, import, in, instanceof, interface, new, null, package, return, super, switch, this, throws, throw, trait, true, try, var, while . Các định danh hợp lệ và không hợp lệ # Hợp lệ def name def item3 def with_under_score def $startByDollarSign // Không hợp lệ def 3abc def a+b def a#b // Hợp lệ foo.as2 foo.asertAA foo.breakXYZ foo.case99 foo.catch42Kiểu dữ liệu map def aha = [:] aha.'xx' = "XXX many XX"; aha.'girl' = "BIG GIRL"; assert aha.'xx' == "XXX many XX"; assert aha.'girl' == 'BIG GIRL'Quy định đặt tên định danh: Bắt đầu bởi chữ cái, ký hiệu dollar sign, hoặc dấu gạch dưới. Không được phép bắt đầu bởi số. Chữ cái là các ký tự nằm trong khoảng
Một số định danh đặc biệt // https://www.fileformat.info/info/unicode/char/00c0/index.htm def À = 1 assert À == 1Cách sử dụng map File Hello.groovy có nội dung map = [:] map.'''triple abc''' map."""triple xyz""" map./love string/ map.$/doll love string/$ def something = [:] def firstName = "Van" something."NguyenBich-${firstName}" = "Nguoi yeu cu" assert something."NguyenBich-Van" == "Nguoi yeu cu" println(something.'NguyenBich-Van') println(something."NguyenBich-Van")Lưu ý với cách đặt tên key trong kiểu dữ liệu map, những thứ dưới đây là cho phép foo = [:] foo./yeu em/ = "Yêu em Vân" foo.$/bo em trang/$ = "Bỏ em Trang"Chạy lệnh groovy Hello.groovyKết quả là Picked up _JAVA_OPTIONS: -Xmx512M Nguoi yeu cu Nguoi yeu cuCái này gọi là Groovy GString, khả năng nội suy xâu ký tự của Groovy. Javadoc https://docs.groovy-lang.org/latest/html/api/groovy/lang/GString.html Các ký tự đặc biệt cho phép trong string là
Ví dụ liên quan đến GString def sum = "The sum of 2 and 3 equals ${2 + 3}" assert sum.toString() == 'The sum of 2 and 3 equals 5' def sum2 = "The sum of 2 and 3 equals ${def a = 1; def b = 6; a + b}" assert sum2.toString() == 'The sum of 2 and 3 equals 7' def nguoi_nao_do = [ten: 'Van', tuoi: 33] assert "$nguoi_nao_do.ten hien tai $nguoi_nao_do.tuoi tuoi" == "Van hien tai 33 tuoi" def so_pi = 3.1415926535 def sParameterLessClosure = "1 + 2 == ${-> 3}" assert sParameterLessClosure == '1 + 2 == 3' def sOneParameterClosure = "1 + 2 == ${w -> w << 3}" assert sOneParameterClosure == '1 + 2 == 3'Ví dụ liên quan đến eager GString và lazy GString def number = 1 // Mục 1 def eagerGString = "value == ${number}" def lazyGString = "value == ${-> number}" assert eagerGString == "value == 1" // Mục 2 assert lazyGString == "value == 1" // Mục 3 number = 2 // Mục 4 assert eagerGString == "value == 1" // Mục 5 assert lazyGString == "value == 2" // Mục 6
Một số dạng đặc biệt của string def fooPattern = /.*chu_cuoi.*/ assert fooPattern == '.*chu_cuoi.*' def escapeSlash = /The character \/ is a forward slash/ assert escapeSlash == 'The character / is a forward slash' def nhieu_dong_chuoi_tron = /mot hai ba bon/ assert nhieu_dong_chuoi_tron.contains('\n') def mau_sac = 'xanh' def noi_suy_chuoi_tron = /mot chiec xe mau ${mau_sac}/ assert noi_suy_chuoi_tron == 'mot chiec xe mau xanh'Nguồn: https://viblo.asia/p/java-overview-phan-1-spring-vs-spring-boot-vs-spring-mvc-3P0lPDpblox |