Esempio n. 1
0
        /// <summary>
        /// To be used with transactions.
        /// Saves all the images inside the folder with this path-name
        /// under the given Category. No deep loading.
        /// </summary>
        /// <param name="cmd">The command object asociated with a transaction.</param>
        /// <param name="parent">the target category</param>
        /// <param name="folderName">the folder full path</param>
        internal static void loadFromFolder(SqlCommand cmd, 
            Category parent, string folderName, LoadInfoViewer viewer)
        {
            string[] fileNames = getFileNames(folderName);
            int count = 0;

            for (int i = 0; i < fileNames.Length; i++)
            {
                Image img = new Image(fileNames[i], parent);
                viewer.Invoke(
                    new ShowFilenameDlegate(viewer.showFileName),
                    new object[] {fileNames[i]}
                );
                saveImage(cmd, img, fileNames[i]);// transactional save
                count++;
                int percent = (count*100)/fileNames.Length;
                viewer.Invoke(
                    new ShowProccessDlegate(viewer.showProccess),
                    new object[] {percent}
                );
                img = null;
            }
            fileNames = null;
            System.GC.Collect();
        }
Esempio n. 2
0
 internal static void deepLoadFromFolder(SqlCommand cmd, 
     Category parent, string fileName, LoadInfoViewer viewer)
 {
     string[] subDirs = Directory.GetDirectories(fileName);
     foreach (string subDir in subDirs)
     {
         deepLoadFromFolder(cmd, parent, subDir, viewer);
     }
     loadFromFolder(cmd, parent, fileName, viewer);
 }
Esempio n. 3
0
        /// <summary>
        /// Creates new category from the file system.
        /// no save() is required after that.
        /// </summary>
        /// <param name="parentCategory">the parent</param>
        /// <param name="fileName">the full path</param>
        /// <param name="deep">determine if you will make a deep copy, 
        /// or you will just copy only the images in this category</param>
        public Category(Category parentCategory, String fileName, bool deep)
        {
            if (parentCategory == null || fileName == null)
            {
                throw new ObjectDBException("Problem creating category!", this);
            }
            // the user of the new category is the
            // same as the user of the parent category
            this.parent = parentCategory;
            user = parentCategory.User;
            SqlTransaction trans = null;
            try
            {
                trans = user.Connection.getInstance().BeginTransaction();
                SqlCommand cmd = new SqlCommand();

                cmd.Connection = user.Connection.getInstance();
                cmd.Transaction = trans;
                cmd.CommandType = CommandType.StoredProcedure;
                if (!deep)
                {
                    FileManger.saveSingleFolder(cmd, this, fileName);
                }
                else
                {

                }
                trans.Commit();
            }
            catch (SqlException e)
            {
                if (trans != null) trans.Rollback();
                throw new ServerInfoException(
                    e.Message, user.Connection.ServerInfos, e);
            }
            catch (Exception e)
            {
                if (trans != null) trans.Rollback();
                if (e is ServerInfoException) throw e;
                else
                {
                    throw new ObjectDBException("Problem creating new category!", this, e);
                }

            }
            shared = false;
        }
Esempio n. 4
0
        /// <summary>
        /// transactional save for a category 
        /// </summary>
        /// <param name="cmd">the SqlCommand to be used</param>
        /// <param name="cat">the category to be saved should've been asigned
        ///  a ParentCategory before using this method and should 
        ///  not be present in the DB.</param>
        internal static void saveCategory(SqlCommand cmd, 
            Category cat)
        {
            cmd.CommandText = "sp_CreateNewCategory";
            cmd.Parameters.Add("@Name", cat.Name);
            cmd.Parameters.Add("@Parent_Id", cat.ParentCategory.ID);
            cmd.Parameters.Add("@User_Id", cat.User.ID);
            SqlParameter categoryIdParam =
                cmd.Parameters.Add("@Category_Id", SqlDbType.Int);
            categoryIdParam.Direction = ParameterDirection.Output;
            SqlParameter dateModifiedParam =
                cmd.Parameters.Add("@Date_Modified", SqlDbType.DateTime);
            dateModifiedParam.Direction = ParameterDirection.Output;

            cmd.ExecuteNonQuery();

            cat.ID = (Int32) categoryIdParam.Value;
            cat.DateModified = (DateTime)dateModifiedParam.Value;
        }
Esempio n. 5
0
 protected virtual void Dispose(bool disposing)
 {
     // Check to see if Dispose has already been called.
     if(!this.disposed)
     {
         if(disposing)
         {
             try
             {
                 performLogout();
             }
             catch (Exception) {}
             mDBConnection.closeConnection();
             this.alias = null;
             this.rootCategory = null;
             // TODO: Add rootCategory.Dispose();
         }
     }
     disposed = true;
 }
Esempio n. 6
0
        public void save()
        {
            if (this.id >= 0)
            {
                // User should not have an id to be inserted
                throw new ObjectDBException("Problem creating user!", this);
            }
            try
            {
                SqlCommand cmd = new SqlCommand(
                    "sp_CreateNewUser",
                    mDBConnection.getInstance()
                );
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Alias", alias);
                cmd.Parameters.Add("@Password", password);
                cmd.Parameters.Add("@Name", userName);
                cmd.Parameters.Add("@Surname", userSurname);
                SqlParameter userIdParam =
                    cmd.Parameters.Add("@User_Id", SqlDbType.Int);
                userIdParam.Direction = ParameterDirection.Output;
                SqlParameter rootCategoryIdParam =
                    cmd.Parameters.Add("@Root_Category_Id", SqlDbType.Int);
                rootCategoryIdParam.Direction = ParameterDirection.Output;

                cmd.ExecuteNonQuery();

                id = (Int32) userIdParam.Value;
                Int32 rootCategoryId = (Int32) rootCategoryIdParam.Value;
                rootCategory = new Category(rootCategoryId, cmd, this);
            }
            catch (SqlException e)
            {
                throw new ServerInfoException(
                    e.Message, mDBConnection.ServerInfos, e);
            }
            catch (Exception e)
            {
                if (e is ServerInfoException) throw e;
                else
                {
                    throw new ObjectDBException("Problem loading user", this, e);
                }

            }
        }
Esempio n. 7
0
 public void performLogout()
 {
     // end of session with user
     mDBConnection.closeConnection();
     rootCategory = null;
 }
Esempio n. 8
0
        public void delete()
        {
            if (this.id == -1)
            {
                // only existing users can be deleted
                throw new ObjectDBException("Problem deleting user!", this);
            }
            try
            {
                SqlCommand cmd = new SqlCommand(
                    "sp_DeleteUserById",
                    mDBConnection.getInstance()
                );
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@User_Id", id);

                cmd.ExecuteNonQuery();
                // TODO: point Category to null i.e cascade delete DBObjects
                id = -1;
                alias = null;
                password = null;
                userSurname = null;
                userName = null;
                rootCategory = null;
            }
            catch (SqlException e)
            {
                throw new ServerInfoException(
                    e.Message, mDBConnection.ServerInfos, e);
            }
            catch (Exception e)
            {
                if (e is ServerInfoException) throw e;
                else
                {
                    throw new ObjectDBException("Problem loading user", this, e);
                }

            }
            Dispose();
        }
Esempio n. 9
0
        public void save()
        {
            if (this.id >= 0 || parent == null || categoryName == null)
            {
                // Category should not have an id to be inserted
                throw new ObjectDBException("Problem creating category!", this);
            }
            // the user of the new category is the
            // same as the user of the parent category
            user = parent.user;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = user.Connection.getInstance();
                // no transaction needed here
                cmd.CommandType = CommandType.StoredProcedure;
                FileManger.saveCategory(cmd, this);
            }
            catch (SqlException e)
            {
                throw new ServerInfoException(
                    e.Message, user.Connection.ServerInfos, e);
            }
            catch (Exception e)
            {
                if (e is ServerInfoException) throw e;
                else
                {
                    throw new ObjectDBException("Problem creating new category!", this, e);
                }

            }
            parent = null;
            shared = false;
        }
Esempio n. 10
0
        private void createNewCategoryNode(Category newCat, TreeNode parentNode)
        {
            MainFormTreeView.BeginUpdate();
            TreeNode newNode = new TreeNode(newCat.Name);
            newNode.Tag = newCat;
            newNode.ImageIndex = 0;
            newNode.SelectedImageIndex = 0;

            parentNode.Nodes.Add(newNode);
            MainFormTreeView.EndUpdate();

            newNode.EnsureVisible();
            MainFormTreeView.LabelEdit = true;
            newNode.BeginEdit();
        }
Esempio n. 11
0
 public Image(String fileName, Category parentCategory)
 {
     this.realFileName = fileName;
     this.parentCategory = parentCategory;
     this.user = parentCategory.User;
 }
Esempio n. 12
0
        public void save()
        {
            if (this.id >= 0 || parentCategory == null || realFileName == null)
            {
                // Image should not have an id to be inserted
                throw new ObjectDBException("Problem creating image!", this);
            }
            // the user of the new image is the
            // same as the user of the parent category
            SqlTransaction trans = null;
            try
            {
                trans = user.Connection.getInstance().
                    BeginTransaction(IsolationLevel.Serializable);
                SqlCommand cmd = new SqlCommand();

                cmd.Connection = user.Connection.getInstance();
                cmd.Transaction = trans;
                cmd.CommandType = CommandType.StoredProcedure;
                FileManger.saveImage(cmd, this, realFileName);
                trans.Commit();
            }
            catch (SqlException e)
            {
                if (trans != null) trans.Rollback();
                throw new ServerInfoException(
                    e.Message, user.Connection.ServerInfos, e);
            }
            catch (Exception e)
            {
                if (trans != null) trans.Rollback();
                if (e is ServerInfoException) throw e;
                else
                {
                    throw new ObjectDBException("Problem creating new category!", this, e);
                }

            }
            parentCategory = null;
            realFileName = null;
            shared = false;
        }
Esempio n. 13
0
        /// <summary>
        /// To be used with transactions.
        /// Saves the category given and all the images 
        /// inside the folder name.
        /// No deep loading.
        /// </summary>
        /// <param name="cmd">The command object asociated with a transaction.</param>
        /// <param name="cat">the category to be saved</param>
        /// <param name="folderName">the folder full path</param>
        internal static void saveSingleFolder(SqlCommand cmd, 
            Category cat, string folderName)
        {
            int lastSeparatorIndex = folderName.LastIndexOf(@"\");
            cat.Name = folderName.Substring(lastSeparatorIndex + 1);
            saveCategory(cmd, cat);// transactional save

            string[] fileNames = getFileNames(folderName);
            if (fileNames.Length <= 0)
            {
                // this will rowback the transaction
                throw new ObjectDBException("No images in this folder!", cat);
            }
            for (int i = 0; i < fileNames.Length; i++)
            {
                Image img = new Image(fileNames[i], cat);
                saveImage(cmd, img, fileNames[i]);// transactional save
            }
        }
Esempio n. 14
0
 private void MenuItemAddFolder_Click(object sender, System.EventArgs e)
 {
     TreeNode selectedNode = MainFormTreeView.SelectedNode;
     if (selectedNode != null && selectedNode.Tag is Category)
     {
         Category item = selectedNode.Tag as Category;
         if (FolderChooserDialog.ShowDialog() == DialogResult.OK && item != null)
         {
             string realFileName = FolderChooserDialog.SelectedPath;
             try
             {
                 // load the directory and all the images inside
                 Category newCat = new Category(item, realFileName, false);
                 createNewCategoryNode(newCat, selectedNode);
             }
             catch (ServerInfoException ex)
             {
                 MessageBox.Show(this, ex.Message, "Info",
                     MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
             catch (ObjectDBException ex)
             {
                 string message = ex.Message;
                 if (ex.InnerException != null)
                 {
                     message += "\n" + ex.InnerException.Message;
                 }
                 MessageBox.Show(this, message, "Error",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
     }
     else
     {
         MessageBox.Show(this, "Please select a target categorty!", "Warning",
             MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Esempio n. 15
0
        /// <summary>
        /// Uses the same conn object (dosen't close the conn)
        /// and dosen't wrap the exceptions. 
        /// To be used only in this class.
        /// </summary>
        /// <param name="id">the identifier of the current user</param>
        protected void load(Int32 id, SqlCommand cmd)
        {
            cmd.CommandText = "sp_GetUser";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@User_Id", id);

            SqlDataReader reader = cmd.ExecuteReader();
            Int32 rootCategoryId = -1;
            if (reader.Read())
            {
                alias = (string)reader["alias"];
                userName = (string)reader["user_name"];
                userSurname = (string)reader["user_surname"];
                rootCategoryId = (Int32)reader["root_category_id"];
            }
            reader.Close();

            if (rootCategoryId != -1)
            {
                rootCategory = new Category(rootCategoryId, cmd, this);
            }
            this.id = id;
        }
Esempio n. 16
0
 public Category(Category parentCategory)
 {
     this.parent = parentCategory;
     this.user = parentCategory.User;
 }
Esempio n. 17
0
        private void loadSharedCategories(SqlCommand cmd, ArrayList dbItems)
        {
            cmd.CommandText = "SELECT * FROM v_RootSharedCategories";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Clear();

            SqlDataReader reader = cmd.ExecuteReader();
            ArrayList catIds = new ArrayList();
            while (reader.Read())
            {
                Int32 catId = (Int32)reader["category_id"];
                catIds.Add(catId);
            }
            reader.Close();

            foreach (Int32 catId in catIds)
            {
                Category cat = new Category(catId, cmd, this);
                dbItems.Add(cat);
            }
        }
Esempio n. 18
0
 private void NewCategory_OnClick(System.Object sender, System.EventArgs e)
 {
     TreeNode selectedNode = MainFormTreeView.GetNodeAt(treeMouseLocation);
     if (selectedNode != null && selectedNode.Tag is Category)
     {
         Category parent = selectedNode.Tag as Category;
         try
         {
             Category newCat = new Category(parent);
             newCat.Name = "New Category";
             newCat.save();
             createNewCategoryNode(newCat, selectedNode);
         }
         catch (ServerInfoException ex)
         {
             MessageBox.Show(this, ex.Message, "Info",
                 MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         catch (ObjectDBException ex)
         {
             MessageBox.Show(this, ex.Message, "Error",
                 MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }