Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PicklistHelper {
- public class EntryConfiguration {
- private String soql;
- private String key;
- private Schema.DescribeFieldResult field;
- private Boolean required;
- /**
- * If soql is provided, picklist entries are fetched via respective soql. Note that the Ids in the
- * queried result sets represent the values of the picklist entries (as opposed to their labels).
- */
- public EntryConfiguration(String key, Schema.DescribeFieldResult field, Boolean required, String soql) {
- this.key = key;
- this.field = field;
- this.required = required;
- this.soql = soql;
- }
- }
- /**
- * Returns a map of picklists that can directly be assigned to component variables of type List
- * and used in an aura:iteration for ui:inputSelectOption.
- * @see EntryConfiguration
- */
- public static String getPicklists(List<EntryConfiguration> configs) {
- Map<String, List<Map<String, String>>> m = new Map<String, List<Map<String, String>>>();
- for(EntryConfiguration config:configs) {
- List<Map<String, String>> picklistEntries = new List<Map<String, String>>();
- if(String.isBlank(config.soql)) {
- List<Schema.PicklistEntry> ples = config.field.getPickListValues();
- for(Schema.PicklistEntry ple: ples) {
- Map<String, String> entry = new Map<String, String>();
- entry.put('label', ple.getLabel());
- entry.put('value', ple.getValue());
- picklistEntries.add(entry);
- }
- } else {
- List<sObject> objects = Database.query(config.soql);
- for(sObject o:objects) {
- Object field = o.get(config.field.getName());
- if(field != null) {
- String label = String.valueOf(field);
- Map<String, String> entry = new Map<String, String>();
- entry.put('label', label);
- entry.put('value', o.Id);
- picklistEntries.add(entry);
- }
- }
- }
- if(!config.required) {
- Map<String, String> entry = new Map<String, String>();
- entry.put('label', System.Label.crta.List_all);
- entry.put('value', '');
- picklistEntries.add(entry);
- }
- m.put(config.key, picklistEntries);
- }
- String s = JSON.serialize(m);
- return s;
- }
- private class PicklistEntryWrapper {
- //String active {get; set;}
- //String defaultValue {get; set;}
- String label {get; set;}
- //String value {get; set;}
- String validFor {get; set;}
- }
- private class PicklistEntryWrapperWithValue {
- String label {get; set;}
- String value {get; set;}
- String validFor {get; set;}
- }
- private static final String base64Chars
- = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789+/';
- public static String getDependentEntries(String objApiName, String sDependentField, String sControllingField) {
- Schema.SObjectField dependentField
- = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sDependentField);
- Schema.SObjectField controllingField
- = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sControllingField);
- String s = getDependentEntries(dependentField, controllingField);
- return s;
- }
- public static String getDependentEntriesWithValues(String objApiName, String sDependentField, String sControllingField) {
- Schema.SObjectField dependentField
- = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sDependentField);
- Schema.SObjectField controllingField
- = System.Schema.getGlobalDescribe().get(objApiName).getDescribe().fields.getMap().get(sControllingField);
- String s = getDependentEntriesWithValues(dependentField, controllingField);
- return s;
- }
- public static String getDependentEntries(Schema.SObjectField dependentField,
- Schema.SObjectField controllingField) {
- // validFor property cannot be accessed via a method or a property,
- // so we need to serialize the PicklistEntry object and then deserialize into a wrapper.
- List<Schema.PicklistEntry> contrEntries = controllingField.getDescribe().getPicklistValues();
- List<Schema.PicklistEntry> depEntriesOrig = dependentField.getDescribe().getPicklistValues();
- List<PicklistEntryWrapper> depEntries = wrapPicklistEntries(depEntriesOrig);
- // Set up the return container - Map<ControllingValue, List<DependentValues>>
- Map<String, List<String>> objResults = new Map<String, List<String>>();
- List<String> controllingValues = new List<String>();
- for(Schema.PicklistEntry ple : contrEntries) {
- String label = ple.getLabel();
- objResults.put(label, new List<String>());
- controllingValues.add(label);
- }
- for(PicklistEntryWrapper plew : depEntries) {
- String label = plew.label;
- String validForBits = base64ToBits(plew.validFor);
- for(Integer i = 0; i < validForBits.length(); i++) {
- // For each bit, in order: if it's a 1, add this label to the dependent list
- // for the corresponding controlling value
- String bit = validForBits.mid(i, 1);
- if(bit == '1') {
- objResults.get(controllingValues.get(i)).add(label);
- }
- }
- }
- /*
- string fieldDependencies = '';
- //Map<String, Set<String>> m = new Map<String, Set<String>>();
- for(string controllervalue : objResults.keySet()){
- if(objResults.get(controllervalue) != null){
- List<String> dependencies = objResults.get(controllervalue);
- for(string dependentValue : dependencies) {
- fieldDependencies+= '{"techarea":"'+controllervalue+'","subtecharea":"'+dependentValue+'","keywords":"","label":"'+controllervalue+' => '+dependentValue+' => "},';
- }
- }
- }
- */
- //fieldDependencies = JSON.serialize(objResults);
- //system.debug('@@@ fieldDependencies @@@ '+fieldDependencies);
- //return fieldDependencies;
- return JSON.serialize(objResults);
- }
- public static String getDependentEntriesWithValues(Schema.SObjectField dependentField,
- Schema.SObjectField controllingField){
- List<Schema.PicklistEntry> contrEntries = controllingField.getDescribe().getPicklistValues();
- List<Schema.PicklistEntry> depEntriesOrig = dependentField.getDescribe().getPicklistValues();
- List<PicklistEntryWrapperWithValue> depEntries = wrapPicklistEntriesWithValues(depEntriesOrig);
- Map<String, List<String>> objResults = new Map<String, List<String>>();
- List<String> controllingValues = new List<String>();
- for(Schema.PicklistEntry ple : contrEntries) {
- String label = ple.getLabel();
- String value = ple.getValue();
- objResults.put(value, new List<String>());
- controllingValues.add(value);
- }
- for(PicklistEntryWrapperWithValue plew : depEntries) {
- String label = plew.label;
- String value = plew.value;
- String validForBits = base64ToBits(plew.validFor);
- for(Integer i = 0; i < validForBits.length(); i++) {
- // For each bit, in order: if it's a 1, add this label to the dependent list
- // for the corresponding controlling value
- String bit = validForBits.mid(i, 1);
- if(bit == '1') {
- objResults.get(controllingValues.get(i)).add(label+'|'+value);
- }
- }
- }
- return JSON.serialize(objResults);
- }
- // Convert decimal to binary representation (alas, Apex has no native method :-(
- // eg. 4 => '100', 19 => '10011', etc.
- // Method: Divide by 2 repeatedly until 0. At each step note the remainder (0 or 1).
- // These, in reverse order, are the binary.
- @TestVisible
- private static String decimalToBinary(Integer val) {
- String bits = '';
- while(val > 0) {
- Integer remainder = Math.mod(val, 2);
- val = Integer.valueOf(Math.floor(val / 2));
- bits = String.valueOf(remainder) + bits;
- }
- return bits;
- }
- // Convert a base64 token into a binary/bits representation
- // e.g. 'gAAA' => '100000000000000000000'
- private static String base64ToBits(String validFor) {
- if(String.isEmpty(validFor)) return '';
- String validForBits = '';
- for(Integer i = 0; i < validFor.length(); i++) {
- String thisChar = validFor.mid(i, 1);
- Integer val = base64Chars.indexOf(thisChar);
- String bits = decimalToBinary(val).leftPad(6, '0');
- validForBits += bits;
- }
- return validForBits;
- }
- private static List<PicklistEntryWrapper> wrapPicklistEntries(List<Schema.PicklistEntry> ples) {
- return (List<PicklistEntryWrapper>)
- JSON.deserialize(JSON.serialize(ples), List<PicklistEntryWrapper>.class);
- }
- private static List<PicklistEntryWrapperWithValue> wrapPicklistEntriesWithValues(List<Schema.PicklistEntry> ples) {
- return (List<PicklistEntryWrapperWithValue>)
- JSON.deserialize(JSON.serialize(ples), List<PicklistEntryWrapperWithValue>.class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement