using BeWo.Data.Access; using BeWo.Data.Entities; using BS.Shared; using BS.Shared.DataContracts; using DevExpress.XtraRichEdit; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; namespace BeWo.Service.Dokumentverwaltung { public class PlaceholderManager { public virtual List GetPlatzhalter() { return null; } public virtual string ReplacePlaceholdersInTemplate(DokumentvorlageDC template, long? objectOid, TableID tid) { //Nur für abgeleitete Klassen return null; } public virtual void ReplacePlaceholderInText(RichEditDocumentServer rtfServer, PlatzhalterDC ph, Object obj) { if (!String.IsNullOrEmpty(ph.Platzhalter)) { String platzhalter = String.Format("[{0}]", ph.Platzhalter); String platzhalterWert = GetPlaceholderValue(ph, obj); //var doc = rtfServer.Document as DevExpress.XtraRichEdit.API.Native.Implementation.NativeDocument; //var list = rtfServer.Document.FindAll("blablubb", DevExpress.XtraRichEdit.API.Native.SearchOptions.None); if (platzhalterWert == null) { platzhalterWert = String.Empty; } foreach (var shape in rtfServer.Document.Shapes) { var subdoc = shape.ShapeFormat?.TextBox?.Document; if (subdoc != null) { subdoc.ReplaceAll(platzhalter, platzhalterWert, DevExpress.XtraRichEdit.API.Native.SearchOptions.CaseSensitive); } } //var list = rtfServer.MailMerge( sd.FindAll(platzhalter1, DevExpress.XtraRichEdit.API.Native.SearchOptions.CaseSensitive); rtfServer.Document.ReplaceAll(platzhalter, platzhalterWert, DevExpress.XtraRichEdit.API.Native.SearchOptions.CaseSensitive); //sd.ReplaceAll(platzhalter1, GetPlaceholderValue(ph, obj), DevExpress.XtraRichEdit.API.Native.SearchOptions.CaseSensitive); //if (text.Contains(platzhalter1)) //{ // int test = 0; //} //if (text.Contains(platzhalter1) || text.Contains(platzhalter2) || text.Contains(platzhalter3)) //{ // return text.Replace(ph.Platzhalter, GetPlaceholderValue(ph, obj)); //} } } public virtual String GetPlaceholderValue(PlatzhalterDC ph, Object obj) { if (!String.IsNullOrEmpty(ph.FieldName)) { var returnObj = GetPlaceholderValue(ph.FieldName, obj); if (returnObj != null) { if (!String.IsNullOrEmpty(ph.Format)) { return String.Format("{0:" + ph.Format + "}", returnObj); } return returnObj.ToString(); } } else { int test = 0; } return ""; } public virtual Object GetPlaceholderValue(String fieldName, Object obj) { var names = fieldName.Split('.'); if (names.Length > 0) { var name = names[0]; var prop = obj.GetType().GetProperty(names[0]); if (prop != null) { Object newObj = prop.GetValue(obj); if (names.Length > 1 && newObj != null) { String newFieldname = ""; for (int i = 1; i < names.Length; i++) { if (newFieldname.Length > 0) { newFieldname += "."; } newFieldname += names[i]; } return GetPlaceholderValue(newFieldname, newObj); } if (newObj != null) { return newObj; } } } return null; } public virtual Object GetEntityObject(long oid) { return null; } } }