Visar inlägg med etikett GroovyWS. Visa alla inlägg
Visar inlägg med etikett GroovyWS. Visa alla inlägg

torsdag 16 januari 2014

DAO to Service

Compile and run separatelly!

       
//JAVA BEAN, Expando Bean works bad without binding of CXF
package test;

public class Customer
{

        private int id;
        private String lastname;
        private String firstname;
        private String city;
        
        
  public int getId()
  {
    return this.id;
  }
  
  public void setId(int paramInt)
  {
    this.id = paramInt;
  }
  
  public String getLastname()
  {
    return this.lastname;
  }
  
  public void setLastname(String paramString)
  {
    this.lastname = paramString;
  }
  
  public String getFirstname()
  {
    return this.firstname;
  }
  
  public void setFirstname(String paramString)
  {
    this.firstname = paramString;
  }
  
  public String getCity()
  {
    return this.city;
  }
  
  public void setCity(String paramString)
  {
    this.city = paramString;
  }

}

//Groovy DAO


package test
//In-memory DB 
@Grab(group='com.h2database', module='h2', version='1.3.168')

import groovy.sql.Sql

class CustomerDAO
{   


   
    private def sql
    public CustomerDAO()
    { 
        init()
    }
    private void init()
    {
        sql = Sql.newInstance("jdbc:h2:mem:db1", "sa", "sa", "org.h2.Driver")
        sql.execute("""CREATE TABLE Customers
                        (
                        id int,
                        lastname varchar(255),
                        firstname varchar(255),
                        city varchar(255)
                        ); 
                    """)

        sql.execute("""INSERT INTO CUSTOMERS (id, lastname, firstname, city) VALUES (1,'Enrot','Tommy','Stavanger');""") 
        println sql.rows("select * from CUSTOMERS")

    }
    Boolean addCustomer(Integer id, String lastname, String firstname, String city)
    {
        def r = sql.execute("""INSERT INTO CUSTOMERS (id, lastname, firstname, city)
                       VALUES ($id, $lastname, $firstname, $city); """)
        return r==null?false:true;
    }
    
    Customer getCustomerByLastname(String lastname)
    {              
       GroovyRowResult res =  sql.firstRow("select id,lastname,firstname,city from CUSTOMERS WHERE lastname=$lastname")
       println res.containsKey('id')
       println res.id+", "+res.lastname+", "+res.firstname+", "+res.city
      
       return  new Customer(id:res.id,lastname:res.lastname,firstname:res.firstname,city:res.city)
    }
}


//Kick on WS

@Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2')
import groovyx.net.ws.WSServer


def server = new WSServer()

server.setNode("test.CustomerDAO", "http://localhost:6999/CustomerService")

server.start()
       
 

onsdag 6 november 2013

Groovy Web Service

It's very simple to create a web service with Groovy, look a this:
       

@Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2')
import groovyx.net.ws.WSServer
import java.security.SecureRandom
 import java.math.BigInteger
class UtilsService
{
     double add(double arg0, double arg1)
     {
          return (arg0 + arg1)
     }

    double square(double arg0)
    {
         return (arg0 * arg0)
    }
 
   String randomPassword()
   {
       SecureRandom random = new SecureRandom()
       return new BigInteger(130, random).toString(32)
   }

   long random()
  {
         Random random = new Random();
         long randInt = random.nextLong()
        if (randInt < 0) 
        {  
            randInt = randInt*-1L; 
        } return randInt;
  }

  String randomPhrase()
  {
      String[] randomStrings = new String[4];
      Random random = new Random();
      for(int i = 0; i < randomStrings.length; i++)
     {
          char[] word = new char[random.nextInt(8)+3];
         // words of length 3 through 10. (1 and 2 letter words are boring.)
          for(int j = 0; j < word.length; j++)
         {
            word[j] = (char)(99 + random.nextInt(26));
         }
         randomStrings[i] = new String(word);
     }
     String words = "";
     for(String word:randomStrings)
     {
         words+= word+" ";      
     }
     return words.trim();
 }
 
}
def server = new WSServer()
//Start your service
server.setNode("UtilsService", "http://localhost:6980/UtilsService")
server.start()


Now you can test this with SoapUI or with this code:

       

@Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2')

import groovyx.net.ws.WSClient

def proxy = new WSClient("http://localhost:6980/UtilsService?WSDL", this.class.classLoader) proxy.initialize()

 result = proxy.add(23.88, 24.88)
println "You are sum is: "+result

Simple currency converter in Groovy Swing Style

Simple currency converter in Groovy Swing Style:

       
@Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2')

 import groovyx.net.ws.WSClient

class TryIt
{
      groovy.swing.SwingBuilder swing = new groovy.swing.SwingBuilder()
      def proxy = new WSClient("http://www.webservicex.net/CurrencyConvertor.asmx?WSDL",                       TryIt.class.classLoader)
      def currency = ['USD', 'EUR', 'CAD', 'GBP', 'AUD', 'SGD','SEK','DKK','NOK']
      def rate = 0.0
      def exc = 1.0
   
     void main()
    {
           proxy.initialize()
           def refresh = swing.action( name:'Refresh', closure:this.&refreshText, mnemonic:'R' )
           def frame = swing.frame(title:'Currency Demo')
           {
                  panel
                 {
                       label 'Currency rate from '
                       comboBox(id:'from', items:currency)
                       label ' to '
                      comboBox(id:'to', items:currency)
                      label ' rate '
                      textField(id:'currency', columns:10, rate.toString(),editable:false)
                      label ' exchange '
                      textField(id:'exchange', columns:10, exc.toString())
                      button(text:'Go !', action:refresh)
               }
          }
          frame.pack()
          frame.show()
   }
   def refreshText(event)
   {
          rate = proxy.ConversionRate(swing.from.getSelectedItem(), swing.to.getSelectedItem())                               swing.currency.text = rate
          swing.exchange.text = rate*Double.parseDouble(swing.exchange.text)
   }
}

new TryIt().main()