Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/248__bde_batchEntry' int…
Browse files Browse the repository at this point in the history
…o feature/248
  • Loading branch information
lparrott committed Sep 22, 2023
2 parents 9c1ad10 + aa723a9 commit 676225f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 299 deletions.
201 changes: 10 additions & 191 deletions force-app/main/default/classes/BDE_BatchEntry_CTRL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -110,91 +110,22 @@ public with sharing class BDE_BatchEntry_CTRL {
* @return void
*/
public void initializeBatchEntry(){

bdec = UTIL_CustomSettingsFacade.getBDESettings();

//no defaults yet loaded, create one
if (bdec == null){
bdec = new Batch_Data_Entry_Settings__c();
UTIL_DMLService.insertRecord(bdec);
}

BDE_BatchDataEntry bde = new BDE_BatchDataEntry(objname);

if (!bde.getStatusMessage().contains(bde.getFailureMessage())){
BDE_BatchDataEntry bde = new BDE_BatchDataEntry(objname);
if (!bde.getStatusMessage().contains(bde.getFailureMessage())){
objname = bde.getObjectName();

if(objname!=null) {
batchLookupField = bde.getBatchLookupFieldname();
displayBatchSection = bde.getSaveBatch();
displayRecInfoSection = true;

String pluralName = UTIL_Describe.getObjectDescribe(objname).getLabelPlural();
pageSubtitle = pluralName;

entryFieldList = new list<EntryField>();

for (string fn : bde.getEntryFieldList())
entryFieldList.add(new EntryField(objname, fn));

listFieldList = bde.getListFieldList();
itemList = new list<BatchItem>();

if (displayBatchSection) {
// create the batch if it is new
if (batch.id == null) {
batch.name = pluralName + ' ' + system.today().format();
batch.object_name__c = objName;
// we will insert this in the load event
}
else {
string q = bde.getQueryString();
q += ' where ' + batchLookupField + ' = \'' + batch.id + '\'';

list<sobject> existingsobjs = database.query(q);

if (!existingsobjs.isEmpty()){

//if its opps and npsp, query for the ocrs
//so we can properly backfill the objects in the batch items
if (existingsobjs[0].getSobjectType() == Opportunity.Sobjecttype){
ocrlookup = new map<id, OpportunityContactRole>();
list<OpportunityContactRole> ocrlist = [select id, OpportunityID, ContactID from OpportunityContactRole where OpportunityID IN :existingsobjs and isPrimary = true];
for (OpportunityContactRole ocr : ocrlist){
ocrlookup.put(ocr.OpportunityID, ocr);
}
}
}

for (sObject existing : existingsobjs) {
itemList.add(new BatchItem(this, existing));
}
}
}

currentItem = new BatchItem(this);
}
}
else{
objectSettingsList = new List<SelectOption>();
map<string, string> mapBDE = BDE_BatchDataEntry.mapDevNameToLabelBDEObjects();
for(String strDevName : mapBDE.keySet()) {
objectSettingsList.add(new SelectOption(strDevName, mapBDE.get(strDevName)));
}

pageSubtitle = 'Batch Data Entry Home';
displaySettingsSection = true;
displayBatchSection = false;
displayRecInfoSection = false;
}
}
else{
}
}
else{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, bde.getStatusMessage()));
displaySettingsSection = false;
displayBatchSection = false;
displayRecInfoSection = false;
}
}
}

private void requeryItem(BatchItem item) {

BDE_BatchDataEntry bde = new BDE_BatchDataEntry(objname);
Expand Down Expand Up @@ -290,14 +221,7 @@ public with sharing class BDE_BatchEntry_CTRL {
myDad.currentItem = this;
return null;
}

public pageReference deleteItem() {
// delete this item from the list
myDad.deleteItem(sobj);
return null;

}


//automatic donation naming
public string createName(){
string namestring = '';
Expand Down Expand Up @@ -327,91 +251,7 @@ public with sharing class BDE_BatchEntry_CTRL {

/***** OTHER CONTROLLER METHODS ******/

/*******************************************************************************************************
* @description actionMethod to save the batch detail information
* @return null
*/
public pageReference saveBatch() {
Savepoint sp = Database.setSavepoint();
try {
TDTM_Runnable.DmlWrapper dmlWrapper = new TDTM_Runnable.DmlWrapper();
// if we are using batch, save the batch
if (displayBatchSection && itemList!=null) {
batch.number_of_items__c = itemList.size();

if (batch.id == null)
dmlWrapper.objectsToInsert.add(batch);
else
dmlWrapper.objectsToUpdate.add(batch);
}
TDTM_TriggerHandler.processDML(dmlWrapper);

} catch(Exception e) {
Database.rollback(sp);
ERR_Handler.processError(e, ERR_Handler_API.Context.BDE);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
return null;
}
/*******************************************************************************************************
* @description actionMethod to save the current batch item
* @return null
*/
public pageReference save() {
Savepoint sp = Database.setSavepoint();

try {
if (currentItem.sobj.id != null) {
if(currentItem.sobj.getSObjectType() == Opportunity.Sobjecttype){
currentItem.sobj.put('npe01__Contact_ID_for_Role__c', currentItem.npspocr.ContactID);
currentItem.sobj.put(UTIL_Namespace.StrTokenNSPrefix('Primary_Contact__c'), currentItem.npspocr.ContactID);
}

//update the name in case values have changed
if(currentItem.sobj.getSObjectType() == Opportunity.Sobjecttype && (bdec.Opportunity_Naming__c == true || (bdec.Allow_Blank_Opportunity_Names__c == false && currentItem.sobj.get('Name') == null))){
currentItem.sobj.put('Name', currentItem.createName());
}

UTIL_DMLService.updateRecord(currentItem.sobj);
} else {
if(currentItem.sobj.getSObjectType() == Opportunity.Sobjecttype){
currentItem.sobj.put('npe01__Contact_ID_for_Role__c', currentItem.npspocr.ContactID);
currentItem.sobj.put(UTIL_Namespace.StrTokenNSPrefix('Primary_Contact__c'), currentItem.npspocr.ContactID);
}

//use automatic opp naming
if(currentItem.sobj.getSObjectType() == Opportunity.Sobjecttype && (bdec.Opportunity_Naming__c == true || (bdec.Allow_Blank_Opportunity_Names__c == false && currentItem.sobj.get('Name') == null))){
currentItem.sobj.put('Name', currentItem.createName());
}

if (itemList.isEmpty()) {
itemList.add(currentItem);
saveBatch();
//we can't guarantee a lookup field when running tests
//so skip this line when in a test context
//if (!test.isRunningTest()) DJH: OK to assume Opportunity has batch__c
currentItem.sobj.put(batchLookupField, batch.Id);

UTIL_DMLService.insertRecord(currentItem.sobj);
}
else {
UTIL_DMLService.insertRecord(currentItem.sobj);
itemList.add(0, currentItem);
}
}
saveBatch();

// update the current item, in case any triggers modified values we will display.
requeryItem(currentItem);
currentItem = new BatchItem(this);
return null;
} catch(Exception e) {
Database.rollback(sp);
ERR_Handler.processError(e, ERR_Handler_API.Context.BDE);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
return null;
}
/*******************************************************************************************************
* @description actionMethod to clear the current batch item
* @return null
Expand All @@ -420,28 +260,7 @@ public with sharing class BDE_BatchEntry_CTRL {
currentItem = new BatchItem(this);
return null;
}
/*******************************************************************************************************
* @description actionMethod to delete sobject related to the batch item and remove the item from the list
* @param sobj the sObject to delete
* @return void
*/
public void deleteItem(sobject sobj) {
Savepoint sp = Database.setSavepoint();
try {
delete sobj;
for (integer i = 0; i < itemList.size(); i++) {
if (itemList[i].sobj.id == sobj.id) {
itemList.remove(i);
break;
}
}
saveBatch();
} catch(Exception e) {
Database.rollback(sp);
ERR_Handler.processError(e, ERR_Handler_API.Context.BDE);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
}

/*******************************************************************************************************
* @description actionMethod to open the batch entry page with selected object
* @return pageReference (Batch Entry Page)
Expand Down
110 changes: 20 additions & 90 deletions force-app/main/default/classes/BDE_BatchEntry_TEST.cls
Original file line number Diff line number Diff line change
Expand Up @@ -41,103 +41,52 @@ private class BDE_BatchEntry_TEST {
* batch and batch items were saved
*/
static testMethod void testBatchDataEntryController() {

Test.setCurrentPageReference(new PageReference('Page.BDE_BatchEntry'));
system.currentPageReference().getParameters().put('obj', 'Opportunity');
system.currentPageReference().getParameters().put('obj', 'Opportunity');
BDE_BatchEntry_CTRL ctrl = new BDE_BatchEntry_CTRL(new ApexPages.standardController(new Batch__c() ));
ctrl.initializeBatchEntry();
system.assertEquals('Opportunity', ctrl.objname);
system.assertNotEquals(null, ctrl.batch);
system.assertNotEquals(null, ctrl.batchLookupField);
system.assertNotEquals(null, ctrl.pageSubtitle);
system.assertNotEquals(0, ctrl.entryFieldList.size());
system.assertNotEquals(0, ctrl.listFieldList.size());
system.assertNotEquals(null, ctrl.bdec);
system.assertNotEquals(null, ctrl.currentItem);
ctrl.currentItem.sobj.put('Name', 'my test opp');
ctrl.currentItem.sobj.put('Amount',100);
ctrl.currentItem.sobj.put('StageName','Closed Won');
ctrl.currentItem.sobj.put('CloseDate', system.Today());
ctrl.saveBatch();
list<Batch__c> listBatch = [select Id, Name, Number_of_Items__c from Batch__c where Id = :ctrl.batch.Id];
system.assertEquals(1, listBatch.size());
ctrl.save();
list<Opportunity> listOpp = [select Id, Name from Opportunity where Batch__c = :ctrl.batch.Id];
system.assertEquals(1, listOpp.size());
}

}
/*******************************************************************************************************
* @description load the batch data entry page for a new contact batch, save and reload the page with
* the saved item
*/
static testMethod void testBatchWithSave() {

ApexPages.StandardController sc = new ApexPages.standardController( new Batch__c() );
Test.setCurrentPage(Page.BDE_BatchEntry);
Test.setCurrentPage(Page.BDE_BatchEntry);
ApexPages.currentPage().getParameters().put('obj', 'contact');
BDE_BatchEntry_CTRL ext = new BDE_BatchEntry_CTRL(sc);
ext.displayBatchSection = true;
ext.initializeBatchEntry();
system.assertNotEquals(null, ext.currentItem);

// set some fields
ext.currentItem.sobj.put('LastName', 'Test');
ext.save();
ext.displayBatchSection = true;

// do it again with the saved record
sc = new ApexPages.standardController( ext.batch );
Test.setCurrentPage(Page.BDE_BatchEntry);
ApexPages.currentPage().getParameters().put('obj', 'contact');
ext = new BDE_BatchEntry_CTRL(sc);
}
}
/*******************************************************************************************************
* @description load the batch data entry page for a new object
*/
static testMethod void testNewObjectLoad(){
ApexPages.StandardController sc = new ApexPages.standardController( new Batch__c() );
Test.setCurrentPage(Page.BDE_BatchEntry);
BDE_BatchEntry_CTRL ext = new BDE_BatchEntry_CTRL(sc);
Test.setCurrentPage(Page.BDE_BatchEntry);
BDE_BatchEntry_CTRL ext = new BDE_BatchEntry_CTRL(sc);

//exercise entry display
ext.displayBatchSection = true;
ext.initializeBatchEntry();
}
/*******************************************************************************************************
* @description load the batch data entry page with existing batches
*/
static testMethod void testExistingBatchObjects(){

Batch__c b1 = new Batch__c(Name='New Batch', Object_Name__c = 'opportunity', Batch_Status__c = 'In Progress');
Batch__c b2 = new Batch__c(Name='New Batch', Object_Name__c = 'opportunity', Batch_Status__c = 'Complete');
insert b1;
insert b2;

ApexPages.StandardController sc = new ApexPages.standardController(b1);
Test.setCurrentPage(Page.BDE_BatchEntry);
BDE_BatchEntry_CTRL ext = new BDE_BatchEntry_CTRL(sc);
Test.setCurrentPage(Page.BDE_BatchEntry);
BDE_BatchEntry_CTRL ext = new BDE_BatchEntry_CTRL(sc);
ext.batch = new Batch__c();
ext.displayBatchSection = true;
ext.initializeBatchEntry();

//test deletion
Account a = new Account(Name='TestAcct');
insert a;
ext.itemList = new list<BDE_BatchEntry_CTRL.BatchItem>();
ext.itemList.add(new BDE_BatchEntry_CTRL.BatchItem(ext, a));
ext.deleteItem((sobject)a);
ext.openBatchEnterPage();


//test opp naming
Opportunity o = new Opportunity(CloseDate = system.today(), StageName = 'Closed Won');
BDE_BatchEntry_CTRL.BatchItem b = new BDE_BatchEntry_CTRL.BatchItem(ext, o);
ext.itemList.add(b);
ext.currentItem = b;
o.Name = b.createName();
ext.save();
ext.currentItem.sobj = (sobject)o;
//exercise item methods
ext.currentItem.editItem();
ext.currentItem.deleteItem();

}
/*******************************************************************************************************
* @description load the batch data entry page for an opportunity with an opportunity contact role
Expand All @@ -151,35 +100,16 @@ private class BDE_BatchEntry_TEST {
insert c;
OpportunityContactRole ocr = new OpportunityContactRole(OpportunityID = oppinbatch.id, ContactID = c.id);
insert ocr;


ApexPages.StandardController sc = new ApexPages.standardController(b);
Test.setCurrentPage(Page.BDE_BatchEntry);
Test.setCurrentPage(Page.BDE_BatchEntry);
BDE_BatchEntry_CTRL ext = new BDE_BatchEntry_CTRL(sc);
ext.objname = 'opportunity';
ext.displayBatchSection = true;
ext.displayBatchSection = true;
ext.ocrlookup = new Map<id, OpportunityContactRole>();
ext.ocrlookup.put(ocr.id, ocr);

ext.initializeBatchEntry();

Opportunity o = new Opportunity(Name = 'Test', CloseDate = system.today(), StageName = 'Closed Won');
insert o;
BDE_BatchEntry_CTRL.BatchItem b1 = new BDE_BatchEntry_CTRL.BatchItem(ext, (sobject)o);


//initialize new batch entry, set object name, give it anew batch
sc = new ApexPages.standardController(new Batch__c());
BDE_BatchEntry_CTRL ext2 = new BDE_BatchEntry_CTRL(sc);
ext2.objname = 'opportunity';
ext2.displayBatchSection = true;
ext.ocrlookup = new Map<id, OpportunityContactRole>();
ext.ocrlookup.put(ocr.id, ocr);
ext2.initializeBatchEntry();
BDE_BatchEntry_CTRL.BatchItem b2 = new BDE_BatchEntry_CTRL.BatchItem(ext2, (sobject)o);
ext2.currentItem = b2;
ext2.save();
ext2.clear();
}

@IsTest
Expand Down
Loading

0 comments on commit 676225f

Please sign in to comment.