Beispiel #1
0
        /// <summary>
        /// Creates a new group with the name passed as parameter if it does not exist.
        /// </summary>
        /// <param name="name">The name of the new group.</param>
        /// <returns>True if the creation was successful, false otherwise.</returns>
        public bool CreateGroup(string name)
        {
            if (!_registered)
            {
                return(false);
            }
            if (String.IsNullOrEmpty(name))
            {
                return(false);
            }

            if (name == defGroup)
            {
                return(false);
            }
            Storage.StorageManager sto        = new Storage.StorageManager();
            List <string>          groupNames = new List <string>();

            groupNames.Add(name);
            List <Storage.Group> results = sto.addGroups(groupNames, _userId);

            if (results == null)
            {
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new group with the name passed to the parameter if the group does not exist
        /// and adds the contacts passed to the parameter.
        /// </summary>
        /// <param name="name">The group name to create.</param>
        /// <param name="contactList"> The list of contacts to be added to the group.</param>
        /// <returns> True if the group was successfully created and contacts have been added, false otherwise.</returns>
        public bool CreateGroup(string name, List <Contact> contactList)
        {
            if (!_registered)
            {
                return(false);
            }
            if (String.IsNullOrEmpty(name) || contactList == null)
            {
                return(false);
            }

            if (name == defGroup)
            {
                return(false);
            }
            Storage.StorageManager sto        = new Storage.StorageManager();
            List <string>          groupNames = new List <string>();

            groupNames.Add(name);
            List <Storage.Group> groups = sto.addGroups(groupNames, _userId);

            if (groups == null || groups.Count == 0)
            {
                return(false);
            }

            List <Storage.Contact> contacts = ConvertToStorageContact(contactList);

            Storage.Group defStoGroup = null;
            foreach (Storage.Group g in sto.getGroupsByUserID(_userId))
            {
                if (g.nameGroup.Equals(defGroup))
                {
                    defStoGroup = g;
                    break;
                }
            }
            List <Storage.GroupContact> result2 = sto.addContactsToGroup(contacts, groups[0]);

            if (result2 == null)
            {
                return(false);
            }

            sto.removeContactsFromGroup(contacts, defStoGroup);

            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Register the user on the database saving all external accounts that the user subscribed. On success, UserID property will be changed.
        /// </summary>
        /// <param name="Nickname">The nickname of the user</param>
        /// <param name="Email">The email of the user</param>
        /// <returns>True on succes or if already registered, false otherwise (in this case nothing is saved on the database)</returns>
        public RegisterMeResult RegisterMe(string Nickname, string Email, out string messageError)
        {
            messageError = "";
            if (_registered)
            {
                return(RegisterMeResult.OK);
            }
            if (String.IsNullOrEmpty(Nickname))
            {
                messageError = "Nickname is not valid.";
                return(RegisterMeResult.E_EMAIL);
            }
            if (String.IsNullOrEmpty(Email))
            {
                messageError = "Email is not valid.";
                return(RegisterMeResult.E_EMAIL);
            }
            try
            {
                new System.Net.Mail.MailAddress(Email);
            }
            catch (Exception)
            {
                messageError = "Email is not valid.";
                return(RegisterMeResult.E_EMAIL);
            }
            Storage.StorageManager sto      = new Storage.StorageManager();
            Storage.User           tempUser = sto.addUser(Nickname, Email);
            if (tempUser == null)
            {
                messageError = "An error has occured saving the new user on the database.";
                return(RegisterMeResult.E_GENERIC);
            }
            _userId = tempUser.userID;

            List <string> groupNames = new List <string>();

            groupNames.Add(defGroup);

            List <Storage.Group> groupResult = sto.addGroups(groupNames, _userId);

            if (groupResult == null)
            {
                //rollback
                sto.removeEntity <Storage.User>(_userId);
                _userId = -1;
                {
                    messageError = "An error has occured during the creation of default group.";
                    return(RegisterMeResult.E_GENERIC);
                }
            }
            // Save all externalAccount
            foreach (int serviceID in _loggedServices.Keys)
            {
                Storage.ExternalAccount extAcc = sto.addExternalAccount(_userId, _loggedServices[serviceID].getUsername(), serviceID);
                if (extAcc == null)
                {
                    //rollback
                    sto.removeEntity <Storage.User>(_userId);
                    sto.removeEntity <Storage.Group>(groupResult.First().groupID);
                    _userId      = -1;
                    messageError = "An error has occured saving currently logged accounts.";
                    return(RegisterMeResult.E_GENERIC);
                }
            }

            _username   = Nickname;
            _email      = Email;
            _registered = true;

            return(RegisterMeResult.OK);
        }