@ -12,11 +12,6 @@ import java.util.regex.Pattern; 
			
		
	
		
		
			
				
					
					import  java.util.zip.ZipEntry ; import  java.util.zip.ZipEntry ;  
			
		
	
		
		
			
				
					
					import  java.util.zip.ZipInputStream ; import  java.util.zip.ZipInputStream ;  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					import  javax.activation.MimetypesFileTypeMap ;  
			
		
	
		
		
			
				
					
					import  javax.jcr.Node ;  
			
		
	
		
		
			
				
					
					import  javax.jcr.RepositoryException ;  
			
		
	
		
		
			
				
					
					import  javax.jcr.Session ;  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					import  org.apache.sling.api.resource.Resource ; import  org.apache.sling.api.resource.Resource ;  
			
		
	
		
		
			
				
					
					import  org.apache.sling.api.resource.ResourceResolver ; import  org.apache.sling.api.resource.ResourceResolver ;  
			
		
	
		
		
			
				
					
					import  org.osgi.framework.Constants ; import  org.osgi.framework.Constants ;  
			
		
	
	
		
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
					@ -43,66 +38,91 @@ public class ScormExtractor implements WorkflowProcess { 
			
		
	
		
		
			
				
					
					    private  static  final  Logger  log  =  LoggerFactory . getLogger ( ScormExtractor . class ) ;      private  static  final  Logger  log  =  LoggerFactory . getLogger ( ScormExtractor . class ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					    ResourceResolver  resourceResolver ;      ResourceResolver  resourceResolver ;   
			
		
	
		
		
			
				
					
					    Session  session ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					    @Reference      @Reference   
			
		
	
		
		
			
				
					
					    QueryBuilder  queryBuilder ;      QueryBuilder  queryBuilder ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					    @Override      @Override   
			
		
	
		
		
			
				
					
					    public  void  execute ( WorkItem  wi ,  WorkflowSession  ws ,  MetaDataMap  mdm )  throws  WorkflowException  {      public  void  execute ( WorkItem  wi ,  WorkflowSession  ws ,  MetaDataMap  mdm )  throws  WorkflowException  {   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        resourceResolver  =  ws . adaptTo ( ResourceResolver . class ) ;          resourceResolver  =  ws . adaptTo ( ResourceResolver . class ) ;   
			
		
	
		
		
			
				
					
					        session  =  ws . adaptTo ( Session . class ) ;  
 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					        log . warn ( "SCORM WORKFLOW: MetaDataMap args: "  +  mdm . get ( "PROCESS_ARGS" ,  "string" ) . toString ( ) ) ;   
			
		
	
		
		
			
				
					
					        String [ ]  params  =  mdm . get ( "PROCESS_ARGS" ,  "string" ) . toString ( ) . split ( "," ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        / *  Initially  created  for  Workflow  Launcher   
			
		
	
		
		
			
				
					
					        String  excludePath  =  params [ 0 ] ;   
			
		
	
		
		
			
				
					
					        String  saveToLocation  =  params [ 1 ] ;   
			
		
	
		
		
			
				
					
					        String  payloadPath  =  wi . getContentPath ( ) . replace ( "/jcr:content/renditions/original" , "" ) ;   
			
		
	
		
		
			
				
					
					        unzipFile ( payloadPath ,  saveToLocation ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        String  nodePath  =  wi . getContentPath ( ) . replace ( ".zip/jcr:content/renditions/original" , "" ) ;   
			
		
	
		
		
			
				
					
					        cleanup ( nodePath ,  resourceResolver ) ;   
			
		
	
		
		
			
				
					
					        * /   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        /* Created for manually triggered workflows */          /* Created for manually triggered workflows */   
			
		
	
		
		
			
				
					
					        String  payloadPath  =  wi . getContentPath ( ) . replace ( "/jcr:content/renditions/original" , "" ) ;          String  payloadPath  =  wi . getContentPath ( ) . replace ( "/jcr:content/renditions/original" , "" ) ;   
			
		
	
		
		
			
				
					
					        String [ ]  params  =  mdm . get ( "PROCESS_ARGS" ,  "string" ) . toString ( ) . split ( "," ) ;   
			
		
	
		
		
			
				
					
					        String [ ]  fileExtensions  =  params ;  // Valid extensions com from the 'Arguments' field in the workflows 'Process' tab
          String [ ]  fileExtensions  =  params ;  // Valid extensions com from the 'Arguments' field in the workflows 'Process' tab
   
			
		
	
		
		
			
				
					
					         
			
		
	
		
		
			
				
					
					        unzipFile ( payloadPath ,  fileExtensions ) ;          unzipFile ( payloadPath ,  fileExtensions ) ;   
			
		
	
		
		
			
				
					
					        cleanup ( payloadPath ,  resourceResolver ) ;   
			
		
	
		
		
			
				
					
					    }   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					    private  void  cleanup ( String  path ,  ResourceResolver  resourceResolver )  {   
			
		
	
		
		
			
				
					
					        log . warn ( "SCORM WORKFLOW: inside cleanup: path - "  +  path ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        Resource  resource  =  resourceResolver . getResource ( path ) ;   
			
		
	
		
		
			
				
					
					        log . warn ( "SCORM WORKFLOW: inside cleanup: resource - "  +  resource ) ;   
			
		
	
		
		
			
				
					
					        / * Node  zipNode  =  resource . adaptTo ( Node . class ) ;   
			
		
	
		
		
			
				
					
					        try  {   
			
		
	
		
		
			
				
					
					            resourceResolver . delete ( resource ) ;   
			
		
	
		
		
			
				
					
					            resourceResolver . commit ( ) ;   
			
		
	
		
		
			
				
					
					        }  catch  ( PersistenceException  e )  {   
			
		
	
		
		
			
				
					
					            e . printStackTrace ( ) ;   
			
		
	
		
		
			
				
					
					        } * /   
			
		
	
		
		
			
				
					
					    }      }   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					    private  void  unzipFile ( String  filePath ,  String [ ]  validFileExtensions )  { //, String outputFolder) {
      private  void  unzipFile ( String  filePath ,  String [ ]  validFileExtensions )  { //, String outputFolder) {
   
			
		
	
		
		
			
				
					
					        String  nodePath  =  filePath . replaceAll ( "(?:.[^/]+).zip(/jcr:content/renditions/original)?" , "" ) ;  // remove filename to get AEM pathing for original zip file
          String  nodePath  =  filePath . replaceAll ( "(?:.[^/]+).zip(/jcr:content/renditions/original)?" , "" ) ;   
			
				
				
			
		
	
		
		
			
				
					
					        Pattern  p  =  Pattern . compile ( "(?:.[^/]+).(" + String . join ( "|" ,  validFileExtensions ) + ")(/jcr:content/renditions/original)?" ) ;    // pattern to search for filename only
          Pattern  p  =  Pattern . compile ( "(?:.[^/]+).(" + String . join ( "|" ,  validFileExtensions ) + ")(/jcr:content/renditions/original)?" ) ;    // the pattern to search for
   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        log . warn ( "SCORM WORKFLOW: inside unzipFile: filePath - "  +  filePath ) ;   
			
		
	
		
		
			
				
					
					        log . warn ( "SCORM WORKFLOW: inside unzipFile: nodePath - "  +  nodePath ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        Resource  zipfile  =  resourceResolver . getResource ( filePath ) ;          Resource  zipfile  =  resourceResolver . getResource ( filePath ) ;   
			
		
	
		
		
			
				
					
					        AssetManager  am  =  resourceResolver . adaptTo ( AssetManager . class ) ;          AssetManager  am  =  resourceResolver . adaptTo ( AssetManager . class ) ;   
			
		
	
		
		
			
				
					
					        log . warn ( "SCORM WORKFLOW: Resource - "  +  zipfile . getResourceType ( ) ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        Asset  a  =  zipfile . adaptTo ( Asset . class ) ;          Asset  a  =  zipfile . adaptTo ( Asset . class ) ;   
			
		
	
		
		
			
				
					
					        InputStream  is  =  a . getOriginal ( ) . getStream ( ) ;          InputStream  is  =  a . getOriginal ( ) . getStream ( ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        //NOTE: now working in initial directory level, need to extract individual assets from zip
          //NOTE: now working in initial directory level, need to extract to new folders 
   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					        try  ( ZipInputStream  zipIn  =  new  ZipInputStream ( is ) )  {          try  ( ZipInputStream  zipIn  =  new  ZipInputStream ( is ) )  {   
			
		
	
		
		
			
				
					
					            ZipEntry  entry  =  zipIn . getNextEntry ( ) ;              ZipEntry  entry  =  zipIn . getNextEntry ( ) ;   
			
		
	
		
		
			
				
					
					            while  ( entry  ! =  null )  {              while  ( entry  ! =  null )  {   
			
		
	
		
		
			
				
					
					                Matcher  m  =  p . matcher ( entry . getName ( ) ) ;  // set up matcher to parse original filename
                   Matcher  m  =  p . matcher ( entry . getName ( ) ) ;   
			
				
				
			
		
	
		
		
			
				
					
					                String  matchedString  =  m . find ( ) ? m . group ( 0 ) : "no match" ;  // extract filename w/o pathing
                   String  matchedString  =  m . find ( ) ? m . group ( 0 ) : "no match" ;   
			
				
				
			
		
	
		
		
			
				
					
					                String  extractedFilename  =  matchedString . replace ( "/" ,  "" ) ;  // filename w/o leading '/'
                  log . warn ( "SCORM WORKFLOW: matcher - "  +  matchedString ) ;   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					                String [ ]  fileName  =  entry . getName ( ) . split ( "\\." ) ;  // filename split to get extension
                  String [ ]  fileName  =  entry . getName ( ) . split ( "\\." ) ;   
			
				
				
			
		
	
		
		
			
				
					
					                String  fileExtension  =  fileName . length  > =  2  ?  fileName [ fileName . length - 1 ] : "none" ;  // file extension
                  String  zipFilePath  =  nodePath  +  matchedString ;   
			
				
				
			
		
	
		
		
			
				
					
					                boolean  extensionIsValid  =  Arrays . stream ( validFileExtensions ) . anyMatch ( fileExtension : : equals ) ;  // file extension check
                  String  zipFileExtension  =  fileName . length  > =  2  ?  fileName [ fileName . length - 1 ] : "none" ;   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					                boolean  extensionIsValid  =  Arrays . stream ( validFileExtensions ) . anyMatch ( zipFileExtension : : equals ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					                // Extract files 
                  // Extract file
   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					                if  ( ! entry . isDirectory ( )  & &  extensionIsValid )  {                  if  ( ! entry . isDirectory ( )  & &  extensionIsValid )  {   
			
		
	
		
		
			
				
					
					                    File  f  =  new  File ( extractedFilename ) ;  //create temporary file
                      log . warn ( "SCORM WORKFLOW: zipFilePath - "  +  zipFilePath ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					                    log . warn ( "SCORM WORKFLOW: zipFileExtension - "  +  zipFileExtension ) ;   
			
		
	
		
		
			
				
					
					                    File  f  =  new  File ( entry . getName ( ) ) ;   
			
		
	
		
		
			
				
					
					                    try  ( FileOutputStream  fos  =  new  FileOutputStream ( f ) )  {                      try  ( FileOutputStream  fos  =  new  FileOutputStream ( f ) )  {   
			
		
	
		
		
			
				
					
					                        byte [ ]  buffer  =  new  byte [ 1024 ] ;                          byte [ ]  buffer  =  new  byte [ 1024 ] ;   
			
		
	
		
		
			
				
					
					                        int  length ;                          int  length ;   
			
		
	
		
		
			
				
					
					                        while  ( ( length  =  zipIn . read ( buffer ) )  > =  0 )  {                          while  ( ( length  =  zipIn . read ( buffer ) )  > =  0 )  {   
			
		
	
		
		
			
				
					
					                            fos . write ( buffer ,  0 ,  length ) ;                              fos . write ( buffer ,  0 ,  length ) ;   
			
		
	
		
		
			
				
					
					                        }                          }   
			
		
	
		
		
			
				
					
					
                        log . warn ( "SCORM WORKFLOW: saving to AEM: "  +  zipFilePath ) ;   
			
				
				
			
		
	
		
		
			
				
					
					                        log . warn ( "SCORM WORKFLOW: saving to AEM: "  +  extractedFilename ) ;   
			
		
	
		
		
	
		
		
			
				
					
					                        InputStream  fis  =  new  FileInputStream ( f ) ;                          InputStream  fis  =  new  FileInputStream ( f ) ;   
			
		
	
		
		
			
				
					
					                        String  aemFilePath  =  nodePath  +  matchedString ;  // AEM DAM path with filename
                          am . createAsset ( zipFilePath ,  fis ,  null ,  true ) ;   
			
				
				
			
		
	
		
		
			
				
					
					
                        log . warn ( "SCORM WORKFLOW: file extracted: "  +  zipFilePath ) ;   
			
				
				
			
		
	
		
		
			
				
					
					                        MimetypesFileTypeMap  fileTypeMap  =  new  MimetypesFileTypeMap ( ) ;   
			
		
	
		
		
			
				
					
					                        String  mimeType  =  fileTypeMap . getContentType ( f . getName ( ) ) ;  // get MIME type
   
			
		
	
		
		
			
				
					
					                          
			
		
	
		
		
			
				
					
					                        am . createAsset ( aemFilePath ,  fis ,  mimeType ,  true ) ;  // save to AEM DAM
   
			
		
	
		
		
			
				
					
					                        f . delete ( ) ;  //delete temporary file once it has been saved to AEM.
   
			
		
	
		
		
			
				
					
					                        setAssetMetadata ( aemFilePath ,  a ) ;   
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					                    }                      }   
			
		
	
		
		
			
				
					
					                }                  }   
			
		
	
		
		
			
				
					
					                                   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -111,32 +131,10 @@ public class ScormExtractor implements WorkflowProcess { 
			
		
	
		
		
			
				
					
					            }              }   
			
		
	
		
		
			
				
					
					        }  catch  ( FileNotFoundException  e )  {          }  catch  ( FileNotFoundException  e )  {   
			
		
	
		
		
			
				
					
					            log . error ( e . getMessage ( ) ) ;              log . error ( e . getMessage ( ) ) ;   
			
		
	
		
		
			
				
					
					            e . printStackTrace ( ) ;              log . error ( e . getStackTrace ( ) . toString ( ) ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					        }  catch  ( IOException  e )  {          }  catch  ( IOException  e )  {   
			
		
	
		
		
			
				
					
					            log . error ( e . getMessage ( ) ) ;              log . error ( e . getMessage ( ) ) ;   
			
		
	
		
		
			
				
					
					            e . printStackTrace ( ) ;              e . printStackTrace ( ) ;   
			
		
	
		
		
			
				
					
					        }          }   
			
		
	
		
		
			
				
					
					    }      }   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					    private  void  setAssetMetadata ( String  filePath ,  Asset  parentAsset )  {   
			
		
	
		
		
			
				
					
					        Resource  r  =  resourceResolver . getResource ( filePath ) ;   
			
		
	
		
		
			
				
					
					        Node  assetNode  =  r . adaptTo ( Node . class ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        String  parentLMSID  =  parentAsset . getMetadataValue ( "mcaps:lmsId" ) ;   
			
		
	
		
		
			
				
					
					        String  skills  =  parentAsset . getMetadataValue ( "mcaps:skills" ) ;   
			
		
	
		
		
			
				
					
					        String [ ]  parentSkills  =  skills . replace ( ", " ,  "," ) . split ( "," ) ;   
			
		
	
		
		
			
				
					
					        String  parentCategories  =  parentAsset . getMetadataValue ( "mcaps:category" ) ;   
			
		
	
		
		
			
				
					
					        String  parentCourseUrl  =  parentAsset . getMetadataValue ( "mcaps:courseUrl" ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					        try  {   
			
		
	
		
		
			
				
					
					                Node  metadataNode  =  assetNode . getNode ( "jcr:content/metadata" ) ;   
			
		
	
		
		
			
				
					
					                metadataNode . setProperty ( "mcaps:lmsId" ,  parentLMSID ) ;   
			
		
	
		
		
			
				
					
					                metadataNode . setProperty ( "mcaps:skills" ,  parentSkills ) ;   
			
		
	
		
		
			
				
					
					                metadataNode . setProperty ( "mcaps:categories" ,  parentCategories ) ;   
			
		
	
		
		
			
				
					
					                metadataNode . setProperty ( "mcaps:courseUrl" ,  parentCourseUrl ) ;   
			
		
	
		
		
			
				
					
					                session . save ( ) ;   
			
		
	
		
		
			
				
					
					        }  catch  ( RepositoryException  e )  {   
			
		
	
		
		
			
				
					
					            e . printStackTrace ( ) ;   
			
		
	
		
		
			
				
					
					        }   
			
		
	
		
		
			
				
					
					    }   
			
		
	
		
		
			
				
					
					} }