Skip to content

Commit

Permalink
Added changes that have been applied to Ortoo's version
Browse files Browse the repository at this point in the history
Note that this will no longer compile in isolation due to a dependency on fflib_extension and ISearchCritiera
  • Loading branch information
rob-baillie-ortoo committed Apr 28, 2022
1 parent c7ee215 commit f9704f4
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ package-lock.json


sfdx-source/group*
research/
research/
.pmdCache
65 changes: 59 additions & 6 deletions sfdx-source/apex-common/main/classes/fflib_Application.cls
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,67 @@ public virtual class fflib_Application
public virtual Object newInstance(Type serviceInterfaceType)
{
// Mock implementation?
if(m_serviceInterfaceTypeByMockService.containsKey(serviceInterfaceType))
return m_serviceInterfaceTypeByMockService.get(serviceInterfaceType);
if( m_serviceInterfaceTypeByMockService.containsKey( serviceInterfaceType ) )
{
return m_serviceInterfaceTypeByMockService.get( serviceInterfaceType );
}

String serviceInterfaceName = serviceInterfaceType.getName();

// Create an instance of the type implementing the given interface
// Check if a configured implementation exists in the Custom Metadata
Type serviceImpl = m_serviceInterfaceTypeByServiceImplType.get(serviceInterfaceType);
if(serviceImpl==null)
throw new DeveloperException('No implementation registered for service interface ' + serviceInterfaceType.getName());
return serviceImpl.newInstance();
if ( serviceImpl != null )
{
try {
return serviceImpl.newInstance();
}
catch ( Exception e ) {
throw new DeveloperException( 'Implementation for service interface ' + serviceInterfaceName + ' (' + serviceImpl.getName() + ') could not be constructed', e );
}
}

// Check if we can use a default service instead, based on the name IServiceInterface => ServiceInterfaceImpl
if ( ! isAServiceInterfaceName( serviceInterfaceName ) )
{
throw new DeveloperException( 'No implementation registered for service interface ' + serviceInterfaceName + ' and default implementation cannot be determined from the name (not an interface in the standard naming convention (Ixxxx)' );
}

String defaultServiceName = buildDefaultServiceName( serviceInterfaceName );

Type defaultServiceType = Type.forName( defaultServiceName );

if ( defaultServiceType == null ) {
throw new DeveloperException( 'No implementation registered for service interface ' + serviceInterfaceName + ' and no default implementation found with the name ' + defaultServiceName );
}

try {
if ( !serviceInterfaceName.endsWith( 'ILoggerService' ) )
{
LoggerService.log( LoggerService.LEVEL.INFO, 'Using default implementation ' + defaultServiceName + ' for ' + serviceInterfaceName );
}

return defaultServiceType.newInstance();
}
catch ( Exception e ) {
throw new DeveloperException( 'Default implementation for service interface ' + serviceInterfaceName + ' (' + defaultServiceName + ') could not be constructed', e );
}
}

@testVisible
private Boolean isAServiceInterfaceName( String serviceName )
{
return ( ( ! serviceName.contains( '.' ) && serviceName.startsWith( 'I' ) ) || serviceName.substringAfterLast( '.' ).startsWith( 'I' ) );
}

@testVisible
private String buildDefaultServiceName( String serviceInterfaceName )
{
String[] serviceInterfaceNameParts = serviceInterfaceName.split( '\\.' );
String serviceInterfaceNameLastPart = serviceInterfaceNameParts[ serviceInterfaceNameParts.size() - 1 ];
String defaultServiceImpl = serviceInterfaceNameLastPart.substringAfter( 'I' ) + 'Impl';
serviceInterfaceNameParts[ serviceInterfaceNameParts.size() - 1 ] = defaultServiceImpl;

return String.join( serviceInterfaceNameParts, '.' );
}

@TestVisible
Expand Down
34 changes: 30 additions & 4 deletions sfdx-source/apex-common/main/classes/fflib_QueryFactory.cls
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,23 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
**/
public Schema.SObjectType table {get; private set;}
@TestVisible
private Set<String> fields;
private String conditionExpression;
private Set<String> fields;
private String conditionExpression {
get {
if ( conditionCriteria != null && String.isNotBlank( conditionExpression ) ) {
// currently an impossible scenario, assuming that setCondition is always used to set the conditions up.
throw new fflib_Application.DeveloperException( 'Both criteria and expression were set on the query factory : ' + conditionExpression + ' + ' + conditionCriteria );
}
else {
if ( conditionCriteria != null ) {
return conditionCriteria.toSOQL();
}
}
return conditionExpression;
}
set;
}
private ISearchCriteria conditionCriteria;
private Integer limitCount;
private Integer offsetCount;
private List<Ordering> order;
Expand Down Expand Up @@ -325,8 +340,19 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
**/
public fflib_QueryFactory setCondition(String conditionExpression){
this.conditionExpression = conditionExpression;
this.conditionCriteria = null;
return this;
}

/**
* @param conditionExpression Sets the WHERE clause to the string provided by the given ISearchCriteria
**/
public fflib_QueryFactory setCondition(ISearchCriteria criteria){
this.conditionCriteria = criteria;
this.conditionExpression = null;
return this;
}

/**
* @returns the current value of the WHERE clause, if any, as set by {@link #setCondition}
**/
Expand Down Expand Up @@ -688,7 +714,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
}
}
result += ' FROM ' + (relationship != null ? relationship.getRelationshipName() : table.getDescribe().getName());
if(conditionExpression != null)
if( String.isNotBlank( conditionExpression ) )
result += ' WHERE '+conditionExpression;

if(order.size() > 0){
Expand All @@ -701,7 +727,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
if(limitCount != null)
result += ' LIMIT '+limitCount;

if(offsetCount != null)
if(offsetCount != null && offsetCount > 0)
result += ' OFFSET '+offsetCount;

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
**/
public virtual with sharing class fflib_SObjectDomain
extends fflib_SObjects
extends fflib_SObjects2
implements fflib_ISObjectDomain
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract with sharing class fflib_SObjectSelector
/**
* Enforce FLS Security
**/
private Boolean m_enforceFLS = false;
protected Boolean m_enforceFLS = false;

/**
* Enforce CRUD Security
Expand Down
Loading

0 comments on commit f9704f4

Please sign in to comment.