Пример #1
0
        /// <summary>
        /// Serialize 2SexyContent data to an xml string.
        /// </summary>
        /// <param name="zoneId">ID of 2SexyContent zone</param>
        /// <param name="applicationId">ID of 2SexyContent application</param>
        /// <param name="contentTypeId">ID of 2SexyContent type</param>
        /// <param name="languageSelected">Language of the data to be serialized (null for all languages)</param>
        /// <param name="languageFallback">Language fallback of the system</param>
        /// <param name="languageScope">Languages supported of the system</param>
        /// <param name="languageReference">How value references to other languages are handled</param>
        /// <param name="resourceReference">How value references to files and pages are handled</param>
        /// <param name="selectedIds">array of IDs to export only these</param>
        /// <returns>A string containing the xml data</returns>
        public string CreateXml(int zoneId, int applicationId, int contentTypeId, string languageSelected, string languageFallback, IEnumerable<string> languageScope, LanguageReferenceExport languageReference, ResourceReferenceExport resourceReference, int[] selectedIds)
        {
            var contentType = GetContentType(zoneId, applicationId, contentTypeId);
            if (contentType == null)
                return null;

            var languages = new List<string>();
            if (!string.IsNullOrEmpty(languageSelected))
            {
                languages.Add(languageSelected);
            }
            else if (languageScope.Any())
            {   // Export all languages
                languages.AddRange(languageScope);
            }
            else
            {
                languages.Add(string.Empty);
            }

            var documentRoot = GetDocumentRoot(null);
            var document = GetDocument(documentRoot);

            var entities = contentType.Entities.Where(entity => entity.ChangeLogIDDeleted == null);
            if (selectedIds != null && selectedIds.Length > 0)
                entities = entities.Where(e => selectedIds.Contains(e.EntityID));

            foreach (var entity in entities)
            {

                foreach (var language in languages)
                {
                    var documentElement = GetDocumentEntityElement(entity.EntityGUID, language, contentType.Name);
                    documentRoot.Add(documentElement);

                    var attributes = contentType.GetAttributes();
                    foreach (var attribute in attributes)
                    {
                        if (attribute.Type == "Entity")
                        {   // Handle separately
                            documentElement.AppendEntityReferences(entity, attribute);
                        }
                        else if (languageReference.IsResolve())
                        {
                            documentElement.AppendValueResolved(entity, attribute, language, languageFallback, resourceReference);
                        }
                        else
                        {
                            documentElement.AppendValueReferenced(entity, attribute, language, languageFallback, languageScope, languages.Count() > 1, resourceReference);
                        }
                    }
                }
            }

            return document.ToString();
        }
Пример #2
0
 public HttpResponseMessage ExportContent(int appId, string language, string defaultLanguage, string contentType,
     RecordExport recordExport, ResourceReferenceExport resourcesReferences,
     LanguageReferenceExport languageReferences, string selectedIds = null)
 {
     // do security check
     if(!PortalSettings.UserInfo.IsInRole(PortalSettings.AdministratorRoleName))// todo: copy to 8.5 "Administrators")) // note: user.isinrole didn't work
         throw new HttpRequestException("Needs admin permissions to do this");
     return eavCtc.ExportContent(appId, language, defaultLanguage, contentType, recordExport, resourcesReferences,
         languageReferences, selectedIds);
 }
Пример #3
0
 public HttpResponseMessage ExportContent(int appId, string language, string defaultLanguage, string contentType,
                                          RecordExport recordExport, ResourceReferenceExport resourcesReferences,
                                          LanguageReferenceExport languageReferences)
 {
     // do security check
     if (!PortalSettings.UserInfo.IsInRole("Administrators")) // note: user.isinrole didn't work
     {
         throw new HttpRequestException("Needs admin permissions to do this");
     }
     return(eavCtc.ExportContent(appId, language, defaultLanguage, contentType, recordExport, resourcesReferences,
                                 languageReferences));
 }
Пример #4
0
        /// <summary>
        /// Append an element to this. The element will get the name of the attribute, and if possible the value will 
        /// be referenced to another language (for example [ref(en-US,ro)].
        /// </summary>
        public static void AppendValueReferenced(this XElement element, Eav.Entity entity, Attribute attribute, string language, string languageFallback, IEnumerable<string> languageScope, bool referenceParentLanguagesOnly, ResourceReferenceExport resourceReferenceOption)
        {
            var valueName = attribute.StaticName;
            var value = entity.GetAttributeValue(attribute, language);
            if (value == null)
            {
                element.Append(valueName, "[]");
                return;
            }

            var valueLanguage = value.ValuesDimensions.Select(reference => reference.Dimension.ExternalKey)
                                         .FirstOrDefault(l => l == language); // value.GetLanguage(language);
            if (valueLanguage == null)
            {   // If no language is found, serialize the plain value
                element.AppendValue(valueName, value, resourceReferenceOption);
                return;
            }

            var valueLanguagesReferenced = value.GetLanguagesReferenced(language, true)
                                                .OrderBy(lang => lang != languageFallback)
                                                .ThenBy(lan => lan);;
            if (valueLanguagesReferenced.Count() == 0)
            {   // If the value is a head value, serialize the plain value
                element.AppendValue(valueName, value, resourceReferenceOption);
                return;
            }

            var valueLanguageReferenced = default(string);
            var valueLanguageReadOnly = value.IsLanguageReadOnly(language);
            if (referenceParentLanguagesOnly)
            {
                valueLanguageReferenced = valueLanguagesReferenced.FirstOrDefault
                    (
                        lang => languageScope.IndexOf(lang) < languageScope.IndexOf(language)
                    );
            }
            else if (valueLanguageReadOnly)
            {   // If one language is serialized, do not serialize read-write values
                // as references
                valueLanguageReferenced = valueLanguagesReferenced.First();
            }

            if (valueLanguageReferenced == null)
            {
                element.AppendValue(valueName, value, resourceReferenceOption);
                return;
            }

            element.Append(valueName, string.Format("[ref({0},{1})]", valueLanguageReferenced, valueLanguageReadOnly ? "ro" : "rw"));
        }
Пример #5
0
        public HttpResponseMessage ExportContent(int appId, string language, string defaultLanguage, string contentType,
            RecordExport recordExport, ResourceReferenceExport resourcesReferences,
            LanguageReferenceExport languageReferences, string selectedIds = null)
        {
            AppId = appId;

            // todo: continue here!
            var ct = CurrentContext.AttribSet.GetAttributeSetWithEitherName(contentType);
            var contentTypeId = ct.AttributeSetID;// GetContentTypeId(contentType);
            var contentTypeName = ct.Name;// GetContentTypeName(contentType);
            var contextLanguages = GetContextLanguages();

            // check if we have an array of ids
            int[] ids = null;
            try
            {
                if (recordExport == RecordExport.Selection && !string.IsNullOrWhiteSpace(selectedIds))
                    ids = selectedIds.Split(',').Select(int.Parse).ToArray();
            }
            catch (Exception e)
            {
                throw new Exception("trouble finding selected IDs to export", e);
            }

            var fileContent = recordExport == RecordExport.Blank
                ? new XmlExport().CreateBlankXml(CurrentContext.ZoneId, appId, contentTypeId) 
                : new XmlExport().CreateXml(CurrentContext.ZoneId, appId, contentTypeId, language ?? "", defaultLanguage, contextLanguages, languageReferences, resourcesReferences, ids);

            var fileName = $"2sxc {contentTypeName.Replace(" ", "-")} {language} {(recordExport == RecordExport.Blank ? "Template" : "Data")} {DateTime.Now.ToString("yyyyMMddHHmmss")}.xml";

            var response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(fileContent);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
            response.Content.Headers.ContentLength = fileContent.Length;
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = fileName
            };
            return response;
        }
Пример #6
0
 /// <summary>
 /// Append an element to this. The element will have the value of the EavValue. File and page references 
 /// can optionally be resolved.
 /// </summary>
 public static void AppendValue(this XElement element, XName name, EavValue value, ResourceReferenceExport resourceReferenceOption)
 {
     if (value == null)
     {
         element.Append(name, "[]");
     }
     else if (value.Value == null)
     {
         element.Append(name, "[]");
     }
     else if (resourceReferenceOption.IsResolve())
     {
         element.Append(name, value.ResolveValueReference());
     }
     else if (value.Value == string.Empty)
     {
         element.Append(name, "[\"\"]");
     }
     else
     {
         element.Append(name, value.Value);
     }
 }
Пример #7
0
        /// <summary>
        /// Append an element to this. The element will get the name of the attribute, and if possible the value will
        /// be referenced to another language (for example =ref(en-US,ro).
        /// </summary>
        public static void AppendValueReferenced(this XElement element, Entity entity, Attribute attribute, string language, string languageFallback, IEnumerable <string> languageScope, bool referenceParentLanguagesOnly, ResourceReferenceExport resourceReferenceOption)
        {
            var valueName = attribute.StaticName;
            var value     = entity.GetAttributeValue(attribute, language);

            if (value == null)
            {
                element.Append(valueName, "=default()");
                return;
            }

            var valueLanguage = value.GetLanguage(language);

            if (valueLanguage == null)
            {   // If no language is found, serialize the plain value
                element.AppendValue(valueName, value, resourceReferenceOption);
                return;
            }

            var valueLanguagesReferenced = value.GetLanguagesReferenced(language, true)
                                           .OrderBy(lang => lang != languageFallback)
                                           .ThenBy(lan => lan);;

            if (valueLanguagesReferenced.Count() == 0)
            {   // If the value is a head value, serialize the plain value
                element.AppendValue(valueName, value, resourceReferenceOption);
                return;
            }

            var valueLanguageReferenced = default(string);
            var valueLanguageReadOnly   = value.IsLanguageReadOnly(language);

            if (referenceParentLanguagesOnly)
            {
                valueLanguageReferenced = valueLanguagesReferenced.FirstOrDefault
                                          (
                    lang => languageScope.IndexOf(lang) < languageScope.IndexOf(language)
                                          );
            }
            else if (valueLanguageReadOnly)
            {   // If one language is serialized, do not serialize read-write values
                // as references
                valueLanguageReferenced = valueLanguagesReferenced.First();
            }

            if (valueLanguageReferenced == null)
            {
                element.AppendValue(valueName, value, resourceReferenceOption);
                return;
            }

            element.Append(valueName, string.Format("=ref({0},{1})", valueLanguageReferenced, valueLanguageReadOnly ? "ro" : "rw"));
        }
Пример #8
0
        /// <summary>
        /// Append an element to this. If the attribute is named xxx and the value is 4711 in the language specified,
        /// the element appended will be <xxx>4711</xxx>. File and page references can be resolved optionally.
        /// </summary>
        public static void AppendValueResolved(this XElement element, Entity entity, Attribute attribute, string language, string languageFallback, ResourceReferenceExport resourceReferenceOption)
        {
            var valueName = attribute.StaticName;
            var value     = entity.GetAttributeValue(attribute, language, languageFallback);

            element.AppendValue(valueName, value, resourceReferenceOption);
        }
Пример #9
0
 /// <summary>
 /// Append an element to this. The element will have the value of the EavValue. File and page references
 /// can optionally be resolved.
 /// </summary>
 public static void AppendValue(this XElement element, XName name, EavValue value, ResourceReferenceExport resourceReferenceOption)
 {
     if (value == null)
     {
         element.Append(name, "=default()");
     }
     else if (resourceReferenceOption.IsResolve())
     {
         element.Append(name, value.ResolveValueReference());
     }
     else
     {
         element.Append(name, value.Value);
     }
 }
Пример #10
0
 /// <summary>
 /// Append an element to this. If the attribute is named xxx and the value is 4711 in the language specified, 
 /// the element appended will be <xxx>4711</xxx>. File and page references can be resolved optionally.
 /// </summary>
 public static void AppendValueResolved(this XElement element, Eav.Entity entity, Attribute attribute, string language, string languageFallback, ResourceReferenceExport resourceReferenceOption)
 {
     var valueName = attribute.StaticName;
     var value = entity.GetAttributeValue(attribute, language, languageFallback);
     element.AppendValue(valueName, value, resourceReferenceOption);
 }
Пример #11
0
        /// <summary>
        /// Serialize 2SexyContent data to an xml string.
        /// </summary>
        /// <param name="zoneId">ID of 2SexyContent zone</param>
        /// <param name="applicationId">ID of 2SexyContent application</param>
        /// <param name="contentTypeId">ID of 2SexyContent type</param>
        /// <param name="languageSelected">Language of the data to be serialized (null for all languages)</param>
        /// <param name="languageFallback">Language fallback of the system</param>
        /// <param name="languageScope">Languages supported of the system</param>
        /// <param name="languageReference">How value references to other languages are handled</param>
        /// <param name="resourceReference">How value references to files and pages are handled</param>
        /// <returns>A string containing the xml data</returns>
        public string CreateXml(int zoneId, int applicationId, int contentTypeId, string languageSelected, string languageFallback, IEnumerable <string> languageScope, LanguageReferenceExport languageReference, ResourceReferenceExport resourceReference)
        {
            var contentType = GetContentType(zoneId, applicationId, contentTypeId);

            if (contentType == null)
            {
                return(null);
            }

            var languages = new List <string>();

            if (!string.IsNullOrEmpty(languageSelected))
            {
                languages.Add(languageSelected);
            }
            else if (languageScope.Any())
            {   // Export all languages
                languages.AddRange(languageScope);
            }
            else
            {
                languages.Add(string.Empty);
            }

            var documentRoot = GetDocumentRoot(contentType.Name, null);
            var document     = GetDocument(documentRoot);

            var entities = contentType.Entities.Where(entity => entity.ChangeLogIDDeleted == null);

            foreach (var entity in entities)
            {
                foreach (var language in languages)
                {
                    var documentElement = GetDocumentEntityElement(entity.EntityGUID, language);
                    documentRoot.Add(documentElement);

                    var attributes = contentType.GetAttributes();
                    foreach (var attribute in attributes)
                    {
                        if (languageReference.IsResolve())
                        {
                            documentElement.AppendValueResolved(entity, attribute, language, languageFallback, resourceReference);
                        }
                        else
                        {
                            documentElement.AppendValueReferenced(entity, attribute, language, languageFallback, languageScope, languages.Count() > 1, resourceReference);
                        }
                    }
                }
            }

            return(document.ToString());
        }
 public static bool IsResolve(this ResourceReferenceExport option)
 {
     return(option == ResourceReferenceExport.Resolve);
 }