-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from lvivJavaClub/dslLiveCodding
implemented simple DSL to create appartaments
- Loading branch information
Showing
7 changed files
with
124 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters