Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
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;
import org.springframework.web.bind.annotation.PathVariable;
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")
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 4 additions & 3 deletions dsl-scripts/simple.mydsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apartment {
location "location"
location "location11"
price 1
sqft 1
phone 'phone'
realtorName 'realtorName'
mail 'mail'
}

apartment("location", {
apartment("location22", {
price 1
sqft 1
phone 'phone'
Expand All @@ -16,10 +16,11 @@ apartment("location", {
})


apartment("location") {
apartment("location33") {
price 1
sqft 1
phone 'phone'
realtorName 'realtorName'
mail 'mail'
}

26 changes: 26 additions & 0 deletions dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy
Original file line number Diff line number Diff line change
@@ -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()
}
}
32 changes: 32 additions & 0 deletions dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion dsl/src/main/resources/idea.gdsl
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
}