From b38a93ff86fc0ab44144bbe7841bcbf4f7783100 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 29 Apr 2020 15:01:06 +0000 Subject: [PATCH] --- source/Plugins/AyaNova.Plugin.V8/V8.cs | 377 +++++++++++-------------- 1 file changed, 169 insertions(+), 208 deletions(-) diff --git a/source/Plugins/AyaNova.Plugin.V8/V8.cs b/source/Plugins/AyaNova.Plugin.V8/V8.cs index f7d1601..c4e4532 100644 --- a/source/Plugins/AyaNova.Plugin.V8/V8.cs +++ b/source/Plugins/AyaNova.Plugin.V8/V8.cs @@ -179,9 +179,13 @@ namespace AyaNova.PlugIn.V8 /* TODO: - * Error should post somehow to server so can see at server what went wrong - * custom fields processor for: User, Client, Contract, HeadOffice, LoanItem, part, project, purchaseorder, unit, unitmodel, vendor, workorderitem - * See util.cs in winformapp for all custom field shit, lines 3114 is a good starting point + * wiki / attached docs + * + * locales + * + * + * + * */ @@ -208,7 +212,7 @@ namespace AyaNova.PlugIn.V8 //BIZ objects await ExportUsers(progress); - //dumpLocales(tempArchiveFolder, progress); + // await ExportLocales(progress); @@ -277,8 +281,6 @@ namespace AyaNova.PlugIn.V8 } #endregion locales - - #region Seeds private class GZSeeds { @@ -348,10 +350,10 @@ namespace AyaNova.PlugIn.V8 private async System.Threading.Tasks.Task ExportUsers(ProgressForm progress) { //Step 1: export the CustomFields to FormCustom if applicable so that when doing individual items we can export their custom data too - var ocf=ObjectHasCustomFieldDataToExport("User"); + var ocf = ObjectHasCustomFieldDataToExport("User"); bool ShouldExportCustom = ocf != null; -var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); + var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); //Step 2: export the users @@ -410,7 +412,7 @@ var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); //Custom fields? if (ShouldExportCustom) { - d.customFields = CustomFieldData(c,DateCustomFields); + d.customFields = CustomFieldData(c, DateCustomFields); } var a = await util.PostAsync("User", d.ToString()); @@ -554,65 +556,124 @@ var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); #endregion TAG ITEMS //-------------------------------------------- - #endregion object dump methods + #endregion object export - #region Dump - - // /// - // /// Write out the object properties as JSON - // /// - // /// - // /// - // private void ExportObjectToFolder(object o, string objectFileName, List excludeProperties, - // TypeAndID tid, string forceTypeString = "", JObject jExtra = null) - // { - - // var typestring = o.GetType().ToString(); - // if (!string.IsNullOrWhiteSpace(forceTypeString)) - // { - // typestring = forceTypeString; - // } - - // var dumpFolder = tempArchiveFolder + Path.DirectorySeparatorChar + typestring; - // makeFolderIfNotExist(dumpFolder); - - // var outputFileName = dumpFolder + Path.DirectorySeparatorChar + objectFileName + ".json"; - // var wikiOutputPath = dumpFolder + Path.DirectorySeparatorChar + objectFileName + Path.DirectorySeparatorChar + "files"; - - // JsonSerializer serializer = new JsonSerializer(); - // serializer.NullValueHandling = NullValueHandling.Include; - // serializer.ContractResolver = new ExcludeNamedPropertiesContractResolver(excludeProperties); - // serializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; + #region Custom fields exporter - //#if(DEBUG) - // serializer.Formatting = Formatting.Indented; - //#endif - // // serializer.Converters.Add(new JavaScriptDateTimeConverter()); - // // serializer.NullValueHandling = NullValueHandling.Ignore; + //export objects custom field data into jobject string + private string CustomFieldData(object biz, List dateFields) + { + dynamic d = new JObject(); - // //generate file name, should be ID of object plus .json + for (int x = 0; x < 10; x++) + { + object o = (object)biz.GetType().GetProperty("Custom" + x.ToString()).GetValue(biz, null); + string s = o.ToString(); + if (string.IsNullOrWhiteSpace(s)) + { + s = null; + } + if (s != null && dateFields.Contains(x)) + { + //parse out to UTC date + DateTime dt = new DateTime(); + if (DateTime.TryParse(s, out dt)) + { + s = dt.ToUniversalTime().ToString("s"); + } + } + if (!string.IsNullOrWhiteSpace(s)) + d["c" + (x + 1).ToString()] = s; - // JObject jo = JObject.FromObject(o, serializer); - // if (jExtra != null) - // { - // jo.Add("jextra", jExtra); - // } + } + return d.ToString(); + } + static public ObjectCustomFields ObjectHasCustomFieldDataToExport(string sObject) + { + ObjectCustomFields ocf = ObjectCustomFields.GetItems(sObject); + if (ocf.Count == 0) + { + return null; + } - // using (StreamWriter sw = new StreamWriter(outputFileName)) - // using (JsonWriter writer = new JsonTextWriter(sw)) - // { + foreach (ObjectCustomField f in ocf) + { + if (f.Visible) + return ocf; + } + return null; + } - // // serializer.Serialize(writer, o); - // serializer.Serialize(writer, jo); - // } + private async System.Threading.Tasks.Task> ExportCustomFieldSchema(ObjectCustomFields ocf, string v7CustomFieldObjectName, string RavenCustomTranslationKeyObjectName) + { + var ret = new List(); + //NOTE: this code inspired by winforApp::Util.cs PrepareCustomFieldsGrid method + dynamic d = new JObject(); + d.formkey = RavenCustomTranslationKeyObjectName; + dynamic dtemplate = new JArray(); + foreach (ObjectCustomField f in ocf) + { + if (f.Visible) + { + int n = Convert.ToInt32(f.FieldName.Replace("Custom", "")) + 1;//raven custom fields are 1 based, v7 are zero based + CustomFieldLocaleKeys.Add(RavenCustomTranslationKeyObjectName + n.ToString(), + util.LocaleText.GetLocalizedText(v7CustomFieldObjectName + ".Label." + f.FieldName)); - // //WIKI / ATTACHMENTS - // DumpWikiPageAndAttachments(tid, wikiOutputPath); - // } + dynamic dt = new JObject(); + dt.fld = RavenCustomTranslationKeyObjectName + "Custom" + n.ToString(); + dt.hide = false; + dt.required = false; + switch (f.FieldType) + { + case FormFieldDataTypes.Currency: + dt.type = util.AyaUiFieldDataType.Currency; + break; + case FormFieldDataTypes.DateOnly: + dt.type = util.AyaUiFieldDataType.Date; + ret.Add(n - 1); + break; + case FormFieldDataTypes.DateTime: + dt.type = util.AyaUiFieldDataType.DateTime; + ret.Add(n - 1); + break; + case FormFieldDataTypes.Number: + dt.type = util.AyaUiFieldDataType.Decimal; + break; + case FormFieldDataTypes.Text: + dt.type = util.AyaUiFieldDataType.Text; + break; + case FormFieldDataTypes.TimeOnly: + dt.type = util.AyaUiFieldDataType.Time; + ret.Add(n - 1); + break; + case FormFieldDataTypes.TrueFalse: + dt.type = util.AyaUiFieldDataType.Bool; + break; + default: + dt.type = util.AyaUiFieldDataType.Text; + break; + } + dtemplate.Add(dt); + } + } + d.template = dtemplate.ToString(); + //ok, were here because there *are* custom fields available + var a = await util.GetAsync("FormCustom/" + RavenCustomTranslationKeyObjectName); + var ctoken = util.CTokenFromResponse(a); + d.concurrencyToken = ctoken; + await util.PutAsync("FormCustom/" + RavenCustomTranslationKeyObjectName, d.ToString()); + + return ret; + } + + #endregion custom fields + + #region WIKI page exporter + //WIKI #region Wikiable objects reference @@ -640,25 +701,23 @@ var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); */ #endregion - private void ExportWikiPageAndAttachments(TypeAndID tid, string wikiOutputPath) + private async System.Threading.Tasks.Task ExportWikiPageAndAttachments(TypeAndID tid) { + //todo: upload attached files + //todo: return wiki as string - // //may not exist - // if (!WikiPage.HasWiki(tid.ID)) return; + //may not exist + if (!WikiPage.HasWiki(tid.ID)) return null; - // WikiPage w = WikiPage.GetItem(tid); - // var content = w.GetContentAsString; - // AyaFileList fl = AyaFileList.GetList(w.ID); + WikiPage w = WikiPage.GetItem(tid); + var content = w.GetContentAsString; + AyaFileList fl = AyaFileList.GetList(w.ID); - // if (string.IsNullOrWhiteSpace(content) && fl.Count < 1) return; + if (string.IsNullOrWhiteSpace(content) && fl.Count < 1) return null; - // makeFolderIfNotExist(wikiOutputPath); - // if (!string.IsNullOrWhiteSpace(content)) - // { - // //write out the html wiki page - // File.WriteAllText(wikiOutputPath + Path.DirectorySeparatorChar + "w.html", content); - // } + + // //files // foreach (AyaFileList.AyaFileListInfo i in fl) @@ -713,11 +772,19 @@ var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); // } // } + + + + if (!string.IsNullOrWhiteSpace(content)) + return content; + else + return null; } - #endregion dump + #endregion wiki - #region Utility methods + + #region TAGS private void Tagit(Guid g, List tags) { if (g == Guid.Empty) return; @@ -727,10 +794,8 @@ var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); { tags.Add(t); } - } - private void SetTags(dynamic d, List tags) { dynamic dtags = new JArray(); @@ -739,117 +804,40 @@ var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); d.tags = dtags; } + #endregion tags - //export objects custom field data into jobject string - private string CustomFieldData(object biz, List dateFields) - { - dynamic d=new JObject(); - for (int x = 0; x < 10; x++) - { - object o = (object)biz.GetType().GetProperty("Custom"+x.ToString()).GetValue(biz, null); - string s = o.ToString(); - if (string.IsNullOrWhiteSpace(s)) - { - s = null; - } - if (s!=null && dateFields.Contains(x)) - { - //parse out to UTC date - DateTime dt=new DateTime(); - if (DateTime.TryParse(s, out dt)) - { - s = dt.ToUniversalTime().ToString("s"); - } - } - if(!string.IsNullOrWhiteSpace(s)) - d["c"+(x+1).ToString()] = s; - - } - return d.ToString(); - } - - static public ObjectCustomFields ObjectHasCustomFieldDataToExport(string sObject) - { - ObjectCustomFields ocf = ObjectCustomFields.GetItems(sObject); - if (ocf.Count == 0) - { - return null; - } - - foreach (ObjectCustomField f in ocf) - { - if (f.Visible) - return ocf; - } - return null; - } - - private async System.Threading.Tasks.Task> ExportCustomFieldSchema(ObjectCustomFields ocf, string v7CustomFieldObjectName, string RavenCustomTranslationKeyObjectName) - { - var ret = new List(); - //NOTE: this code inspired by winforApp::Util.cs PrepareCustomFieldsGrid method - dynamic d = new JObject(); - d.formkey = RavenCustomTranslationKeyObjectName; - dynamic dtemplate = new JArray(); - foreach (ObjectCustomField f in ocf) - { - if (f.Visible) - { - int n = Convert.ToInt32(f.FieldName.Replace("Custom", "")) + 1;//raven custom fields are 1 based, v7 are zero based - CustomFieldLocaleKeys.Add(RavenCustomTranslationKeyObjectName + n.ToString(), - util.LocaleText.GetLocalizedText(v7CustomFieldObjectName + ".Label." + f.FieldName)); - - dynamic dt = new JObject(); - dt.fld = RavenCustomTranslationKeyObjectName + "Custom" + n.ToString(); - dt.hide = false; - dt.required = false; - switch (f.FieldType) - { - case FormFieldDataTypes.Currency: - dt.type = util.AyaUiFieldDataType.Currency; - break; - case FormFieldDataTypes.DateOnly: - dt.type = util.AyaUiFieldDataType.Date; - ret.Add(n-1); - break; - case FormFieldDataTypes.DateTime: - dt.type = util.AyaUiFieldDataType.DateTime; - ret.Add(n - 1); - break; - case FormFieldDataTypes.Number: - dt.type = util.AyaUiFieldDataType.Decimal; - break; - case FormFieldDataTypes.Text: - dt.type = util.AyaUiFieldDataType.Text; - break; - case FormFieldDataTypes.TimeOnly: - dt.type = util.AyaUiFieldDataType.Time; - ret.Add(n - 1); - break; - case FormFieldDataTypes.TrueFalse: - dt.type = util.AyaUiFieldDataType.Bool; - break; - default: - dt.type = util.AyaUiFieldDataType.Text; - break; - } - dtemplate.Add(dt); - } - } - d.template = dtemplate.ToString(); - - //ok, were here because there *are* custom fields available - var a = await util.GetAsync("FormCustom/" + RavenCustomTranslationKeyObjectName); - var ctoken = util.CTokenFromResponse(a); - d.concurrencyToken = ctoken; - await util.PutAsync("FormCustom/" + RavenCustomTranslationKeyObjectName, d.ToString()); - - return ret; - } + + #region OLD JSON EXPORT STUFF + + + #region contract resolver + //public class ExcludeNamedPropertiesContractResolver : DefaultContractResolver + //{ + // private readonly List _excludeProperties; + + // public ExcludeNamedPropertiesContractResolver(List excludeProperties) + // { + // _excludeProperties = excludeProperties; + // } + + // protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) + // { + // IList properties = base.CreateProperties(type, memberSerialization); + + // // only serializer properties that start with the specified character + // //properties = properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList(); + // properties = properties.Where(p => !_excludeProperties.Contains(p.PropertyName)).ToList(); + + // return properties; + // } + //} + #endregion contract resolver + + //private static string EnsureValidFileName(string fileName) //{ // //make lower and replace spaces with dashes @@ -911,36 +899,9 @@ var DateCustomFields = await ExportCustomFieldSchema(ocf, "User", "User"); // } //} - #endregion - - #region contract resolver - public class ExcludeNamedPropertiesContractResolver : DefaultContractResolver - { - private readonly List _excludeProperties; - - public ExcludeNamedPropertiesContractResolver(List excludeProperties) - { - _excludeProperties = excludeProperties; - } - - protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) - { - IList properties = base.CreateProperties(type, memberSerialization); - - // only serializer properties that start with the specified character - //properties = properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList(); - properties = properties.Where(p => !_excludeProperties.Contains(p.PropertyName)).ToList(); - - return properties; - } - } - #endregion contract resolver - + #endregion old //eoc - - - } //eons