Exemplo n.º 1
0
        private ChromelyResponse ExportAllConversations(ChromelyRequest request)
        {
            IDictionary <string, object> parameters = request.Parameters;
            string  postDataJson = (string)request.PostData.EnsureJson();
            JObject data         = JObject.Parse(postDataJson);

            ConversationService conversationService = ConversationService.getInstance();

            List <ConversationAsset> conversations = conversationService.LoadConversations();

            foreach (ConversationAsset conversationAsset in conversations)
            {
                conversationService.SaveConversation(conversationAsset, FileFormat.JSON);
            }

            try {
                string conversationAssetString = data["conversationAsset"].ToString();

                if (conversationAssetString != "")
                {
                    ConversationAsset conversationAsset = JsonConvert.DeserializeObject <ConversationAsset>(conversationAssetString);
                    conversationService.SaveConversation(conversationAsset, FileFormat.JSON);
                }
            } catch (Exception e) {
                Log.Error(e);
            }

            List <ConversationAsset> updatedConversations = conversationService.LoadConversations();
            string           conversationsJson            = JsonConvert.SerializeObject(updatedConversations);
            ChromelyResponse response = new ChromelyResponse();

            response.Data = conversationsJson;
            return(response);
        }
Exemplo n.º 2
0
        public List <ConversationAsset> LoadConversations()
        {
            List <ConversationAsset> conversations = new List <ConversationAsset>();

            // Guard - Only load if working directory is set
            if (FileSystemService.getInstance().WorkingDirectory != null)
            {
                string[] conversationPaths = Directory.GetFiles(FileSystemService.getInstance().WorkingDirectory);

                foreach (string conversationPath in conversationPaths)
                {
                    // GUARD - Only process .bytes files
                    if (!conversationPath.EndsWith(".bytes"))
                    {
                        continue;
                    }

                    // This is a ConversationSpeakerList instead of a Conversation
                    if (conversationPath.Contains(".cvsl.bytes"))
                    {
                        SpeakerListAsset speakerListAsset = LoadSpeakersList(conversationPath);
                        SaveJsonSpeakerList(speakerListAsset, speakerListAsset.FilePath);
                    }
                    else
                    {
                        ConversationAsset conversationAsset = LoadConversation(conversationPath);
                        conversations.Add(conversationAsset);
                    }
                }
            }

            return(conversations);
        }
Exemplo n.º 3
0
 public void SaveConversation(ConversationAsset conversationAsset, FileFormat fileFormat, string path)
 {
     if (fileFormat == FileFormat.JSON)
     {
         SaveJsonConversation(conversationAsset, path);
     }
     else if (fileFormat == FileFormat.BINARY)
     {
         SaveBinaryConversation(conversationAsset, path);
     }
 }
Exemplo n.º 4
0
        private void SaveBinaryConversation(ConversationAsset conversationAsset, string path)
        {
            RuntimeTypeModel runtimeTypeModel = TypeModel.Create();

            try {
                using (FileStream fileStream = new FileStream(Path.ChangeExtension(path, ".bytes"), FileMode.Create)) {
                    runtimeTypeModel.Serialize(fileStream, conversationAsset.Conversation);
                }
            } catch (Exception error) {
                Log.Error(error.ToString());
            }
        }
Exemplo n.º 5
0
        public ConversationAsset ImportConversation(string filePath)
        {
            ConversationAsset conversationAsset = null;

            try {
                string       json         = File.ReadAllText(filePath);
                Conversation conversation = JsonConvert.DeserializeObject <Conversation>(json);
                conversationAsset = new ConversationAsset(filePath, conversation);
            } catch (Exception error) {
                Log.Error(error.ToString());
                return(null);
            }

            return(conversationAsset);
        }
Exemplo n.º 6
0
        public ConversationAsset LoadConversation(string filePath)
        {
            ConversationAsset conversationAsset = null;
            RuntimeTypeModel  runtimeTypeModel  = TypeModel.Create();

            try {
                using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) {
                    Conversation conversation = runtimeTypeModel.Deserialize(fileStream, null, typeof(Conversation)) as Conversation;
                    conversationAsset = new ConversationAsset(filePath, conversation);
                }
            } catch (Exception error) {
                Log.Error(error.ToString());
                return(null);
            }

            return(conversationAsset);
        }
Exemplo n.º 7
0
        private ChromelyResponse ImportConversation(ChromelyRequest request)
        {
            IDictionary <string, object> requestParams = request.Parameters;
            string path = (string)requestParams["path"];

            ConversationService conversationService = ConversationService.getInstance();

            try {
                ConversationAsset conversationAsset = conversationService.ImportConversation(path);
                conversationService.SaveConversation(conversationAsset, FileFormat.BINARY);
            } catch (Exception e) {
                Log.Error(e);
            }

            List <ConversationAsset> conversations = conversationService.LoadConversations();
            string           conversationsJson     = JsonConvert.SerializeObject(conversations);
            ChromelyResponse response = new ChromelyResponse();

            response.Data = conversationsJson;
            return(response);
        }
Exemplo n.º 8
0
        private ChromelyResponse UpdateConversations(ChromelyRequest request)
        {
            IDictionary <string, object> parameters = request.Parameters;
            string  postDataJson = (string)request.PostData.EnsureJson();
            JObject data         = JObject.Parse(postDataJson);

            ConversationService conversationService = ConversationService.getInstance();

            try {
                ConversationAsset conversationAsset = JsonConvert.DeserializeObject <ConversationAsset>(data["conversationAsset"].ToString());
                conversationService.SaveConversation(conversationAsset, FileFormat.BINARY);
            } catch (Exception e) {
                Log.Error(e);
            }

            List <ConversationAsset> conversations = conversationService.LoadConversations();
            string           conversationsJson     = JsonConvert.SerializeObject(conversations);
            ChromelyResponse response = new ChromelyResponse();

            response.Data = conversationsJson;
            return(response);
        }
Exemplo n.º 9
0
 public void SaveConversation(ConversationAsset conversationAsset, FileFormat fileFormat)
 {
     SaveConversation(conversationAsset, fileFormat, conversationAsset.FilePath);
 }
Exemplo n.º 10
0
 private void SaveJsonConversation(ConversationAsset conversationAsset, string path)
 {
     File.WriteAllText(Path.ChangeExtension(path, ".json"), JsonConvert.SerializeObject(conversationAsset.Conversation, Formatting.Indented));
 }