//================================= CLASS CONSTRUCTOR ========================================
function Dnl(idownloadCallback)
{
//MEMBER VARIABLES
//The callback for downloading entities ...
 this.downloadCallback = null;
//The classes composite ...
 this.classes = null;
//The documents composite ... is a child of classes composite ...
 this.documents = null;
//The attachments composite ... is a child of classes composite ...
 this.attachments = null;
//If there are documents waiting to be downloaded ...
 this.mustDnlDocs = null;
//If there are attachments waiting to be downloaded ...
 this.mustDnlAttach = null;

 this.initialize(idownloadCallback);
}

//====================================== STATIC ==============================================
//CONSTANTS

//====================================== MEMBERS =============================================
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GETTER/SETTER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dnl.prototype.getDocuments = function()
{
 return this.documents;
}

Dnl.prototype.getAttachments = function()
{
 return this.attachments;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ METHODS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dnl.prototype.setDefaults = function()
{
 this.downloadCallback = null;
 this.classes = null;
 this.documents = null;
 this.attachments = null;
 this.mustDnlDocs = null;
 this.mustDnlAttach = null;
}

Dnl.prototype.initialize = function(idownloadCallback)
{
//Initializing params ...
 this.downloadCallback = idownloadCallback;
 this.mustDnlDocs = false;
 this.mustDnlAttach = false;
//Creating composites ...
 this.classes = new DnlCompositeEntity();
 this.documents = new DnlCompositeEntity();
 this.attachments = new DnlCompositeEntity();
//Attaching composites ...
 this.classes.add(this.documents);
 this.classes.add(this.attachments);
}

Dnl.prototype.onDocumentStatusChanged = function(icontext,ientity)
{
 if (ientity.getStatus() == DnlEntity.STATUS_MUSTDOWNLOAD)
  icontext.mustDnlDocs = true;
}

Dnl.prototype.onAttachmentStatusChanged = function(icontext,ientity)
{
 if (ientity.getStatus() == DnlEntity.STATUS_MUSTDOWNLOAD)
  icontext.mustDnlAttach = true;
}

Dnl.prototype.addDocument = function(idocumentId)
{
 var
  idnlEntity = new DnlEntity();

 idnlEntity.setId(idocumentId);
 idnlEntity.setEStatusChanged({context:this,method:this.onDocumentStatusChanged});
 this.documents.add(idnlEntity);
 return idnlEntity;
}

Dnl.prototype.addAttachment = function(iattachmentId,iattachmentOp)
{
 var
  idnlEntity = new DnlEntity();

 idnlEntity.setId(iattachmentId);
 idnlEntity.setOperation(iattachmentOp);
 idnlEntity.setEStatusChanged({context:this,method:this.onAttachmentStatusChanged});
 this.attachments.add(idnlEntity);
 return idnlEntity;
}

Dnl.prototype.hasMustDownloadStatus = function(ientity)
{
 return ientity.getStatus() == DnlEntity.STATUS_MUSTDOWNLOAD;
}

Dnl.prototype.download = function()
{
 var
  retDocuments,
  retAttachments;
//Downloading the documents ...
 if (this.mustDnlDocs == true)
  retDocuments = this.documents.query(this.hasMustDownloadStatus);
 else
  retDocuments = new Array();
//Downloading the attachments ...
 if (this.mustDnlAttach == true)
  retAttachments = this.attachments.query(this.hasMustDownloadStatus);
 else
  retAttachments = new Array();
//Downloading ...
 if (retDocuments.length > 0 || retAttachments.length > 0)
 {
  this.downloadCallback(retDocuments,retAttachments);
  if (retDocuments.length > 0)
  {
   for (var i=0;i<retDocuments.length;i++)
    retDocuments[i].setStatus(DnlEntity.STATUS_DOWNLOADING);
   retDocuments.splice(0,retDocuments.length);
   this.mustDnlDocs = false;
  }
  if (retAttachments.length > 0)
  {
   for (var i=0;i<retAttachments.length;i++)
    retAttachments[i].setStatus(DnlEntity.STATUS_DOWNLOADING);
   retAttachments.splice(0,retAttachments.length);
   this.mustDnlAttach = false;
  }
 }
 retDocuments = null;
 retAttachments = null;
}

Dnl.prototype.finalize = function()
{
 this.classes.finalize();
 this.setDefaults();
}
