views:

46

answers:

1

Hi all,

I have a "Course" class which contains collection of "CourseItems".
Each CourseItem has a folder associated with it(for its files).
I need to add the CourseItem to a Zip file and after zipping it encrypting.
Using FZip and As3crypto I can do the zip and encrypt the zip file.
Whether to do Encryption or not is upto the user .
Currently my code looks similar to (ignore sytax):

Edit: I will try to add some more info + more description
Basically a Course is a folder(user selectable) with subfolders(courseitems).
mapping subfolder structure to the courseitem object(s).

       //main code
    var encryptCourse:Boolean;
     encryptCourse=true; // read from user..checkbox
    var course:Course = new Course("path_coursefolder");//from user input

     course.createCourseItems();

    //read course folder and create "courseitem" for each subfolder
    //and save in "courseItems" collection
 //after creating pack course - zip and encrypt
   course.Pack(encryptCourse);//encryptCourse bool encrypt course or not



 class Course
     {
               var courseItems:Array; //set when creating
               public function Pack()
               {
                     for each (var item:CourseItem in courseItems)
                        {
                           item.addEventListener("ZIP_COMPLETE",onitemzip);
                           item.zip();                     
                        } 
               }    

         private function onitemzip(e:Event)
          { 
              //if user selected to encrypt..do encryption 
             //now  i want to call the encrypt method :(
             //item.Encrypt() //cant call this,,how to refer to "item"??
          }
      }

            class CourseItem
            {
              var files:Array; //set when creating
              var _courseZipfile;
              public function ZIP()
              {
                var ziputil = new Ziputil()
                 ziputil.createZip("some_name",files);
                 ziputil.addEventListener(Event.Complete,onZipComplete);
              }

               private function onZipComplete(e:Event)
               {
                 dispatchEvent(new ZipEvent("ZIP_COMPLETE"));   
                 //dispatch and update progess
                 //COULD CALL ENCRYPT HERE..but want to avoid.   
               }

              public function Encrypt()
              {
                //encrypt zip file here
                //event of encrypt finish 
              }
            }

Basically in above design I want to call "Encrypt" method of CourseItem in Course after Zipping.

Note : i had earlier changed the courseitem class.
I had tried moving pack inside CourseItem then i cud handle encryption after zip.
But by this i strongly couple Zip and encrypt methods.
After zipping -> encrypt is called compulsarily .
i want refactor my code in such a way that both methods
zip and encrypt are independant of each other?
How can i do the same ..

Thanks all

+1  A: 

Edit: Following your edits , I have edited my code accordingly

When a user chooses to encrypt, check the value of the Boolean , if true call the encryption method after the zip complete event has been dispatched.

public class Course
{
   public function Pack(encryptCourse:Boolean)
   {
      for each (var item:CourseItem in courseItems)
      {
         item.addEventListener("ZIP_COMPLETE", onitemzip);

         if(encryptCourse)
           item.encrypt = true;

         item.zip();                     
       } 
   } 
}

public class CourseItem
{
    private var _encrypt:Boolean;

    public function set encrypt(value:Boolean):void
    {
       _encrypt = value;
    }

    private function onZipComplete(e:Event)
    {  
      if(_encrypt)
         Encrypt();

      //here you can choose to dispatch a complete event according
      //to the value of _encrypt , if true , dispatch the event 
      //after encryption
      dispatchEvent(new ZipEvent("ZIP_COMPLETE")); 
    }
}
PatrickS
how to refer to courseitem object "item" which last zipped? and when to call Course.Encrypt?
Amitd
check the edited answer... i'm assuming a lot there , as I don't really know the structure of your app, the basic idea is that the user selects an item with a mouse click, from then on , you have a selected item you can act upon.
PatrickS
oh ok..i have added more description in code and added Edit..hope its more clear now..
Amitd
Check the latest edit.
PatrickS
thx u again :) marked as answer
Amitd