Ext.ns('Ext.clv2.utils')
/**
   * @author: Rodrigo Bartels
   * Date: October 3, 2008.
   * Object that manages the DWR calls for the LearningObjectService Bean.
*/

Ext.clv2.utils.LearningObjectUtils = function(){
    var LearningObjectJSON = Ext.data.Record.create([
         {name: 'id'},
         {name: 'name'},
         {name: 'contentReferenceId'},
         {name: 'contentSize'},
         {name: 'organizableType'},
         {name: 'viewCount'},
         {name: 'totalRating'},
         {name: 'id'},
         {name: 'viewCount'},
         {name: 'template'},
         {name: 'metadata'}
    ]);

    var LearningObjectReader = new Ext.data.JsonReader({
            root:"root",
            id: "id"                     // The property within each row object that provides an ID for the record (optional)
        }, LearningObjectJSON);
    
    var currentLO = null;
    var currentID = null;
    var currentContent = null;
    var currentDraft = null;
    var averageRating = null;
    var currentUserRating = null;
    var version = null;
    var currentOwnerID = null;
    var attachments = null;

    this.getCurrentLO = function(){
        return currentLO;
    };

    this.setCurrentID= function(id){
            currentID = id;
    };

    this.getCurrentOwnerID= function(){
        return currentOwnerID;
    };

    this.setCurrentOwnerID= function(id){
            currentOwnerID = id;
    };

    this.getCurrentID = function(){
        return currentID;
    };

    this.setCurrentLO= function(learningObject){
            currentLO = learningObject;
    };

    this.setCurrentContent= function(id){
           currentContent = id;
    };

    this.getCurrentContent = function(){
         return currentContent;
    };

    this.setCurrentDraft= function(id){
               currentDraft = id;
        };

   this.getCurrentDraft = function(){
         return currentDraft;
    };

    this.getLOReader = function(){
        return LearningObjectReader;
    };

    this.getAverageRating = function(){
        return averageRating;
    };

    this.setAverageRating= function(rating){
           averageRating = rating;
    };

    this.getCurrentUserRating = function(){
        return currentUserRating;
    };

    this.setCurrentUserRating= function(rating){
           currentUserRating = rating;
    };

    this.getVersion = function(){
        return version;
    };

    this.setVersion= function(itemVersion){
           version = itemVersion;
    };

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

    this.setAttachments = function(itemAttachments){
        attachments = itemAttachments;
    }
};

Ext.clv2.utils.LearningObjectUtils.prototype = {
  getLearningObject : function(id){
        var that = this;
        LearningObjectService.findById(
                id,
                {async:false,callback : function(learningObject){
                           learningObject = {root:[ learningObject ]};
                           var currentLearningObject = that.getLOReader().readRecords(learningObject);
                           that.setCurrentLO(currentLearningObject)
                           }
                });
        return this.getCurrentLO();
  },

  getLearningObjectContent : function(id){
        var that = this;
        LearningObjectService.getContentById(
                id,
                {async:false,callback : function(learningObjectContent){
                           that.setCurrentContent(learningObjectContent)
                           }
                });
        return this.getCurrentContent();
  },

  getLearningObjectDraft : function(id){
        var that = this;
        LearningObjectService.getDraftContentById(
                id,
                {async:false,callback : function(learningObjectContent){
                           that.setCurrentDraft(learningObjectContent)
                           }
                });
        return this.getCurrentDraft();
  },

  saveLearningObjectContent : function(id, content){
        //var that = this;
        LearningObjectService.saveContent(
                id, content,
                {async:false, callback : Ext.emptyFn});
  },

  saveLearningObjectMetadata : function(id, generalMetadata, educationalMetadata){
        LearningObjectService.saveMetadata(
                id, generalMetadata, educationalMetadata,
                {async:false, callback : Ext.emptyFn});
  },

  saveLearningObjectDraft : function(id, content){
        //var that = this;
        LearningObjectService.saveDraftContent(
                id, content,
                {async:false, callback : Ext.emptyFn});
  },
  createNewLearningObject : function(name, type, itemId){
       var that = this;
       var duplicatedErrorHandler = function(errMessage, exception){
           that.setCurrentID(-1);
       }
       LearningObjectService.create(name, type, itemId,
                {async:false,
                 callback : function(id){that.setCurrentID(id)},
                 errorHandler:duplicatedErrorHandler
                }
       );
      return this.getCurrentID();
  },

  createLearningObjectFromDemo : function(title, type, content, userId, generalMetadata, educationalMetadata){
       var that = this;
       LearningObjectService.createLearningObjectFromDemo(title, type, content, userId, generalMetadata, educationalMetadata,
                {async:false,
                 callback : function(id){that.setCurrentID(id)}
                }
       );
      return this.getCurrentID();
  },

  removeLearningObject: function(id){
       LearningObjectService.remove(id);
  },

	removeLogicalLearningObject: function(id){
		LearningObjectService.logicalRemove(id);
	},

  renameLearningObject: function(id, newName){
       LearningObjectService.rename(id,newName);
  },

  getLearningObjectCurrentVersion : function(objectId){
    var that = this;
    LearningObjectService.getLearningObjectCurrentVersion(objectId,
                {async:false,
                 callback : function(version){that.setVersion(version)}
                }
       );
      return this.getVersion();
  },

  getLearningObjectAverageRating : function(objectId){
     var that = this;
    LearningObjectService.getLearningObjectAverageRating(objectId,
                {async:false,
                 callback : function(rating){that.setAverageRating(rating)}
                }
       );
      return this.getAverageRating();
  },

  addOrChangeUserRating: function(learningObjectId, userId, rating){
        //var that = this;
        LearningObjectService.addOrChangeRating(
            userId, learningObjectId, rating,
                {async:false, callback : Ext.emptyFn});
  },

  getLearningObjectCurrentUserRating : function(objectId,currentUser){
    var that = this;
    LearningObjectService.getLearningObjectCurrentUserRating(objectId,currentUser,
                {async:false,
                 callback : function(rating){that.setCurrentUserRating(rating)}
                }
       );
    return this.getCurrentUserRating();
  },

  rollbackToVersion: function(id, version){
       LearningObjectService.rollbackContent(id,version,{async:false,
                 callback : Ext.emptyFn});
  },

  publishLearningObject: function(id){
       LearningObjectService.publish(id,{async:false,
                 callback : Ext.emptyFn});
  },

  getLOOwnerId : function(objectId){
    var that = this;
    LearningObjectService.getLearningObjectOwnerId(objectId,
                {async:false,
                 callback : function(id){that.setCurrentOwnerID(id)}
                }
       );
      return this.getCurrentOwnerID();
  },

  getLOOwnerName : function(objectId){
    var name = '';
    LearningObjectService.getLearningObjectOwnerName(objectId,
                {async:false,
                 callback : function(ownerName){name = ownerName}
                }
       );
      return name;
  },

  alignTopic : function(learningObjectId, topicId){
        LearningObjectService.align(
                learningObjectId, topicId,
                {async:false, callback : Ext.emptyFn});
  },

  removeTopic : function(learningObjectId, topicId){
        LearningObjectService.removeAlignment(
                learningObjectId, topicId,
                {async:false, callback : Ext.emptyFn});
  },

  getLOAttachments : function(objectId){
    var that = this;
    LearningObjectService.getLOAttachments(objectId,
                {async:false,
                 callback : function(list){that.setAttachments(list)}
                }
       );
      return this.getAttachments();
  },

  getAttachmentName : function(objectId, attachmentIndex){
    var that = this;
    var content = '';
    LearningObjectService.getAttachmentName(objectId, attachmentIndex,
                {async:false,
                 callback : function(aContent){content = aContent}
                }
       );
      return content;
  },

  getAttachmentType : function(objectId, attachmentIndex){
    var that = this;
    var content = '';
    LearningObjectService.getAttachmentType(objectId, attachmentIndex,
                {async:false,
                 callback : function(aType){content = aType}
                }
       );
      return content;
  },

  getAttachmentCreator : function(objectId, attachmentIndex){
    var creator = '';
    LearningObjectService.getAttachmentCreator(objectId, attachmentIndex,
                {async:false,
                 callback : function(aCreator){creator = aCreator}
                }
       );
      return creator;
  },

  getAttachmentNote : function(objectId, attachmentIndex){
    var note = '';
    LearningObjectService.getAttachmentNote(objectId, attachmentIndex,
                {async:false,
                 callback : function(aNote){note = aNote}
                }
       );
      return note;
  },

  getAttachmentReferenceId : function(objectId, attachmentIndex){
    var referenceId = '';
    LearningObjectService.getAttachmentReferenceId(objectId, attachmentIndex,
                {async:false,
                 callback : function(aReferenceId){referenceId = aReferenceId}
                }
       );
      return referenceId;
  },

  removeAttachment : function(learningObjectId, attachmentId){
        LearningObjectService.removeAttachment(
                learningObjectId, attachmentId,
                {async:false, callback : Ext.emptyFn});
  }
};