Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //wrapper class that is used in the cache
- global class MenuWrapper{
- public List<FieloEE__Section__c> sections {get; set;}
- public Map<Id,List<FieloEE__Section__c>> componentsSection {get; set;}
- public Map<Id,FieloEE__Component__c> componentsMap {get; set;}
- public Set<Id> componentCacheIds;
- }
- //Action method
- public PageReference init(){
- loadSections();
- return null;
- }
- private void loadSections(){
- //verifies if the cache functionality is enabled
- cacheEnabled = FieloEE__PublicSettings__c.getOrgDefaults().CacheEnabled__c;
- if(cacheEnabled != null && cacheEnabled){
- loadSectionsWithCache();
- }else{
- loadSectionsWithoutCache();
- }
- }
- private void loadSectionsWithoutCache(){
- //instantiate wrapper class
- menuWrapper = new MenuWrapper();
- //queries sections
- menuWrapper.sections = [SELECT Id, Name, F_PRM_Asynchronous__c, FieloEE__Type__c, FieloEE__Order__c, FieloEE__Menu__c, FieloEE__CSSClasses__c FROM FieloEE__Section__c WHERE FieloEE__Menu__c =: menu2.Id ORDER BY FieloEE__Order__c ASC NULLS FIRST,CreatedDate ASC];
- //query subsections with components related lists
- List<FieloEE__Section__c> sections = menuWrapper.sections;
- for(FieloEE__Section__c subSection : Database.query('SELECT Id, FieloEE__Type__c, FieloEE__Order__c, Name, FieloEE__Menu__c, FieloEE__Parent__c, FieloEE__CSSClasses__c, (SELECT ' + fields + ' FROM FieloEE__Components__r ORDER BY FieloEE__Order__c asc) FROM FieloEE__Section__c WHERE FieloEE__Parent__c IN : sections ORDER BY FieloEE__Order__c ASC')){
- if(!menuWrapper.componentsSection.containsKey(subSection.FieloEE__Parent__c)){
- menuWrapper.componentsSection.put(subSection.FieloEE__Parent__c, new List<FieloEE__Section__c>{subSection});
- }else{
- menuWrapper.componentsSection.get(subSection.FieloEE__Parent__c).add(subSection);
- }
- //load componentsMap
- FieloEE.PageController.componentsMap.putAll(subSection.FieloEE__Components__r);
- }
- //the componentCacheIds is null due cache is inactive
- }
- private void loadSectionsWithCache(){
- //Get platform cache partition
- orgPart = Cache.Org.getPartition('local.FieloMenus');
- //generates menu key
- keyString = String.valueOf(menu2.Id).subString(0,15);
- //get value from the cache for specified key
- menuWrapper = (MenuWrapper)orgPart.get(keyString);
- //checks if the key exists in the cache, if no then loads the wrapper
- if(menuWrapper == null){
- loadMenuWrapper(keyString);
- }
- }
- private void loadMenuWrapper(String keyString){
- //instantiates the wrapper
- menuWrapper = new MenuWrapper();
- //query sections
- menuWrapper.sections = [SELECT Id, Name, F_PRM_Asynchronous__c, FieloEE__Type__c, FieloEE__Order__c, FieloEE__Menu__c, FieloEE__CSSClasses__c FROM FieloEE__Section__c WHERE FieloEE__Menu__c =: menu2.Id];
- //query subsections
- for(FieloEE__Section__c subSection : Database.query('SELECT Id, FieloEE__Type__c, FieloEE__Order__c, Name, FieloEE__Menu__c, FieloEE__Parent__c, FieloEE__CSSClasses__c, (SELECT ' + fields + ' FROM FieloEE__Components__r ORDER BY FieloEE__Order__c asc) FROM FieloEE__Section__c WHERE FieloEE__Parent__c IN : sectionsList AND FieloEE__Parent__r.F_PRM_Asynchronous__c = false ORDER BY FieloEE__Order__c ASC')){
- //load subsections into the wrapper
- if(!menuWrapper.componentsSection.containsKey(subSection.FieloEE__Parent__c)){
- menuWrapper.componentsSection.put(subSection.FieloEE__Parent__c, new List<FieloEE__Section__c>{subSection});
- }else{
- menuWrapper.componentsSection.get(subSection.FieloEE__Parent__c).add(subSection);
- }
- //load components into the wrapper
- menuWrapper.componentsMap.putAll(subSection.FieloEE__Components__r);
- //loads in the wrapper set the component Ids that are set to be cached (content cache)
- for(FieloEE__Component__c c : subSection.FieloEE__Components__r){
- if(c.Cached__c){
- menuWrapper.componentCacheIds.add(c.Id);
- }
- }
- }
- //save the wrapper into the cache
- orgPart.put(keyString,menuWrapper);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement