Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. // Class which contains useful utility methods required across classes
  2. public class QueryGenerator {
  3.  
  4. public static Map<String, Schema.SObjectType> GLOBALDESCRIBE;
  5.  
  6.  
  7. public static Schema.SObjectType getSObjectType(String objectName){
  8. //lazy load
  9. if(null == GLOBALDESCRIBE){
  10. GLOBALDESCRIBE = Schema.getGlobalDescribe();
  11. }
  12.  
  13. return GLOBALDESCRIBE.get(objectName);
  14. }
  15.  
  16. public static final String QUERY_TEMPLATE = 'SELECT {0} FROM {1} {2}';
  17. public static final String NESTED_QUERY = '({0})';
  18. public static final String COMMA_SEPARATOR = ', ';
  19. public static final String FIELD_ID = 'Id';
  20.  
  21. //Gerate query for all fields in the fieldset and all fields from the related objects
  22. //relatedRelationshipNames is a map of
  23. // Relationshipname => Related ObjectName
  24. public static String generateQuery( Schema.sObjectType fromObject
  25. , Schema.FieldSet fieldSet
  26. , Map<String,String> relationshipVsObjectNameMap
  27. , String whereClause
  28. ){
  29.  
  30. List<String> queryFieldList;
  31.  
  32. if( fieldSet != null ){
  33. queryFieldList = getFieldListFor(fieldSet);
  34. } else {
  35. queryFieldList = getFieldListFor(fromObject);
  36. }
  37.  
  38. //generate nestedQueryClauses
  39. if( null != relationshipVsObjectNameMap ){
  40. queryFieldList.addAll( getInnerQueryList(relationshipVsObjectNameMap) );
  41. }
  42.  
  43. String query = generateQuery(''+fromObject, queryFieldList, whereClause);
  44.  
  45. return query;
  46. }
  47.  
  48. public static List<String> getInnerQueryList(Map<String,String> relationshipVsObjectNameMap){
  49. List<String> queryFieldList = new List<String>();
  50. String objectName;
  51. List<String> relatedFieldList;
  52. String relatedQuery;
  53.  
  54. for( String relationshipName : relationshipVsObjectNameMap.keySet() ){
  55. objectName = relationshipVsObjectNameMap.get(relationshipName);
  56. relatedFieldList = getFieldListFor(getSObjectType(objectName));
  57. relatedQuery = generateQuery( relationshipName
  58. , relatedFieldList
  59. , '');
  60. relatedQuery = String.format(NESTED_QUERY,new String[]{relatedQuery});
  61. queryFieldList.add(relatedQuery);
  62. }
  63. return queryFieldList;
  64. }
  65.  
  66. //generate query for fields within a field set
  67. public static String generateQuery( Schema.sObjectType fromObject
  68. , Schema.FieldSet fieldSet
  69. , String whereClause){
  70.  
  71. return generateQuery(''+fromObject, getFieldListFor(fieldSet), whereClause);
  72. }
  73.  
  74. //generate query for fieldnames passed in
  75. public static String generateQuery( String fromObject
  76. , List<String> fieldNameSet
  77. , String whereClause){
  78.  
  79. //sanity check
  80. whereClause = whereClause == null ? '' : whereClause;
  81.  
  82. //local variables
  83. String queryFieldsClause = '';
  84.  
  85. for(String fieldName : fieldNameSet) {
  86. //if not first time here, add a ,
  87. if(false == String.isBlank(queryFieldsClause)){
  88. queryFieldsClause += COMMA_SEPARATOR;
  89. }
  90.  
  91. queryFieldsClause += fieldName;
  92. }
  93.  
  94. //Corner cases if no fields in the field set field set
  95. //1
  96. if( true == String.isBlank(queryFieldsClause) ){
  97. queryFieldsClause += FIELD_ID;
  98. }
  99.  
  100. String query = String.format(QUERY_TEMPLATE, new String[]{
  101. queryFieldsClause
  102. , ''+fromObject
  103. , whereClause
  104. });
  105.  
  106. return query;
  107. }
  108.  
  109. //Supporting methods
  110. public static List<String> getFieldListFor(Schema.FieldSet fieldSet){
  111. List<String> fieldNameSet = new List<String>();
  112. for(Schema.FieldSetMember fieldSetMember : fieldSet.getFields()) {
  113. fieldNameSet.add(fieldSetMember.getFieldPath());
  114. }
  115. return fieldNameSet;
  116. }
  117.  
  118. public static List<String> getFieldListFor(Schema.sObjectType fromObject){
  119. List<String> fieldNameSet = new List<String>();
  120. Schema.DescribeFieldResult dfr;
  121. for(Schema.SObjectField fieldInstance : fromObject.getDescribe().fields.getMap().values()) {
  122. dfr = fieldInstance.getDescribe();
  123. if( dfr.isAccessible() ){
  124. fieldNameSet.add( dfr.getname() );
  125. }
  126. }
  127. return fieldNameSet;
  128. }
  129.  
  130. public static Boolean startsWith(String sourceString, String character){
  131. //sanity
  132. if( sourceString == null ){
  133. return false;
  134. }
  135. return sourceString.trim().startsWith(character);
  136. }
  137.  
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement