private QbeTemplate parse(SourceBean template) {
QbeTemplate qbeTemplate = null;
String templateName;
SourceBean datamartSB, querySB, formSB, worksheetSB;
String dmName;
SourceBean modalitySB;
List modalities;
SourceBean compositeModalitySB;
SourceBean functionalitiesSB;
JSONObject formJSONTemplate, formValuesJSONTemplate, queryJSON, worksheetJSONTemplate;
try {
qbeTemplate = new QbeTemplate();
templateName = template.getName();
logger.debug("Parsing template [" + templateName + "] ...");
Assert.assertNotNull(templateName, "Root tag cannot be not be null");
if(!TAG_ROOT_COMPOSITE.equalsIgnoreCase(templateName)
&& !TAG_ROOT_NORMAL.equalsIgnoreCase(templateName)){
QbeTemplateParseException e = new QbeTemplateParseException("Malformed template structure");
e.setDescription("template root tag cannot be equals to [" + templateName +"]. " +
"It must be equal to [" + TAG_ROOT_NORMAL + "] or [" + TAG_ROOT_COMPOSITE + "]");
e.addHint("Check document template in document details page");
throw e;
}
boolean isComposite;
isComposite = TAG_ROOT_COMPOSITE.equalsIgnoreCase(templateName);
qbeTemplate.setComposite( isComposite );
modalities = new ArrayList();
dmName = null;
if(qbeTemplate.isComposite()) {
List qbeList;
SourceBean qbeSB;
String dblink;
logger.debug("The QBE described in the template is of type COMPOSITE");
qbeList = template.getAttributeAsList(TAG_ROOT_NORMAL);
for(int i = 0; i < qbeList.size(); i++) {
qbeSB = (SourceBean)qbeList.get(i);
// DATAMART block
if(qbeSB.containsAttribute(TAG_DATAMART)) {
datamartSB = (SourceBean)qbeSB.getAttribute(TAG_DATAMART);
dmName = (String)datamartSB.getAttribute(PROP_DATAMART_NAME);
Assert.assertTrue(!StringUtilities.isEmpty(dmName), "Attribute [" + PROP_DATAMART_NAME +"] in tag [" + TAG_DATAMART + "] must be properly defined");
qbeTemplate.addDatamartName( dmName );
dblink = (String)datamartSB.getAttribute(PROP_DATAMART_DBLINK);
if(dblink != null) {
qbeTemplate.setDbLink( dmName, dblink );
}
} else {
Assert.assertUnreachable("Missing compolsury tag [" + TAG_DATAMART + "]");
}
// MODALITY block
if(qbeSB.containsAttribute(TAG_MODALITY)) {
modalitySB = (SourceBean)qbeSB.getAttribute(TAG_MODALITY);
modalities.add(modalitySB);
} else {
logger.debug("Qbe template associated to datamart [" + dmName + "] does not contain tag [" + TAG_MODALITY +"] so it will be not profiled");
}
}
} else {
logger.debug("The QBE described in the template is of type STANDARD");
// DATAMART block
if(template.containsAttribute(TAG_DATAMART)) {
datamartSB = (SourceBean)template.getAttribute(TAG_DATAMART);
dmName = (String)datamartSB.getAttribute(PROP_DATAMART_NAME);
Assert.assertTrue(!StringUtilities.isEmpty(dmName), "Attribute [" + PROP_DATAMART_NAME +"] in tag [" + TAG_DATAMART + "] must be properly defined");
qbeTemplate.addDatamartName( dmName );
} else {
Assert.assertUnreachable("Missing compolsury tag [" + TAG_DATAMART + "]");
}
// MODALITY block
if(template.containsAttribute(TAG_MODALITY)) {
modalitySB = (SourceBean)template.getAttribute(TAG_MODALITY);
modalities.add(modalitySB);
} else {
logger.debug("Qbe template does not contain tag [" + TAG_MODALITY +"] so it will be not profiled");
}
// query block
if(template.containsAttribute(TAG_QUERY)) {
querySB = (SourceBean) template.getAttribute(TAG_QUERY);
queryJSON = new JSONObject(querySB.getCharacters());
qbeTemplate.setProperty("query", queryJSON);
} else {
logger.debug("Qbe template does not contain tag [" + TAG_QUERY +"]");
}
// form block
if(template.containsAttribute(TAG_FORM)) {
formSB = (SourceBean) template.getAttribute(TAG_FORM);
formJSONTemplate = new JSONObject(formSB.getCharacters());
qbeTemplate.setProperty("formJSONTemplate", formJSONTemplate);
} else {
logger.debug("Qbe template does not contain tag [" + TAG_FORM +"]");
}
// form values block
if(template.containsAttribute(TAG_FORM_VALUES)) {
formSB = (SourceBean) template.getAttribute(TAG_FORM_VALUES);
formValuesJSONTemplate = new JSONObject(formSB.getCharacters());
qbeTemplate.setProperty("formValuesJSONTemplate", formValuesJSONTemplate);
} else {
logger.debug("Qbe template does not contain tag [" + TAG_FORM_VALUES +"]");
}
// worksheet block
if(template.containsAttribute(TAG_WORKSHEET_DEFINITION)) {
worksheetSB = (SourceBean) template.getAttribute(TAG_WORKSHEET_DEFINITION);
worksheetJSONTemplate = new JSONObject(worksheetSB.getCharacters());
qbeTemplate.setProperty("worksheetJSONTemplate", worksheetJSONTemplate);
} else {
logger.debug("Qbe template does not contain tag [" + TAG_WORKSHEET_DEFINITION +"]");
}
}
compositeModalitySB = new SourceBean(TAG_MODALITY);
for(int i = 0; i < modalities.size(); i++) {
modalitySB = (SourceBean)modalities.get(i);
String recursiveFilteringAttr = (String)modalitySB.getAttribute(QbeXMLModelAccessModality.ATTR_RECURSIVE_FILTERING);
if(!StringUtilities.isEmpty(recursiveFilteringAttr)) {
compositeModalitySB.setAttribute(QbeXMLModelAccessModality.ATTR_RECURSIVE_FILTERING, recursiveFilteringAttr);
}
List tables = modalitySB.getAttributeAsList(TAG_MODALITY_TABLE);
for(int j = 0; j < tables.size(); j++) {
SourceBean tableSB = (SourceBean)tables.get(j);
compositeModalitySB.setAttribute(tableSB);
}
}
if(compositeModalitySB != null && compositeModalitySB.getAttribute(TAG_MODALITY_TABLE) != null) {
QbeXMLModelAccessModality datamartModelAccessModality = new QbeXMLModelAccessModality(compositeModalitySB);
qbeTemplate.setDatamartModelAccessModality(datamartModelAccessModality);
}
if(template.containsAttribute(TAG_EXTERNAL_SERVICES)) {
SourceBean extServiceConfig = (SourceBean) template.getAttribute(TAG_EXTERNAL_SERVICES);
List services = extServiceConfig.getAttributeAsList(TAG_EXTERNAL_SERVICE);
Iterator it = services.iterator();
while (it.hasNext()) {
SourceBean aServiceConfig = (SourceBean) it.next();
logger.debug("Reading external service configuration....");
String description = (String) aServiceConfig.getAttribute(PROP_SERVICE_DESCRIPTION);
logger.debug("Description = [" + description + "]");
String endpoint = (String) aServiceConfig.getAttribute(PROP_SERVICE_ENDPOINT);
logger.debug("Endpoint = [" + endpoint + "]");
String operation = (String) aServiceConfig.getAttribute(PROP_SERVICE_OPERATION);
logger.debug("Operation = [" + operation + "]");
String requiredColumns = (String) aServiceConfig.getAttribute(PROP_SERVICE_REQUIREDCOLUMNS);
logger.debug("Required columns = [" + requiredColumns + "]");
if (description == null || description.trim().equals("")) {
logger.error("External service configuration is not valid: " + PROP_SERVICE_DESCRIPTION + " attribute is mandatory.");
QbeTemplateParseException e = new QbeTemplateParseException("Wrong external service configuration");
e.setDescription("External service configuration is not valid: " + PROP_SERVICE_DESCRIPTION