DB connection for DDL in groovy

DANGER!!!

In hybris hac, you cannot run DDL (data definition language) commands. You need unprotected connection to db. You can use below example. DDL commands are dangerous!!!

import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

def config = spring.getBean("configurationService").getConfiguration();

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(config.getString("db.driver"));
dataSource.setUrl(config.getString("db.url"));
dataSource.setUsername(config.getString("db.username"));
dataSource.setPassword(config.getString("db.password"));
JdbcTemplate jdbc = new JdbcTemplate(dataSource)
jdbc.execute("ALTER TABLE MYTABLE ALTER (P_MYFIELD BIGINT)");

Update object private property with groovy

You can update private field an object with below sample code.

def myBean = spring.getBean("myBean");

def f = myBean.getClass().getDeclaredField("myField");
f.setAccessible(true);
f.set(myBean, "new string value");

Getting external IP with groovy

URL url = new URL("http://mkysoft.com/ip.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
return org.apache.commons.io.IOUtils.toString(is);