Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.92 KB | None | 0 0
  1. public class PicklistHelper {
  2.  
  3. public class EntryConfiguration {
  4. private String soql;
  5. private String key;
  6. private Schema.DescribeFieldResult field;
  7. private Boolean required;
  8. /**
  9. * If soql is provided, picklist entries are fetched via respective soql. Note that the Ids in the
  10. * queried result sets represent the values of the picklist entries (as opposed to their labels).
  11. */
  12. public EntryConfiguration(String key, Schema.DescribeFieldResult field, Boolean required, String soql) {
  13. this.key = key;
  14. this.field = field;
  15. this.required = required;
  16. this.soql = soql;
  17. }
  18. }
  19.  
  20. /**
  21. * Returns a map of picklists that can directly be assigned to component variables of type List
  22. * and used in an aura:iteration for ui:inputSelectOption.
  23. * @see EntryConfiguration
  24. */
  25. public static String getPicklists(List<EntryConfiguration> configs) {
  26. Map<String, List<Map<String, String>>> m = new Map<String, List<Map<String, String>>>();
  27. for(EntryConfiguration config:configs) {
  28. List<Map<String, String>> picklistEntries = new List<Map<String, String>>();
  29. if(String.isBlank(config.soql)) {
  30. List<Schema.PicklistEntry> ples = config.field.getPickListValues();
  31. for(Schema.PicklistEntry ple: ples) {
  32. Map<String, String> entry = new Map<String, String>();
  33. entry.put('label', ple.getLabel());
  34. entry.put('value', ple.getValue());
  35. picklistEntries.add(entry);
  36. }
  37. } else {
  38. List<sObject> objects = Database.query(config.soql);
  39. for(sObject o:objects) {
  40. Object field = o.get(config.field.getName());
  41. if(field != null) {
  42. String label = String.valueOf(field);
  43. Map<String, String> entry = new Map<String, String>();
  44. entry.put('label', label);
  45. entry.put('value', o.Id);
  46. picklistEntries.add(entry);
  47. }
  48. }
  49. }
  50. if(!config.required) {
  51. Map<String, String> entry = new Map<String, String>();
  52. entry.put('label', System.Label.crta.List_all);
  53. entry.put('value', '');
  54. picklistEntries.add(entry);
  55. }
  56. m.put(config.key, picklistEntries);
  57. }
  58. String s = JSON.serialize(m);
  59. return s;
  60. }
  61.  
  62.  
  63. private class PicklistEntryWrapper {
  64. //String active {get; set;}
  65. //String defaultValue {get; set;}
  66. String label {get; set;}
  67. //String value {get; set;}
  68. String validFor {get; set;}
  69. }
  70.  
  71.  
  72. private class PicklistEntryWrapperWithValue {
  73.  
  74. String label {get; set;}
  75. String value {get; set;}
  76. String validFor {get; set;}
  77. }
  78.  
  79. private static final String base64Chars
  80. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789+/';
  81.  
  82.  
  83. public static String getDependentEntries(String objApiName, String sDependentField, String sControllingField) {
  84. Schema.SObjectField dependentField
  85. = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sDependentField);
  86. Schema.SObjectField controllingField
  87. = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sControllingField);
  88. String s = getDependentEntries(dependentField, controllingField);
  89. return s;
  90. }
  91.  
  92.  
  93. public static String getDependentEntriesWithValues(String objApiName, String sDependentField, String sControllingField) {
  94. Schema.SObjectField dependentField
  95. = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sDependentField);
  96. Schema.SObjectField controllingField
  97. = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sControllingField);
  98. String s = getDependentEntriesWithValues(dependentField, controllingField);
  99. return s;
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. public static String getDependentEntries(Schema.SObjectField dependentField,
  108. Schema.SObjectField controllingField) {
  109.  
  110. // validFor property cannot be accessed via a method or a property,
  111. // so we need to serialize the PicklistEntry object and then deserialize into a wrapper.
  112. List<Schema.PicklistEntry> contrEntries = controllingField.getDescribe().getPicklistValues();
  113. List<Schema.PicklistEntry> depEntriesOrig = dependentField.getDescribe().getPicklistValues();
  114. List<PicklistEntryWrapper> depEntries = wrapPicklistEntries(depEntriesOrig);
  115.  
  116. // Set up the return container - Map<ControllingValue, List<DependentValues>>
  117. Map<String, List<String>> objResults = new Map<String, List<String>>();
  118. List<String> controllingValues = new List<String>();
  119.  
  120. for(Schema.PicklistEntry ple : contrEntries) {
  121. String label = ple.getLabel();
  122. objResults.put(label, new List<String>());
  123. controllingValues.add(label);
  124. }
  125.  
  126. for(PicklistEntryWrapper plew : depEntries) {
  127. String label = plew.label;
  128. String validForBits = base64ToBits(plew.validFor);
  129. for(Integer i = 0; i < validForBits.length(); i++) {
  130. // For each bit, in order: if it's a 1, add this label to the dependent list
  131. // for the corresponding controlling value
  132. String bit = validForBits.mid(i, 1);
  133. if(bit == '1') {
  134. objResults.get(controllingValues.get(i)).add(label);
  135. }
  136. }
  137. }
  138. /*
  139. string fieldDependencies = '';
  140. //Map<String, Set<String>> m = new Map<String, Set<String>>();
  141. for(string controllervalue : objResults.keySet()){
  142. if(objResults.get(controllervalue) != null){
  143. List<String> dependencies = objResults.get(controllervalue);
  144. for(string dependentValue : dependencies) {
  145. fieldDependencies+= '{"techarea":"'+controllervalue+'","subtecharea":"'+dependentValue+'","keywords":"","label":"'+controllervalue+' => '+dependentValue+' => "},';
  146. }
  147.  
  148. }
  149. }
  150. */
  151. //fieldDependencies = JSON.serialize(objResults);
  152. //system.debug('@@@ fieldDependencies @@@ '+fieldDependencies);
  153. //return fieldDependencies;
  154. return JSON.serialize(objResults);
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. public static String getDependentEntriesWithValues(Schema.SObjectField dependentField,
  162. Schema.SObjectField controllingField){
  163.  
  164. List<Schema.PicklistEntry> contrEntries = controllingField.getDescribe().getPicklistValues();
  165. List<Schema.PicklistEntry> depEntriesOrig = dependentField.getDescribe().getPicklistValues();
  166. List<PicklistEntryWrapperWithValue> depEntries = wrapPicklistEntriesWithValues(depEntriesOrig);
  167.  
  168. Map<String, List<String>> objResults = new Map<String, List<String>>();
  169. List<String> controllingValues = new List<String>();
  170.  
  171. for(Schema.PicklistEntry ple : contrEntries) {
  172. String label = ple.getLabel();
  173. String value = ple.getValue();
  174. objResults.put(value, new List<String>());
  175. controllingValues.add(value);
  176. }
  177.  
  178.  
  179. for(PicklistEntryWrapperWithValue plew : depEntries) {
  180. String label = plew.label;
  181. String value = plew.value;
  182. String validForBits = base64ToBits(plew.validFor);
  183. for(Integer i = 0; i < validForBits.length(); i++) {
  184. // For each bit, in order: if it's a 1, add this label to the dependent list
  185. // for the corresponding controlling value
  186. String bit = validForBits.mid(i, 1);
  187. if(bit == '1') {
  188. objResults.get(controllingValues.get(i)).add(label+'|'+value);
  189. }
  190. }
  191. }
  192.  
  193.  
  194. return JSON.serialize(objResults);
  195.  
  196. }
  197.  
  198.  
  199.  
  200. // Convert decimal to binary representation (alas, Apex has no native method :-(
  201. // eg. 4 => '100', 19 => '10011', etc.
  202. // Method: Divide by 2 repeatedly until 0. At each step note the remainder (0 or 1).
  203. // These, in reverse order, are the binary.
  204. @TestVisible
  205. private static String decimalToBinary(Integer val) {
  206. String bits = '';
  207. while(val > 0) {
  208. Integer remainder = Math.mod(val, 2);
  209. val = Integer.valueOf(Math.floor(val / 2));
  210. bits = String.valueOf(remainder) + bits;
  211. }
  212. return bits;
  213. }
  214.  
  215. // Convert a base64 token into a binary/bits representation
  216. // e.g. 'gAAA' => '100000000000000000000'
  217. private static String base64ToBits(String validFor) {
  218. if(String.isEmpty(validFor)) return '';
  219.  
  220. String validForBits = '';
  221.  
  222. for(Integer i = 0; i < validFor.length(); i++) {
  223. String thisChar = validFor.mid(i, 1);
  224. Integer val = base64Chars.indexOf(thisChar);
  225. String bits = decimalToBinary(val).leftPad(6, '0');
  226. validForBits += bits;
  227. }
  228.  
  229. return validForBits;
  230. }
  231.  
  232. private static List<PicklistEntryWrapper> wrapPicklistEntries(List<Schema.PicklistEntry> ples) {
  233. return (List<PicklistEntryWrapper>)
  234. JSON.deserialize(JSON.serialize(ples), List<PicklistEntryWrapper>.class);
  235. }
  236.  
  237.  
  238. private static List<PicklistEntryWrapperWithValue> wrapPicklistEntriesWithValues(List<Schema.PicklistEntry> ples) {
  239. return (List<PicklistEntryWrapperWithValue>)
  240. JSON.deserialize(JSON.serialize(ples), List<PicklistEntryWrapperWithValue>.class);
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement