public void MappingDocSeriliazationTest() { MappingDocument mapDoc = new MappingDocument(); MappingElement mapeElColl = new MappingElement(); mapeElColl.ClassName = "Class1"; mapeElColl.TableName = "Table1"; mapeElColl.PrimaryKeyName = "Id"; MappingRule mapEl = new MappingRule(); mapEl.ColumnName = "column1"; mapEl.FieldName = "field1"; mapEl.DefaultValue = "somevalue"; mapeElColl.Add(mapEl); mapDoc.Add(mapeElColl); string xml = MappingDocument.GetXml(mapDoc); MappingDocument loadedDoc = MappingDocument.LoadFromXml(xml); string xml2 = MappingDocument.GetXml(loadedDoc); if (xml != xml2) { throw new AssertFailedException(); } }
/// <summary> /// Initializes the mapping document. /// </summary> /// <param name="context">The context.</param> protected override void InitializeMappingDocument(BusinessContext context) { InitializeMappingDocumentRequest request = ((InitializeMappingDocumentRequest)context.Request); MetaClass metaClass = DataContext.Current.GetMetaClass(context.GetTargetMetaClassName()); List <PrimaryKeyId> primaryKeyIds = new List <PrimaryKeyId>(); // Create Default Mapping MappingDocument retVal = new MappingDocument(); DataTable dataTable = request.Data.Tables[request.TableIndex]; MappingElement mapping = new MappingElement(dataTable.TableName, metaClass.Name); retVal.Add(mapping); MappingElementBuilder builder = new MappingElementBuilder(retVal); //Call creation mapping document by saved patterns DefaultMappingHelper.CreateMapingByPatternComparision(dataTable, metaClass, builder); // Write Response InitializeMappingDocumentResponse response = new InitializeMappingDocumentResponse(); response.MappingDocument = retVal; context.SetResponse(response); }
/// <summary> /// Initializes the mapping document. /// </summary> /// <param name="context">The context.</param> protected override void InitializeMappingDocument(BusinessContext context) { InitializeMappingDocumentRequest request = ((InitializeMappingDocumentRequest)context.Request); MetaClass metaClass = DataContext.Current.GetMetaClass(context.GetTargetMetaClassName()); List<PrimaryKeyId> primaryKeyIds = new List<PrimaryKeyId>(); // Create Default Mapping MappingDocument retVal = new MappingDocument(); DataTable dataTable = request.Data.Tables[request.TableIndex]; MappingElement mapping = new MappingElement(dataTable.TableName, metaClass.Name); retVal.Add(mapping); MappingElementBuilder builder = new MappingElementBuilder(retVal); //Call creation mapping document by saved patterns DefaultMappingHelper.CreateMapingByPatternComparision(dataTable, metaClass, builder); // Write Response InitializeMappingDocumentResponse response = new InitializeMappingDocumentResponse(); response.MappingDocument = retVal; context.SetResponse(response); }
/// <summary> /// Handles the SelectedIndexChanged event of the ddCSV control. /// Parse and return IMDR for CSV file /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> void ddCSV_SelectedIndexChanged(object sender, EventArgs e) { DataSet rawData = GetRawDataForCSV(); InitializeMappingDocumentRequest imdr = new InitializeMappingDocumentRequest(_className, rawData, 0); InitializeMappingDocumentResponse resp = (InitializeMappingDocumentResponse)BusinessManager.Execute(imdr); MappingDocument md = resp.MappingDocument; ViewState["_ir"] = new ImportRequest(_className, rawData, md); BindDG(); }
/// <summary> /// Processes the mapping. /// </summary> /// <param name="xmlMapRules">The XML map rules.</param> /// <param name="mapDataSet">The map data set.</param> public void ProcessMapping(MappingDocument mappingDoc, DataSet dataSet) { List<MappedObject> mappedObjectList = new List<MappedObject>(); //Process retrieve mapping values and fill meta creation structure foreach (MappingElement mapElColl in mappingDoc) { MappedObject mappedObject = null; //try { mappedObject = PrepareMappingSet(mapElColl, dataSet, mappedObjectList); } //catch (Exception e) { //MappingError errorInfo = new MappingError(MappingErrorType.MappingDeclarationError, // e.Message); //MappingEngineErrorEventArgs args = new MappingEngineErrorEventArgs(errorInfo); //args.MappingElement = mapElColl; //args.Exception = e; //RaiseErrorEvent(args); //if ((args.ResolveError) && (_errResolveAtteptNum < _errResolveAtteptMax)) //{ // _errResolveAtteptNum++; // //Try resolve error // mappedObject = PrepareMappingSet(args.MappingElement, dataSet, // mappedObjectList); //} //else //{ // _errorInfoList.Add(errorInfo); // throw new MappingException(e.Message); // } //throw; } //Creation meta object from mapping data if (mappedObject != null) { using (TransactionScope tran = DataContext.Current.BeginTransaction()) { //Process create meta object from mapping meta structure CreateMetaObjects(mappedObject, dataSet); tran.Commit(); } mappedObjectList.Add(mappedObject); } } }
/// <summary> /// Opens a text file, reads all lines of the file, and then closes the file. Generates the RDF mapping dictionary. /// </summary> /// <param name="path">The file to open for reading.</param> /// <returns>RDF mapping dictionary</returns> public Dictionary <string, string> Read(string path) { MappingDocument document = JsonConvert.DeserializeObject <MappingDocument>(File.ReadAllText(path)); Dictionary <string, string> map = new Dictionary <string, string>(); foreach (var item in document.Map) { map.Add(ReplaceNamespace(document, item.Key), ReplaceNamespace(document, item.Value)); } return(map); }
private string ReplaceNamespace(MappingDocument document, string value) { foreach (string ns in document.Namespaces.Keys) { string formattedNamespace = string.Format("{0}:", ns); if (value.StartsWith(formattedNamespace)) { return(Regex.Replace(value, string.Format(@"^{0}\s*", formattedNamespace), document.Namespaces[ns])); } } return(value); }
public void ExcelMappingTest() { //IMcOleDbHelper helper = (IMcOleDbHelper)Activator.GetObject(typeof(IMcOleDbHelper), // ConfigurationManager.AppSettings["McOleDbServiceString"]); //DataSet dataSet = helper.ConvertExcelToDataSet("price.xls"); DataSet dataSet = ConvertExcelToDataSet("price.xls"); MappingEngine mapEngine = new MappingEngine(); mapEngine.MappingErrorEvent += ErrorHandler; MappingDocument mapDoc = MappingDocument.LoadFromFile("priceMapping.xml"); mapEngine.ProcessMapping(mapDoc, dataSet); }
public void MappingDataTest() { DataSet dataSet = LoadDataSetfromXML("DataSet.xsd", "DataSet.xml"); MappingEngine mapEngine = new MappingEngine(); string mapFileName = @"BookMapping.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(mapFileName); MappingDocument mapDoc = MappingDocument.LoadFromXml(xmlDoc.OuterXml); mapEngine.ProcessMapping(mapDoc, dataSet); }
/// <summary> /// Creates the default by mapping element. /// </summary> public static void CreateMapingByPatternComparision(DataTable srcData, MetaClass dstMetaClass, MappingElementBuilder builder) { if (srcData == null) { throw new ArgumentNullException("srcData"); } if (dstMetaClass == null) { throw new ArgumentNullException("dstMetaClass"); } if (builder == null) { throw new ArgumentNullException("builder"); } //MappingDocument enDefaultMappingDoc = MappingDocument.LoadFromXml(VCardType.LocRM.GetString("DefaultMapping", // CultureInfo.GetCultureInfo("en-US"))); MappingDocument enDefaultMappingDoc = MappingDocument.LoadFromXml(GetWebResourceString("{IbnFramework.OutlookMappingPattern:Outlook2007}", CultureInfo.GetCultureInfo("en-US"))); //MappingDocument ruDefaultMappingDoc = MappingDocument.LoadFromXml(VCardType.LocRM.GetString("DefaultMapping", // CultureInfo.GetCultureInfo("ru-RU"))); MappingDocument ruDefaultMappingDoc = MappingDocument.LoadFromXml(GetWebResourceString("{IbnFramework.OutlookMappingPattern:Outlook2007}", CultureInfo.GetCultureInfo("ru-RU"))); MappingElement srcEl = null; MappingElement dstEl = null; //Recognize source lang and type DataColumnCollection dataColl = srcData.Columns; //Recognize language pattern if (enDefaultMappingDoc != null && enDefaultMappingDoc.Count != 0) { if (DataColumnSourceIsMatch(dataColl, enDefaultMappingDoc[0])) { srcEl = enDefaultMappingDoc[0]; if (FieldDestinationIsMatch(dstMetaClass.Fields, enDefaultMappingDoc[0])) { dstEl = enDefaultMappingDoc[0]; } } } if (ruDefaultMappingDoc != null && ruDefaultMappingDoc.Count != 0) { if (DataColumnSourceIsMatch(dataColl, ruDefaultMappingDoc[0])) { srcEl = ruDefaultMappingDoc[0]; if (FieldDestinationIsMatch(dstMetaClass.Fields, ruDefaultMappingDoc[0])) { dstEl = ruDefaultMappingDoc[0]; } } } //Pattern found, build mapping by pattern if (srcEl != null && dstEl != null) { for (int i = 0; i < srcEl.Count; i++) { builder.AssignCopyValueRule(srcEl[i].ColumnName, dstEl[i].FieldName); } } else { //Pattern not found, build mapping by field friendly names comparison foreach (DataColumn dataCol in srcData.Columns) { CreateMapingByFriendlyNameComparison(dataCol, dstMetaClass, builder); } } }
/// <summary> /// Handles the ActiveStepChanged event of the ucWizard control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> void ucWizard_ActiveStepChanged(object sender, EventArgs e) { //step1 if (ucWizard.ActiveStep.ID == "step1") { ViewState["_ir"] = null; } //step2 if (ucWizard.ActiveStep.ID == "step3") { #region upload file if (fSourceFile.PostedFile != null && fSourceFile.PostedFile.ContentLength > 0) { ProcessFileCache(Server.MapPath(Mediachase.UI.Web.Util.CommonHelper.ChartPath)); String dir = Mediachase.UI.Web.Util.CommonHelper.ChartPath; string wwwpath = dir + Guid.NewGuid().ToString("N"); wwwpath += Path.GetExtension(fSourceFile.PostedFile.FileName); hdnFilePath.Value = wwwpath; using (Stream sw = File.Create(Server.MapPath(wwwpath))) { fSourceFile.PostedFile.InputStream.Seek(0, SeekOrigin.Begin); System.IO.BinaryReader br = new System.IO.BinaryReader(fSourceFile.PostedFile.InputStream); int iBufferSize = 655360; // 640 KB byte[] outbyte = br.ReadBytes(iBufferSize); while (outbyte.Length > 0) { sw.Write(outbyte, 0, outbyte.Length); outbyte = br.ReadBytes(iBufferSize); } br.Close(); } } #endregion divCSV.Visible = (rbSourceType.SelectedIndex == 2); #region file parsing IIncomingDataParser parser = null; DataSet rawData = null; try { switch (rbSourceType.SelectedIndex) { case 0: IMCOleDBHelper helper = (IMCOleDBHelper)Activator.GetObject(typeof(IMCOleDBHelper), ConfigurationManager.AppSettings["McOleDbServiceString"]); rawData = helper.ConvertExcelToDataSet(Server.MapPath(hdnFilePath.Value)); break; case 1: parser = new XmlIncomingDataParser(); rawData = parser.Parse(Server.MapPath(hdnFilePath.Value), null); break; case 2: rawData = GetRawDataForCSV(); break; case 3: rawData = VCardUtil.ConvertFile(Server.MapPath(hdnFilePath.Value)); break; } } catch (Exception ex) { CHelper.GenerateErrorReport(ex); ViewState["ServiceError"] = true; ViewState["ErrorFileName"] = Server.MapPath(hdnFilePath.Value); ucWizard.MoveTo(this.step4); return; } #endregion if (ViewState["_ir"] == null) { InitializeMappingDocumentRequest imdr = new InitializeMappingDocumentRequest(_className, rawData, 0); InitializeMappingDocumentResponse resp = (InitializeMappingDocumentResponse)BusinessManager.Execute(imdr); MappingDocument md = resp.MappingDocument; _ir = new ImportRequest(_className, rawData, md); ViewState["_ir"] = _ir; } BindDG(); } if (ucWizard.ActiveStep.ID == "step4") { if (ViewState["ServiceError"] != null && (bool)ViewState["ServiceError"]) { string fileName = ViewState["ErrorFileName"].ToString(); if (fileName.EndsWith("xlsx") && !Is2007OfficeSystemDriverInstalled(fileName)) { lblResult.Text = GetGlobalResourceObject("IbnFramework.Common", "McOleDbServiceWarningXlsx").ToString(); } else { lblResult.Text = GetGlobalResourceObject("IbnFramework.Common", "McOleDbServiceWarning").ToString(); } } foreach (Control c in ucWizard.ActiveStep.Controls) { if (c is Button) { Button btn = (Button)c; string param = String.Empty; if (!String.IsNullOrEmpty(_commandName)) { param = (new CommandParameters(_commandName)).ToString(); } string script = Mediachase.Ibn.Web.UI.WebControls.CommandHandler.GetCloseOpenedFrameScript(this.Page, param); script += " return false;"; btn.OnClientClick = script; } } } }