[rules-users] Using Advanced Enumeration Concepts in guvnor

Stephen Masters stephen.masters at me.com
Tue Jan 7 09:00:51 EST 2014


There may be better ways of doing it in newer versions of Guvnor, but the way I did it for for 5.3 made use of Spring. Unfortunately, this did not work in 5.5.

Your code will need to do all of its database connectivity itself. Therefore it does need to include the DB connectivity jars. I ensured that this was as easy as possible by using the Maven Shade plugin to build a jar which contained all of my dependencies. If your connection details are in a properties file, then it’s easy enough to reference that outside the application Jar.

The code below is extracted from an application I wrote to do this using Spring. Possibly if you are using Seam throughout your application then as long as you are using the same version that Guvnor is built upon, then it might be easier.

    public List<String> isoCodes() {
        List<String> values = new ArrayList<String>();
        for (Currency ccy : finder().findCurrencies()) {
            values.add(ccy.getISO());
        }
        Collections.sort(values);
        return values;
    }

    private static CurrencyFinder finder() {
        return (CurrencyFinder) GuvnorEnumContext.getContext().getBean("currencyFinder");
    }


/**
 * Maintains a static reference to a Spring {@link ApplicationContext}, and a
 * static getContext() method, which will initialize the context if necessary.
 */
public class GuvnorEnumContext {

    private static ApplicationContext ctx = null;

    GuvnorEnumContext() {
    }

    GuvnorEnumContext(ApplicationContext applicationContext) {
        ctx = applicationContext;
    }

    public static ApplicationContext getContext() {
        if (ctx == null) {
            ctx = new AnnotationConfigApplicationContext(GuvnorEnumConfig.class);
        }
        return ctx;
    }

}


/**
 * This configuration class is used by the Guvnor enumeration services to set up
 * annotation-based injection of beans throughout the application.
 */
@EnableSpringConfigured
@Configuration
@ComponentScan("com.yourcompany")
public class GuvnorEnumConfig {

}






On 7 Jan 2014, at 13:30, abhinay_agarwal <abhinay_agarwal at infosys.com> wrote:

> The following sentences are taking directly from the user guide:
> 
> / 'Person.age' : (new com.yourco.DataHelper()).getListOfAges()
> 
> This assumes you have a class called "DataHelper" which has a method
> "getListOfAges()" which returns a List of strings (and is on the classpath).
> You can of course mix these "dynamic" enumerations with fixed lists. You
> could for example load from a database using JDBC. The data enumerations are
> loaded the first time you use the guided editor in a session./
> 
> Now, I would like to know how guvnor would connect to the database ?
> 
> Inside the getListOfAges(), I want to hit the database and get the list of
> ages, put it inside a Map<String, List&lt;String>>.
> 
> The example that is shown in the user guide is again hard coding the values.
> 
>> From the user guide :
> 
> /public class SampleDataSource2 {
>    public Map<String>, List<String> loadData() {
>       Map data = new HashMap();
>       List d = new ArrayList();
>       d.add("value1");
>      d.add("value2");
>      data.put("Fact.field", d);
>      return data;
>  }
> }/
> 
> 
> AFAIK, the jar of this class SampleDataSource2 will be uploaded to guvnor,
> so should I keep the connection details to database inside the same class ?
> If yes,
> 
> 1. How will guvnor be able to directly connect to the database ? 
> 2. Should I add the jars for database connectivity as well ?
> 3. Is keeping the connection details inside the jar, not a security threat ?
> 
> I am not sure if I understand completely how this enumeration works, please
> feel free to guide me, if I am wrong somewhere.
> 
> Regards,
> Abhinay
> 
> 
> 
> 
> --
> View this message in context: http://drools.46999.n3.nabble.com/Using-Advanced-Enumeration-Concepts-in-guvnor-tp4027563.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users




More information about the rules-users mailing list