• Rahul Mahendrakar
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
public class StageGateTaskController 
{   
    @AuraEnabled
    public static List<StockRequestWrap> getStageGate(String recordId, string stage)
    {
        System.debug(recordId+stage);
        if(stage=='Ideation')
        {
            stage='Ideation Stage';    
        }
        //User u = [Select Department From User Where id = :UserInfo.getUserId()];
        //String dept = u.Department;
        
        List<Stage_Gate_Task__c> task1=[select Id,User__c,Due_Date__c,Subject__c,Comment__c,Department__c,Task_status__c,CreatedDate,LastModifiedDate,Completed_Date__c from Stage_Gate_Task__c where Stage_Gate__c=:recordID and Stage__c=: stage ];
        system.debug('task'+task1);
        //system.debug('Completed_Date__c'+task1[0].Completed_Date__c);
        List<StockRequestWrap> stockReqwrap1 = new List<StockRequestWrap>();
        for(Stage_Gate_Task__c task: task1)
        {
            
            //System.debug('created Date'+task.CreatedDate.format('yyyy-MM-dd'));
            StockRequestWrap stockReqwrap = new StockRequestWrap();
            stockReqwrap.sobjectType = 'Stage_Gate_Task__c';
            stockReqwrap.Id = task.Id;
            stockReqwrap.AssignTo = task.User__c;
            stockReqwrap.DueDate = task.Due_Date__c;
            stockReqwrap.AssignDate = date.newinstance(task.CreatedDate.year(), task.CreatedDate.month(), task.CreatedDate.day());
            stockReqwrap.completionDate = task.Completed_Date__c;
            stockReqwrap.Subject = task.Subject__c;
            stockReqwrap.Description = task.Comment__c;
            stockReqwrap.Dept = task.Department__c;
            stockReqwrap.Status = task.Task_status__c;
            
            stockReqwrap1.add(stockReqwrap);
        }
        system.debug('stockReqwrap1'+stockReqwrap1); 
        
        return stockReqwrap1; 
    }
    public class StockRequestWrap{
        @AuraEnabled
        public id Id{get;set;}
        @AuraEnabled
        public String sobjectType{get;set;}
        @AuraEnabled
        public String AssignTo{get;set;}
        @AuraEnabled
        public Date DueDate{get;set;}
         @AuraEnabled
        public Date AssignDate{get;set;}
         @AuraEnabled
        public Date completionDate{get;set;}
        @AuraEnabled
        public String Subject{get;set;}
        @AuraEnabled
        public String Description{get;set;}
        @AuraEnabled
        public String Dept{get;set;}
        @AuraEnabled
        public String Status{get;set;}
    }
    @AuraEnabled
    public static List<ContentDocument> getFiles(String recordId, string Title,string Title1){ 
        system.debug('recordId'+recordId);
        ID filter = recordId;
        List<ContentDocumentLink> cdlList = [SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=:filter];                         
        Set<Id> documentIds = new Set<Id>(); 
        
        for(ContentDocumentLink cdl:cdlList){  
            documentIds.add(cdl.ContentDocumentId); 
            
        }  
        if(Title!=null){
            ContentDocument docList= [SELECT Id, Title, FileType FROM ContentDocument WHERE Id IN :documentIds order by createddate DESC limit 1];
            if(docList!=null){
                docList.Title=Title;
                update docList;
            }
            
        }
        if(Title1!=null){
            ContentDocument docList= [SELECT Id, Title, FileType FROM ContentDocument WHERE Id IN :documentIds order by createddate DESC limit 1];
            if(docList!=null){
                docList.Title=Title1;
                update docList;
            }
        }
        //public link creation
        //Delete Pre Public link
          list<ContentDistribution> prev =[select DistributionPublicUrl,Name from ContentDistribution where ContentDocumentId = :documentIds];
        delete prev;
        
        list<ContentDocumentLink> cdl = [select contentdocument.id, contentdocument.title, contentdocument.filetype from contentdocumentlink where linkedentityid = :filter];
        list<ContentVersion> cv = [select id,contentdocumentid from contentversion where contentdocumentid =: documentIds];
      
        
        system.debug('cdl'+cdl.size()+'cv'+cv.size());
        system.debug('documentIds'+documentIds);
        list<ContentDistribution> cdList = new list<ContentDistribution>();
        integer i=0;
        for(ContentDocumentLink conDL:cdl)
        {      
             
                    ContentDistribution cd = new ContentDistribution();
                    cd.Name = conDL.contentdocument.title;
                    cd.ContentVersionId = cv[i++].id;
                    cd.PreferencesAllowViewInBrowser= true;
                    cd.PreferencesLinkLatestVersion=true;
                    cd.PreferencesNotifyOnVisit=false;
                    cd.PreferencesPasswordRequired=false;
                    cd.PreferencesAllowOriginalDownload= true;
                      System.debug(cd);
                    cdList.add(cd);
                
              
        }
        
       
        insert cdList;
        /*ContentDistribution cd = new ContentDistribution();
        cd.Name = cdl.contentdocument.title;
        cd.ContentVersionId = cv.id;
        cd.PreferencesAllowViewInBrowser= true;
        cd.PreferencesLinkLatestVersion=true;
        cd.PreferencesNotifyOnVisit=false;
        cd.PreferencesPasswordRequired=false;
        cd.PreferencesAllowOriginalDownload= true;
        insert cd;*/
        System.debug([SELECT Id, Title, FileType FROM ContentDocument WHERE Id IN :documentIds]);
        return [SELECT Id, Title, FileType FROM ContentDocument WHERE Id IN :documentIds];        
    } 
    
    @AuraEnabled
    public static List<ContentVersion> getContents(Id FileRecordId) {
        /*    Set<Id> contentIds = new Set<Id>();

for (ContentDocument cont :[Select Id ,Title from ContentDocument where ]) {
contentIds.add(cont.Id);
}

return [SELECT Id,Title FROM ContentVersion WHERE ContentDocumentId IN :contentIds];*/
        return null;
    }
    
    //Get Files
    @auraEnabled
    public static List<ContentDocument> getTaskFiles( String FileRecordId){ 
        
        System.debug('Request from Task '+FileRecordId);
        ID filter = FileRecordId;
        
        List<ContentDocumentLink> cdlList = [SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=:filter];                         
        Set<Id> documentIds = new Set<Id>(); 
        
        for(ContentDocumentLink cdl:cdlList){  
            documentIds.add(cdl.ContentDocumentId);  
        } 
        System.debug([SELECT Id, Title, SystemModStamp FROM ContentDocument WHERE Id IN :documentIds]);
        return [SELECT Id, Title, SystemModStamp FROM ContentDocument WHERE Id IN :documentIds and (NOT Title like '%Due_Date_Attachement%')]; 
        
    } 
    @AuraEnabled
    public static void submitapproval(Id recordId){ 
        Messaging.CustomNotification notification = new Messaging.CustomNotification();
        notification.setBody('Process owner submitted for Approval!');
        notification.setTitle('Process owner submitted for Approval!');
        notification.setSenderId(Userinfo.getUserId());
        CustomNotificationType type = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'stageGateApproval'];
        notification.setNotificationTypeId(type.id);
        notification.setTargetId(recordId); // target object id
        notification.send(new Set<String> { '0052w000007ryisAAA' });
        
    }
   @AuraEnabled
    public static string processEmail(String Subject, String Message,Id RecordId,string Stage){
        String sMessage='';
        try{
            List<Gate_Keeper__c> gateKeepers = [select Id,Name,Users__c,Stage__c,Stage_Gate__c,Users__r.Email__c from Gate_Keeper__c where Stage_Gate__c=: RecordId];
            //system.debug(gateKeeper.Users__r.Email__c);
            for(Gate_Keeper__c gateKeeper:gateKeepers ){
            
               string Messages=Message+'&GId='+gateKeeper.Id;
              string email = gateKeeper.Users__r.Email__c;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {email}; 
            mail.setToAddresses(toAddresses);
            mail.setSubject(Subject);
            mail.setHtmlBody(Messages);
            Messaging.SendEmailResult [] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
            sMessage='Success';  
            }
            /*List<Gatekeeper_QS__c> gatekeeper1 = [select Id,Name,Mail_send__c from Gatekeeper_QS__c where Stage__c=:Stage and Stage_Gate__c=:RecordId];
            if(gatekeeper1.size()>0)
            {
                gatekeeper1[0].Mail_Send__c = true;
                update gatekeeper1;   
            }*/
            
        }
        catch(Exception ex){
            sMessage=ex.getLineNumber()+'\n'+ex.getCause()+'\n'+ex.getMessage()+'\n'+ex.getStackTraceString();
            system.debug('sMessage'+sMessage);
        }
        return sMessage;
    } 
     @AuraEnabled
    public static string getApprovalStatus(String recordId, string stage)
    {
        Gate_Keeper__c GateKeeper = [SELECT id, name, Ideation_Stage_Comments__c,Stage_Gate__c,Stage__c,Business_Case_Response__c,Developer_Response__c,Ideation_Stage_Response__c,Scope_Response__c,Sign_off_Launch_Response__c,Test_Validate_Response__c FROM Gate_Keeper__c WHERE Stage_Gate__c=: recordId and Stage__c=: stage limit 1];
        string status;
        if(stage == 'Ideation Stage')
            {
                status = GateKeeper.Ideation_Stage_Response__c;
            }
            
            if(stage == 'Scope')
            {
               status = GateKeeper.Scope_Response__c;
            }
            
            if(stage == 'Business Case')
            {
                status = GateKeeper.Business_Case_Response__c;
            }

            if(stage == 'Develop')
            {
               status = GateKeeper.Developer_Response__c;
            }
           
            if(stage == 'Test & Validate')
            {
               status = GateKeeper.Test_Validate_Response__c;
            }
           
            if(stage == 'Sign Off & Launch')
            {
               status = GateKeeper.Sign_off_Launch_Response__c;
            }
        return status;
    }
    
   @AuraEnabled
    public static string RecordId(string stage)
    {
      Id devRecordTypeId = Schema.SObjectType.Stage_Gate_Question__c.getRecordTypeInfosByName().get(stage).getRecordTypeId();
        system.debug('devRecordTypeId'+devRecordTypeId);
       return devRecordTypeId;  
    }
    /*@AuraEnabled
    public static List<ApproverDetails>ApproverDetails(string recordId,string stage)
    {
        Stage_Gate__c approval = [select Id,Name,Approved_By__c,CEO__c,Issued_By__c,Marketing_Head__c from Stage_Gate__c where Id=:recordId];
        List<ApproverDetails> Details = new  List<ApproverDetails>();
        ApproverDetails a = new ApproverDetails();
        a.Approved=approval.Approved_By__c;
        a.CEO=approval.CEO__c;
        a.Issued=approval.Issued_By__c;
        a.MarketingHead=approval.Marketing_Head__c;
        Details.add(a);
        return details;
    }
    public class ApproverDetails{
        @AuraEnabled
       public string Approved{get;set;}
         @AuraEnabled
       public string CEO{get;set;}
        @AuraEnabled
       public string Issued{get;set;}
        @AuraEnabled
       public string MarketingHead{get;set;}
        
    }*/
    /*@AuraEnabled
    public static string GatekeeperRecordId(string stage)
    {
      Id devRecordTypeId = Schema.SObjectType.Stage_Gate_Question__c.getRecordTypeInfosByName().get(stage).getRecordTypeId();
        system.debug('devRecordTypeId'+devRecordTypeId);
       return devRecordTypeId;  
    } */
}