// download all the attachments of a wit to the local folders public void getAttachment(String witId, String fileAssociationId, String fileName, String userProfilepath) { AccessTokenDao accesstokenDao = new AccessTokenDao(); String token = accesstokenDao.getAccessToken(Common.userName); String url = Resource.endpoint + "wittyparrot/api/attachments/associationId/" + fileAssociationId + ""; var client = new RestClient(); client.BaseUrl = new Uri(url); var request = new RestRequest(); request.Method = Method.GET; request.Parameters.Clear(); request.AddParameter("Authorization", "Bearer " + token, ParameterType.HttpHeader); request.RequestFormat = DataFormat.Json; // execute the request IRestResponse response = client.Execute(request); if (response.ErrorException != null) { var statusMessage = RestUtils.getErrorMessage(response.StatusCode); MessageBox.Show(statusMessage == "" ? response.StatusDescription : statusMessage); var myException = new ApplicationException(response.StatusDescription, response.ErrorException); throw myException; } byte[] r = client.DownloadData(request); String fullPath = userProfilepath + "//files//attachments//"; if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } // save the file details to docs table Docs doc = new Docs(); doc.docId = fileName.GetHashCode().ToString(); doc.localPath = fullPath; doc.fileName = fileName; doc.witId = witId; AttachmentDao attachmentDao = new AttachmentDao(); attachmentDao.saveDocs(doc); File.WriteAllBytes(fullPath + fileName, r); }
// Saves the doc info into the db, it includes the name, mime and local path of the attchment files public void saveDocs(Docs docs) { try { var docsInsertQuery = Resource.ResourceManager.GetString("docs_insert"); sql_con = new SQLiteConnection(Common.localDatabasePath, true); sql_cmd = new SQLiteCommand(docsInsertQuery, sql_con); sql_cmd.Parameters.Add("@doc_id", DbType.String); sql_cmd.Parameters["@doc_id"].Value = docs.docId; sql_cmd.Parameters.Add("@file_name", DbType.String); sql_cmd.Parameters["@file_name"].Value = docs.fileName; sql_cmd.Parameters.Add("@mime_type", DbType.String); sql_cmd.Parameters["@mime_type"].Value = docs.mimeType; sql_cmd.Parameters.Add("@size", DbType.String); sql_cmd.Parameters["@size"].Value = docs.size; sql_cmd.Parameters.Add("@wit_id", DbType.String); sql_cmd.Parameters["@wit_id"].Value = docs.witId; sql_cmd.Parameters.Add("@local_path", DbType.String); sql_cmd.Parameters["@local_path"].Value = docs.localPath; sql_cmd.Parameters.Add("@container_dir_path", DbType.String); sql_cmd.Parameters["@container_dir_path"].Value = docs.containerPath; sql_con.Open(); sql_cmd.ExecuteNonQuery(); } catch (SQLiteException e) { throw e; } finally { sql_con.Close(); } }
public List<Docs> getDocsOfWit(String witId) { List<Docs> docs = null; try { sql_con = new SQLiteConnection(Common.localDatabasePath, true); sql_cmd = new SQLiteCommand("select * from docs where wit_id=@wit_id", sql_con); sql_cmd.Parameters.Add("@wit_id", DbType.String); sql_cmd.Parameters["@wit_id"].Value = witId; sql_con.Open(); SQLiteDataReader reader = sql_cmd.ExecuteReader(); docs = new List<Docs>(); while (reader.Read()) { Docs doc = new Docs(); doc.docId = StringUtils.ConvertFromDBVal<string>(reader["doc_id"]); doc.fileName = StringUtils.ConvertFromDBVal<string>(reader["file_name"]); doc.localPath = StringUtils.ConvertFromDBVal<string>(reader["local_path"]); docs.Add(doc); } } catch (SQLiteException e) { throw e; } finally { sql_con.Close(); } return docs; }