/// <summary>
        /// Updates a Course Document record in the EJS
        /// </summary>
        internal static void UpdateCourseDocument(SqlConnection dBconnection,
                                                  ejsSessionToken Token, ejsCourseDocument document)
        {
            SqlCommand    command = new SqlCommand();
            SqlDataReader reader  = null;

            command.CommandTimeout = 60;

            try
            {
                command.Connection  = dBconnection;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "UpdateCourseDocument";

                command.Parameters.Add("UserId", SqlDbType.UniqueIdentifier).Value                 = Token.UserId;
                command.Parameters.Add("Title", System.Data.SqlDbType.NVarChar, 150).Value         = document.Name;
                command.Parameters.Add("Description", System.Data.SqlDbType.NVarChar, 500).Value   = document.Description;
                command.Parameters.Add("DocumentId", System.Data.SqlDbType.UniqueIdentifier).Value = document.DocumentId;
                command.Parameters.Add("IsAvailable", System.Data.SqlDbType.Bit).Value             = document.IsAvailable;
                command.Parameters.Add("CourseId", SqlDbType.Int).Value = document.CourseId;

                command.ExecuteNonQuery();
            }
            finally
            {
                command.Dispose();
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
        }
        /// <summary>
        /// Deletes a Course Document from the EJS
        /// </summary>
        internal static int DeleteCourseDocument(SqlConnection dBconnection,
                                                 ejsSessionToken Token, ejsCourseDocument document)
        {
            SqlCommand    command = new SqlCommand();
            SqlDataReader reader  = null;

            command.CommandTimeout = 60;

            try
            {
                command.Connection  = dBconnection;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "DeleteCourseDocument";

                command.Parameters.Add("UserId", SqlDbType.UniqueIdentifier).Value     = Token.UserId;
                command.Parameters.Add("DocumentId", SqlDbType.UniqueIdentifier).Value = document.DocumentId;

                SqlParameter returnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
                returnValue.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(returnValue);

                command.ExecuteNonQuery();
                int resultCode = (int)returnValue.Value;
                return(resultCode);
            }
            finally
            {
                command.Dispose();
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
        }
        /// <summary>
        /// Download a copy of a document stored in a course in the database.
        /// </summary>
        internal static byte[] GetCourseDocument(SqlConnection dBconnection,
                                                 ejsSessionToken sessionToken, ejsCourseDocument document)
        {
            SqlCommand    command = null;
            SqlDataReader reader  = null;

            try
            {
                byte[] result = new byte[document.ByteSize];

                int          bufferSize = (int)document.ByteSize;
                byte[]       outbyte    = new byte[bufferSize];
                MemoryStream ms         = new MemoryStream(bufferSize);
                BinaryWriter bw         = new BinaryWriter(ms);
                long         startIndex = 0;
                long         retval;

                command = new SqlCommand("SELECT Data FROM CourseDocuments WHERE DocumentId = @Id", dBconnection);
                command.Parameters.Add("@Id", SqlDbType.UniqueIdentifier).Value = document.DocumentId;
                command.CommandTimeout = 60;

                reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

                while (reader.Read())
                {
                    retval = reader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

                    while (retval == bufferSize)
                    {
                        bw.Write(outbyte);
                        bw.Flush();

                        startIndex += bufferSize;
                        retval      = reader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
                    }

                    bw.Write(outbyte, 0, (int)retval);
                    bw.Flush();
                    bw.Close();

                    ms.Close();
                    ms.Dispose();
                }

                result = ms.GetBuffer();
                return(result);
            }
            finally
            {
                command.Dispose();
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
        }
        internal static void GetAllCourseDocuments(SqlConnection dBconnection,
                                                   ejsSessionToken sessionToken, bool includeNotAvailable, ref List <ejsCourseDocument> result)
        {
            SqlCommand    command = new SqlCommand();
            SqlDataReader reader  = null;

            command.CommandTimeout = 60;

            try
            {
                command.Connection  = dBconnection;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "GetAllCourseDocuments";

                command.Parameters.Add("UserId", SqlDbType.UniqueIdentifier).Value = sessionToken.UserId;
                command.Parameters.Add("IncludeNotAvailable", SqlDbType.Bit).Value = includeNotAvailable;

                reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ejsCourseDocument doc = new ejsCourseDocument();
                        doc.Id           = reader.GetInt32(0);
                        doc.Name         = reader.GetString(1);
                        doc.Description  = reader.GetString(2);
                        doc.DocumentId   = reader.GetGuid(3);
                        doc.CreationDate = reader.GetDateTime(4);
                        doc.IsAvailable  = reader.GetBoolean(5);
                        doc.CourseId     = reader.GetInt32(6);
                        result.Add(doc);
                    }
                }
            }
            finally
            {
                command.Dispose();
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
        }
        /// <summary>
        /// Upload a document to a course in the E J S.
        /// </summary>
        internal static void RegisterDocumentToCourse
            (SqlConnection dBconnection, ejsSessionToken sessionToken,
            ejsCourseDocument document, int courseId, byte[] documentData)
        {
            SqlCommand    command = new SqlCommand();
            SqlDataReader reader  = null;

            command.CommandTimeout = 60;

            try
            {
                command.Connection  = dBconnection;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "AddDocumentToCourse";

                command.Parameters.Add("Title", System.Data.SqlDbType.NVarChar, 150).Value         = document.Name;
                command.Parameters.Add("Description", System.Data.SqlDbType.NVarChar, 500).Value   = document.Description;
                command.Parameters.Add("DocumentId", System.Data.SqlDbType.UniqueIdentifier).Value = document.DocumentId;
                command.Parameters.Add("CreationDate", System.Data.SqlDbType.DateTime).Value       = document.CreationDate;
                command.Parameters.Add("IsAvailable", System.Data.SqlDbType.Bit).Value             = document.IsAvailable;
                command.Parameters.Add("CourseId", SqlDbType.Int).Value = courseId;
                command.Parameters.Add("Data", System.Data.SqlDbType.VarBinary, (int)documentData.Length).Value = documentData;
                command.Parameters.Add("DataSize", System.Data.SqlDbType.BigInt).Value = documentData.Length;

                command.ExecuteNonQuery();
            }
            finally
            {
                command.Dispose();
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
        }