private LinkVM ConvertToLinkVM(dynamic obj) { var result = new LinkVM(); if (obj != null) { result.Url = obj.Value; result.Text = obj.Text; } return(result); }
public void StoreInspectExpandoFields(List <ContentPart> listpart, string key, object value) { var fields = listpart.SelectMany(x => x.Fields.Where(f => f.Name == key)); if (fields != null) { var fieldObj = fields.FirstOrDefault(); if (fieldObj != null) { // provo a registrare il dato in uno dei fields // non posso fare questo // fieldObj.GetType().GetProperty("Value").SetValue(fieldObj, value, null); // perchè non regge il tipo nullabile var theContentItem = listpart.ElementAt(0).ContentItem; string tipofield = fieldObj.GetType().Name; if (tipofield == typeof(EnumerationField).Name) { RegistraValoreEnumerator(fieldObj, "SelectedValues", value); } else if (tipofield == typeof(DateTimeField).Name) { RegistraValore(fieldObj, "DateTime", value); } else if (tipofield == typeof(ContentPickerField).Name || tipofield == typeof(MediaLibraryPickerField).Name) { RegistraValoreContentPickerField(fieldObj, "Ids", value); } else if (tipofield == typeof(TaxonomyField).Name) { var taxobase = _taxonomyService.GetTaxonomyByName(fieldObj.PartFieldDefinition.Settings["TaxonomyFieldSettings.Taxonomy"]); List <TaxoVM> second = null; if (value is List <TaxoVM> ) { second = (List <TaxoVM>)value; } else { second = ConvertToVM((List <dynamic>)value); } List <Int32> ElencoCategorie = second.Select(x => x.Id).ToList(); List <TermPart> ListTermPartToAdd = new List <TermPart>(); if (_taxonomyService.GetTerm(ElencoCategorie.FirstOrDefault()) == null && ElencoCategorie.Count > 0) { throw new Exception("Field " + key + " Taxonomy term with id=" + ElencoCategorie[0].ToString() + " not exist"); } else { #region [ Tassonomia in Lingua ] foreach (Int32 idtermine in ElencoCategorie) { if (_taxonomyService.GetTerm(idtermine) != null) { var taxo_sended_user = _taxonomyService.GetTaxonomy(_taxonomyService.GetTerm(idtermine).TaxonomyId); TermPart termine_selezionato = taxo_sended_user.Terms.Where(x => x.Id == idtermine).FirstOrDefault(); if (theContentItem.As <LocalizationPart>() == null || theContentItem.ContentType == "User") // se il contenuto non ha localization oppure è user salvo il mastercontent del termine { Int32 idmaster = 0; if (termine_selezionato.ContentItem.As <LocalizationPart>() == null) { idmaster = termine_selezionato.ContentItem.Id; } else if (termine_selezionato.ContentItem.As <LocalizationPart>().MasterContentItem == null) { idmaster = termine_selezionato.ContentItem.As <LocalizationPart>().Id; } else { idmaster = termine_selezionato.ContentItem.As <LocalizationPart>().MasterContentItem.Id; } TermPart toAdd = taxobase.Terms.Where(x => x.Id == idmaster).FirstOrDefault(); if (toAdd == null) { toAdd = taxobase.Terms.Where(x => x.ContentItem.As <LocalizationPart>().MasterContentItem.Id == idmaster).FirstOrDefault(); } if (ListTermPartToAdd.Contains(toAdd) == false) { ListTermPartToAdd.Add(toAdd); } } else // se il contenuto ha localization e non è user salvo il termine come mi viene passato // TODO: testare pertinenza della lingua Contenuto in italianao=>termine in italiano { TermPart toAdd = termine_selezionato; if (ListTermPartToAdd.Contains(toAdd) == false) { ListTermPartToAdd.Add(toAdd); } } } } #endregion [ Tassonomia in Lingua ] } _taxonomyService.UpdateTerms(theContentItem, ListTermPartToAdd, fieldObj.Name); } else if (tipofield == typeof(LinkField).Name) { LinkVM second = null; if (value is LinkVM) { second = (LinkVM)value; } else { second = ConvertToLinkVM(value); } RegistraValore(fieldObj, "Value", second.Url); RegistraValore(fieldObj, "Text", second.Text); } else { RegistraValore(fieldObj, "Value", value); } } } else { //provo a registrare il dato nella proprieta del current user if (!(key.IndexOf(".") > 0) && key.ToLower() != "contenttype" && key.ToLower() != "id" && key.ToLower() != "language") { throw new Exception("Field " + key + " not in contentitem"); } } }
public void StoreInspectExpandoFields(List <ContentPart> listpart, string key, object value) { var fields = listpart.SelectMany(x => x.Fields.Where(f => f.Name == key)); if (fields != null) { var fieldObj = fields.FirstOrDefault(); if (fieldObj != null) { // provo a registrare il dato in uno dei fields // non posso fare questo // fieldObj.GetType().GetProperty("Value").SetValue(fieldObj, value, null); // perchè non regge il tipo nullabile var theContentItem = listpart.ElementAt(0).ContentItem; string tipofield = fieldObj.GetType().Name; if (tipofield == typeof(EnumerationField).Name) { RegistraValoreEnumerator(fieldObj, "SelectedValues", value); } else if (tipofield == typeof(DateTimeField).Name) { RegistraValore(fieldObj, "DateTime", value); } else if (tipofield == typeof(ContentPickerField).Name || tipofield == typeof(MediaLibraryPickerField).Name) { RegistraValoreContentPickerField(fieldObj, "Ids", value); } else if (tipofield == typeof(TaxonomyField).Name) { var taxobase = _taxonomyService.GetTaxonomyByName(fieldObj.PartFieldDefinition.Settings["TaxonomyFieldSettings.Taxonomy"]); List <TaxoVM> second = null; if (value is List <TaxoVM> ) { second = (List <TaxoVM>)value; } else { second = ConvertToVM((List <dynamic>)value); } List <Int32> ElencoCategorie = second.Select(x => x.Id).ToList(); List <TermPart> ListTermPartToAdd = new List <TermPart>(); if (_taxonomyService.GetTerm(ElencoCategorie.FirstOrDefault()) == null && ElencoCategorie.Count > 0) { throw new Exception("Field " + key + " Taxonomy term with id=" + ElencoCategorie[0].ToString() + " not exist"); } else { #region [ Tassonomia in Lingua ] foreach (Int32 idtermine in ElencoCategorie) { var term = _taxonomyService.GetTerm(idtermine); if (term != null) { var taxo_sended_user = _taxonomyService.GetTaxonomy(_taxonomyService.GetTerm(idtermine).TaxonomyId); TermPart termine_selezionato = taxo_sended_user.Terms.Where(x => x.Id == idtermine).FirstOrDefault(); if (theContentItem.As <LocalizationPart>() == null || theContentItem.ContentType == "User") // se il contenuto non ha localization oppure è user salvo il mastercontent del termine { var termTranslations = _localizationServices.GetLocalizations(term).Select(x => x.As <TermPart>()); //get all translations for the term ListTermPartToAdd.Add(term); //adds the original term ListTermPartToAdd.AddRange(termTranslations); // adds the translations of term } else // se il contenuto ha localization e non è user salvo il termine come mi viene passato // TODO: testare pertinenza della lingua Contenuto in italianao=>termine in italiano { TermPart toAdd = termine_selezionato; if (ListTermPartToAdd.Contains(toAdd) == false) { ListTermPartToAdd.Add(toAdd); } } } } ListTermPartToAdd = ListTermPartToAdd.Distinct().ToList(); //removes duplicated #endregion [ Tassonomia in Lingua ] } _taxonomyService.UpdateTerms(theContentItem, ListTermPartToAdd, fieldObj.Name); } else if (tipofield == typeof(LinkField).Name) { LinkVM second = null; if (value is LinkVM) { second = (LinkVM)value; } else { second = ConvertToLinkVM(value); } RegistraValore(fieldObj, "Value", second.Url); RegistraValore(fieldObj, "Text", second.Text); } else if (tipofield == "SecureFileField") { // Using value as dynamic because value is a dynamic ExpandoObject. // The RegistraValore routine throws an exception otherwise (Object must implement IConvertible). // I need to check if the ExpandoObject (inheriting IDictionary) contains the property I'm looking for. if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("Url")) { RegistraValore(fieldObj, "Url", (value as dynamic).Url); } if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("AlternateText")) { RegistraValore(fieldObj, "AlternateText", (value as dynamic).AlternateText); } if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("Class")) { RegistraValore(fieldObj, "Class", (value as dynamic).Class); } if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("Style")) { RegistraValore(fieldObj, "Style", (value as dynamic).Style); } if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("Alignment")) { RegistraValore(fieldObj, "Alignment", (value as dynamic).Alignment); } if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("Width")) { RegistraValore(fieldObj, "Width", (value as dynamic).Width); } if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("Height")) { RegistraValore(fieldObj, "Height", (value as dynamic).Height); } if (((IDictionary <string, object>)((dynamic)value)).ContainsKey("Upload")) { RegistraValore(fieldObj, "Upload", (value as dynamic).Height); } if ((((IDictionary <string, object>)((dynamic)value)).ContainsKey("Base64File")) && (((IDictionary <string, object>)((dynamic)value)).ContainsKey("FileName"))) { RegistraFile(fieldObj, (value as dynamic).Base64File, (value as dynamic).FileName); } } else { RegistraValore(fieldObj, "Value", value); } } } else { //provo a registrare il dato nella proprieta del current user if (!(key.IndexOf(".") > 0) && key.ToLower() != "contenttype" && key.ToLower() != "id" && key.ToLower() != "language") { throw new Exception("Field " + key + " not in contentitem"); } } }