diff --git a/framework/default/fflib-apex-extensions/default/classes/criteria/fflib_Comparator.cls b/framework/default/fflib-apex-extensions/default/classes/criteria/fflib_Comparator.cls index 2e8ae2a6cb1..2f462c78d47 100644 --- a/framework/default/fflib-apex-extensions/default/classes/criteria/fflib_Comparator.cls +++ b/framework/default/fflib-apex-extensions/default/classes/criteria/fflib_Comparator.cls @@ -30,11 +30,6 @@ */ public with sharing class fflib_Comparator { - // TODO: move - consider adding to the original library - public interface Evaluatable - { - Object toValue(); - } public interface Comparable { @@ -64,13 +59,8 @@ public with sharing class fflib_Comparator public static Integer compare(Object object1, Object object2) { // TODO: test - if ( object2 instanceOf fflib_Comparator.Comparable ) { - return ((fflib_Comparator.Comparable)object2).compare( object1 ); - } - - // TODO: test - if ( object2 instanceOf Evaluatable ) { - object2 = ((Evaluatable)object2)?.toValue(); + if ( object2 instanceOf Comparable ) { + return ((Comparable)object2).compare( object1 ); } if (object1 == null && object2 == null) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls b/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls index fd265e316a5..0f41bbdb1fe 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls @@ -1,22 +1,29 @@ /** - * Class that provides a list of inner classes that can be used as targets for comparisons in ortoo_Criteria. + * Class that provides: * - * Specifically, the classes represent the different Date Literals that SOQL supports + * * Shorthands for specific dates related to today. + * E.g. today, yesterday, tomorrow, startOfLastWeek + * + * * A list of inner classes that can be used as targets for comparisons in ortoo_Criteria. + * Specifically, the classes represent a subset of the Date Literals that SOQL supports + * + * * Note that if the date shorthands are used, then their behaviour can be overridden in tests + * by changing the value of 'today'. * * @group fflib Extension */ public inherited sharing class ortoo_DateLiterals { - // TODO: datetime behaviours too? - public class NonEvaluatableException extends ortoo_Exception {} private interface Literal extends fflib_Criteria.Literal {} - private interface Evaluatable extends fflib_Comparator.Evaluatable {} private interface Comparable extends fflib_Comparator.Comparable {} - private static Date today = Date.today(); + public static Date today = Date.today(); + /** + * Dynamic representation of the date 'tomorrow', based on the same class's representation of 'today' + */ public static Date tomorrow { get { @@ -24,6 +31,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'yesterday', based on the same class's representation of 'today' + */ public static Date yesterday { get { @@ -31,6 +41,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'start of this week', based on the same class's representation of 'today' + */ public static Date startOfThisWeek { get { @@ -38,6 +51,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'end of this week', based on the same class's representation of 'today' + */ public static Date endOfThisWeek { get { @@ -45,6 +61,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'start of last week', based on the same class's representation of 'today' + */ public static Date startOfLastWeek { get { @@ -52,6 +71,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'end of last week', based on the same class's representation of 'today' + */ public static Date endOfLastWeek { get { @@ -59,6 +81,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'start of next week', based on the same class's representation of 'today' + */ public static Date startOfNextWeek { get { @@ -66,6 +91,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'end of next week', based on the same class's representation of 'today' + */ public static Date endOfNextWeek { get { @@ -73,6 +101,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'start of this month', based on the same class's representation of 'today' + */ public static Date startOfThisMonth { get { @@ -80,6 +111,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'end of this month', based on the same class's representation of 'today' + */ public static Date endOfThisMonth { get { @@ -87,6 +121,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'start of next month', based on the same class's representation of 'today' + */ public static Date startOfNextMonth { get { @@ -94,6 +131,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'end of next month', based on the same class's representation of 'today' + */ public static Date endOfNextMonth { get { @@ -101,6 +141,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'start of last month', based on the same class's representation of 'today' + */ public static Date startOfLastMonth { get { @@ -108,6 +151,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * Dynamic representation of the date 'end of last month', based on the same class's representation of 'today' + */ public static Date endOfLastMonth { get { @@ -115,48 +161,69 @@ public inherited sharing class ortoo_DateLiterals } } - public class Today implements Literal, Evaluatable + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'TODAY' + */ + public class Today extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'TODAY'; } - public Date toValue() - { + protected override Date getStartDate() { + return today; + } + + protected override Date getEndDate() { return today; } } - public class Yesterday implements Literal, Evaluatable + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'YESTERDAY' + */ + public class Yesterday extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'YESTERDAY'; } - public Date toValue() - { + protected override Date getStartDate() { + return yesterday; + } + + protected override Date getEndDate() { return yesterday; } } - public class Tomorrow implements Literal, Evaluatable + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'TOMORROW' + */ + public class Tomorrow extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'TOMORROW'; } - public Date toValue() - { + protected override Date getStartDate() { + return tomorrow; + } + + protected override Date getEndDate() { return tomorrow; } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'LAST_WEEK' + */ public class LastWeek extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'LAST_WEEK'; } @@ -170,9 +237,12 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'THIS_WEEK' + */ public class ThisWeek extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'THIS_WEEK'; } @@ -186,9 +256,12 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'NEXT_WEEK' + */ public class NextWeek extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'NEXT_WEEK'; } @@ -202,9 +275,12 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'LAST_MONTH' + */ public class LastMonth extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'LAST_MONTH'; } @@ -218,9 +294,12 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'THIS_MONTH' + */ public class ThisMonth extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'THIS_MONTH'; } @@ -234,9 +313,12 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'NEXT_MONTH' + */ public class NextMonth extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'NEXT_MONTH'; } @@ -250,9 +332,12 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'LAST_90_DAYS' + */ public class Last90Days extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'LAST_90_DAYS'; } @@ -266,9 +351,12 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'NEXT_90_DAYS' + */ public class Next90Days extends DateRangeLiteral implements Literal, Comparable { - public String toLiteral() + public override String toLiteral() { return 'NEXT_90_DAYS'; } @@ -282,6 +370,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'LAST_N_DAYS' + */ public class LastNDays extends NumericallyQualifiedLiteral implements Literal, Comparable { public LastNDays( Integer numberOfThings ) @@ -301,6 +392,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'NEXT_N_DAYS' + */ public class NextNDays extends NumericallyQualifiedLiteral implements Literal, Comparable { public NextNDays( Integer numberOfThings ) @@ -320,6 +414,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'LAST_N_WEEKS' + */ public class LastNWeeks extends NumericallyQualifiedLiteral implements Literal, Comparable { public LastNWeeks( Integer numberOfThings ) @@ -339,6 +436,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'NEXT_N_WEEKS' + */ public class NextNWeeks extends NumericallyQualifiedLiteral implements Literal, Comparable { public NextNWeeks( Integer numberOfThings ) @@ -355,6 +455,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'LAST_N_MONTHS' + */ public class LastNMonths extends NumericallyQualifiedLiteral implements Literal, Comparable { public LastNMonths( Integer numberOfThings ) @@ -374,6 +477,9 @@ public inherited sharing class ortoo_DateLiterals } } + /** + * ortoo_Criteria compatible literal that represents the SOQL Date Literal 'NEXT_N_MONTHS' + */ public class NextNMonths extends NumericallyQualifiedLiteral implements Literal, Comparable { public NextNMonths( Integer numberOfThings ) @@ -408,7 +514,7 @@ public inherited sharing class ortoo_DateLiterals this.baseLiteral = baseLiteral; } - public String toLiteral() + public override String toLiteral() { return this.baseLiteral + ':' + this.numberOfThings; } @@ -416,16 +522,44 @@ public inherited sharing class ortoo_DateLiterals private abstract class DateRangeLiteral implements ortoo_DateLiterals.Comparable { + protected abstract String toLiteral(); protected abstract Date getStartDate(); protected abstract Date getEndDate(); public Integer compare( Object otherValue ) { - return new DateRangeComparer().compare( (Date)otherValue, getStartDate(), getEndDate() ); + if ( otherValue instanceOf Date ) + { + return new DateToDateRangeComparer().compare( (Date)otherValue, getStartDate(), getEndDate() ); + } + if ( otherValue instanceOf DateTime ) + { + return new DateTimeToDateRangeComparer().compare( (DateTime)otherValue, getStartDate(), getEndDate() ); + } + + throw new NonEvaluatableException( 'Attempted to compare a non Date / DateTime with a Date Literal' ) + .setErrorCode( FrameworkErrorCodes.NON_EVALUATABLE_CRITERIA ) + .addContext( 'DateLiteral', toLiteral() ); + } + } + + private class DateTimeToDateRangeComparer + { + public Integer compare( DateTime value, Date startDate, Date endDate ) { + + if ( value < DateTime.newInstance( startDate.year(), startDate.month(), startDate.day(), 0, 0, 0 ) ) + { + return -1; + } + if ( value > DateTime.newInstance( endDate.year(), endDate.month(), endDate.day(), 23, 59, 29 ) ) + { + return 1; + } + return 0; } } - private class DateRangeComparer + private class DateToDateRangeComparer { public Integer compare( Date value, Date startDate, Date endDate ) { if ( value < startDate )