public ActionResult AddLocalPackage(HttpPostedFileBase file)
        {
            if(file == null)
            {
                ModelState.AddModelError("PackageFileValidation", "No file selected. Please select a package file to upload.");
                return LocalRepository();
            }

            if (!Path.GetExtension(file.FileName).EndsWith("nupkg"))
            {
                ModelState.AddModelError("PackageFileValidation", "The file uploaded is not a valid package file, only Nuget packages are supported");
                return LocalRepository();
            }

            IPackage package;
            try
            {
                package = new ZipPackage(file.InputStream);
            }
            catch (Exception ex)
            {
                LogHelper.Error<PackagingEditorController>("Package could not be unziped.", ex);

                ModelState.AddModelError("PackageFileValidation", "The Nuget package file uploaded could not be read");
                return LocalRepository();
            }

            try
            {
                var fileName = Path.Combine(BackOfficeRequestContext.PackageContext.LocalPackageManager.SourceRepository.Source, file.FileName);
                file.SaveAs(fileName);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("PackageFileValidation", "The package file could not be saved. " + ex.Message);
                return LocalRepository();
            }

            if(!string.IsNullOrWhiteSpace(Request.Form["autoinstall"]))
            {
                BackOfficeRequestContext.PackageContext.LocalPackageManager.InstallPackage(package, false);

                var logger = new PackageLogger(BackOfficeRequestContext, HttpContext, package);
                var installation = new PackageInstallation(BackOfficeRequestContext, HttpContext, package);

                //Copy files from package folder to destination and log results
                var fileResults = installation.CopyPackageFiles();
                foreach (var info in fileResults)
                {
                    logger.Log(info.IsCopiable, info.Message());
                }
                //Import data and log results
                var dataResults = installation.ImportData();
                foreach (var attributeType in dataResults.AttributeTypes)
                {
                    logger.Log(attributeType.IsImportable, string.Format("AttributeType {0}", attributeType.ObjectId.Value.ToString()));
                }
                foreach (var schema in dataResults.Schemas)
                {
                    logger.Log(schema.IsImportable, string.Format("Schema {0}", schema.ObjectId.Value.ToString()));
                }
                foreach (var schemaRelation in dataResults.SchemaRelations)
                {
                    logger.Log(schemaRelation.IsImportable, string.Format("Schema Relation {0}", schemaRelation.ObjectId.Value.ToString()));
                }
                foreach (var entity in dataResults.Entities)
                {
                    logger.Log(entity.IsImportable, string.Format("Entity {0}", entity.ObjectId.Value.ToString()));
                }
                foreach (var entityRelation in dataResults.EntityRelations)
                {
                    logger.Log(entityRelation.IsImportable, string.Format("Entity Relation {0}", entityRelation.ObjectId.Value.ToString()));
                }
                foreach (var language in dataResults.Languages)
                {
                    logger.Log(language.IsImportable, string.Format("Language {0}", language.ObjectId.Value.ToString()));
                }

                //Notifications.Add(new NotificationMessage(package.Title + " has been installed", "Package installed", NotificationType.Success));
                //SuccessfulOnRedirectAttribute.EnsureRouteData(this, "id", package.Id);

                return RedirectToAction("RecycleApplication", new { id = package.Id, state = PackageInstallationState.Installing });
            }

            Notifications.Add(new NotificationMessage(package.Title + " added to local repository", "Package added", NotificationType.Success));
            SuccessfulOnRedirectAttribute.EnsureRouteData(this, "id", package.Id);

            return RedirectToAction("LocalRepository");
        }
        public ActionResult ManagePackage()
        {
            var toInstallVal = ValueProvider.GetValue("install") == null 
                ? new object[] { } 
                : ValueProvider.GetValue("install").AttemptedValue.Split('-');
            var toInstall = toInstallVal.Length > 0 ? toInstallVal[0] : null;

            var toUninstallVal = ValueProvider.GetValue("uninstall") == null
                ? new object[] { }
                : ValueProvider.GetValue("uninstall").AttemptedValue.Split('-');
            var toUninstall = toUninstallVal.Length > 0 ? toUninstallVal[0] : null;

            var toRemoveVal = ValueProvider.GetValue("remove") == null
                ? new object[] { }
                : ValueProvider.GetValue("remove").AttemptedValue.Split('-');
            var toRemove = toRemoveVal.Length > 0 ? toRemoveVal[0] : null;

            if (toInstall != null)
            {
                //get the package from the source
                var package = BackOfficeRequestContext.PackageContext.LocalPackageManager.SourceRepository.FindPackage(toInstall.ToString());
                var version = Version.Parse(toInstallVal[1].ToString());
                BackOfficeRequestContext.PackageContext.LocalPackageManager.InstallPackage(package.Id, version, false);

                var logger = new PackageLogger(BackOfficeRequestContext, HttpContext, package);
                var installation = new PackageInstallation(BackOfficeRequestContext, HttpContext, package);

                //Copy files from package folder to destination and log results
                var fileResults = installation.CopyPackageFiles();
                foreach (var info in fileResults)
                {
                    logger.Log(info.IsCopiable, info.Message());
                }
                //Import data and log results
                var dataResults = installation.ImportData();
                foreach (var attributeType in dataResults.AttributeTypes)
                {
                    logger.Log(attributeType.IsImportable, string.Format("AttributeType {0}", attributeType.ObjectId.Value.ToString()));
                }
                foreach (var schema in dataResults.Schemas)
                {
                    logger.Log(schema.IsImportable, string.Format("Schema {0}", schema.ObjectId.Value.ToString()));
                }
                foreach (var schemaRelation in dataResults.SchemaRelations)
                {
                    logger.Log(schemaRelation.IsImportable, string.Format("Schema Relation {0}", schemaRelation.ObjectId.Value.ToString()));
                }
                foreach (var entity in dataResults.Entities)
                {
                    logger.Log(entity.IsImportable, string.Format("Entity {0}", entity.ObjectId.Value.ToString()));
                }
                foreach (var entityRelation in dataResults.EntityRelations)
                {
                    logger.Log(entityRelation.IsImportable, string.Format("Entity Relation {0}", entityRelation.ObjectId.Value.ToString()));
                }
                foreach (var language in dataResults.Languages)
                {
                    logger.Log(language.IsImportable, string.Format("Language {0}", language.ObjectId.Value.ToString()));
                }

                //Notifications.Add(new NotificationMessage(package.Title + " has been installed", "Package installed", NotificationType.Success));
                SuccessfulOnRedirectAttribute.EnsureRouteData(this, "id", package.Id);

                logger.Persist();

                return RedirectToAction("RecycleApplication", new { id = package.Id, state = PackageInstallationState.Installing });
            }

            if (toUninstall != null)
            {
                //get the package from the installed location
                var nugetPackage = BackOfficeRequestContext.PackageContext.LocalPackageManager.LocalRepository.FindPackage(toUninstall.ToString());
                var packageFolderName = BackOfficeRequestContext.PackageContext.LocalPathResolver.GetPackageDirectory(nugetPackage);
                
                //execute some tasks
                var taskExeContext = _packageInstallUtility.GetTaskExecutionContext(nugetPackage, packageFolderName, PackageInstallationState.Uninstalling, this);
                _packageInstallUtility.RunPrePackageUninstallActions(taskExeContext, packageFolderName);

                BackOfficeRequestContext.PackageContext.LocalPackageManager.UninstallPackage(nugetPackage, false, false);

                //Notifications.Add(new NotificationMessage(nugetPackage.Title + " has been uninstalled", "Package uninstalled", NotificationType.Success));
                SuccessfulOnRedirectAttribute.EnsureRouteData(this, "id", nugetPackage.Id);

                return RedirectToAction("RecycleApplication", new { id = nugetPackage.Id, state = PackageInstallationState.Uninstalling });
            }
            
            if (toRemove != null)
            {

                var package = BackOfficeRequestContext.PackageContext.LocalPackageManager.SourceRepository.FindPackage(toRemove.ToString());
                var packageFile = BackOfficeRequestContext.PackageContext.LocalPathResolver.GetPackageFileName(package);


                //delete the package folder... this will check if the file exists by just the package name or also with the version
                if (BackOfficeRequestContext.PackageContext.LocalPackageManager.FileSystem.FileExists(packageFile))
                {
                    BackOfficeRequestContext.PackageContext.LocalPackageManager.FileSystem.DeleteFile(
                        Path.Combine(BackOfficeRequestContext.PackageContext.LocalPackageManager.SourceRepository.Source, packageFile));    
                }
                else
                {
                    var fileNameWithVersion = packageFile.Substring(0, packageFile.IndexOf(".nupkg")) + "." + package.Version + ".nupkg";
                    BackOfficeRequestContext.PackageContext.LocalPackageManager.FileSystem.DeleteFile(
                        Path.Combine(BackOfficeRequestContext.PackageContext.LocalPackageManager.SourceRepository.Source, fileNameWithVersion));    
                }
                
                Notifications.Add(new NotificationMessage(package.Title + " has been removed from the local repository", "Package removed", NotificationType.Success));
                SuccessfulOnRedirectAttribute.EnsureRouteData(this, "id", package.Id);

                return RedirectToAction("LocalRepository");

            }


            return HttpNotFound();
        }