Skip to content

Commit

Permalink
Add feature #44 Grant selector to editor, refactor selector methods t…
Browse files Browse the repository at this point in the history
…o separate classes
  • Loading branch information
iandrosov committed Dec 26, 2023
1 parent 4aa7f52 commit 888276b
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 238 deletions.
146 changes: 36 additions & 110 deletions force-app/main/default/classes/GGW_ApplicationCtrl.cls
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,11 @@ public without sharing class GGW_ApplicationCtrl {
@AuraEnabled
public static GGW_GrantApplicationWrapper getApplication(String recordId){
String tempId = validateAppId(recordId);

List<GGW_Selected_Item__c> appItems = querySelectedItemsByGrant(tempId);
List<GGW_Selected_Item__c> appItems = GGW_SectionSelector.querySelectedItemsByGrant(tempId);
GGW_Grant_Application__c grant = GGW_ApplicationSelector.queryGrantApp(tempId);
// Save current app state
GGW_Util.saveGrantState(tempId);

GGW_GrantApplicationWrapper app = new GGW_GrantApplicationWrapper();
if(!appItems.isEmpty()){
app.recordid = grant.Id;
app.logostate = grant.Include_Logo__c;
app.logodisplayurl = getValidLogoURL(grant);
app.name = appItems[0].Application_Name__c;
app.status = appItems[0].Grant_Application__r.Status__c;
app.unselectSectionList = getLeftoverSections(appItems);
app.selectedContentBlock = createContentBlockWrapperList(appItems);
}
GGW_GrantApplicationWrapper app = createGrantWrapper(grant, appItems);
return app;
}
@AuraEnabled (cacheable=true)
Expand Down Expand Up @@ -74,15 +63,15 @@ public without sharing class GGW_ApplicationCtrl {
*/
@AuraEnabled
public static String createContentDistribution(String grantId, String cvid){
ContentVersion file = getContentFile(cvid);
ContentVersion file = GGW_ContentSelector.getContentFile(cvid);
// Creating ContentDistribution record
ContentDistribution cdr = insertContentDistribution(file);
if(cdr.Id == null){
return null; // could not insert ContentDistribution no CRUD
}
// After Distribution record need to get DistributionPublicUrl, ContentDownloadUrl
// and update Grant record to use these for display
ContentDistribution cdURL = getContentDistributionById(cdr.Id);
ContentDistribution cdURL = GGW_ContentSelector.getContentDistributionById(cdr.Id);
System.debug('## ContentDownloadUrl:'+cdURL.ContentDownloadUrl);
System.debug('## DistributionPublicUrl: '+cdURL.DistributionPublicUrl);
// Update Grant with new logo
Expand All @@ -96,7 +85,7 @@ public without sharing class GGW_ApplicationCtrl {
*/
@AuraEnabled
public static void saveSelectedSectionText(String itemid, String blockid){
GGW_Content_Block__c cBlock = queryContentBlockById(blockid);
GGW_Content_Block__c cBlock = GGW_ContentBlockSelector.queryContentBlockById(blockid);
// Construct selected Item to update
GGW_Selected_Item__c item = new GGW_Selected_Item__c();
item.Id = itemid;
Expand All @@ -114,7 +103,7 @@ public without sharing class GGW_ApplicationCtrl {
*/
@AuraEnabled
public static GGW_SectionWrapper createNewSection(String name){
GGW_Section__c maxOrder = findMaxOrderSection();
GGW_Section__c maxOrder = GGW_SectionSelector.findMaxOrderSection();
GGW_Section__c s = new GGW_Section__c();
s.Name = name;
s.Recommended__c = true;
Expand Down Expand Up @@ -178,7 +167,7 @@ public without sharing class GGW_ApplicationCtrl {
@AuraEnabled(cacheable=true)
public static List<GGW_ContentBlockWrapper> getContentBlocks(String sectionId){
List<GGW_ContentBlockWrapper> cbwResultList = new List<GGW_ContentBlockWrapper>();
List<GGW_Content_Block__c> cbList = queryContentBlocksBySection(sectionId);
List<GGW_Content_Block__c> cbList = GGW_ContentBlockSelector.queryContentBlocksBySection(sectionId);
// Get map of block tags to display as badges
Map<String,List<String>> blockTags = getBlockTopics();
for(GGW_Content_Block__c c : cbList){
Expand Down Expand Up @@ -232,7 +221,7 @@ public without sharing class GGW_ApplicationCtrl {
List<GGW_SectionWrapper> swList = new List<GGW_SectionWrapper>();
String lang = GGW_Util.getGrantLanguage();
Set<String> setOfSectionsWithBlock = getBlocksForSection();
List<GGW_Section__c> sectionList = querySectionsByLanguage(lang);
List<GGW_Section__c> sectionList = GGW_SectionSelector.querySectionsByLanguage(lang);
for(GGW_Section__c s : sectionList){
GGW_SectionWrapper sw = new GGW_SectionWrapper(s,setOfSectionsWithBlock);
swList.add(sw);
Expand All @@ -244,7 +233,7 @@ public without sharing class GGW_ApplicationCtrl {
public static List<GGW_SectionWrapper> findSections(String searchKey) {
List<GGW_SectionWrapper> swList = new List<GGW_SectionWrapper>();
if(searchKey != null && searchKey.length() > 0){
List<GGW_Section__c> sectionList = querySectionsByName(searchKey);
List<GGW_Section__c> sectionList = GGW_SectionSelector.querySectionsByName(searchKey);

for(GGW_Section__c s : sectionList){
GGW_SectionWrapper sw = new GGW_SectionWrapper(s);
Expand Down Expand Up @@ -280,19 +269,39 @@ public without sharing class GGW_ApplicationCtrl {
return GGW_Util.getSelectOptionFromPicklist(ggGtSteObj, ggGtStLanField, false);
}


// --------------------------------
// -- END LWC Controller Methods --
// --------------------------------
private static GGW_GrantApplicationWrapper createGrantWrapper(GGW_Grant_Application__c grant, List<GGW_Selected_Item__c> appItems){
GGW_GrantApplicationWrapper app = new GGW_GrantApplicationWrapper();
app.recordid = grant.Id;
app.logostate = grant.Include_Logo__c;
app.logodisplayurl = getValidLogoURL(grant);
app.status = grant.Status__c;
app.textname = grant.Name;
app.name = getValidApplicationNameLookupURL(appItems); // Link to standard record
app.unselectSectionList = getLeftoverSections(appItems);
app.selectedContentBlock = createContentBlockWrapperList(appItems);
return app;
}
private static String getValidApplicationNameLookupURL(List<GGW_Selected_Item__c> appItems){
if(!appItems.isEmpty()){
return appItems[0].Application_Name__c;
}
return null;
}
private static void deleteImageLogo(String recordId){
/*
ContentDocumentLink cdl = [SELECT Id, LinkedEntityId, ContentDocumentId, IsDeleted, Visibility, ShareType
FROM ContentDocumentLink
WHERE LinkedEntityId =: recordId WITH SECURITY_ENFORCED];
WHERE LinkedEntityId =: recordId WITH SECURITY_ENFORCED LIMIT 1];
ContentDistribution cntdistr = [SELECT Id, Name, ContentVersionId, ContentDocumentId, RelatedRecordId, ContentDownloadUrl
FROM ContentDistribution
WHERE ContentDocumentId =: cdl.ContentDocumentId WITH SECURITY_ENFORCED];

WHERE ContentDocumentId =: cdl.ContentDocumentId WITH SECURITY_ENFORCED LIMIT 1];
*/
ContentDocumentLink cdl = GGW_ContentSelector.getDocLinkByLinkedEntity(recordId);
ContentDistribution cntdistr = GGW_ContentSelector.getContentDistributionByDocId(cdl.ContentDocumentId);
ContentDocument cd = new ContentDocument();
cd.Id = cdl.ContentDocumentId;
if(Schema.sObjectType.ContentDistribution.isDeletable()){
Expand Down Expand Up @@ -357,20 +366,6 @@ public without sharing class GGW_ApplicationCtrl {
}
}
}
private static ContentDistribution getContentDistributionById(Id cdid){
ContentDistribution cdr = [SELECT Id, Name, ContentVersionId,
ContentDownloadUrl, DistributionPublicUrl
FROM ContentDistribution
WHERE Id =: cdid WITH SECURITY_ENFORCED LIMIT 1];
return cdr;
}
// Query ContentVersion by id
private static ContentVersion getContentFile(String cvid){
ContentVersion file = [SELECT Id, Title
FROM ContentVersion
WHERE Id =: cvid WITH SECURITY_ENFORCED];
return file;
}
private static ContentDistribution insertContentDistribution(ContentVersion file){
ContentDistribution cdr = new ContentDistribution(
Name = file.Title,
Expand All @@ -382,21 +377,6 @@ public without sharing class GGW_ApplicationCtrl {
}
return cdr;
}
// Return section with max order to add next section
private static GGW_Section__c findMaxOrderSection(){
List<GGW_Section__c> maxOrderList = [SELECT Sort_Order__c
FROM GGW_Section__c
WHERE Suggested__c = true WITH SECURITY_ENFORCED ORDER BY Sort_Order__c desc];
GGW_Section__c maxOrder = null;
// Handle situation when Sort order is null - TODO MAY be good to require it
for(GGW_Section__c section : maxOrderList ){
if(section.Sort_Order__c != null && section.Sort_Order__c > 0){
maxOrder = section; // assigned MAX order object first found END LOOP here
break; // EXIT Loop
}
}
return maxOrder;
}
// Fix for issue #67 WHne pkg installed omn fresh org NO sample section data defult sort to 1
// Use decimal insted on Integer a s fieds number ais decimal
private static Decimal getSectionSortOrder(GGW_Section__c maxOrder){
Expand All @@ -419,7 +399,7 @@ public without sharing class GGW_ApplicationCtrl {
}
// Delete Grant selected section items that are not on the list to reorder function
private static void cleanSelectedSections(List<String> sectionList, String appId){
List<GGW_Selected_Item__c> appItems = querySelectedItemsByGrant(appId);
List<GGW_Selected_Item__c> appItems = GGW_SectionSelector.querySelectedItemsByGrant(appId);
Set<String> sectionSet = new Set<String>(sectionList);
List<GGW_Selected_Item__c> removeItems = new List<GGW_Selected_Item__c>();
for(GGW_Selected_Item__c item : appItems){
Expand Down Expand Up @@ -447,7 +427,7 @@ public without sharing class GGW_ApplicationCtrl {
List<GGW_SectionWrapper> swList = new List<GGW_SectionWrapper>();
Set<String> setExcludeSections = getExcludeSectionSet(appItems);
Set<String> setOfSectionsWithBlock = getBlocksForSection();
List<GGW_Section__c> sectionList = querySections();
List<GGW_Section__c> sectionList = GGW_SectionSelector.querySections();

for(GGW_Section__c s : sectionList){
system.debug('## Left Section: '+s.Name+' ID: '+s.Id);
Expand All @@ -468,7 +448,7 @@ public without sharing class GGW_ApplicationCtrl {
}
private static Set<String> getBlocksForSection(){
Set<String> s1 = new Set<String>();
List<GGW_Content_Block__c> cbList = queryContentBlocks();
List<GGW_Content_Block__c> cbList = GGW_ContentBlockSelector.queryContentBlocks();
for(GGW_Content_Block__c c : cbList){
s1.add(c.Id);
}
Expand Down Expand Up @@ -529,38 +509,6 @@ public without sharing class GGW_ApplicationCtrl {
}

///--- BASIC SELECTOR METHODS
public static List<GGW_Selected_Item__c> querySelectedItemsByGrant(String appId){
List<GGW_Selected_Item__c> appItems = [SELECT Id, Application_Name__c, Grant_Application__c, GGW_Section__c,
Section_Name__c,Selected_Block__c, Sort_Order__c, Grant_Application__r.Status__c,
Selected_Block__r.Description__c, Text_Block__c, Language__c
FROM GGW_Selected_Item__c
WHERE Grant_Application__c =: appId WITH SECURITY_ENFORCED ORDER BY Sort_Order__c];
return appItems;
}

private static List<GGW_Section__c> querySectionsByLanguage(String lang){
List<GGW_Section__c> sectionList = [SELECT Id, Name, CreatedDate, Recommended__c, Suggested__c,
Sort_Order__c, Language__c
FROM GGW_Section__c
WHERE Suggested__c = true AND Language__c =: lang
WITH SECURITY_ENFORCED ORDER BY Sort_Order__c];
return sectionList;
}
private static List<GGW_Section__c> querySections(){
List<GGW_Section__c> sectionList = [SELECT Id, Name, CreatedDate, Recommended__c, Suggested__c,
Sort_Order__c, Language__c
FROM GGW_Section__c WITH SECURITY_ENFORCED ORDER BY Sort_Order__c];
return sectionList;
}
private static List<GGW_Section__c> querySectionsByName(String searchKey){
String key = '%' + searchKey + '%';
List<GGW_Section__c> sectionList = [SELECT Id, Name, CreatedDate, Recommended__c, Suggested__c,
Sort_Order__c, Language__c
FROM GGW_Section__c
WHERE Name LIKE :key AND Suggested__c = false
WITH SECURITY_ENFORCED ORDER BY Sort_Order__c LIMIT 20];
return sectionList;
}

// Return list of TopicAssignments for ContentBlocks
private static List<TopicAssignment> queryTopicAssignments(){
Expand All @@ -570,27 +518,5 @@ public without sharing class GGW_ApplicationCtrl {
return lst;
}

// BLOCK Query
private static GGW_Content_Block__c queryContentBlockById(String blockid){
GGW_Content_Block__c cBlock = [SELECT Id, Name, Description__c
FROM GGW_Content_Block__c
WHERE Id =: blockid WITH SECURITY_ENFORCED LIMIT 1];
return cBlock;
}
private static List<GGW_Content_Block__c> queryContentBlocksBySection(String sectionId){
List<GGW_Content_Block__c> cbList = [SELECT Id, Name, Description__c, Short_Description__c,
Section__c, Section__r.Name, CreatedDate,
Language__c
FROM GGW_Content_Block__c
WHERE Section__c =: sectionId WITH SECURITY_ENFORCED];
return cbList;
}
private static List<GGW_Content_Block__c> queryContentBlocks(){
List<GGW_Content_Block__c> cbList = [SELECT Id, Name, Description__c, Short_Description__c,
Section__c, Section__r.Name, CreatedDate,
Language__c
FROM GGW_Content_Block__c WITH SECURITY_ENFORCED ORDER BY Section__c];
return cbList;
}

}
8 changes: 4 additions & 4 deletions force-app/main/default/classes/GGW_ApplicationCtrlTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public class GGW_ApplicationCtrlTest {
}
}
GGW_Grant_Application__c app = GGW_ApplicationCtrl.newGrant('MyTest Grant Sections', sections);
List<GGW_Selected_Item__c> selItemList = GGW_ApplicationCtrl.querySelectedItemsByGrant(app.Id);
List<GGW_Selected_Item__c> selItemList = GGW_SectionSelector.querySelectedItemsByGrant(app.Id);
//List<GGW_Selected_Item__c> selItemList = [SELECT Id, Name, GGW_Section__c,Grant_Application__c, Sort_Order__c
// FROM GGW_Selected_Item__c WHERE Grant_Application__c =:app.Id];
String itemid = selItemList[0].Id;
Expand All @@ -264,7 +264,7 @@ public class GGW_ApplicationCtrlTest {
GGW_ApplicationCtrl.saveSelectedSectionText(itemid, bwList[0].recordid);
Test.stopTest();
// assertion checks that item saved the Block text
List<GGW_Selected_Item__c> savedItemList = GGW_ApplicationCtrl.querySelectedItemsByGrant(app.Id);
List<GGW_Selected_Item__c> savedItemList = GGW_SectionSelector.querySelectedItemsByGrant(app.Id);
System.assertEquals(blocktext, savedItemList[0].Text_Block__c, 'Block text did not match template');
}
@isTest
Expand All @@ -278,7 +278,7 @@ public class GGW_ApplicationCtrlTest {
}
}
GGW_Grant_Application__c app = GGW_ApplicationCtrl.newGrant('Grant App Order', sections);
List<GGW_Selected_Item__c> selItemList = GGW_ApplicationCtrl.querySelectedItemsByGrant(app.Id);
List<GGW_Selected_Item__c> selItemList = GGW_SectionSelector.querySelectedItemsByGrant(app.Id);
// List of selected Iteam IDs - GGW_Selected_Item__c
List<String> itemList = new List<String>();
// FLip order first/last
Expand All @@ -293,7 +293,7 @@ public class GGW_ApplicationCtrlTest {
GGW_ApplicationCtrl.reorderSections(itemList, app.Id);
Test.stopTest();
// Check fliped order
List<GGW_Selected_Item__c> selOrderedList = GGW_ApplicationCtrl.querySelectedItemsByGrant(app.Id);
List<GGW_Selected_Item__c> selOrderedList = GGW_SectionSelector.querySelectedItemsByGrant(app.Id);
System.assertEquals(last, selOrderedList[0].Id, 'Last seleected section item did not match expected');
System.assertEquals(first, selOrderedList[selOrderedList.size()-1].Id, 'First seleectd section item did not match selected');
}
Expand Down
32 changes: 32 additions & 0 deletions force-app/main/default/classes/GGW_ContentBlockSelector.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*
* GGW_ContentBlockSelector Salesforce Content Block text data selector class support GGW.
*
*/
public with sharing class GGW_ContentBlockSelector {
public static GGW_Content_Block__c queryContentBlockById(String blockid){
GGW_Content_Block__c cBlock = [SELECT Id, Name, Description__c
FROM GGW_Content_Block__c
WHERE Id =: blockid WITH SECURITY_ENFORCED LIMIT 1];
return cBlock;
}
public static List<GGW_Content_Block__c> queryContentBlocksBySection(String sectionId){
List<GGW_Content_Block__c> cbList = [SELECT Id, Name, Description__c, Short_Description__c,
Section__c, Section__r.Name, CreatedDate,
Language__c
FROM GGW_Content_Block__c
WHERE Section__c =: sectionId WITH SECURITY_ENFORCED];
return cbList;
}
public static List<GGW_Content_Block__c> queryContentBlocks(){
List<GGW_Content_Block__c> cbList = [SELECT Id, Name, Description__c, Short_Description__c,
Section__c, Section__r.Name, CreatedDate,
Language__c
FROM GGW_Content_Block__c WITH SECURITY_ENFORCED ORDER BY Section__c];
return cbList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading

0 comments on commit 888276b

Please sign in to comment.