diff --git a/README.md b/README.md index ecd8898..2f9346c 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Sandbox to play with spring cloud features | Realtor service| Realtor Api Service | 8080| To call other services used Feign, RestTemplate | | Storage service| Storage of Apartment Records Service | 8091| H2 based service for ApartmentRecord data storage. | | API gateway service| Zull API Gateway Service | 8090| | +| DSL executor service | DSL executor service | 8088| | # Dev diff --git a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java index fd9f1a8..0ffaf89 100644 --- a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java +++ b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java @@ -1,13 +1,7 @@ package com.lohika.jclub.dsl.service; -import groovy.lang.GroovyShell; -import groovy.util.DelegatingScript; - import com.lohika.jclub.dsl.MyDsl; -import com.lohika.jclub.rating.client.RatingServiceClient; -import com.lohika.jclub.storage.client.StorageServiceClient; -import org.codehaus.groovy.control.CompilerConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; @@ -15,10 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; @RestController @RequestMapping(path = "/dsl") @@ -28,27 +19,10 @@ public class DslController { private String basepath; @Autowired - private StorageServiceClient storageServiceClient; - - @Autowired - private RatingServiceClient ratingServiceClient; + private DslService dslService; @GetMapping(path = "/{scriptName}") - public Object runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException { - File file = new File(basepath + scriptName + ".groovy"); - String script = new String(Files.readAllBytes(Paths.get(file.getPath()))); - - MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient); - - CompilerConfiguration configuration = new CompilerConfiguration(); - configuration.setScriptBaseClass(DelegatingScript.class.getName()); - - GroovyShell groovy = new GroovyShell(configuration); - - DelegatingScript delegatingScript = (DelegatingScript) groovy.parse(script); - delegatingScript.setDelegate(dsl); - delegatingScript.run(); - - return dsl; + public MyDsl runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException { + return dslService.runScript(scriptName); } } diff --git a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java new file mode 100644 index 0000000..52a54ee --- /dev/null +++ b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java @@ -0,0 +1,57 @@ +package com.lohika.jclub.dsl.service; + +import groovy.lang.GroovyShell; +import groovy.util.DelegatingScript; + +import com.lohika.jclub.dsl.MyDsl; +import com.lohika.jclub.rating.client.RatingServiceClient; +import com.lohika.jclub.storage.client.StorageServiceClient; + +import org.codehaus.groovy.control.CompilerConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +@Service +public class DslService { + private static final String DSL_EXTENSION = ".mydsl"; + + @Value("${dsl.basepath}") + private String basepath; + + @Autowired + private StorageServiceClient storageServiceClient; + + @Autowired + private RatingServiceClient ratingServiceClient; + + public MyDsl runScript(String scriptName) throws IOException { + String script = getScriptByName(scriptName); + return run(script); + } + + private String getScriptByName(String scriptName) throws IOException { + File file = new File(basepath + scriptName + DSL_EXTENSION); + return new String(Files.readAllBytes(Paths.get(file.getPath()))); + } + + private MyDsl run(String script) { + MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient); + + CompilerConfiguration configuration = new CompilerConfiguration(); + configuration.setScriptBaseClass(DelegatingScript.class.getName()); + + GroovyShell groovy = new GroovyShell(configuration); + + DelegatingScript delegatingScript = (DelegatingScript) groovy.parse(script); + delegatingScript.setDelegate(dsl); + delegatingScript.run(); + + return dsl; + } +} diff --git a/dsl-scripts/simple.mydsl b/dsl-scripts/simple.mydsl index 26b0867..3d54975 100644 --- a/dsl-scripts/simple.mydsl +++ b/dsl-scripts/simple.mydsl @@ -1,5 +1,5 @@ apartment { - location "location" + location "location11" price 1 sqft 1 phone 'phone' @@ -7,7 +7,7 @@ apartment { mail 'mail' } -apartment("location", { +apartment("location22", { price 1 sqft 1 phone 'phone' @@ -16,10 +16,11 @@ apartment("location", { }) -apartment("location") { +apartment("location33") { price 1 sqft 1 phone 'phone' realtorName 'realtorName' mail 'mail' } + diff --git a/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy b/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy new file mode 100644 index 0000000..6edcb53 --- /dev/null +++ b/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy @@ -0,0 +1,26 @@ +package com.lohika.jclub.dsl + +import com.lohika.jclub.storage.client.Apartment +import groovy.transform.builder.Builder +import groovy.transform.builder.SimpleStrategy + +@Builder(prefix = "", builderStrategy = SimpleStrategy.class) +class ApartmentDsl { + String location + double price + double sqft + String phone + String realtorName + String mail + + def toEntity() { + Apartment.builder() + .location(location) + .price(price) + .sqft(sqft) + .phone(phone) + .realtorName(realtorName) + .mail(mail) + .build() + } +} diff --git a/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy b/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy index 1dd3c63..b5ea678 100644 --- a/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy +++ b/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy @@ -13,8 +13,40 @@ class MyDsl { private RatingServiceClient ratingServiceClient private StorageServiceClient storageServiceClient + def apartments = [] + MyDsl(RatingServiceClient ratingServiceClient, StorageServiceClient storageServiceClient) { this.storageServiceClient = storageServiceClient this.ratingServiceClient = ratingServiceClient } + + def rating(String location, double myPrice, double mySqft) { + Apartment apartment = Apartment.builder() + .sqft(mySqft) + .location(location) + .price(myPrice) + .build() + + ratingServiceClient.getRating(apartment).rating + } + + def apartment(Closure closure) { + ApartmentDsl apartment = new ApartmentDsl() + closure.delegate = apartment + closure() + + storageServiceClient.create(apartment.toEntity()) + apartments.add(apartment) + } + + def apartment(String location, Closure closure) { + ApartmentDsl apartment = new ApartmentDsl() + apartment.location = location + + closure.delegate = apartment + closure() + + storageServiceClient.create(apartment.toEntity()) + apartments.add(apartment) + } } diff --git a/dsl/src/main/resources/idea.gdsl b/dsl/src/main/resources/idea.gdsl index 621cb21..eb4859a 100644 --- a/dsl/src/main/resources/idea.gdsl +++ b/dsl/src/main/resources/idea.gdsl @@ -20,6 +20,6 @@ contributor([contributorBody]) { method name: 'sqft', type: 'void', params: [value: 'java.lang.Double'] method name: 'realtorName', type: 'void', params: [value: 'java.lang.String'] method name: 'mail', type: 'void', params: [value: 'java.lang.String'] + method name: 'phone', type: 'void', params: [value: 'java.lang.String'] } } -