Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Class which contains useful utility methods required across classes
- public class QueryGenerator {
- public static Map<String, Schema.SObjectType> GLOBALDESCRIBE;
- public static Schema.SObjectType getSObjectType(String objectName){
- //lazy load
- if(null == GLOBALDESCRIBE){
- GLOBALDESCRIBE = Schema.getGlobalDescribe();
- }
- return GLOBALDESCRIBE.get(objectName);
- }
- public static final String QUERY_TEMPLATE = 'SELECT {0} FROM {1} {2}';
- public static final String NESTED_QUERY = '({0})';
- public static final String COMMA_SEPARATOR = ', ';
- public static final String FIELD_ID = 'Id';
- //Gerate query for all fields in the fieldset and all fields from the related objects
- //relatedRelationshipNames is a map of
- // Relationshipname => Related ObjectName
- public static String generateQuery( Schema.sObjectType fromObject
- , Schema.FieldSet fieldSet
- , Map<String,String> relationshipVsObjectNameMap
- , String whereClause
- ){
- List<String> queryFieldList;
- if( fieldSet != null ){
- queryFieldList = getFieldListFor(fieldSet);
- } else {
- queryFieldList = getFieldListFor(fromObject);
- }
- //generate nestedQueryClauses
- if( null != relationshipVsObjectNameMap ){
- queryFieldList.addAll( getInnerQueryList(relationshipVsObjectNameMap) );
- }
- String query = generateQuery(''+fromObject, queryFieldList, whereClause);
- return query;
- }
- public static List<String> getInnerQueryList(Map<String,String> relationshipVsObjectNameMap){
- List<String> queryFieldList = new List<String>();
- String objectName;
- List<String> relatedFieldList;
- String relatedQuery;
- for( String relationshipName : relationshipVsObjectNameMap.keySet() ){
- objectName = relationshipVsObjectNameMap.get(relationshipName);
- relatedFieldList = getFieldListFor(getSObjectType(objectName));
- relatedQuery = generateQuery( relationshipName
- , relatedFieldList
- , '');
- relatedQuery = String.format(NESTED_QUERY,new String[]{relatedQuery});
- queryFieldList.add(relatedQuery);
- }
- return queryFieldList;
- }
- //generate query for fields within a field set
- public static String generateQuery( Schema.sObjectType fromObject
- , Schema.FieldSet fieldSet
- , String whereClause){
- return generateQuery(''+fromObject, getFieldListFor(fieldSet), whereClause);
- }
- //generate query for fieldnames passed in
- public static String generateQuery( String fromObject
- , List<String> fieldNameSet
- , String whereClause){
- //sanity check
- whereClause = whereClause == null ? '' : whereClause;
- //local variables
- String queryFieldsClause = '';
- for(String fieldName : fieldNameSet) {
- //if not first time here, add a ,
- if(false == String.isBlank(queryFieldsClause)){
- queryFieldsClause += COMMA_SEPARATOR;
- }
- queryFieldsClause += fieldName;
- }
- //Corner cases if no fields in the field set field set
- //1
- if( true == String.isBlank(queryFieldsClause) ){
- queryFieldsClause += FIELD_ID;
- }
- String query = String.format(QUERY_TEMPLATE, new String[]{
- queryFieldsClause
- , ''+fromObject
- , whereClause
- });
- return query;
- }
- //Supporting methods
- public static List<String> getFieldListFor(Schema.FieldSet fieldSet){
- List<String> fieldNameSet = new List<String>();
- for(Schema.FieldSetMember fieldSetMember : fieldSet.getFields()) {
- fieldNameSet.add(fieldSetMember.getFieldPath());
- }
- return fieldNameSet;
- }
- public static List<String> getFieldListFor(Schema.sObjectType fromObject){
- List<String> fieldNameSet = new List<String>();
- Schema.DescribeFieldResult dfr;
- for(Schema.SObjectField fieldInstance : fromObject.getDescribe().fields.getMap().values()) {
- dfr = fieldInstance.getDescribe();
- if( dfr.isAccessible() ){
- fieldNameSet.add( dfr.getname() );
- }
- }
- return fieldNameSet;
- }
- public static Boolean startsWith(String sourceString, String character){
- //sanity
- if( sourceString == null ){
- return false;
- }
- return sourceString.trim().startsWith(character);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement