use of org.jaffa.rules.rulemeta.Rule in project jaffa-framework by jaffa-projects.
the class MetaDataWriter method write.
/**
* Writes the supplied meta data into the supplied source file
*/
public static void write(ClassMetaDataDto cmd) throws MetaDataWriterException {
if (cmd.getSourceFileName() == null) {
log.error("No source folder was supplied for meta data export.");
throw new MetaDataWriterException(MetaDataWriterException.SOURCE_NOT_FOUND);
}
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
} catch (javax.xml.parsers.ParserConfigurationException dbe) {
log.error("No source folder was supplied for meta data export.");
throw new MetaDataWriterException(MetaDataWriterException.PARSE_ERROR, null, dbe);
}
Element aop = document.createElement("aop");
Element metadata = document.createElement("metadata");
metadata.setAttribute("tag", "jaffa.rules");
metadata.setAttribute("class", cmd.getName());
if (cmd.getExecutionRealm() != null && !cmd.getExecutionRealm().equals(""))
metadata.setAttribute("execution-realm", cmd.getExecutionRealm());
if (cmd.getExtendsClass() != null && !cmd.getExtendsClass().equals(""))
metadata.setAttribute("extends-class", cmd.getExtendsClass());
if (cmd.getLanguage() != null && !cmd.getLanguage().equals(""))
metadata.setAttribute("language", cmd.getLanguage());
if (cmd.getCondition() != null && !cmd.getCondition().equals(""))
metadata.setAttribute("condition", cmd.getCondition());
List<RuleMetaDataDto> classRules = cmd.getRules();
if (classRules != null) {
for (RuleMetaDataDto classRule : classRules) {
Rule ruleInfo = RuleRepository.instance().getRuleByName(classRule.getRuleName());
Element propertyRule = document.createElement(classRule.getRuleName());
Map<String, String> params = classRule.getParameters();
Boolean customProcess = false;
// If label rule contains a token and a tokenValue then update applicationResources with the new token value
if (classRule.getRuleName().equals("label")) {
String token = null, value = null;
if (params != null) {
for (Map.Entry<String, String> param : params.entrySet()) {
if (param.getKey().equals("token")) {
token = param.getValue();
}
if (param.getKey().equals("tokenValue")) {
value = param.getValue();
}
}
}
if (token != null && value != null) {
try {
LabelHelper.setLabel(token, value);
} catch (FrameworkException e) {
log.debug(e.getLocalizedMessage());
}
customProcess = true;
}
}
if (!customProcess) {
if (params != null) {
for (Map.Entry<String, String> param : params.entrySet()) {
if (ruleInfo.getTextParameter() != null && ruleInfo.getTextParameter().equals(param.getKey())) {
propertyRule.setTextContent(param.getValue());
} else {
propertyRule.setAttribute(param.getKey(), param.getValue());
}
}
}
metadata.appendChild(propertyRule);
}
}
}
List<PropertyMetaDataDto> fields = cmd.getProperties();
List<String> processedProperties = new LinkedList<String>();
if (fields != null) {
for (PropertyMetaDataDto field : fields) {
if (processedProperties.indexOf(field.getPropertyName().toLowerCase()) >= 0) {
log.error("Property has been defined multiple times: " + field.getPropertyName());
throw new MetaDataWriterException(MetaDataWriterException.DUPLICATE_PROPERTY);
}
processedProperties.add(field.getPropertyName().toLowerCase());
Element property = document.createElement("property");
property.setAttribute("name", field.getPropertyName());
if (field.getExtendsProperty() != null && !field.getExtendsProperty().equals(""))
metadata.setAttribute("extends-property", field.getExtendsProperty());
if (field.getExtendsClass() != null && !field.getExtendsClass().equals(""))
metadata.setAttribute("extends-class", field.getExtendsClass());
if (field.getLanguage() != null && !field.getLanguage().equals(""))
metadata.setAttribute("language", field.getLanguage());
if (field.getCondition() != null && !field.getCondition().equals(""))
property.setAttribute("condition", field.getCondition());
List<RuleMetaDataDto> rules = field.getRules();
if (rules != null) {
for (RuleMetaDataDto rule : rules) {
Rule ruleInfo = RuleRepository.instance().getRuleByName(rule.getRuleName());
Element propertyRule = document.createElement(rule.getRuleName());
Map<String, String> params = rule.getParameters();
if (params != null) {
for (Map.Entry<String, String> param : params.entrySet()) {
if (ruleInfo.getTextParameter() != null && ruleInfo.getTextParameter().equals(param.getKey())) {
propertyRule.setTextContent(param.getValue());
} else {
propertyRule.setAttribute(param.getKey(), param.getValue());
}
}
}
property.appendChild(propertyRule);
}
}
metadata.appendChild(property);
}
}
aop.appendChild(metadata);
document.appendChild(aop);
File file = null;
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
file = new File(new URI(cmd.getSourceFileName()));
log.debug("Writing aop file to: " + file.getAbsolutePath());
File parentDir = file.getParentFile();
if (!parentDir.exists())
parentDir.mkdirs();
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
} catch (javax.xml.transform.TransformerException te) {
log.error("Error writing xml document into file.");
throw new MetaDataWriterException(MetaDataWriterException.XML_PARSE_ERROR, null, te);
} catch (URISyntaxException se) {
log.error("Source path could not be converted to URI");
throw new MetaDataWriterException(MetaDataWriterException.SOURCE_NOT_FOUND, null, se);
}
try {
MetaDataRepository.instance().unload(file.toURI().toString());
AopXmlLoader.getInstance().processAopPaths(Collections.singletonList(file.toString().toString()));
} catch (JaffaRulesFrameworkException jrfe) {
log.error("Error loading/unloading class meta data from file.");
throw new MetaDataWriterException(MetaDataWriterException.FILE_ERROR, null, jrfe);
}
}
use of org.jaffa.rules.rulemeta.Rule in project jaffa-framework by jaffa-projects.
the class TestSupport method setupContext.
/**
* Common initializations
*
* @throws Exception
*/
@BeforeClass
public static void setupContext() throws Exception {
synchronized (lockObject) {
if (appContext != null) {
return;
}
// System.out.println("setupContext()");
appContext = new AnnotationConfigApplicationContext(JaffaRulesConfig.class, TestConfig.class);
assertNotNull(appContext);
Rule label = new Rule();
label.setName("label");
Parameter value = new Parameter();
value.setName("value");
label.addParameter(value);
Parameter condition = new Parameter();
condition.setName("condition");
label.addParameter(condition);
RuleRepository.instance().addRule(label);
// get fake model bean from combined app context
fakeModel = appContext.getBean("fakeModel", FakeModel.class);
assertNotNull(fakeModel);
engine = mock(IPersistenceEngine.class);
IPersistenceEngineFactory persistenceEngineFactory = mock(IPersistenceEngineFactory.class);
when(persistenceEngineFactory.newPersistenceEngine()).thenReturn(engine);
PersistenceEngineFactory.setFactory(persistenceEngineFactory);
}
}
use of org.jaffa.rules.rulemeta.Rule in project jaffa-framework by jaffa-projects.
the class RuleRepositoryTest method testAddRule.
/**
* Registering a Rule should behave as though it were loaded from an XML element.
*/
@Test
public void testAddRule() {
RuleRepository target = RuleRepository.instance();
Rule rule = new Rule();
rule.setName("x");
target.addRule(rule);
Rule actual = target.getRuleByName("x");
assertSame(rule, actual);
}
use of org.jaffa.rules.rulemeta.Rule in project jaffa-framework by jaffa-projects.
the class ClassMetaData method getInheritableRules.
/**
* Iterates through the input rules, returning only those that can be inherited as defined by the 'includes' and 'excludes' attributes of the SUPER rule.
* All rules will be returned if the 'includes' and 'excludes' attributes are not defined.
* This method also removes the non-inheritable rules as defined by the 'inheritable' attribute of the rulemeta.
*
* @param superRule the SUPER rule.
* @param inheritedRules the rules to be inherited.
* @return inheritable rules.
*/
static List<RuleMetaData> getInheritableRules(RuleMetaData superRule, List<RuleMetaData> inheritedRules) {
if (inheritedRules != null && inheritedRules.size() > 0) {
String includesParam = superRule.getParameter("includes");
String excludesParam = superRule.getParameter("excludes");
String[] includes = includesParam != null ? includesParam.split(",") : null;
String[] excludes = excludesParam != null ? excludesParam.split(",") : null;
if (includes != null)
Arrays.sort(includes);
if (excludes != null)
Arrays.sort(excludes);
for (Iterator<RuleMetaData> i = inheritedRules.iterator(); i.hasNext(); ) {
RuleMetaData inheritedRule = i.next();
String ruleName = inheritedRule.getName();
if ((includes != null && Arrays.binarySearch(includes, ruleName) < 0) || (excludes != null && Arrays.binarySearch(excludes, ruleName) >= 0)) {
if (log.isDebugEnabled())
log.debug(inheritedRule + " excluded as per the includes/excludes definition in " + superRule);
i.remove();
} else {
Rule ruleInfo = RuleRepository.instance().getRuleByName(ruleName);
if (!ruleInfo.isInheritable()) {
if (log.isDebugEnabled())
log.debug(inheritedRule + " excluded as per the 'inheritable' attribute in the rulemeta " + ruleInfo);
i.remove();
}
}
}
}
return inheritedRules;
}
use of org.jaffa.rules.rulemeta.Rule in project jaffa-framework by jaffa-projects.
the class RuleTest method testExecutionRealms.
/**
* Test execution realms.
*/
@Test
public void testExecutionRealms() {
Rule target = new Rule("mandatory");
target.executionRealms("web,business");
String[] realms = target.getExecutionRealms();
assertEquals(2, realms.length);
// The result should be sorted.
assertEquals("business", realms[0]);
assertEquals("web", realms[1]);
}
Aggregations