Skip to content

Commit

Permalink
Extend the ERDDelayedExtraQualifierAssignment to handle different qua…
Browse files Browse the repository at this point in the history
…lifier operators beyond the default "equals". The default behaviour remains unchanged.
  • Loading branch information
fbarthez authored and darkv committed Nov 18, 2015
1 parent e17724e commit db91fb4
Showing 1 changed file with 128 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@
import er.extensions.eof.qualifiers.ERXPrimaryKeyListQualifier;

/**
* Very useful when you want to restrict the things a user can see during searches or in list pages.
* Set it up via a rule like:<pre><code>
* Very useful when you want to restrict the things a user can see during
* searches or in list pages. Set it up via a rule like:
*
* <pre>
* <code>
* entity.name = "Movie" and session.user.role &lt;&gt; "admin"
* =&gt;
* extraRestrictingQualifier = {
* "studio" = "session.user.studios";
* } [er.directtoweb.ERDDelayedExtraQualifierAssignment]
* </code></pre>
* then in your query page use sth like:<pre><code>
* </code>
* </pre>
*
* then in your query page use sth like:
*
* <pre>
* <code>
* public EODataSource queryDataSource() {
* EODataSource ds = super.queryDataSource();
* if (ds != null &amp;&amp; (ds instanceof EODatabaseDataSource)) {
Expand All @@ -46,15 +54,43 @@
* fs.setQualifier(q);
* }
* return ds;
* }</code></pre>
* }</code>
* </pre>
*
* This should guarantee that the user can only see the Movies that are made by
* studios contained in his studio relationship. If the value is null, then this
* qualifier will not be added. To search for NULL, return
* NSKeyValueCoding.NullValue.<br>
* <br>
* To use another than the default "equals" operator, specify one of the
* following abbreviations:
* <ul>
* <li>ne (not equals)
* <li>gt (greater than)
* <li>gte (greater than or equal)
* <li>lt (less than)
* <li>lte (less than or equal)
* <li>like (case-sensitive like)
* <li>ilike (case-insensitive like)
* </ul>
*
* The following example will limit results to objects that don't have the same
* id as the source object (often useful for self-referencing relationships) and
* whose startDateTime is less than the source object's startDateTime:
*
* <pre>
* {
* "id" = {
* "ne" = "object.id";
* };
* "startDateTime" = {
* "lt" = "object.startDateTime";
* };
* }
* </pre>
*
* This should guarantee that the user can only see the Movies that
* are made by studios contained in his studio relationship.
* If the value is null, then this qualifier will not be added. To search for NULL,
* return NSKeyValueCoding.NullValue.
* @author ak
*/

public class ERDDelayedExtraQualifierAssignment extends ERDDelayedAssignment {
/**
* Do I need to update serialVersionUID?
Expand All @@ -65,7 +101,7 @@ public class ERDDelayedExtraQualifierAssignment extends ERDDelayedAssignment {

/** logging support */
public static final Logger log = Logger.getLogger(ERDDelayedExtraQualifierAssignment.class);

/**
* Static constructor required by the EOKeyValueUnarchiver
* interface. If this isn't implemented then the default
Expand All @@ -74,28 +110,28 @@ public class ERDDelayedExtraQualifierAssignment extends ERDDelayedAssignment {
* @param eokeyvalueunarchiver to be unarchived
* @return decoded assignment of this class
*/
public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver eokeyvalueunarchiver) {
public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver eokeyvalueunarchiver) {
return new ERDDelayedExtraQualifierAssignment(eokeyvalueunarchiver);
}

/**
/**
* Public constructor
* @param u key-value unarchiver used when unarchiving
* from rule files.
*/
*/
public ERDDelayedExtraQualifierAssignment (EOKeyValueUnarchiver u) { super(u); }
/**

/**
* Public constructor
* @param key context key
* @param value of the assignment
*/
public ERDDelayedExtraQualifierAssignment (String key, Object value) { super(key,value); }

protected EOQualifier qualifierForArray(String key, NSArray objects) {
if(objects == null)
if (objects == null)
return null;
if(objects.count() == 0)
if (objects.count() == 0)
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorEqual, null);
return new ERXPrimaryKeyListQualifier(key, objects);
}
Expand All @@ -104,36 +140,93 @@ protected EOQualifier qualifierForObject(String key, Object object) {
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorEqual, object);
}

protected EOQualifier extraQualifier(D2WContext c, NSDictionary dict) {
NSMutableArray qualifiers = new NSMutableArray();
protected EOQualifier qualifierForObject(String key, NSDictionary object) {
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorEqual, object);
}

protected EOQualifier qualifierForOperatorAndObject(String key,
String operatorKey,
Object value) {
if ("eq".equals(operatorKey)) {
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorEqual, value);
} else if ("ne".equals(operatorKey)) {
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorNotEqual,
value);
} else if ("gt".equals(operatorKey)) {
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorGreaterThan,
value);
} else if ("gte".equals(operatorKey)) {
return new EOKeyValueQualifier(key,
EOQualifier.QualifierOperatorGreaterThanOrEqualTo, value);
} else if ("lt".equals(operatorKey)) {
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorLessThan,
value);
} else if ("lte".equals(operatorKey)) {
return new EOKeyValueQualifier(key,
EOQualifier.QualifierOperatorLessThanOrEqualTo, value);
} else if ("like".equals(operatorKey)) {
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorLike, value);
} else if ("ilike".equals(operatorKey)) {
return new EOKeyValueQualifier(key,
EOQualifier.QualifierOperatorCaseInsensitiveLike, value);
}
return new EOKeyValueQualifier(key, EOQualifier.QualifierOperatorNotEqual, value);
}

protected EOQualifier extraQualifier(D2WContext c, NSDictionary<String, Object> dict) {
NSMutableArray<EOQualifier> qualifiers = new NSMutableArray<EOQualifier>();
EOQualifier result = null;
for(Enumeration e = dict.keyEnumerator(); e.hasMoreElements(); ) {
String key = (String)e.nextElement();
Object value = c.valueForKeyPath((String)dict.objectForKey(key));
if(value != null) {
EOQualifier q;
if(value instanceof NSArray) {
q = qualifierForArray(key, (NSArray)value);
} else {
if(value == NSKeyValueCoding.NullValue) {
value = null;
for (String key : dict.allKeys()) {
Object value = null;
if (dict.objectForKey(key) instanceof NSDictionary) {
// qualifier definition with operator
NSDictionary qDict = (NSDictionary) dict.objectForKey(key);
if (qDict.size() == 1) {
String operatorKey = (String) qDict.allKeys().lastObject();
String contextKeyPath = (String) qDict.objectForKey(operatorKey);
if ("NSKeyValueCoding.NullValue".equals(contextKeyPath)) {
value = NSKeyValueCoding.NullValue;
} else {
value = c.valueForKeyPath(contextKeyPath);
}
if (value != null) {
EOQualifier q = qualifierForOperatorAndObject(key, operatorKey,
value);
qualifiers.addObject(q);
}
}
} else {
value = c.valueForKeyPath((String) dict.objectForKey(key));
if (value != null) {
EOQualifier q;
if (value instanceof NSArray) {
q = qualifierForArray(key, (NSArray) value);
} else {
if (value == NSKeyValueCoding.NullValue) {
value = null;
}
q = qualifierForObject(key, value);
}
if (q != null) {
qualifiers.addObject(q);
}
q = qualifierForObject(key, value);
}
if(q != null) qualifiers.addObject(q);
}
}
if(qualifiers.count() > 0)
if (qualifiers.count() > 0)
result = new EOAndQualifier(qualifiers);
if (log.isDebugEnabled()) {
log.debug("Computed qualifier: " + result);
}
return result;
}

@Override
public Object fireNow(D2WContext c) {
Object result = null;
Object value = value();
if(value != null && value instanceof NSDictionary) {
result = extraQualifier(c, (NSDictionary)value);
if (value != null && value instanceof NSDictionary) {
result = extraQualifier(c, (NSDictionary) value);
}
return result;
}
Expand Down

0 comments on commit db91fb4

Please sign in to comment.