I want a service to populate the Map. I just want to access the map from the Application context.
I was thinking of calling the service from a constructor and populating a property Map keyValueMap:
Code:
public class RefData {
private ArrayList RefDatalist;
private Map keyValueMap;
public static Logger logger = Logger.getLogger(RefData.class);
public RefData() {
RefDataService service = new RefDataService();
try {
keyValueMap = service.getRefDataMap();
}
catch (Throwable th) {
logger.info("An Exception has been thrown.");
logger.info(th.getMessage());
throw new ApplicationRuntimeException (th);
}
}
}
I then want to store this map, built in the constructor, in the Application Context for the presentation layer to access.
The service builds the reference data dynamically so I am not sure that the Map element will work but maybe just constructor injection:
Here is how I build the Map:
public class GetRefDataService extends HibernateDaoSupport {
private String key;
private PersistSession persist;
/**
*
*/
public GetRefDataService(PersistSession persist) {
setPersist(persist);
}
public Map buildKeyValueMap() throws Throwable {
RefData refData = new RefData();
Map keyValueMap = new HashMap();
ArrayList keys = refData.returnCodeTypeList();
for (int i=0; i<keys.size();i++) {
String key = keys.get(i).toString();
Iterator value = getTextForCode(key);
keyValueMap.put(key, value);
}
return keyValueMap;
}
public Map execute() throws Throwable {
Map refDataMap = buildKeyValueMap();
return refDataMap;
}
private Iterator getTextForCode(String key) throws Throwable {
List list = null;
String refDataType = key;
//Replace "find" with findByNamedQuery here!**************************************************************
list = persist.find("from REF_DATA in class ReferenceData where REF_DATA_TYPE = '"+ refDataType + "'");
return list.iterator();
}
//Accessors here
}