Пример #1
0
 /// <summary>
 /// Rename a model system.  The model system can not be currently
 /// being edited.
 /// </summary>
 /// <param name="user">The user issuing the command.</param>
 /// <param name="newName">The new name of the model system.</param>
 /// <param name="error">An error message if the operation fails.</param>
 /// <returns>True if the operation succeeds, false otherwise with an error message.</returns>
 public bool RenameModelSystem(User user, ModelSystemHeader modelSystem, string newName, out CommandError?error)
 {
     if (user is null)
     {
         throw new ArgumentNullException(nameof(user));
     }
     if (modelSystem is null)
     {
         throw new ArgumentNullException(nameof(modelSystem));
     }
     if (string.IsNullOrWhiteSpace(newName))
     {
         error = new CommandError("The name of the model system must not be blank.");
         return(false);
     }
     lock (_sessionLock)
     {
         if (!Project.CanAccess(user))
         {
             error = new CommandError("The user can not access this project.", true);
             return(false);
         }
         return(Project.RenameModelSystem(modelSystem, newName, out error));
     }
 }
Пример #2
0
        /// <summary>
        /// Exports a model system to file.
        /// </summary>
        /// <param name="user">The user that is issuing the command.</param>
        /// <param name="modelSystemHeader">The model system to export.</param>
        /// <param name="exportPath">The location to export the model system to.</param>
        /// <param name="error">An error message if the operation fails.</param>
        /// <returns>True if the operation succeeds, false otherwise with error message.</returns>
        public bool ExportModelSystem(User user, ModelSystemHeader modelSystemHeader, string exportPath, out CommandError?error)
        {
            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (modelSystemHeader is null)
            {
                throw new ArgumentNullException(nameof(modelSystemHeader));
            }

            if (string.IsNullOrWhiteSpace(exportPath))
            {
                error = new CommandError("The path to save the model system to must not be empty.");
                return(false);
            }
            lock (_sessionLock)
            {
                if (!Project.CanAccess(user))
                {
                    error = new CommandError("The user does not have access to the project.", true);
                    return(false);
                }
                if (_activeSessions.TryGetValue(modelSystemHeader, out var mss))
                {
                    error = new CommandError("The model system is currently being edited and can not be exported.");
                    return(false);
                }
                return(ModelSystemFile.ExportModelSystem(this, user, modelSystemHeader, exportPath, out error));
            }
        }
Пример #3
0
 /// <summary>
 /// Create a model system session allowing for the editing of a model system.
 /// </summary>
 /// <param name="user">The user that is requesting access.</param>
 /// <param name="modelSystemHeader">The model system reference to load.</param>
 /// <param name="session">The resulting session.</param>
 /// <param name="error">An error message if the operation fails.</param>
 /// <returns>True if the operation succeeds, false otherwise with an error message.</returns>
 public bool EditModelSystem(User user, ModelSystemHeader modelSystemHeader, out ModelSystemSession?session, out CommandError?error)
 {
     if (user is null)
     {
         throw new ArgumentNullException(nameof(user));
     }
     if (modelSystemHeader is null)
     {
         throw new ArgumentNullException(nameof(modelSystemHeader));
     }
     session = null;
     lock (_sessionLock)
     {
         if (!Project.CanAccess(user))
         {
             error = new CommandError("The given user does not have access to this project!", true);
             return(false);
         }
         if (!Project.ContainsModelSystem(modelSystemHeader))
         {
             error = new CommandError("The model system header provided does not belong to this project!");
             return(false);
         }
         if (!_activeSessions.TryGetValue(modelSystemHeader, out session))
         {
             if (ModelSystem.Load(this, modelSystemHeader, out session, out error))
             {
                 _activeSessions.Add(modelSystemHeader, session !);
                 Interlocked.Increment(ref _references);
                 return(true);
             }
             return(false);
         }
         else
         {
             session.AddReference();
         }
         error = null;
         return(true);
     }
 }
Пример #4
0
        /// <summary>
        /// Remove a model system from the project.
        /// </summary>
        /// <param name="user">The user issuing the command.</param>
        /// <param name="modelSystem">The model system to remove.</param>
        /// <param name="error">An error message if the operation fails.</param>
        /// <returns>True if the operation succeeds, false otherwise with an error message.</returns>
        public bool RemoveModelSystem(User user, ModelSystemHeader modelSystem, out CommandError?error)
        {
            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (modelSystem is null)
            {
                throw new ArgumentNullException(nameof(modelSystem));
            }

            lock (_sessionLock)
            {
                if (Project.Owner != user)
                {
                    error = new CommandError("You can not remove a model system that you are not the owner of.", true);
                    return(false);
                }
                if (_activeSessions.ContainsKey(modelSystem))
                {
                    error = new CommandError("You can not remove a model system that is currently being edited!");
                    return(false);
                }
                var modelSystemFile = modelSystem.ModelSystemPath;
                if (!Project.Remove(this, modelSystem, out error))
                {
                    return(false);
                }
                try
                {
                    File.Delete(modelSystemFile);
                }
#pragma warning disable CA1031 // If the model system file is already gone, then the operation actually succeeded.
                catch (IOException)
                {
                }
#pragma warning restore CA1031 // Do not catch general exception types
                return(true);
            }
        }
Пример #5
0
 /// <summary>
 /// Create a new model system with the given name.  The name must be unique.
 /// </summary>
 /// <param name="modelSystemName">The name of the model system (must be unique within the project).</param>
 /// <param name="modelSystem">The resulting model system session</param>
 /// <param name="error">An error message if the operation fails.</param>
 /// <returns>True if the operation succeeds, false otherwise with an error message.</returns>
 public bool CreateNewModelSystem(User user, string modelSystemName, out ModelSystemHeader?modelSystem, out CommandError?error)
 {
     modelSystem = null;
     if (!ProjectController.ValidateProjectName(modelSystemName, out error))
     {
         return(false);
     }
     lock (_sessionLock)
     {
         if (!Project.CanAccess(user))
         {
             error = new CommandError("The user does not have access to this project!", true);
             return(false);
         }
         if (Project.ContainsModelSystem(modelSystemName))
         {
             error = new CommandError("A model system with this name already exists.");
             return(false);
         }
         modelSystem = new ModelSystemHeader(Project, modelSystemName);
         return(Project.Add(this, modelSystem, out error));
     }
 }