Exemplo n.º 1
0
        public async Task <IActionResult> GetExportTemplateByObjectId(string id, TemplateType templateType)
        {
            ExportTemplateByObjectIdResult template = await GetValidExportTemplateByIdAndType(id, templateType);

            ObjectExportTemplate exportTemplate = new ObjectExportTemplate();

            exportTemplate.IsDefaultCode  = template.IsDefault;
            exportTemplate.ExportTemplate = TranslateTemplateLabel(template.Template);
            string objectName = await GetObjectNameByType(id, templateType);

            if (!string.IsNullOrEmpty(objectName))
            {
                exportTemplate.ExportTemplate.Label = objectName + " (" + exportTemplate.ExportTemplate.Label + ")";
            }

            return(Ok(exportTemplate));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the valid export template by object id and template type
        /// </summary>
        /// <param name="id">Object Id</param>
        /// <param name="templateType">Template Type</param>
        /// <returns>Valid Export Template</returns>
        private async Task <ExportTemplateByObjectIdResult> GetValidExportTemplateByIdAndType(string id, TemplateType templateType)
        {
            ExportTemplateByObjectIdResult result = new ExportTemplateByObjectIdResult();

            result.IsDefault = false;

            GoNorthProject project = await _projectDbAccess.GetDefaultProject();

            ExportTemplate template = await _exportTemplateDbAccess.GetTemplateByCustomizedObjectId(project.Id, id);

            if (template == null)
            {
                result.IsDefault = true;

                FlexFieldObject flexFieldObject = null;
                if (templateType == TemplateType.ObjectNpc)
                {
                    flexFieldObject = await _npcDbAccess.GetFlexFieldObjectById(id);
                }
                else if (templateType == TemplateType.ObjectItem)
                {
                    flexFieldObject = await _itemDbAccess.GetFlexFieldObjectById(id);
                }
                else if (templateType == TemplateType.ObjectSkill)
                {
                    flexFieldObject = await _skillDbAccess.GetFlexFieldObjectById(id);
                }

                if (flexFieldObject != null)
                {
                    template = await _exportTemplateDbAccess.GetTemplateByCustomizedObjectId(project.Id, flexFieldObject.TemplateId);
                }

                if (template == null)
                {
                    template = await _defaultTemplateProvider.GetDefaultTemplateByType(project.Id, templateType);
                }
            }

            result.Template = template;
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Exports an object
        /// </summary>
        /// <param name="exportFormat">Format to export (Script, JSON or LanguageFile)</param>
        /// <param name="id">Id of the object to export</param>
        /// <param name="templateType">Template type</param>
        /// <returns>Export result</returns>
        private async Task <ExportObjectResult> RunExportObject(string exportFormat, string id, TemplateType templateType)
        {
            // Check Access
            if ((templateType == TemplateType.ObjectNpc && !User.IsInRole(RoleNames.Kortisto)) ||
                (templateType == TemplateType.ObjectItem && !User.IsInRole(RoleNames.Styr)) ||
                (templateType == TemplateType.ObjectSkill && !User.IsInRole(RoleNames.Evne)))
            {
                throw new UnauthorizedAccessException();
            }

            // Get Exporter
            string exporterKey = exportFormat.ToLowerInvariant();

            if (!_exporters.ContainsKey(exporterKey))
            {
                throw new InvalidOperationException("Unknown Export Format");
            }
            IObjectExporter exporter = _exporters[exporterKey];

            // Get Objects
            bool             objectFound = false;
            ExportObjectData objectData  = new ExportObjectData();

            if (templateType == TemplateType.ObjectNpc)
            {
                KortistoNpc npc = await _npcDbAccess.GetFlexFieldObjectById(id);

                if (npc != null)
                {
                    objectData.ExportData.Add(ExportConstants.ExportDataObject, npc);
                    objectData.ExportData.Add(ExportConstants.ExportDataObjectType, ExportConstants.ExportObjectTypeNpc);

                    TaleDialog dialog = await _dialogDbAccess.GetDialogByRelatedObjectId(id);

                    objectData.ExportData.Add(ExportConstants.ExportDataDialog, dialog);

                    objectFound = true;
                }
            }
            else if (templateType == TemplateType.ObjectItem)
            {
                StyrItem item = await _itemDbAccess.GetFlexFieldObjectById(id);

                if (item != null)
                {
                    objectData.ExportData.Add(ExportConstants.ExportDataObject, item);
                    objectData.ExportData.Add(ExportConstants.ExportDataObjectType, ExportConstants.ExportObjectTypeItem);
                    objectFound = true;
                }
            }
            else if (templateType == TemplateType.ObjectSkill)
            {
                EvneSkill skill = await _skillDbAccess.GetFlexFieldObjectById(id);

                if (skill != null)
                {
                    objectData.ExportData.Add(ExportConstants.ExportDataObject, skill);
                    objectData.ExportData.Add(ExportConstants.ExportDataObjectType, ExportConstants.ExportObjectTypeSkill);
                    objectFound = true;
                }
            }
            else
            {
                throw new InvalidOperationException("Template Type not supported for exporting");
            }

            if (!objectFound)
            {
                throw new KeyNotFoundException();
            }

            // Get Template
            ExportTemplateByObjectIdResult template = await GetValidExportTemplateByIdAndType(id, templateType);

            // Run Export
            ExportObjectResult result = await exporter.ExportObject(template.Template, objectData);

            result.ObjectFilename = ((FlexFieldObject)objectData.ExportData[ExportConstants.ExportDataObject]).Name;
            string regexSearch      = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
            Regex  illegalCharRegex = new Regex(string.Format("[{0}]", regexSearch));

            result.ObjectFilename = illegalCharRegex.Replace(result.ObjectFilename, string.Empty);
            return(result);
        }