Browse Source

groovy stuff

kpmy 8 năm trước cách đây
mục cha
commit
3af4c44f9e

+ 6 - 0
pom.xml

@@ -87,6 +87,12 @@
             <artifactId>javax.inject</artifactId>
             <version>1</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.codehaus.groovy</groupId>
+            <artifactId>groovy-all</artifactId>
+            <version>2.4.9</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 33 - 1
src/main/java/in/ocsf/report/app/controller/TestController.java

@@ -2,17 +2,25 @@ package in.ocsf.report.app.controller;/* kpmy 05.03.2017 */
 
 import com.querydsl.collections.CollQueryFactory;
 import com.querydsl.core.Tuple;
+import com.querydsl.core.types.Predicate;
 import com.querydsl.jpa.impl.JPAQueryFactory;
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
 import in.ocsf.report.api.ReportPrototype;
 import in.ocsf.report.api.StagePrototype;
 import in.ocsf.report.api.TablePrototype;
 import in.ocsf.report.app.object.Address;
 import in.ocsf.report.app.repo.AddressRepository;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.ResourceUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.io.File;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.util.List;
 
 import static in.ocsf.report.app.object.QAddress.address;
@@ -40,8 +48,21 @@ public class TestController {
         return null;
     }
 
+    private Processor proxify(Object obj) {
+        return (Processor) Proxy.newProxyInstance(obj.getClass().getClassLoader(),
+                new Class[]{Processor.class},
+                new InvocationHandler() {
+                    @Override
+                    public Object invoke(Object proxy, Method method, Object[] args)
+                            throws Throwable {
+                        Method m = obj.getClass().getMethod(method.getName());
+                        return m.invoke(obj, args);
+                    }
+                });
+    }
+
     @RequestMapping("test/repo/address")
-    public List<Address> addr() {
+    public List<Address> addr() throws Exception {
         List<Address> top = addrRepo.findActualTop();
         for (Address address : top) {
             List<Address> next = addrRepo.findAll(where(hasParent(address.getGuid())).and(isActual()));
@@ -60,6 +81,17 @@ public class TestController {
         Tuple yarres = jpaQueryFactory.select(address.name, address.region).from(address).where(address.region.like("76").and(address.parentGuid.isNull())).fetchFirst();
 
         String yarName2 = yarres.get(address.name);
+
+        Binding binding = new Binding();
+        binding.setVariable("address", address);
+
+        GroovyShell groovyShell = new GroovyShell(binding);
+        Processor p = proxify(groovyShell.evaluate(new File(ResourceUtils.getURL("classpath:/groovy/fias/address/QueryAddress.groovy").getFile())));
+        Iterable<Address> yars0 = addrRepo.findAll(p.process());
         return top;
     }
+
+    public interface Processor {
+        Predicate process();
+    }
 }

+ 13 - 0
src/main/resources/groovy/fias/address/QueryAddress.groovy

@@ -0,0 +1,13 @@
+package groovy.fias.address
+
+import com.querydsl.core.types.Predicate
+
+import static in.ocsf.report.app.object.QAddress.address
+
+/* kpmy 13.03.2017 */
+
+Predicate process() {
+    return address.region.like("76").and(address.parentGuid.isNull())
+}
+
+return this