Add getter methods to domain for returning maps

This commit is contained in:
Wim Velzeboer
2021-06-28 10:21:02 +01:00
parent a2afb1104e
commit 8642a54b57
2 changed files with 502 additions and 6 deletions
+222 -6
View File
@@ -1,6 +1,8 @@
# fflib-apex-extensions
## Primitive Domain - SObject
The primitive domain SObject adds a lot of additional getter methods to the domain. It also includes getting Maps directly from the domain. (see the `get...By...` methods)
This class should be used to extend Domain classes.
```apex
@@ -24,13 +26,32 @@ Protected methods
- [getDateFieldValues](#getDateFieldValues)
- [getDateTimeFieldValues](#getDateTimeFieldValues)
- [getDecimalFieldValues](#getDecimalFieldValues)
- [getDoubleFieldValues](#getdoublefieldvalues)
- [getFieldByField](#getfieldbyfield)
- [getFieldsByField](#getfieldsbyfield)
- [getIdFieldByIdField](#getidfieldbyidfield)
- [getIdFieldsByIdField](#getidfieldsbyidfield)
- [getIdFieldValues](#getidfieldvalues)
- [getIntegerFieldValues](#getintegerfieldvalues)
- [getLongFieldValues](#getlongfieldvalues)
- [getSObjectByIdField](#getsobjectbyidfield)
- [getSObjectsByIdField](#getsobjectsbyidfield)
- [getSObjectByStringField](#getsobjectbystringfield)
- [getSObjectsByStringField](#getsobjectsbystringfield)
- [getStringFieldByIdField](#getstringfieldbyidfield)
- [getStringFieldByStringField](#getstringfieldbystringfield)
- [getStringFieldValues](#getstringfieldvalues)
- [setFieldValue](#setfieldvalue) _(Added method overload)_
#### getSObjectById
Returns the contents of the Domain by their primary Key ('Id' field)
`public Map<Id, SObject> getSObjectById()`
<br/><br/>
<br/><br/><br/>
#### getRecords
Returns only the SObjects from the domain matching the given Ids.
@@ -43,7 +64,7 @@ Returns only the SObjects from the domain matching the given Ids.
Return the SObject records contained in the domain matching the criteria
- `public List<SObject> getRecords(fflib_Criteria criteria)`<br/>
<br/><br/>
<br/><br/><br/>
#### addError
@@ -51,14 +72,14 @@ Adds an error message to the records in the domain
`protected void addError(String message)`<br/>
`protected void addError(Schema.SObjectField sObjectField, String message)`
<br/><br/>
<br/><br/><br/>
#### clearField
Clear the field value on all the records of the domain
`protected void clearField(Schema.SObjectField sObjectField)`<br/>
`protected void clearFields(Set<Schema.SObjectField> sObjectFields)`
<br/><br/>
<br/><br/><br/>
#### getDateFieldValues
Get the Date values from the given SObjectField, null values will be omitted.
@@ -66,7 +87,7 @@ If a criteria is provided, only the value of the records matches the criteria wi
`protected Dates getDateFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected Dates getDateFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/>
<br/><br/><br/>
#### getDateTimeFieldValues
Get the Datetime values from the given SObjectField, null values will be omitted.
@@ -74,7 +95,7 @@ If a criteria is provided, only the value of the records matches the criteria wi
`protected DateTimes getDateTimeFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected DateTimes getDateTimeFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/>
<br/><br/><br/>
#### getDecimalFieldValues
Get the Decimal values from the given SObjectField, null values will be omitted.
@@ -82,3 +103,198 @@ If a criteria is provided, only the value of the records matches the criteria wi
`protected Decimals getDecimalFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected Decimals getDecimalFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/><br/>
#### getDoubleFieldValues
Get the Double values from the given SObjectField, null values will be omitted.
If a criteria is provided, only the value of the records matches the criteria will be returned.
`protected Doubles getDoubleFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected Doubles getDoubleFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/><br/>
#### getFieldByField
Get a map with the values of two fields. Key fields containing null values are omitted
`protected Map<Object, Object> getFieldByField(Schema.SObjectField valueField, Schema.SObjectField keyField)`
_Example_
```apex
Accounts accounts = Accounts.newInstance(records);
Map<Object, Object> accountNameById = accounts.getFieldByField(Account.Name, Account.Id);
```
<br/><br/>
#### getFieldsByField
Get a map with the values of two fields. Key fields containing null values are omitted.
`protected Map<Object, Set<Object>> getFieldsByField(Schema.SObjectField valueField, Schema.SObjectField keyField)`
_Example_
```apex
Contacts contacts = Contacts.newInstance(records);
Map<Object, Set<Object>> contactIdByAccountId = contacts.getFieldsByField(Contact.Id, Contact.AccountId);
```
<br/><br/>
#### getIdFieldByIdField
Get a map with the values of two Id fields. Key fields containing null values are omitted.
`protected Map<Id, Id> getIdFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)`
_Example_
```apex
Contacts contacts = Contacts.newInstance(records);
Map<Id, Id> accountIdByContactId = contacts.getIdFieldByIdField(Contact.AccountId, Contact.Id);
```
<br/><br/>
#### getIdFieldsByIdField
Get a map with the values of two Id fields with a one to many relation. Key fields containing null values are omitted.
`protected Map<Id, Set<Id>> getIdFieldsByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)`
_Example_
```apex
Contacts contacts = Contacts.newInstance(records);
Map<Id, Set<Id>> contactIdByAccountId = contacts.getIdFieldsByIdField(Contact.Id, Contact.AccountId);
```
<br/><br/>
#### getIdFieldValues
Gets the values from the given Id field.
If a criteria is provided, only the value of the records matches the criteria will be returned.
`protected Ids getIdFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected Ids getIdFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/><br/>
#### getIntegerFieldValues
Gets the values from the given Integer field.
If a criteria is provided, only the value of the records matches the criteria will be returned.
`protected Integers getIntegerFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected Integers getIntegerFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/><br/>
#### getLongFieldValues
Gets the values from the given Long field.
If a criteria is provided, only the value of the records matches the criteria will be returned.
`protected Longs getLongFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected Longs getLongFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/></br>
#### getSObjectByIdField
Get a map with the record mapped to the given Id field value.
Key fields containing null values are omitted.
`protected Map<Id, SObject> getSObjectByIdField(Schema.SObjectField sObjectField)`
_Example_
```apex
Account account = Account.newInstance(records);
Map<Id, SObject> accountById = account.getSObjectByIdField(Account.Id);
```
<br/><br/>
#### getSObjectsByIdField
Get a map with the records mapped to the given Id field value. Key fields containing null values are omitted.
`protected Map<Id, List<SObject>> getSObjectsByIdField(Schema.SObjectField sObjectField)`
_Example_
```apex
Contacts contacts = Contacts.newInstance(records);
Map<Id, List<SObject>> contactsByAccountId = contacts.getSObjectsByIdField(Contact.AccountId);
```
<br/><br/>
#### getSObjectByStringField
Get a map with the record mapped to the given String field value. Key fields containing null values are omitted.
`protected Map<String, SObject> getSObjectByStringField(Schema.SObjectField sObjectField)`
_Example_
```apex
Account account = Account.newInstance(records);
Map<String, SObject> accountByNumber = account.getSObjectByStringField(Account.AccountNumber);
```
<br/><br/>
#### getSObjectsByStringField
Get a map with the records mapped to the given String field value. Key fields containing null values are omitted.
`protected Map<String, List<SObject>> getSObjectsByStringField(Schema.SObjectField sObjectField)`
_Example_
```apex
Account account = Account.newInstance(records);
Map<String, SObject> accountByName = account.getSObjectsByStringField(Account.AccountName);
```
<br/><br/>
#### getStringFieldByIdField
Get a map with the given String field value mapped to the given Id field. Key fields containing null values are omitted.
`protected Map<Id, String> getStringFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)`
_Example_
```apex
Account account = Account.newInstance(records);
Map<Id, String> accountNameById = account.getStringFieldByIdField(Account.AccountName, Account.Id);
```
<br/><br/>
#### getStringFieldByStringField
Get a map with the records mapped to the given String field value. Key fields containing null values are omitted.
`protected Map<String, String> getStringFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField)`
_Example_
```apex
Account account = Account.newInstance(records);
Map<Id, String> accountNameById = account.getStringFieldByStringField(Account.AccountName, Account.Id);
```
<br/><br/>
#### getStringFieldValues
Gets the values for the provided String field.
If a criteria is provided, only the value of the records matches the criteria will be returned.
`protected Strings getStringFieldValues(Schema.SObjectField sObjectField)`<br/>
`protected Strings getStringFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)`
<br/><br/><br/>
#### setFieldValue
This method overload is added to give the value to the given field only when the criteria are met for the record.
`protected void setFieldValue(Schema.SObjectField sObjectField, Object value, fflib_Criteria criteria)`
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
@@ -312,7 +312,126 @@ public virtual class SObjects
}
/**
* Get a map with the values of two fields.
* Key fields containing null values are omitted
*
* @param valueField The field to use as the Value of the Map
* @param keyField The field to use as the Key of the map
*
* @return Returns a map with the values of two fields
*
* @example
* Accounts accounts = Accounts.newInstance(records);
* Map<Object, Object> accountNameById = accounts.getFieldByField(Account.Name, Account.Id);
*/
protected virtual Map<Object, Object> getFieldByField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{
Map<Object, Object> result = new Map<Object, Object>();
for (SObject record : getRecords())
{
if (record.get(keyField) == null) continue;
result.put(
record.get(keyField),
record.get(valueField)
);
}
return result;
}
/**
* Get a map with the values of two fields
* Key fields containing null values are omitted
*
* @param valueField The field to use as the Value of the Map
* @param keyField The field to use as the Key of the map
*
* @return Returns a map with the values of two fields
*
* @example
* Contacts contacts = Contacts.newInstance(records);
* Map<Object, Set<Object>> contactIdByAccountId = contacts.getFieldsByField(Contact.Id, Contact.AccountId);
*/
protected virtual Map<Object, Set<Object>> getFieldsByField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{
Map<Object, Set<Object>> result = new Map<Object, Set<Object>>();
for (SObject record : getRecords())
{
Object keyFieldValue = record.get(keyField);
if (keyFieldValue == null) continue;
if (!result.containsKey(keyFieldValue))
{
result.put(keyFieldValue, new Set<Object>());
}
result.get(keyFieldValue).add(record.get(valueField));
}
return result;
}
/**
* Get a map with the values of two Id fields
* Key fields containing null values are omitted
*
* @param valueField The Id field to use as the Value of the Map
* @param keyField The Id field to use as the Key of the map
*
* @return Returns a map with the values of two fields
*
* @example
* Contacts contacts = Contacts.newInstance(records);
* Map<Id, Id> accountIdByContactId = contacts.getIdFieldByIdField(Contact.AccountId, Contact.Id);
*/
protected virtual Map<Id, Id> getIdFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{
Map<Id, Id> result = new Map<Id, Id>();
for (SObject record : getRecords())
{
if (record.get(keyField) == null) continue;
result.put(
(Id) record.get(keyField),
(Id) record.get(valueField)
);
}
return result;
}
/**
* Get a map with the values of two Id fields with a one to many relation
* Key fields containing null values are omitted
*
* @param valueField The Id field to use as the Value of the Map
* @param keyField The Id field to use as the Key of the map
*
* @return Returns a map with the values of two fields
*
* @example
* Contacts contacts = Contacts.newInstance(records);
* Map<Id, Set<Id>> contactIdByAccountId = contacts.getIdFieldsByIdField(Contact.Id, Contact.AccountId);
*/
protected virtual Map<Id, Set<Id>> getIdFieldsByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{
Map<Id, Set<Id>> result = new Map<Id, Set<Id>>();
for (SObject record : getRecords())
{
Id keyFieldId = (Id) record.get(keyField);
if (keyFieldId == null) continue;
if (!result.containsKey(keyFieldId))
{
result.put(keyFieldId, new Set<Id>());
}
result.get(keyFieldId).add((Id) record.get(valueField));
}
return result;
}
/**
* Gets the values from the given Id field
*
* @param sObjectField The Schema.SObjectField to get their Id values
* Fields containing null values are omitted
*
* @return The Id values of the given sObjectField, null values are omitted
*/
@@ -324,6 +443,7 @@ public virtual class SObjects
{
Object fieldValue = record.get(sObjectField);
if (fieldValue == null) continue;
result.add(Id.valueOf(String.valueOf(fieldValue)));
}
return new Ids(result);
@@ -356,6 +476,7 @@ public virtual class SObjects
{
Object fieldValue = record.get(sObjectField);
if (fieldValue == null) continue;
result.add(Integer.valueOf(fieldValue));
}
return new Integers(result);
@@ -406,6 +527,163 @@ public virtual class SObjects
}
/**
* Get a map with the record mapped to the given Id field value
* Key fields containing null values are omitted
*
* @param sObjectField The field to use as key for the map
*
* @return Returns a map with the record mapped to the given Id field value
*
* @example
* Account account = Account.newInstance(records);
* Map<Id, SObject> accountById = account.getSObjectByIdField(Account.Id);
*/
protected virtual Map<Id, SObject> getSObjectByIdField(Schema.SObjectField sObjectField)
{
Map<Id, SObject> result = new Map<Id, SObject>();
for (SObject record : getRecords())
{
if (record.get(sObjectField) == null) continue;
result.put((Id) record.get(sObjectField), record);
}
return result;
}
/**
* Get a map with the records mapped to the given Id field value
* Key fields containing null values are omitted
*
* @param sObjectField The field to use as key for the map
*
* @return Returns a map with the records mapped to the given Id field value
*
* @example
* Contacts contacts = Contacts.newInstance(records);
* Map<Id, List<SObject>> contactsByAccountId = contacts.getSObjectsByIdField(Contact.AccountId);
*/
protected virtual Map<Id, List<SObject>> getSObjectsByIdField(Schema.SObjectField sObjectField)
{
Map<Id, List<SObject>> result = new Map<Id, List<SObject>>();
for (SObject record : getRecords())
{
Id fieldId = (Id) record.get(sObjectField);
if (record.get(fieldId) == null) continue;
if (!result.containsKey(fieldId))
{
result.put(fieldId, new List<SObject>());
}
result.get(fieldId).add(record);
}
return result;
}
/**
* Get a map with the record mapped to the given String field value
* Key fields containing null values are omitted
*
* @param sObjectField The field to use as key for the map
*
* @return Returns a map with the record mapped to the given String field value
*
* @example
* Account account = Account.newInstance(records);
* Map<String, SObject> accountByNumber = account.getSObjectByStringField(Account.AccountNumber);
*/
protected virtual Map<String, SObject> getSObjectByStringField(Schema.SObjectField sObjectField)
{
Map<String, SObject> result = new Map<String, SObject>();
for (SObject record : getRecords())
{
if (record.get(sObjectField) == null) continue;
String fieldValue = (String) record.get(sObjectField);
result.put(fieldValue, record);
}
return result;
}
/**
* Get a map with the records mapped to the given String field value
* Key fields containing null values are omitted
*
* @param sObjectField The field to use as key for the map
*
* @return Returns a map with the records mapped to the given String field value
*
* @example
* Account account = Account.newInstance(records);
* Map<String, SObject> accountByName = account.getSObjectsByStringField(Account.AccountName);
*/
protected virtual Map<String, List<SObject>> getSObjectsByStringField(Schema.SObjectField sObjectField)
{
Map<String, List<SObject>> result = new Map<String, List<SObject>>();
for (SObject record : getRecords())
{
String fieldValue = (String) record.get(sObjectField);
if (fieldValue == null) continue;
if (!result.containsKey(fieldValue))
{
result.put(fieldValue, new List<SObject>());
}
result.get(fieldValue).add(record);
}
return result;
}
/**
* Get a map with the given String field value mapped to the given Id field
* Key fields containing null values are omitted
*
* @param valueField The String field to use as value for the map
* @param keyField The Id field to use as key for the map
*
* @return a map with the given String field value mapped to the given Id field
*
* @example
* Account account = Account.newInstance(records);
* Map<Id, String> accountNameById = account.getStringFieldByIdField(Account.AccountName, Account.Id);
*/
protected virtual Map<Id, String> getStringFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{
Map<Id, String> result = new Map<Id, String>();
Map<Object, Object> fieldByField = getFieldByField(keyField, valueField);
for (Object obj : fieldByField.keySet())
{
result.put((Id) obj, (String) fieldByField.get(obj));
}
return result;
}
/**
* Get a map with the records mapped to the given String field value
* Key fields containing null values are omitted
*
* @param valueField The field to use as value for the map
* @param keyField The field to use as key for the map
*
* @return Returns a map with the record mapped to the given Id field value
*
* @example
* Account account = Account.newInstance(records);
* Map<Id, String> accountNameById = account.getStringFieldByStringField(Account.AccountName, Account.Id);
*/
protected virtual Map<String, String> getStringFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{
Map<String, String> result = new Map<String, String>();
Map<Object, Object> fieldByField = getFieldByField(keyField, valueField);
for (Object obj : fieldByField.keySet())
{
result.put((String) obj, (String) fieldByField.get(obj));
}
return result;
}
/**
* Gets the values for the provided String field
*
* @param sObjectField The Schema.SObjectField to get their values
*
* @return The String values of the given sObjectField
@@ -435,6 +713,8 @@ public virtual class SObjects
}
/**
* Sets the given value to the given field only when the criteria is met for the record
*
* @param sObjectField The Schema.SObjectField to change its value
* @param value The new value for the given sObjectField
* @param criteria The criteria condition that must be met to include the value in the return