public void add(RuleBaseElementId name, String code) throws RuleBaseException {
try {
List<RuleBaseConfiguration> existingRules =
rulePersistence.load(new RuleBaseElement(name).toMetadata());
if (!existingRules.isEmpty()) {
throw new RuleBaseException("rule already exists");
}
} catch (PersistenceException e1) {
throw new RuleBaseException("could not load existing rules from persistence service", e1);
}
RuleBaseElement objectToPersist = new RuleBaseElement(name, code);
Map<String, String> metaData = objectToPersist.toMetadata();
RuleBaseConfiguration conf = new RuleBaseConfiguration(metaData, objectToPersist);
try {
rulePersistence.persist(conf);
} catch (PersistenceException e) {
throw new RuleBaseException(e);
}
try {
builder.reloadPackage(name.getPackageName());
} catch (RuleBaseException e) {
try {
rulePersistence.remove(metaData);
throw e;
} catch (PersistenceException e1) {
throw new RuleBaseException("could not remove previously added rule, that broke the rulebase", e1);
}
}
}