private string MoveAndSaveOriginalFileInContentDiscriptor(DatasetVersion datasetVersion) { TaskManager TaskManager = (TaskManager)Session["TaskManager"]; //dataset id and data structure id are available via datasetVersion properties,why you are passing them via the BUS? Javad long datasetId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASET_ID]); long dataStructureId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASTRUCTURE_ID]); DatasetManager datasetManager = new DatasetManager(); string title = TaskManager.Bus[TaskManager.DATASET_TITLE].ToString(); string ext = ".xlsm";// TaskManager.Bus[TaskManager.EXTENTION].ToString(); ExcelWriter excelWriter = new ExcelWriter(); // Move Original File to its permanent location String tempPath = TaskManager.Bus[TaskManager.FILEPATH].ToString(); string originalFileName = TaskManager.Bus[TaskManager.FILENAME].ToString(); string storePath = excelWriter.GetFullStorePathOriginalFile(datasetId, datasetVersion.Id, originalFileName); string dynamicStorePath = excelWriter.GetDynamicStorePathOriginalFile(datasetId, datasetVersion.VersionNo, originalFileName); //Why using the excel writer, isn't any function available in System.IO.File/ Directory, etc. Javad FileHelper.MoveFile(tempPath, storePath); //Register the original data as a resource of the current dataset version ContentDescriptor originalDescriptor = new ContentDescriptor() { OrderNo = 1, Name = "original", MimeType = "application/xlsm", URI = dynamicStorePath, DatasetVersion = datasetVersion, }; if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(originalDescriptor.Name)) > 0) { // remove the one contentdesciptor foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors) { if (cd.Name == originalDescriptor.Name) { cd.URI = originalDescriptor.URI; } } } else { // add current contentdesciptor to list datasetVersion.ContentDescriptors.Add(originalDescriptor); } return storePath; }
private string SaveFileInContentDiscriptor(DatasetVersion datasetVersion) { try { //XXX put function like GetStorePathOriginalFile or GetDynamicStorePathOriginalFile // the function is available in the abstract class datawriter ExcelWriter excelWriter = new ExcelWriter(); // Move Original File to its permanent location String tempPath = TaskManager.Bus[TaskManager.FILEPATH].ToString(); string originalFileName = TaskManager.Bus[TaskManager.FILENAME].ToString(); string storePath = excelWriter.GetFullStorePathOriginalFile(datasetVersion.Dataset.Id, datasetVersion.VersionNo, originalFileName); string dynamicStorePath = excelWriter.GetDynamicStorePathOriginalFile(datasetVersion.Dataset.Id, datasetVersion.VersionNo, originalFileName); string extention = TaskManager.Bus[TaskManager.EXTENTION].ToString(); Debug.WriteLine("extention : " + extention); //Why using the excel writer, isn't any function available in System.IO.File/ Directory, etc. Javad FileHelper.MoveFile(tempPath, storePath); string mimeType = MimeMapping.GetMimeMapping(originalFileName); //Register the original data as a resource of the current dataset version ContentDescriptor originalDescriptor = new ContentDescriptor() { OrderNo = 1, Name = "unstructuredData", MimeType = mimeType, URI = dynamicStorePath, DatasetVersion = datasetVersion, }; // add current contentdesciptor to list datasetVersion.ContentDescriptors.Add(originalDescriptor); return storePath; } catch(Exception e) { return ""; } }
private string GenerateDownloadFile(DatasetVersion datasetVersion) { TaskManager TaskManager = (TaskManager)Session["TaskManager"]; //dataset id and data structure id are available via datasetVersion properties,why you are passing them via the BUS? Javad long datasetId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASET_ID]); long dataStructureId = Convert.ToInt64(TaskManager.Bus[TaskManager.DATASTRUCTURE_ID]); DatasetManager datasetManager = new DatasetManager(); string title = TaskManager.Bus[TaskManager.DATASET_TITLE].ToString(); string ext = ".xlsm";// TaskManager.Bus[TaskManager.EXTENTION].ToString(); ExcelWriter excelWriter = new ExcelWriter(); // create the generated file and determine its location string path = excelWriter.CreateFile(datasetId, datasetVersion.VersionNo, dataStructureId, title, ext); string dynamicPath = excelWriter.GetDynamicStorePath(datasetId, datasetVersion.VersionNo, title, ext); //Register the generated data file as a resource of the current dataset version ContentDescriptor generatedDescriptor = new ContentDescriptor() { OrderNo = 1, Name = "generated", MimeType = "application/xlsm", URI = dynamicPath, DatasetVersion = datasetVersion, }; if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(generatedDescriptor.Name)) > 0) { // remove the one contentdesciptor foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors) { if (cd.Name == generatedDescriptor.Name) { cd.URI = generatedDescriptor.URI; } } } else { // add current contentdesciptor to list datasetVersion.ContentDescriptors.Add(generatedDescriptor); } // note: the descriptors are not persisted yet, they will be persisted if the caller of this method persists the datasetVersion object. return path; }
/// <summary> /// Detaches the content descriptor object from its corresponding dataset version, and then deletes the content descriptor. /// </summary> /// <param name="entity">The content descriptor object to be deleted.</param> /// <returns>True if successful, False otherwise.</returns> public bool DeleteContentDescriptor(ContentDescriptor entity) { Contract.Requires(entity != null); Contract.Requires(entity.Id >= 0); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<ContentDescriptor> repo = uow.GetRepository<ContentDescriptor>(); //repo.Evict(entity); //entity = repo.Reload(entity); // Javad: There is a problem with relaoding the entity, which is: // when the entity is reloaded, another onject of it is created, which is different that the one attached to the DatasetVersion. // They have the same PK, but diffect HashCode, hence NH complains about having more than one object in session(s). // To be sure the entity is object equial to the one in the ContentDescriptors list, the follwing statement should work. entity.DatasetVersion.ContentDescriptors.Remove(entity); // The follwoing line should not be needed, if entity is the same object as in the list //entity.DatasetVersion.ContentDescriptors.Remove( // entity.DatasetVersion.ContentDescriptors.FirstOrDefault(p => p.Id.Equals(entity.Id)) // ); entity.DatasetVersion = null; repo.Delete(entity); uow.Commit(); } // if any problem was detected during the commit, an exception will be thrown! return (true); }
/// <summary> /// Resource descriptors are the way to link resources to data set versions (and some other entity types, too). /// The resources can be persisted in the local or a remote file system or any other location reachable via a URL. /// The resource itself can be any type of file, service returning a resource, a normal webpage, and so on. /// The method creates a resource descriptor, links it to the provided data set version and persists the descriptor. /// </summary> /// <param name="name">A friendly name for the resource, mainly used in the UI</param> /// <param name="mimeType">The type of the resource. Used in the methods that transfer and/or process the resource</param> /// <param name="uri">The URI of the resource, may contain protocol, access method, authorization information , etc.</param> /// <param name="orderNo">The order of the resource in the list of all resources associated to the same dataset version.</param> /// <param name="datasetVersion"></param> /// <returns>A persisted <seealso cref="ContentDescriptor"/> object linked to the <paramref name="datasetVersion"/></returns> /// <remarks>The method does not have access to the resource itself, and does not persist it.</remarks> public ContentDescriptor CreateContentDescriptor(string name, string mimeType, string uri, Int32 orderNo, DatasetVersion datasetVersion) { Contract.Requires(!string.IsNullOrWhiteSpace(name)); Contract.Requires(!string.IsNullOrWhiteSpace(mimeType)); Contract.Requires(!string.IsNullOrWhiteSpace(uri)); Contract.Requires(datasetVersion != null); // check whether is it needed that the dataset is checked out to add descriptor Contract.Ensures(Contract.Result<ContentDescriptor>() != null); ContentDescriptor e = new ContentDescriptor() { Name = name, MimeType = mimeType, OrderNo = orderNo, URI = uri, DatasetVersion = datasetVersion, }; e.DatasetVersion.ContentDescriptors.Add(e); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<ContentDescriptor> repo = uow.GetRepository<ContentDescriptor>(); repo.Put(e); uow.Commit(); } return (e); }
/// <summary> /// checks out the dataset and creates a new version on it. The new version acts like a working copy while it is not committed, hence editable. /// </summary> /// <param name="datasetId"></param> /// <param name="username"></param> private bool checkOutDataset(Int64 datasetId, string username, DateTime timestamp) { bool checkedOut = false; //XmlDocument doc = new XmlDocument(); //doc.LoadXml(@"<Metadata>Empty</Metadata>"); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<Dataset> repo = uow.GetRepository<Dataset>(); var q = repo.Query(p => p.Id == datasetId && p.Status == DatasetStatus.CheckedIn && (p.CheckOutUser.Equals(string.Empty) || p.CheckOutUser == null)); Dataset ds = q.FirstOrDefault(); if (ds != null) { DatasetVersion dsNewVersion = new DatasetVersion() { Timestamp = timestamp, //Metadata = doc, //ExtendedPropertyValues = new List<ExtendedPropertyValue>(), //ContentDescriptors = new List<ContentDescriptor>(), Status = DatasetVersionStatus.CheckedOut, Dataset = ds, }; // if there is a previous version, copy its metadata, content descriptors and extended property values to the newly created version if (ds.Versions.Count() > 0) { var previousCheckedInVersion = ds.Versions.Where(p => p.Status == DatasetVersionStatus.CheckedIn).First(); if (previousCheckedInVersion != null && previousCheckedInVersion.Timestamp >= timestamp) // it is an error { throw new Exception(string.Format("The provided timestamp {0} is earlier than the timestamp of the latest checked in version!", timestamp)); } if (previousCheckedInVersion != null) { dsNewVersion.Metadata = previousCheckedInVersion.Metadata; dsNewVersion.ExtendedPropertyValues = previousCheckedInVersion.ExtendedPropertyValues; foreach (var item in previousCheckedInVersion.ContentDescriptors) { ContentDescriptor cd = new ContentDescriptor() { MimeType = item.MimeType, Name = item.Name, OrderNo = item.OrderNo, URI = item.URI, DatasetVersion = dsNewVersion, }; dsNewVersion.ContentDescriptors.Add(cd); } } } ds.Status = DatasetStatus.CheckedOut; ds.LastCheckIOTimestamp = timestamp; ds.CheckOutUser = getUserIdentifier(username); ds.Versions.Add(dsNewVersion); repo.Put(ds); uow.Commit(); checkedOut = true; } } return (checkedOut); }
/// <summary> /// Having a changed content descriptor entity, the method applies the changes to the original entity and persists the changes. /// </summary> /// <param name="entity">The editedVersion version of the content descriptor entity.</param> /// <returns>The changed instance.</returns> /// <remarks>The entity should already exists in the database.</remarks> public ContentDescriptor UpdateContentDescriptor(ContentDescriptor entity) { Contract.Requires(entity != null, "provided entity can not be null"); Contract.Requires(entity.Id >= 0, "provided entity must have a permanent ID"); Contract.Ensures(Contract.Result<ContentDescriptor>() != null && Contract.Result<ContentDescriptor>().Id >= 0, "No entity is persisted!"); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<ContentDescriptor> repo = uow.GetRepository<ContentDescriptor>(); repo.Put(entity); // Merge is required here!!!! uow.Commit(); } return (entity); }