package com.orientechnologies.orient.core.metadata.security;

import com.orientechnologies.orient.core.record.OElement;
import com.orientechnologies.orient.core.sql.OCommandSQLParsingException;
import com.orientechnologies.orient.core.sql.OSQLEngine;

public class OSecurityPolicy {


  public enum Scope { CREATE, READ, BEFORE_UPDATE, AFTER_UPDATE, DELETE, EXECUTE }

  private OElement element;

  public OSecurityPolicy(OElement element) {
    this.element = element;
  }

  public OElement getElement() {
    return element;
  }

  public void setElement(OElement element) {
    this.element = element;
  }

  public String getName() {
    return element.getProperty("name");
  }

  public void setName(String name) {
    element.setProperty("name", name);
  }

  public boolean isActive() {
    return Boolean.TRUE.equals(this.element.getProperty("active"));
  }

  public void setActive(Boolean active) {
    this.element.setProperty("active", active);
  }


  public String getCreateRule() {
    return element.getProperty("create");
  }

  public void setCreateRule(String rule) throws IllegalArgumentException {
    validatePredicate(rule);
    element.setProperty("create", rule);
  }


  public String getReadRule() {
    return element.getProperty("read");
  }

  public void setReadRule(String rule) throws IllegalArgumentException {
    validatePredicate(rule);
    element.setProperty("read", rule);
  }

  public String getBeforeUpdateRule() {
    return element.getProperty("beforeUpdate");
  }

  public void setBeforeUpdateRule(String rule) throws IllegalArgumentException {
    validatePredicate(rule);
    element.setProperty("beforeUpdate", rule);
  }

  public String getAfterUpdateRule() {
    return element.getProperty("afterUpdate");
  }

  public void setAfterUpdateRule(String rule) throws IllegalArgumentException {
    validatePredicate(rule);
    element.setProperty("afterUpdate", rule);
  }

  public String getDeleteRule() {
    return element.getProperty("delete");
  }

  public void setDeleteRule(String rule) throws IllegalArgumentException {
    validatePredicate(rule);
    element.setProperty("delete", rule);
  }

  public String getExecuteRule() {
    return element.getProperty("execute");
  }

  public void setExecuteRule(String rule) throws IllegalArgumentException {
    validatePredicate(rule);
    element.setProperty("execute", rule);
  }

  public String get(Scope scope) {
    switch (scope) {
    case CREATE: return getCreateRule();
    case READ: return getReadRule();
    case BEFORE_UPDATE: return getBeforeUpdateRule();
    case AFTER_UPDATE: return getAfterUpdateRule();
    case DELETE: return getDeleteRule();
    case EXECUTE: return getExecuteRule();
    default: throw new IllegalArgumentException();
    }
  }


  protected void validatePredicate(String predicate) throws IllegalArgumentException {
    if (predicate == null || predicate.trim().length() == 0) {
      return;
    }
    try {
      OSQLEngine.parsePredicate(predicate);
    } catch (OCommandSQLParsingException ex) {
      throw new IllegalArgumentException("Invalid predicate: " + predicate);
    }
  }
}

@@ -994,32 +994,57 @@
 e() %7B%0A    return
+ element == null ? null :
  element.getProp
@@ -1055,24 +1055,24 @@
 (%22create%22);%0A
-
   %7D%0A%0A  publi
@@ -1249,32 +1249,57 @@
 e() %7B%0A    return
+ element == null ? null :
  element.getProp
@@ -1505,32 +1505,57 @@
 e() %7B%0A    return
+ element == null ? null :
  element.getProp
@@ -1784,32 +1784,57 @@
 e() %7B%0A    return
+ element == null ? null :
  element.getProp
@@ -2055,32 +2055,57 @@
 e() %7B%0A    return
+ element == null ? null :
  element.getProp
@@ -2302,32 +2302,32 @@
 ExecuteRule() %7B%0A
-
     return eleme
@@ -2312,32 +2312,57 @@
 e() %7B%0A    return
+ element == null ? null :
  element.getProp

Add null checks in security policy management