예제 #1
0
        public ActionResult AddCodeFileCurationTasks(FileViewModel model)
        {
            var logger = LogManager.GetLogger("FileController");

            using (var db = ApplicationDbContext.Create())
            {
                var user = db.Users
                           .Where(x => x.UserName == User.Identity.Name)
                           .FirstOrDefault();
                if (user == null)
                {
                    return(RedirectToAction("Index"));
                }

                if (!user.IsAdministrator)
                {
                    throw new HttpException(403, "This is only allowed for administrators.");
                }

                // Fetch the appropriate ManagedFile by ID.
                Guid id   = model.Id;
                var  file = GetFile(id, db);

                // Add the code file tasks, if needed.
                logger.Debug("Updating file tasks");
                TaskHelpers.AddProcessingTasksForFile(file, file.CatalogRecord, db, addAllCodeTasks: true);
                logger.Debug("Done updating file tasks");

                // Log the editing of the file.
                var log = new Event()
                {
                    EventType            = EventTypes.EditManagedFile,
                    Timestamp            = DateTime.UtcNow,
                    User                 = user,
                    RelatedCatalogRecord = file.CatalogRecord,
                    Title                = "Edit a File",
                    Details              = "Added code file curation tasks"
                };
                log.RelatedManagedFiles.Add(file);
                db.Events.Add(log);

                db.SaveChanges();

                return(RedirectToAction("General", new { id = file.Id }));
            }
        }
예제 #2
0
        public void IngestIntoCurator(Group group, HttpRequestBase request, string userName)
        {
            MefConfig.RegisterMef(string.Empty);

            // Load and populate all the StudyUnits in the group.
            using (var db = new ApplicationDbContext())
            {
                var    organization = OrganizationHelper.GetOrganizationByHost(request, db);
                string storagePath  = SettingsHelper.GetProcessingDirectory(organization, db);
                var    user         = db.Users.Where(x => x.UserName == userName)
                                      .Include(x => x.Organizations)
                                      .FirstOrDefault();


                // For every StudyUnit
                int i = 1;
                foreach (StudyUnit study in group.StudyUnits)
                {
                    Console.WriteLine("Study " + i.ToString());

                    // Create a CatalogRecord and add it to the database.
                    var record = new CatalogRecord();
                    record.Id           = study.Identifier;
                    record.Organization = organization;

                    // See if we can find a user with the contributor name.
                    // Otherwise just use the current user.
                    string   contributorName = study.DublinCoreMetadata.Contributor.Best;
                    string[] parts           = contributorName.Split(new char[] { ' ' });
                    if (parts.Length == 2)
                    {
                        string first = parts[0];
                        string last  = parts[1];

                        var contribUser = db.Users.FirstOrDefault(x => x.FirstName == first &&
                                                                  x.LastName == last);
                        if (contribUser != null)
                        {
                            record.CreatedBy = contribUser;
                        }
                    }

                    if (record.CreatedBy == null)
                    {
                        record.CreatedBy = user;
                    }

                    record.CreatedDate      = DateTime.UtcNow;
                    record.Version          = 1;
                    record.LastUpdatedDate  = DateTime.UtcNow;
                    record.Status           = CatalogRecordStatus.New;
                    record.DepositAgreement = organization.DepositAgreement;

                    var log = new Event()
                    {
                        EventType            = ImportEventType,
                        Timestamp            = DateTime.UtcNow,
                        User                 = user,
                        RelatedCatalogRecord = record,
                        Title                = "Import"
                    };

                    record.Title = FixString(study.DublinCoreMetadata.Title.Current);
                    if (string.IsNullOrWhiteSpace(record.Title))
                    {
                        record.Title = "Missing title";
                    }

                    Directory.CreateDirectory(Path.Combine(storagePath, record.Id.ToString()));

                    record.Keywords                   = study.Coverage.TopicalCoverage.Subjects.Select(x => x.Value).FirstOrDefault();
                    record.AccessStatement            = FixString(study.DublinCoreMetadata.Rights.Current);
                    record.AuthorsText                = study.DublinCoreMetadata.Creator.Current;
                    record.ResearchDesign             = study.DataCollections.First().Methodology.Methodology.First().Description.Best;
                    record.Location                   = study.Coverage.SpatialCoverage.Description.Current;
                    record.InclusionExclusionCriteria = study.DataCollections.First().Methodology.SamplingProcedure.First().Description.Best;
                    record.UnitOfObservation          = study.AnalysisUnit.Value;
                    record.DataSource                 = study.DublinCoreMetadata.Source.Current;
                    record.ArchiveDate                = study.DublinCoreMetadata.Date;
                    record.Number = study.DublinCoreMetadata.Identifiers.First().Identifier;

                    if (study.Coverage.TemporalCoverage.Dates.Count != 0)
                    {
                        var date = study.Coverage.TemporalCoverage.Dates[0];

                        var dateModel = new DateModel();
                        dateModel.dateType = "Date";
                        dateModel.date     = date.Date.DateOnly.Value.ToString("yyyy-MM-dd");
                        dateModel.isRange  = false;
                        record.FieldDates  = Newtonsoft.Json.JsonConvert.SerializeObject(dateModel);
                    }

                    record.OwnerText = study.DublinCoreMetadata.Publisher.Current;

                    List <string> kindofdata = new List <string>();
                    foreach (var data in study.KindsOfData)
                    {
                        kindofdata.Add(data.Value);
                    }

                    record.DataType = string.Join(",", kindofdata.ToArray());
                    //record.IspsID = study.DublinCoreMetadata.Identifiers;
                    //record.DataSourceInformation =
                    record.DataSourceInformation  = GetUserAttribute(study, "DataSourceInformation");
                    record.SampleSize             = GetUserAttribute(study, "SampleSize");
                    record.RandomizationProcedure = GetUserAttribute(study, "RandomizationProcedure");
                    record.Treatment = GetUserAttribute(study, "Treatment");
                    record.TreatmentAdministration = GetUserAttribute(study, "TreatmentAdministration");
                    record.OutcomeMeasures         = GetUserAttribute(study, "OutcomeMeasures");
                    //record.OwnerContact =
                    record.RelatedDatabase = GetUserAttribute(study, "RelatedDatabase");
                    //record.PrincipalInvestigator = enumerator.Current.Value.Value;
                    //record.AreaOfStudy = GetUserAttribute(study, "AreaOfStudy");
                    //record.Discipline=
                    //record.FeatureImage =
                    //record.FeatureText =
                    record.LocationDetails = GetUserAttribute(study, "LocationDetails");
                    //record.DrupalNodeID =
                    record.RelatedProjects     = GetUserAttribute(study, "RelatedProjects");
                    record.RelatedPublications = GetUserAttribute(study, "RelatedPublications");

                    db.CatalogRecords.Add(record);

                    CreateTaskStatuses(record, user, db);

                    // For every PhysicalInstance in the StudyUnit
                    foreach (var pi in study.PhysicalInstances)
                    {
                        // Create a ManagedFile in the curation system.
                        var file = new ManagedFile();
                        file.Id            = pi.Identifier;
                        file.CatalogRecord = record;
                        file.Number        = GetUserAttribute(pi, "DataFileNumber");
                        file.Version       = 1;
                        //file.Number = ++record.LastFileNumber;
                        file.CreationDate = DateTime.UtcNow;
                        file.Status       = FileStatus.Accepted;
                        file.UploadedDate = DateTime.UtcNow;
                        file.AcceptedDate = DateTime.UtcNow;

                        // Map the Handle to the PersisentLink
                        file.PersistentLink = pi.FileIdentifications
                                              .Select(x => x.Uri.ToString())
                                              .Where(x => x.Contains("hdl.handle.net"))
                                              .FirstOrDefault();

                        file.Name = pi.DublinCoreMetadata.Title.Best;
                        if (string.IsNullOrWhiteSpace(file.Name))
                        {
                            file.Name = "Unnamed file";
                        }

                        file.Title = pi.Description.Best;
                        if (string.IsNullOrWhiteSpace(file.Title))
                        {
                            file.Title = "Untitled";
                        }
                        file.PublicName     = file.Title;
                        file.FormatName     = pi.Format;
                        file.IsPublicAccess = pi.FileIdentifications
                                              .Select(x => x.IsPublic)
                                              .FirstOrDefault();

                        file.Owner = user;
                        var temp = GetSize(pi);
                        //file.Size = Convert.ToInt64(GetUserAttribute(pi, "FileSize").Split('.')[0]);
                        file.Size = temp;
                        file.Type = "Data";

                        file.Software = MetadataHelpers.GetSoftware(file.Name);

                        db.Files.Add(file);

                        TaskHelpers.AddProcessingTasksForFile(file, record, db);

                        log.RelatedManagedFiles.Add(file);

                        // TODO Ingest all the files into the processing space.
                        // For now, write a dummy file.
                        //string dummyPath = Path.Combine(storagePath, record.Id.ToString(), file.Name);
                        //File.WriteAllText(dummyPath, "This is a placeholder file without real content.");
                    }

                    // For every OtherMaterial in the StudyUnit
                    foreach (var material in study.OtherMaterials)
                    {
                        // Create a ManagedFile in the curation system.
                        var file = new ManagedFile();
                        file.CatalogRecord = record;
                        file.Id            = Guid.NewGuid();
                        file.Status        = FileStatus.Accepted;
                        file.UploadedDate  = DateTime.UtcNow; // TODO get the historical date

                        if (material.UrlReference != null)
                        {
                            file.PersistentLink = material.UrlReference.ToString();
                        }

                        file.Name = material.DublinCoreMetadata.Title.Best;
                        if (string.IsNullOrWhiteSpace(file.Name))
                        {
                            file.Name = "Unnamed file";
                        }

                        file.Title = material.DublinCoreMetadata.Description.Best;
                        if (string.IsNullOrWhiteSpace(file.Title))
                        {
                            file.Title = "Unnamed";
                        }

                        file.Owner = user;
                        //file.Size = GetSize(pi);
                        file.Type = "Other";

                        file.Number         = material.DublinCoreMetadata.Identifiers.Select(x => x.Identifier).FirstOrDefault();
                        file.FormatName     = material.MimeType;
                        file.IsPublicAccess = material.DublinCoreMetadata.Rights.Best != "Not Public";

                        long   size    = 0;
                        string sizeStr = material.UserIds
                                         .Where(x => x.Type == "Size")
                                         .Select(x => x.Identifier)
                                         .FirstOrDefault();
                        if (long.TryParse(sizeStr, out size))
                        {
                            file.Size = size;
                        }

                        db.Files.Add(file);

                        TaskHelpers.AddProcessingTasksForFile(file, record, db);

                        // TODO Ingest all the files into the processing space.
                        // For now, write a dummy file.
                        //string dummyPath = Path.Combine(storagePath, record.Id.ToString(), file.Name);
                        //File.WriteAllText(dummyPath, "This is a placeholder file without real content.");
                    }

                    db.Events.Add(log);

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    i++;
                }
            }
        }
예제 #3
0
        public ActionResult General(FileViewModel model)
        {
            var logger = LogManager.GetLogger("FileController");

            logger.Debug("Entering FileController.General() POST handler");

            if (Request.Form.AllKeys.Contains("RemovePersistentId"))
            {
                logger.Debug("Removing persistent ID");
                return(RemovePersistentId(model));
            }
            else if (Request.Form.AllKeys.Contains("AddDataFileCurationTasks"))
            {
                logger.Debug("Adding data file curation tasks");
                return(AddDataFileCurationTasks(model));
            }
            else if (Request.Form.AllKeys.Contains("AddCodeFileCurationTasks"))
            {
                logger.Debug("Adding code file curation tasks");
                return(AddCodeFileCurationTasks(model));
            }


            using (var db = ApplicationDbContext.Create())
            {
                logger.Debug("Created database object");

                var user = db.Users
                           .Where(x => x.UserName == User.Identity.Name)
                           .FirstOrDefault();
                if (user == null)
                {
                    logger.Debug("No user. Returning.");
                    return(RedirectToAction("Index"));
                }

                // Fetch the appropriate ManagedFile by ID.
                Guid id   = model.Id;
                var  file = GetFile(id, db);

                EnsureUserIsAllowed(file.CatalogRecord, db);
                EnsureUserCanEdit(file.CatalogRecord, db);

                logger.Debug("User is allowed");

                if (file.CatalogRecord.IsLocked)
                {
                    logger.Debug("Catalog record is locked. Throwing.");
                    throw new HttpException(400, "This operation cannot be performed while the record is locked");
                }

                string changeSummary = ManagedFileChangeDetector.GetChangeSummary(file, model);
                logger.Debug("Got change summary");

                // Copy the information from the POST to the ManagedFile, ignoring read-only properties.
                Mapper.Map(model, file);
                logger.Debug("Mapped");

                // Log the editing of the file.
                var log = new Event()
                {
                    EventType            = EventTypes.EditManagedFile,
                    Timestamp            = DateTime.UtcNow,
                    User                 = user,
                    RelatedCatalogRecord = file.CatalogRecord,
                    Title                = "Edit a File",
                    Details              = changeSummary
                };
                log.RelatedManagedFiles.Add(file);
                db.Events.Add(log);

                logger.Debug("Logged");

                // Update any tasks, in case the file type has changed.
                logger.Debug("Updating file tasks");
                TaskHelpers.AddProcessingTasksForFile(file, file.CatalogRecord, db);
                logger.Debug("Done updating file tasks");

                // Save the updated record.
                db.SaveChanges();
                logger.Debug("Wrote to database");

                logger.Debug("Returning from FileController.General() POST handler");
                return(RedirectToAction("General", new { id = file.Id }));
            }
        }