You need to sign in to do that
Don't have an account?
praveenreddy gaddam 10
how write a test class for this code? please help me
public class ContentDocumentLinkTriggerHandler {
public static void deleteFileValidation(List<ContentDocument> oldContentDocument){
String Licenseprefix = '';
for(schema.SObjectType s : Schema.getGlobalDescribe().Values()){
if(s.getDescribe().getKeyPrefix() != null && s.getDescribe().getName() == 'License__c'){
Licenseprefix = String.valueof(s.getDescribe().getKeyPrefix());
}
}
Map<Id, Id> contentDocumentIdToOwnerIdMap = new Map<Id, Id>();
for(ContentDocument cd :oldContentDocument){
contentDocumentIdToOwnerIdMap.put(cd.Id, cd.OwnerId);
// System.debug('::: cd.OwnerId :::'+cd.OwnerId);
// System.debug('::: cd.Id :::'+cd.Id);
}
// System.debug('::: contentDocumentIdToOwnerIdMap :::'+contentDocumentIdToOwnerIdMap);
Map<Id, Id> DocumentIdToLicenseIdMap = new Map<Id, Id>();
for(ContentDocumentLink cdl : [SELECT LinkedEntityId,ContentDocumentId
FROM ContentDocumentLink
WHERE ContentDocumentId IN:contentDocumentIdToOwnerIdMap.keySet()])
{
if(cdl.LinkedEntityId!=null && Licenseprefix!= null && String.valueof(cdl.LinkedEntityId).startsWith(Licenseprefix))
DocumentIdToLicenseIdMap.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
}
// system.debug('::: DocumentIdToLicenseIdMap :::'+DocumentIdToLicenseIdMap);
Map<Id, Id> LicenseIdToOwnerId = new Map<Id, Id>();
for(License__c license : [SELECT Id, OwnerId FROM License__c WHERE Id IN:DocumentIdToLicenseIdMap.values()]){
LicenseIdToOwnerId.put(license.Id, license.OwnerId);
}
// System.debug('::: LicenseIdToOwnerId :::'+LicenseIdToOwnerId);
for(ContentDocument cd :oldContentDocument){
Id docParentId;
if(DocumentIdToLicenseIdMap.get(cd.Id)!=null)
docParentId = DocumentIdToLicenseIdMap.get(cd.Id);
Id OwnerId;
if(docParentId!=null && LicenseIdToOwnerId.get(docParentId)!= null)
OwnerId = LicenseIdToOwnerId.get(docParentId);
if(contentDocumentIdToOwnerIdMap.get(cd.Id)!=null && OwnerId!=null &&
contentDocumentIdToOwnerIdMap.get(cd.Id) != OwnerId
){
cd.addError('You Canot delete this file because license owner have permission to delete');
}
}
}
public static void fileUploadcheckboxUpdate(List<ContentDocumentLink> newContentDocumentLinkList){
String Licenseprefix = '';
for(schema.SObjectType s : Schema.getGlobalDescribe().Values()){
if(s.getDescribe().getKeyPrefix() != null && s.getDescribe().getName() == 'License__c'){
Licenseprefix = String.valueof(s.getDescribe().getKeyPrefix());
}
}
Set<id> parentIds = new set<id>();
for ( ContentDocumentLink cdl : newContentDocumentLinkList ) {
if(cdl.LinkedEntityId!=null && Licenseprefix!= null && String.valueof(cdl.LinkedEntityId).startsWith(Licenseprefix))
parentIds.add( cdl.LinkedEntityId );
}
List<License__c> LicenseUpdateFlagList = new List<License__c>();
if(parentIds.size()>0){
for(License__c licence : [SELECT Id, Is_File_Uploaded__c FROM License__c WHERE Id in:parentIds]){
licence.Is_File_Uploaded__c = true;
LicenseUpdateFlagList.add(licence);
}
}
if ( LicenseUpdateFlagList.size()>0 ){
update LicenseUpdateFlagList;
}
}
public static void deleteFileValidation(List<ContentDocument> oldContentDocument){
String Licenseprefix = '';
for(schema.SObjectType s : Schema.getGlobalDescribe().Values()){
if(s.getDescribe().getKeyPrefix() != null && s.getDescribe().getName() == 'License__c'){
Licenseprefix = String.valueof(s.getDescribe().getKeyPrefix());
}
}
Map<Id, Id> contentDocumentIdToOwnerIdMap = new Map<Id, Id>();
for(ContentDocument cd :oldContentDocument){
contentDocumentIdToOwnerIdMap.put(cd.Id, cd.OwnerId);
// System.debug('::: cd.OwnerId :::'+cd.OwnerId);
// System.debug('::: cd.Id :::'+cd.Id);
}
// System.debug('::: contentDocumentIdToOwnerIdMap :::'+contentDocumentIdToOwnerIdMap);
Map<Id, Id> DocumentIdToLicenseIdMap = new Map<Id, Id>();
for(ContentDocumentLink cdl : [SELECT LinkedEntityId,ContentDocumentId
FROM ContentDocumentLink
WHERE ContentDocumentId IN:contentDocumentIdToOwnerIdMap.keySet()])
{
if(cdl.LinkedEntityId!=null && Licenseprefix!= null && String.valueof(cdl.LinkedEntityId).startsWith(Licenseprefix))
DocumentIdToLicenseIdMap.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
}
// system.debug('::: DocumentIdToLicenseIdMap :::'+DocumentIdToLicenseIdMap);
Map<Id, Id> LicenseIdToOwnerId = new Map<Id, Id>();
for(License__c license : [SELECT Id, OwnerId FROM License__c WHERE Id IN:DocumentIdToLicenseIdMap.values()]){
LicenseIdToOwnerId.put(license.Id, license.OwnerId);
}
// System.debug('::: LicenseIdToOwnerId :::'+LicenseIdToOwnerId);
for(ContentDocument cd :oldContentDocument){
Id docParentId;
if(DocumentIdToLicenseIdMap.get(cd.Id)!=null)
docParentId = DocumentIdToLicenseIdMap.get(cd.Id);
Id OwnerId;
if(docParentId!=null && LicenseIdToOwnerId.get(docParentId)!= null)
OwnerId = LicenseIdToOwnerId.get(docParentId);
if(contentDocumentIdToOwnerIdMap.get(cd.Id)!=null && OwnerId!=null &&
contentDocumentIdToOwnerIdMap.get(cd.Id) != OwnerId
){
cd.addError('You Canot delete this file because license owner have permission to delete');
}
}
}
public static void fileUploadcheckboxUpdate(List<ContentDocumentLink> newContentDocumentLinkList){
String Licenseprefix = '';
for(schema.SObjectType s : Schema.getGlobalDescribe().Values()){
if(s.getDescribe().getKeyPrefix() != null && s.getDescribe().getName() == 'License__c'){
Licenseprefix = String.valueof(s.getDescribe().getKeyPrefix());
}
}
Set<id> parentIds = new set<id>();
for ( ContentDocumentLink cdl : newContentDocumentLinkList ) {
if(cdl.LinkedEntityId!=null && Licenseprefix!= null && String.valueof(cdl.LinkedEntityId).startsWith(Licenseprefix))
parentIds.add( cdl.LinkedEntityId );
}
List<License__c> LicenseUpdateFlagList = new List<License__c>();
if(parentIds.size()>0){
for(License__c licence : [SELECT Id, Is_File_Uploaded__c FROM License__c WHERE Id in:parentIds]){
licence.Is_File_Uploaded__c = true;
LicenseUpdateFlagList.add(licence);
}
}
if ( LicenseUpdateFlagList.size()>0 ){
update LicenseUpdateFlagList;
}
}