protected void parseKPIs(NodeList xmlNodes, ImportResults results) throws Exception {
for (int i = 0; i < xmlNodes.getLength(); i++) {
Node item = xmlNodes.item(i);
if (item.getNodeName().equals("kpi")) {
try {
KPI kpi = DataDisplayerServices.lookup().getKPIManager().createKPI();
Node codeNode = item.getAttributes().getNamedItem("code");
if (codeNode != null) kpi.setCode(StringEscapeUtils.unescapeXml(codeNode.getNodeValue()));
NodeList subNodes = item.getChildNodes();
for (int j = 0; j < subNodes.getLength(); j++) {
item = subNodes.item(j);
// Description
if (item.getNodeName().equals("description") && item.hasChildNodes()) {
String description = item.getFirstChild().getNodeValue();
Locale locale = LocaleManager.currentLocale();
Node languageNode = item.getAttributes().getNamedItem("language");
if (languageNode != null) locale = new Locale(languageNode.getNodeValue());
kpi.setDescription(StringEscapeUtils.unescapeXml(description), locale);
}
// Provider
if (item.getNodeName().equals("provider") && item.hasAttributes()) {
String providerCode = item.getAttributes().getNamedItem("code").getNodeValue();
DataProvider provider = results.getDataProviderByCode(providerCode);
if (provider == null) provider = DataDisplayerServices.lookup().getDataProviderManager().getDataProviderByCode(providerCode);
if (provider == null) {
results.getMessages().add(new ImportExportMessage(ImportExportMessage.PROVIDER_CODE_NOT_FOUND, new Object[] {providerCode}));
throw new RuntimeException("Continue with the next KPI...");
}
kpi.setDataProvider(provider);
}
// Displayer
if (item.getNodeName().equals("displayer") && item.hasAttributes() && item.hasChildNodes()) {
String typeUid = item.getAttributes().getNamedItem("type").getNodeValue();
DataDisplayerType type = dataDisplayerManager.getDisplayerTypeByUid(typeUid);
if (type == null) {
results.getMessages().add(new ImportExportMessage(ImportExportMessage.DISPLAYER_TYPE_NOT_FOUND, new Object[] {typeUid}));
throw new RuntimeException("Continue with the next KPI...");
}
DataDisplayerRenderer renderer = null;
Node rendererNode = item.getAttributes().getNamedItem("renderer");
if (rendererNode != null) {
String rendUid = rendererNode.getNodeValue();
renderer = dataDisplayerManager.getDisplayerRendererByUid(rendUid);
if (renderer == null) {
results.getMessages().add(new ImportExportMessage(ImportExportMessage.DISPLAYER_RENDERER_NOT_FOUND, new Object[] {rendUid}));
throw new RuntimeException("Continue with the next KPI...");
}
}
DataDisplayer displayer = type.getXmlFormat().parse(subNodes, results);
if (results.getMessages().hasErrors()) {
throw new Exception(results.getMessages().get(0).toString());
}
displayer.setDataDisplayerType(type);
displayer.setDataDisplayerRenderer(renderer);
kpi.setDataDisplayer(displayer);
}
}
results.addKPI(kpi);
} catch (Exception e) {
// Continue with the next KPI...