/// <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)); } }
/// <summary> /// Import a model system from file. /// </summary> /// <param name="user">The user issuing the import file system command.</param> /// <param name="modelSystemFilePath">The path to the file to import.</param> /// <param name="modelSystemName">The name to give the model system within this project.</param> /// <param name="header">A resulting header for the newly imported model system.</param> /// <param name="error">The error message if the operation fails.</param> /// <returns>True if the operation succeeds, false otherwise with an error message.</returns> public bool ImportModelSystem(User user, string modelSystemFilePath, string modelSystemName, out ModelSystemHeader?header, out CommandError?error) { header = null; if (user is null) { throw new ArgumentNullException(nameof(user)); } if (string.IsNullOrWhiteSpace(modelSystemFilePath)) { throw new ArgumentException(nameof(modelSystemFilePath)); } if (string.IsNullOrWhiteSpace(modelSystemName)) { throw new ArgumentException(nameof(modelSystemName)); } try { using var archive = ZipFile.OpenRead(modelSystemFilePath); lock (_sessionLock) { if (!HasAccess(user)) { error = new CommandError("The user that issued the command does not have access to this project.", true); return(false); } if (Project.ContainsModelSystem(modelSystemName)) { error = new CommandError("A model system with that name already exists!"); return(false); } if (!ModelSystemFile.LoadModelSystemFile(modelSystemFilePath, out var msf, out error)) { return(false); } return(Project.AddModelSystemFromModelSystemFile(modelSystemName, msf !, out header, out error)); } } catch (InvalidDataException e) { error = new CommandError(e.Message); } catch (IOException e) { error = new CommandError(e.Message); } return(false); }