public static string CreateConvertedMetadata(long datasetId, TransmissionType type) { XmlDocument newXml; DatasetManager datasetManager = new DatasetManager(); MetadataStructureManager metadataMetadataStructureManager = new MetadataStructureManager(); try { DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(datasetId); MetadataStructure metadataStructure = metadataMetadataStructureManager.Repo.Get(datasetVersion.Dataset.MetadataStructure.Id); XmlDatasetHelper xmlDatasetHelper = new XmlDatasetHelper(); int versionNr = datasetManager.GetDatasetVersionNr(datasetVersion); string mappingName = metadataStructure.Name; string mappingFileName = xmlDatasetHelper.GetTransmissionInformation(datasetVersion.Id, type, mappingName); //if mapping file not exist if (string.IsNullOrEmpty(mappingFileName)) { return(""); } string pathMappingFile = Path.Combine(AppConfiguration.GetModuleWorkspacePath("DIM"), mappingFileName); XmlMapperManager xmlMapperManager = new XmlMapperManager(TransactionDirection.InternToExtern); xmlMapperManager.Load(pathMappingFile, "exporttest"); newXml = xmlMapperManager.Export(datasetVersion.Metadata, datasetVersion.Id, mappingName, true); string title = datasetVersion.Title; // store in content descriptor string filename = "metadata"; if (String.IsNullOrEmpty(mappingName) || mappingName.ToLower() == "generic") { storeGeneratedFilePathToContentDiscriptor(datasetId, datasetVersion, filename, ".xml"); } else { filename = "metadata_" + mappingName; storeGeneratedFilePathToContentDiscriptor(datasetId, datasetVersion, filename, ".xml"); } return(OutputDatasetManager.GetDynamicDatasetStorePath(datasetId, versionNr, filename, ".xml")); } catch (Exception ex) { throw ex; } finally { datasetManager.Dispose(); metadataMetadataStructureManager.Dispose(); } }
public static XmlDocument GetConvertedMetadata(long datasetId, TransmissionType type, string mappingName, bool storing = true) { using (DatasetManager datasetManager = new DatasetManager()) { XmlDocument newXml; try { DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(datasetId); XmlDatasetHelper xmlDatasetHelper = new XmlDatasetHelper(); string mappingFileName = xmlDatasetHelper.GetTransmissionInformation(datasetVersion.Id, type, mappingName); // no mapping files with mappingName exist if (string.IsNullOrEmpty(mappingFileName)) { return(null); } string pathMappingFile = Path.Combine(AppConfiguration.GetModuleWorkspacePath("DIM"), mappingFileName); XmlMapperManager xmlMapperManager = new XmlMapperManager(TransactionDirection.InternToExtern); xmlMapperManager.Load(pathMappingFile, "exporttest"); newXml = xmlMapperManager.Export(datasetVersion.Metadata, datasetVersion.Id, mappingName, true); string title = datasetVersion.Title; // store in content descriptor if (storing) { if (String.IsNullOrEmpty(mappingName) || mappingName.ToLower() == "generic") { storeGeneratedFilePathToContentDiscriptor(datasetId, datasetVersion, "metadata", ".xml"); } else { storeGeneratedFilePathToContentDiscriptor(datasetId, datasetVersion, "metadata_" + mappingName, ".xml"); } } } catch (Exception ex) { throw ex; } return(newXml); } }
public static string IsValideAgainstSchema(long datasetId, TransmissionType type, string mappingName) { DatasetManager datasetManager = new DatasetManager(); try { XmlDatasetHelper xmlDatasetHelper = new XmlDatasetHelper(); DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(datasetId); string mappingFileName = xmlDatasetHelper.GetTransmissionInformation(datasetVersion.Id, type, mappingName); // if mapping file not exists if (string.IsNullOrEmpty(mappingFileName)) { return(""); } string pathMappingFile = Path.Combine(AppConfiguration.GetModuleWorkspacePath("DIM"), mappingFileName); XmlMapperManager xmlMapperManager = new XmlMapperManager(TransactionDirection.InternToExtern); xmlMapperManager.Load(pathMappingFile, "exporttest"); XmlDocument tmp = GetConvertedMetadata(datasetId, type, mappingName, false); string path = Path.Combine(AppConfiguration.DataPath, "Temp", "System", "convertedMetadata.xml"); if (FileHelper.FileExist(path)) { FileHelper.Delete(path); } FileHelper.CreateDicrectoriesIfNotExist(Path.GetDirectoryName(path)); tmp.Save(path); XmlDocument metadataForImport = new XmlDocument(); metadataForImport.Load(path); return(xmlMapperManager.Validate(metadataForImport)); } catch (Exception ex) { return(ex.Message); } finally { datasetManager.Dispose(); } }
public async Task <HttpResponseMessage> Put(int id) { var request = Request.CreateResponse(); User user = null; string error = ""; DatasetManager datasetManager = new DatasetManager(); UserManager userManager = new UserManager(); EntityPermissionManager entityPermissionManager = new EntityPermissionManager(); try { #region security string token = this.Request.Headers.Authorization?.Parameter; if (String.IsNullOrEmpty(token)) { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "Bearer token not exist.")); } user = userManager.Users.Where(u => u.Token.Equals(token)).FirstOrDefault(); if (user == null) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Token is not valid.")); } //check permissions //entity permissions if (id > 0) { Dataset d = datasetManager.GetDataset(id); if (d == null) { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "the dataset with the id (" + id + ") does not exist.")); } if (!entityPermissionManager.HasEffectiveRight(user.Name, typeof(Dataset), id, RightType.Write)) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "The token is not authorized to write into the dataset.")); } } #endregion security #region check incomming metadata Stream requestStream = await this.Request.Content.ReadAsStreamAsync(); string contentType = this.Request.Content.Headers.ContentType.MediaType; if (string.IsNullOrEmpty(contentType) || (!contentType.Equals("application/xml") && !contentType.Equals("text/plain"))) { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "The transmitted file is not a xml document.")); } if (requestStream == null) { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "Metadata xml was not received.")); } #endregion check incomming metadata #region incomming values check // check incomming values if (id == 0) { error += "dataset id should be greater then 0."; } ////if (data.UpdateMethod == null) error += "update method is not set"; ////if (data.Count == 0) error += "count should be greater then 0. "; //if (data.Columns == null) error += "cloumns should not be null. "; //if (data.Data == null) error += "data is empty. "; //if (data.PrimaryKeys == null || data.PrimaryKeys.Count() == 0) error += "the UpdateMethod update has been selected but there are no primary keys available. "; if (!string.IsNullOrEmpty(error)) { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, error)); } #endregion incomming values check Dataset dataset = datasetManager.GetDataset(id); if (dataset == null) { return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "Dataset not exist.")); } #region convert metadata XmlDocument metadataForImport = new XmlDocument(); metadataForImport.Load(requestStream); // metadataStructure ID var metadataStructureId = dataset.MetadataStructure.Id; var metadataStructrueName = this.GetUnitOfWork().GetReadOnlyRepository <MetadataStructure>().Get(metadataStructureId).Name; // loadMapping file var path_mappingFile = Path.Combine(AppConfiguration.GetModuleWorkspacePath("DIM"), XmlMetadataImportHelper.GetMappingFileName(metadataStructureId, TransmissionType.mappingFileImport, metadataStructrueName)); // XML mapper + mapping file var xmlMapperManager = new XmlMapperManager(TransactionDirection.ExternToIntern); xmlMapperManager.Load(path_mappingFile, "IDIV"); // generate intern metadata without internal attributes var metadataResult = xmlMapperManager.Generate(metadataForImport, 1, true); // generate intern template metadata xml with needed attribtes var xmlMetadatWriter = new XmlMetadataWriter(BExIS.Xml.Helpers.XmlNodeMode.xPath); var metadataXml = xmlMetadatWriter.CreateMetadataXml(metadataStructureId, XmlUtility.ToXDocument(metadataResult)); var metadataXmlTemplate = XmlMetadataWriter.ToXmlDocument(metadataXml); // set attributes FROM metadataXmlTemplate TO metadataResult var completeMetadata = XmlMetadataImportHelper.FillInXmlValues(metadataResult, metadataXmlTemplate); #endregion convert metadata if (completeMetadata != null) { string title = ""; if (datasetManager.IsDatasetCheckedOutFor(id, user.Name) || datasetManager.CheckOutDataset(id, user.Name)) { DatasetVersion workingCopy = datasetManager.GetDatasetWorkingCopy(id); workingCopy.Metadata = completeMetadata; workingCopy.Title = xmlDatasetHelper.GetInformation(id, completeMetadata, NameAttributeValues.title); workingCopy.Description = xmlDatasetHelper.GetInformation(id, completeMetadata, NameAttributeValues.description); //check if modul exist int v = 1; if (workingCopy.Dataset.Versions != null && workingCopy.Dataset.Versions.Count > 1) { v = workingCopy.Dataset.Versions.Count(); } //set status if (workingCopy.StateInfo == null) { workingCopy.StateInfo = new Vaiona.Entities.Common.EntityStateInfo(); } workingCopy.StateInfo.State = DatasetStateInfo.NotValid.ToString(); title = workingCopy.Title; if (string.IsNullOrEmpty(title)) { title = "No Title available."; } datasetManager.EditDatasetVersion(workingCopy, null, null, null); datasetManager.CheckInDataset(id, "via api.", user.Name, ViewCreationBehavior.None); } // ToDo add Index update to this api //if (this.IsAccessible("DDM", "SearchIndex", "ReIndexSingle")) //{ // var x = this.Run("DDM", "SearchIndex", "ReIndexSingle", new RouteValueDictionary() { { "id", datasetId } }); //} LoggerFactory.LogData(id.ToString(), typeof(Dataset).Name, Vaiona.Entities.Logging.CrudState.Created); var es = new EmailService(); es.Send(MessageHelper.GetUpdateDatasetHeader(id), MessageHelper.GetUpdateDatasetMessage(id, title, user.DisplayName), ConfigurationManager.AppSettings["SystemEmail"] ); } return(Request.CreateErrorResponse(HttpStatusCode.OK, "Metadata successfully updated ")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } finally { datasetManager.Dispose(); entityPermissionManager.Dispose(); userManager.Dispose(); request.Dispose(); } }