diff --git a/sfdx-project.json b/sfdx-project.json index f99b10a..940b127 100644 --- a/sfdx-project.json +++ b/sfdx-project.json @@ -16,6 +16,7 @@ "packageAliases": { "fflibApexExtensions": "0HoJ8000000KyjfKAC", "fflibApexExtensions@0.7.0-1": "04tJ8000000kaKzIAI", - "fflibApexExtensions@0.8.0-1-apiUpgrade": "04tJ8000000kaL4IAI" + "fflibApexExtensions@0.8.0-1-apiUpgrade": "04tJ8000000kaL4IAI", + "fflibApexExtensions@0.9.0-1-feature/genericSelector": "04tJ8000000kaPHIAY" } -} +} \ No newline at end of file diff --git a/sfdx-source/apex-extensions/main/default/classes/selectors/fflib_SObjectsSelector.cls b/sfdx-source/apex-extensions/main/default/classes/selectors/fflib_SObjectsSelector.cls new file mode 100644 index 0000000..36f6251 --- /dev/null +++ b/sfdx-source/apex-extensions/main/default/classes/selectors/fflib_SObjectsSelector.cls @@ -0,0 +1,209 @@ +/** + * File Name: fflib_SObjectsSelector + * + * Generic SObject selector class + * + * @author architect ir. Wilhelmus G.J. Velzeboer + * + * Copyright (c), W.G.J. Velzeboer, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above author notice, + * this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * - Neither the name of the author nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +public with sharing class fflib_SObjectsSelector + extends fflib_SObjectSelector { + /** + * The current implementation type of the service + */ + private static System.Type implementationType = fflib_SObjectsSelector.class; + + /** + * The implementation replacement for the service, + */ + private static fflib_SObjectsSelector implementationReplacement; + + /** + * The list of field that should be retrieved with each query + */ + private List sObjectFields { get; set; } + + /** + * Holds the SObjectType of the records to query + */ + private Schema.SObjectType sObjectType { get; set; } + + /** + * Runs the query in System Mode, disabling FLS, CRUD & sharing rules + * + * @param sObjectType The SObjectType of the records to query + * @param sObjectFields a set of SObjectFields to get each time a Quote records is queried + * + * @return New instance of the selector + */ + public static fflib_SObjectsSelector newElevatedInstance(SObjectType sObjectType, Set sObjectFields) { + fflib_SObjectsSelector selector = + (implementationReplacement == null) ? (fflib_SObjectsSelector) implementationType.newInstance() : implementationReplacement; + selector.setDataAccess(fflib_SObjectSelector.DataAccess.SYSTEM_MODE); + selector.setSObjectFields(sObjectFields); + selector.setSObjectType(sObjectType); + return selector; + } + + /** + * Runs the query in System Mode, disabling FLS, CRUD & sharing rules + * + * @param sObjectType The SObjectType of the records to query + * @param sObjectFields a set of SObjectFields to get each time a Quote records is queried + * + * @return New instance of the selector + */ + public static fflib_SObjectsSelector newElevatedInstance(SObjectType sObjectType, Set sObjectFields) { + fflib_SObjectsSelector selector = + (implementationReplacement == null) ? (fflib_SObjectsSelector) implementationType.newInstance() : implementationReplacement; + selector.setDataAccess(fflib_SObjectSelector.DataAccess.SYSTEM_MODE); + selector.setSObjectFields(sObjectType, sObjectFields); + selector.setSObjectType(sObjectType); + return selector; + } + + /** + * Class constructor + * This should not be used directly by an application but only by fflib + */ + public fflib_SObjectsSelector() { + super(); + } + + /** + * The fields to always include in all queries + * + * @return Returns a list of default fields that are always queried + */ + public virtual List getSObjectFieldList() { + return this.sObjectFields; + } + + /** + * Defines the SObjectType for this selector + * + * @return the SObjectType of this selector + */ + public Schema.SObjectType getSObjectType() { + return this.sObjectType; + } + + /** + * Replace the default implementation of the service + * + * @param systemType The System.Type of the replacement class + */ + public static void replaceWith(System.Type systemType) { + fflib_SObjectsSelector.implementationType = systemType; + } + + /** + * Replace the default implementation of the service + * + * @param replacement A replacement instance + * + * @see fflib_SObjectsSelector + */ + public static void replaceWith(fflib_SObjectsSelector replacement) { + fflib_SObjectsSelector.implementationReplacement = replacement; + } + + /** + * Queries records by their record Id + * + * @param idSet A set of record Id values + * + * @return A list of SObject records + */ + public virtual List selectById(Set idSet) { + return Database.query( + newQueryFactory() + .setCondition('Id IN :idSet') + .toSOQL() + ); + } + + /** + * Queries SObject records by their record Id + * + * @param sObjectField The Id field to query + * @param ids A set of record Id values + * + * @return A list of SObject records + */ + public List selectByIdField(SObjectField sObjectField, Set ids) { + return Database.query( + newQueryFactory() + .setCondition(sObjectField.getDescribe().getName() + ' IN :ids') + .toSOQL() + ); + } + + /** + * Sets the list of fields that are fetched on each query + * + * @param sObjectFields A set of SObjectFields from the SObjectType + * + * @return Returns an instance of itself to allow for method chaining + */ + public fflib_SObjectsSelector setSObjectFields(Set sObjectFields) { + this.sObjectFields = new List(sObjectFields); + return this; + } + + /** + * Sets the list of fields that are fetched on each query + * + * @param sObjectType The SObjectType of the provided fields + * @param fieldNames A set of SObject field names + * + * @return Returns an instance of itself to allow for method chaining + */ + public fflib_SObjectsSelector setSObjectFields(SObjectType sObjectType, Set fieldNames) { + Map sObjectFieldsByName = sObjectType.getDescribe().fields.getMap(); + + Set sObjectFields = new Set(); + for (String fieldName : fieldNames) { + if (sObjectFieldsByName.containsKey(fieldName) == false) continue; + + sObjectFields.add(sObjectFieldsByName.get(fieldName)); + } + setSObjectFields(sObjectFields); + return this; + } + + /** + * Sets the SObjectType of the records to query + * + * @param sObjectType The SObjectType of the records to query + * + * @return Returns an instance of itself to allow for method chaining + */ + public fflib_SObjectsSelector setSObjectType(SObjectType sObjectType) { + this.sObjectType = sObjectType; + return this; + } +} \ No newline at end of file diff --git a/sfdx-source/apex-extensions/main/default/classes/selectors/fflib_SObjectsSelector.cls-meta.xml b/sfdx-source/apex-extensions/main/default/classes/selectors/fflib_SObjectsSelector.cls-meta.xml new file mode 100644 index 0000000..cad713d --- /dev/null +++ b/sfdx-source/apex-extensions/main/default/classes/selectors/fflib_SObjectsSelector.cls-meta.xml @@ -0,0 +1,5 @@ + + + 66.0 + Active +