« - »

Look-up Tables: Service configuration

4 June 2009

Yesterday, I tossed out a copy of my new LookupTableService without going into much detail about the use of the service or how to go about setting it up. Today, I’d like to correct that and add a little context to the story.

Basically, I wanted to be able to get to table entries, not as they are defined on the database, but as they are configured in the system. This primarily relates to user-defined properties, which are stored on the database as property01, property02, property03, etc, but which have been given certain characteristics by their creator when the look-up table was defined. Such characteristics include things like name and type and length and source and so on, which is how I wanted to get to things, as opposed to property01, property02, and so forth. That’s why you see this little tidbit of code inside of the service:

  /**
   * <p>Returns the requested table entry.</p>
   *
   * @param tableName the name of the requested table
   * @param entryId the id of the requested entry
   * @return the requested table entry
   */
  private Map<String,Object> buildTableEntry(LookupTable lookupTable, LookupTableEntry lookupTableEntry) {
    Map<String,Object> entry = new TreeMap<String,Object>();

    entry.put("id", lookupTableEntry.getEntryId());
    entry.put("description", lookupTableEntry.getDescription());
    if (lookupTable.getProperties() != null && lookupTable.getProperties().size() > 0) {
      Iterator<LookupTableProperty> i = lookupTable.getProperties().iterator();
      while (i.hasNext()) {
        LookupTableProperty lookupTableProperty = i.next();
        int index = lookupTableProperty.getSequence();
        String name = lookupTableProperty.getName();
        if (StringUtils.isEmpty(lookupTableEntry.getProperty(index))) {
          entry.put(name, "");
        } else {
          entry.put(name, lookupTableEntry.getProperty(index));
        }
      }
    }

    return entry;
  }

The current version just drops the value string into the map at this point, but I have visions of one day using the property’s “type” value to convert the data in to an Integer or Date or whatever object might be appropriate based on the “type” … but I was just too lazy to work all of that out right at the moment.

The service is set up to be configured by Spring, so to make it work, you need to add a little something to your applicationContext.xml file:

<bean id="lookupTableService" class="org.restafarian.core.service.LookupTableService">
  <property name="lookupTableManager" ref="lookupTableManager"/>
  <property name="lookupTableEntryManager" ref="lookupTableEntryManager"/>
  <property name="context" value="${lookup.table.service.context}"/>
</bean>

The service is set up to take advantage of the recent addition of “context” to the Look-up Table framework, so you have to specify which context you want to work with. If you need to access tables from more than one context, say the global context and your application’s specific context, you can simply define an instance of the service for each context required, reusing the same data manager beans for each:

<bean id="globalLookupTableService" class="org.restafarian.core.service.LookupTableService">
  <property name="lookupTableManager" ref="lookupTableManager"/>
  <property name="lookupTableEntryManager" ref="lookupTableEntryManager"/>
  <property name="context" value="global"/>
</bean>

<bean id="applLookupTableService" class="org.restafarian.core.service.LookupTableService">
  <property name="lookupTableManager" ref="lookupTableManager"/>
  <property name="lookupTableEntryManager" ref="lookupTableEntryManager"/>
  <property name="context" value="${my.application.context}"/>
</bean>

Once Spring injects the bean with all of the dependencies and turns it over to your application, then you can just use it to grab tables and table entries as needed:

countryTable = globalLookupTableService.getTable("country");

Now that that is done, I need to get busy putting it to work!


http://blog.restafarian.org/2009/06/look-up-tables-service-configuration/

Leave a reply

You must be logged in to post a comment.