/// <summary> /// This getResourceByContainerName() method returns the resourcemodel of given container name(unique) /// </summary> /// <param name="connection"></param> /// <param name="containerName"></param> /// <returns>resource id</returns> public Model.AzureContainerModel getResourceByContainerName(SqlConnection connection, String containerName) { Model.AzureContainerModel resource = new Model.AzureContainerModel(); try { command = new SqlCommand("SELECT RID, GIVENNAME, OWNER, CONTAINERNAME FROM CONTAINERS WHERE CONTAINERNAME = @CONTAINERNAME"); command.CommandType = CommandType.Text; command.Connection = connection; command.Parameters.AddWithValue("@CONTAINERNAME", containerName); reader = command.ExecuteReader(); //If no container is owned by user or we can say user has deleted his container if (!reader.HasRows) { throw new DBLikeExceptions.CloudContainerNotFoundException(); } while (reader.Read()) { resource.setRid(reader.GetInt32(0)); resource.setGivenName(reader.GetString(1)); resource.setOwner(reader.GetInt32(2)); resource.setContainerName(reader.GetString(3)); } reader.Close(); return(resource); } catch (Exception e) { Console.WriteLine("getResourceByContainerName in ResourceM class says->" + e.Message); } return(null); }
/// <summary> /// This getResourceByGivenName() method returns the resourcemodel by given container name and its owner /// </summary> /// <param name="connection"></param> /// <param name="uid"></param> /// <param name="givenName"></param> /// <returns>Model.ResourceModel</returns> public Model.AzureContainerModel getResourceByGivenName(SqlConnection connection, int uid, String givenName) { Model.AzureContainerModel resource = new Model.AzureContainerModel(); try { command = new SqlCommand("SELECT RID, GIVENNAME, OWNER, CONTAINERNAME FROM CONTAINERS WHERE OWNER = @OWNER AND GIVENNAME = @GIVENNAME"); command.CommandType = CommandType.Text; command.Connection = connection; command.Parameters.AddWithValue("@OWNER", uid); command.Parameters.AddWithValue("@GIVENNAME", givenName); reader = command.ExecuteReader(); //If no container is owned by user or we can say user has deleted his container if (!reader.HasRows) { return(null); } while (reader.Read()) { resource.setRid(reader.GetInt32(0)); resource.setGivenName(reader.GetString(1)); resource.setOwner(reader.GetInt32(2)); resource.setContainerName(reader.GetString(3)); } reader.Close(); return(resource); } catch (SqlException ex) { Console.WriteLine("ResourceM class: getResourceByGivenname method Exception->" + ex.Message); } return(null); }
/// <summary> /// This method should return TRUE if user account created successfully /// FALSE if user record already exist in database /// THE CONTAINER IS NAMED AS USERID [unique identifier] /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public Boolean createUser(String fullname, String username, String password) { Model.UserModel user = new Model.UserModel(); DBManager.UserM u = new DBManager.UserM(); user = u.getUserRecord(connection, username); //if user already exist, false if (!(user == null)) { return(false); } //Insert record into USERS table u.insertIntoUsers(connection, fullname, username, password); user = u.getUserRecord(connection, username); //default private container created //Container get created by the unique user id Console.WriteLine("Container created with " + user.getUid() + " name"); Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer myContainer = BlobStorageManager.BlobStorage.getCloudBlobContainer((user.getUid()).ToString()); myContainer.CreateIfNotExists(); String containerName = (user.getUid()).ToString(); //Insert record into CONTAINERS table DBManager.ResourceM r = new DBManager.ResourceM(); Console.WriteLine(containerName + "////" + user.getUid()); Model.AzureContainerModel re = new Model.AzureContainerModel(); re.setOwner(user.getUid()); re.setContainerName(containerName); re.setGivenName(user.getEmailId()); //Changed here since server front end is considering private container name as user email id Boolean res = r.insertIntoResources(connection, re); return(res); }
/// <summary> /// This createSharedContainer() method creates the shared container which is owned by given userid /// CloudContainerAlreadyExistException if already exists /// </summary> /// <param name="userid"></param> /// <param name="givenname"></param> /// <returns>id for new container on success, else -1</returns> public int createSharedContainer(int userid, String givenname) { DBManager.ResourceM resmgr = new DBManager.ResourceM(); Model.AzureContainerModel resource = new Model.AzureContainerModel(); try { resource = resmgr.getResourceByGivenName(connection, userid, givenname); Model.UserModel user = new Model.UserModel(); DBManager.UserM umgr = new DBManager.UserM(); user = umgr.getUserRecordByID(connection, userid); File f = new File(); String[] list = f.list(user.getEmailId(), ""); for (int i = 0; i < list.Length; i++) { String[] tokens = list[i].Split(':'); if (tokens.Length == 3) { if (tokens[2].Equals(givenname)) //there exist the container with same name { throw new DBLikeExceptions.CloudContainerAlreadyExistException(); } } else if (tokens.Length == 2) { if (tokens[1].Equals(givenname)) //there exist the container with same name { throw new DBLikeExceptions.CloudContainerAlreadyExistException(); } } } if (resource != null) { throw new DBLikeExceptions.CloudContainerAlreadyExistException(); } DBManager.CListM cmgr = new DBManager.CListM(); //cmgr.insertIntoCList(); String[] sharedContainers = cmgr.getSharedContainersOwnedByUser(connection, userid); int counter = 001; //Console.WriteLine("shareddddddddd"+sharedContainers.Length); if (sharedContainers != null) { int size = sharedContainers.Length; String[] containerSuffix = new String[size]; String[] temp; //SHOULD NOT ALLOW USER TO GIVE SAME NAME TO DIFFERENT SHARED CONTAINER for (int i = 0; i < size; i++) { temp = sharedContainers[i].Split(':'); int pos = temp[0].IndexOf("d") + 1; Console.WriteLine("Position" + pos); String scnt = temp[0].Substring(pos); containerSuffix[i] = new string(scnt.Where(char.IsDigit).ToArray()); //no need but leaving as it is } var result = (from m in containerSuffix select m).Max(); Console.WriteLine("result" + result); counter = Int32.Parse(result) + 1; Console.WriteLine("New counter--" + counter); } String containerName = userid + "shared" + counter; CloudBlobContainer myContainer = BlobStorageManager.BlobStorage.getCloudBlobContainer(containerName); myContainer.CreateIfNotExists(); Model.AzureContainerModel resource1 = new Model.AzureContainerModel(); //Insert record into CONTAINERS table resource1.setOwner(userid); resource1.setContainerName(containerName); resource1.setGivenName(givenname); Boolean res = resmgr.insertIntoResources(connection, resource1); if (res) { int rid1 = getContainerID(userid, givenname); return(rid1); } return(-1); } catch (SqlException) { return(-1); } }