Пример #1
0
        /// <summary>
        /// Rename project
        /// </summary>
        /// <param name="projectConfig">Project configuration</param>
        /// <param name="newName">New project name</param>
        static public void RenameProject(ProjectConfiguration projectConfig, string newName, ProjectCatalog catalog)
        {
            // check if project name is empty
            if (newName.Length == 0)
            {
                throw new NotSupportedException(Properties.Resources.ProjectNameCannotBeEmpty);
            }

            // check that project name is correct
            if (!FileHelpers.IsFileNameCorrect(newName))
            {
                throw new NotSupportedException(Properties.Resources.ProjectNameIsNotCorrect);
            }

            // check if project with such name already exists
            bool nameExisted = false;

            foreach (ProjectConfiguration cfg in catalog.Projects)
            {
                if (newName.Equals(cfg.Name, StringComparison.OrdinalIgnoreCase))
                {
                    nameExisted = true;
                }
            }
            if (nameExisted)
            {
                throw new NotSupportedException(Properties.Resources.ProjectNameIsAlreadyExists);
            }

            // todo: check that you have write rename/delete access to the files
            string newProjectPath = Path.Combine(projectConfig.FolderPath, newName);
            //if (!FileHelpers.CheckWriteAccess(newProjectPath))
            //    throw new NotSupportedException(Properties.Resources.WriteAccessDenied);

            string oldDatabasePath = projectConfig.DatabasePath;
            string newDBFileName   = ProjectConfiguration.GetDatabaseFileName(newName);
            string oldDBFileName   = Path.GetFileName(oldDatabasePath);
            int    indexStart      = oldDatabasePath.Length - oldDBFileName.Length;
            string newDBFilePath   = oldDatabasePath.Remove(indexStart);

            newDBFilePath = newDBFilePath + newDBFileName;

            string newDBAbsolutePath = ProjectConfiguration.GetDatabaseAbsolutPath(projectConfig.FolderPath, newDBFilePath);

            File.Move(oldDatabasePath, newDBAbsolutePath);

            string oldProjectFilePath = projectConfig.FilePath;

            projectConfig.Name         = newName;
            projectConfig.DatabasePath = newDBFilePath;
            projectConfig.Save(projectConfig.FilePath);
            File.Delete(oldProjectFilePath);
        }
Пример #2
0
        /// <summary>
        /// Creates project.
        /// </summary>
        /// <param name="name">Project's name.</param>
        /// <param name="folderPath">Project's folder path.</param>
        /// <param name="description">Proejct's description.</param>
        /// <returns>Created project</returns>
        static public Project CreateProject(string name, string folderPath, string description, CapacitiesInfo capacitiesInfo,
                                            OrderCustomPropertiesInfo orderCustomPropertiesInfo, FuelTypesInfo fuelTypesInfo /*serivces*/,
                                            IProjectSaveExceptionHandler logHandler)
        {
            WorkspaceHandler workspaceHandler = new WorkspaceHandler(logHandler);

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (folderPath == null)
            {
                throw new ArgumentNullException("folderPath");
            }
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }

            if (!CheckMaximumLengthConstraint(orderCustomPropertiesInfo))
            {
                throw new ApplicationException(Properties.Messages.Error_OrderCustomPropTooLong);
            }

            bool   isDBCreated = false;
            string dbPath      = "";

            try
            {
                name = name.Trim();

                // make project configuration path
                string projCfgPath = System.IO.Path.Combine(folderPath, name);
                projCfgPath += ProjectConfiguration.FILE_EXTENSION;

                string databasePath = ProjectConfiguration.GetDatabaseFileName(name);
                // create project configuration
                ProjectConfiguration projConfig = new ProjectConfiguration(name, folderPath, description,
                                                                           databasePath, null, DateTime.Now);

                projConfig.Validate();

                projConfig.Save();

                dbPath = projConfig.DatabasePath;

                DatabaseEngine.DeleteDatabase(dbPath);

                // create database
                DatabaseEngine.CreateDatabase(dbPath, SchemeVersion.CreationScript);
                isDBCreated = true;

                Project project = new Project(projCfgPath, capacitiesInfo,
                                              orderCustomPropertiesInfo, workspaceHandler);

                foreach (FuelTypeInfo fuelTypeInfo in fuelTypesInfo)
                {
                    FuelType projectFuelType = new FuelType();
                    projectFuelType.Name        = fuelTypeInfo.Name;
                    projectFuelType.Price       = fuelTypeInfo.Price;
                    projectFuelType.Co2Emission = fuelTypeInfo.Co2Emission;
                    project.FuelTypes.Add(projectFuelType);
                }

                project.Save();

                workspaceHandler.Handled = true;

                return(project);
            }
            catch (Exception ex)
            {
                Logger.Info(ex);
                if (isDBCreated)
                {
                    DatabaseEngine.DeleteDatabase(dbPath);
                }

                throw;
            }
        }