Skip to content

Instantly share code, notes, and snippets.

@saranicole
Created July 15, 2014 14:02
Show Gist options
  • Select an option

  • Save saranicole/ab22c552577f75e2cc8f to your computer and use it in GitHub Desktop.

Select an option

Save saranicole/ab22c552577f75e2cc8f to your computer and use it in GitHub Desktop.
RulesApplied
import com.axeda.drm.sdk.Context
import com.axeda.drm.sdk.scripto.Request
import com.axeda.drm.sdk.device.*
import java.util.regex.Pattern
import com.axeda.drm.sdk.data.CurrentDataFinder
import com.axeda.drm.sdk.rules.engine.ExpressionRule
import com.axeda.drm.sdk.rules.engine.ExpressionRuleFinder
import com.axeda.drm.sdk.rules.engine.Expression
import com.axeda.drm.sdk.rules.engine.RuleAssociation
import com.axeda.drm.sdk.rules.engine.RuleAssociationFinder
import com.axeda.common.sdk.id.Identifier
import com.axeda.drm.sdk.common.EntityType
import com.axeda.drm.sdk.rules.engine.RuleType
import net.sf.json.JSONArray
final Context CONTEXT = Context.getSDKContext()
def root = [result:[rules:[]]]
try{
// replace "FooModel" with the appropriate model name
def list = [
[name: "FooModel",value: "com.axeda.drm.sdk.device.Model"],
]
def objectId = findObjectIdsByName(CONTEXT, list, true)
root.result.rules = findRulesApplied(CONTEXT, objectId, "MODEL")
logger.info(JSONArray.fromObject(root).toString(2))
}
catch(Exception ex){
logger.info(ex.localizedMessage)
}
return true
def findRulesApplied(Context CONTEXT, Long entityId, String entityType){
// entityType can be "MODEL", "DEVICE_INCLUDE", "DEVICE_EXCLUDE"
entityType = entityType ?: "MODEL"
RuleAssociationFinder ruleAssociationFinder = new RuleAssociationFinder(CONTEXT)
ruleAssociationFinder.setEntityType(EntityType.valueOf(entityType))
ruleAssociationFinder.setEntityId(entityId)
def ruleAssocs = ruleAssociationFinder.findAll()
return ruleAssocs.inject([]){ target, ra ->
ExpressionRuleFinder erf = new ExpressionRuleFinder(CONTEXT)
erf.setId(new Identifier(ra.ruleId))
ExpressionRule expressionRule = erf.findOne()
target << [
name: expressionRule.name
, id: expressionRule.id.value
, desc: expressionRule.description
, ifExpr: expressionRule.ifExpression.toString()
, thenExpr: expressionRule.thenExpression.toString()
, enabled: expressionRule.enabled
, consecutive: expressionRule.consecutive
]
target
}
}
def findObjectIdsByName(Context CONTEXT, List list){
return findObjectIdsByName(CONTEXT, list, null)
}
def findObjectIdsByName(Context CONTEXT, List list, boolean returnOneAsLong){
/*
list should have the id as a string as the name, the class name as a string as the value
[["name": "xirgoxt2000","value": "com.axeda.drm.sdk.device.Model"],["name": "23825","value": "com.axeda.drm.sdk.device.Device"]]
*/
def objects = list.inject([:]){ target, namedValue ->
def finderclassname = namedValue.value + "Finder"
if (finderclassname){
try {
def classobj = finderclassname as Class
def finder = classobj.newInstance(CONTEXT)
finder.setName(namedValue.name)
def object = finder.find()
if (object != null){
target."${namedValue.value}" = target."${namedValue.value}" ?: []
target."${namedValue.value}" << [ (namedValue.name) : object.id.value.asType(String)]
}
}
catch (ClassNotFoundException cnfe){
logger.info("Class Not Found for $finderclassname")
}
}
target
}
if (returnOneAsLong){
return objects."${list.last().value}"."${list.last().name}".last().asType(Long)
}
objects
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment