using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Data; using System.Data.SqlClient; using Laserfiche.RepositoryAccess; using WagnerMVCService.Models; using System.Configuration; using Laserfiche.DocumentServices; namespace WagnerMVCService.Controllers { public class ValuesController : ApiController { // GET api/values public IEnumerable Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string Get(int id) { return "value"; } // POST api/values public MessageReturns Post([FromBody] List productDetails) // public string Post([FromBody] WagnerRFQTemplate WagnerRFQTemplate) { // repository login information MessageReturns objMessageReturns = new MessageReturns(); string serverName = Convert.ToString(ConfigurationManager.AppSettings["LFServer"]).Trim(); string repoName = Convert.ToString(ConfigurationManager.AppSettings["LFRepository"]).Trim(); string username = Convert.ToString(ConfigurationManager.AppSettings["LFUserName"]).Trim(); string password = Convert.ToString(ConfigurationManager.AppSettings["LFPassword"]).Trim(); string LFVolume = Convert.ToString(ConfigurationManager.AppSettings["LFVolume"]).Trim(); string LFRepoStartPath = Convert.ToString(ConfigurationManager.AppSettings["LFRepoStartPath"]).Trim(); string LFRepoMainFolderNameRFQ = Convert.ToString(ConfigurationManager.AppSettings["LFRepoMainFolderNameRFQ"]).Trim(); // string LFRepoStartPath = Convert.ToString(ConfigurationManager.AppSettings["LFRepoStartPath"]).Trim(); //string NPSDoc = Convert.ToString(ConfigurationManager.AppSettings["NPSPath"]).Trim(); //List wagnerImportData1 = new List(); //string strlocalpath = @"C:\\mihir vyas\\PDFFolder"; // initialize an instance of List to store the search results //List contents = new List(); // log into the repository RepositoryRegistration repository = new RepositoryRegistration(serverName, repoName); Session session = new Session(); // WagnerImportData wagnerImportData = new WagnerImportData(); // string strcomm = ""; // string strtxt1; try { if (productDetails != null) { session.LogIn(username, password, repository); FieldValueCollection fieldValues = new FieldValueCollection(); if (productDetails != null) { if (productDetails.Count > 0) { List valueListProductType = new List(); List valueListProductName = new List(); // FieldValueCollection fieldValuesUserId = new FieldValueCollection(); //List valueListQuantity = new List(); // FieldValueCollection fieldValuesCurrentDatetime = new FieldValueCollection(); // List valueListRemarks = new List(); for (int i = 0; i < productDetails.Count; i++) { valueListProductType.Add(productDetails[i].ProductType.ToString()); valueListProductName.Add(productDetails[i].ProductName.ToString()); //valueListQuantity.Add(WagnerQuoteTemplate.productDetails[i].Quantity.ToString()); //valueListQuantity.Add("Mediafile"); //valueListRemarks.Add(WagnerQuoteTemplate.productDetails[i].Remarks.ToString()); } object[] valueProductType = valueListProductType.ToArray(); object[] valuesProductName = valueListProductName.ToArray(); //object[] valuesQuantity = valueListQuantity.ToArray(); //object[] valuesRemarks = valueListRemarks.ToArray(); fieldValues.AppendValues("Product_Types", valueProductType); fieldValues.AppendValues("Product_Names", valuesProductName); // fieldValues.AppendValues("Product_Quantity", valuesQuantity); //fieldValues.AppendValues("CurrentDatetime", valuesCurrentDatetime); // fieldValues.AppendValues("Remarks", valuesRemarks); } } //fieldValues.Add("Quote ID", WagnerRFQTemplate.QuoteID); //fieldValues.Add("Quote Title", WagnerRFQTemplate.QuoteTitle); //fieldValues.Add("Received Date", WagnerRFQTemplate.ReceivedDate); //fieldValues.Add("Account Name", WagnerRFQTemplate.AccountName); //fieldValues.Add("Inside Sales Person", WagnerRFQTemplate.InsideSalesPerson); //fieldValues.Add("Outside Sales Person", WagnerRFQTemplate.OutsideSalesPerson); //fieldValues.Add("Product Type", WagnerRFQTemplate.ProductType); //fieldValues.Add("Product Name", WagnerRFQTemplate.ProductName); //fieldValues.Add("Quantity", WagnerRFQTemplate.Quantity); //fieldValues.Add("Quote Status", WagnerRFQTemplate.Status); if ("1"=="1") // thumb image { string folderPath = LFRepoStartPath + "\\" + LFRepoMainFolderNameRFQ; EntryInfo entry = Entry.TryGetEntryInfo(folderPath, session); if (entry != null) { FolderInfo folder = entry as FolderInfo; if (folder != null) { //return folder; } else { throw new DuplicateObjectException("Object already exists"); } } else { CreateHelper(folderPath, session, LFVolume); } FolderInfo lfFolder = Folder.GetFolderInfo(folderPath, session); string fileName = "MYname.pdf"; // file name int NewDocID = Laserfiche.RepositoryAccess.Document.Create(lfFolder, fileName, EntryNameOption.Overwrite, session); DocumentImporter DI = new DocumentImporter(); DocumentInfo newDoc = new DocumentInfo(NewDocID, session); try { // newDoc newDoc.SetTemplate("TestTemplate", fieldValues, true); newDoc.Save(); } catch(Exception ex) { ICollection ILFCollection = newDoc.GetBadFieldValues();// new BadFieldValue(); } DI.Document = newDoc; //Stream fileStream = new MemoryStream(byteArray); DI.OverwritePages = false; // string contentType = "application/pdf"; //string contentType = (contentType == null) ? "application/pdf" : contentType; //DI.ImportEdoc(contentType, wagnerImportData.FilePath); // final import document on repository //string folderpath = "C:\\Mihir Test\\1\\ABC.pdf"; // Byte[] filebyte = File.ReadAllBytes(folderpath); // WagnerRFQTemplate.FileByte = filebyte; // WagnerRFQTemplate.FileName = "ABC.pdf"; //if (WagnerRFQTemplate.FileByte != null) //{ // Stream stream = new MemoryStream(WagnerRFQTemplate.FileByte); // DI.ImportEdoc(contentType, stream); // final import document on repository // //DI.ImportEdoc(contentType, strmp4path); // final import document on repository //} } //end apply metadata // log out of the repository // if (session != null) // session.LogOut(); objMessageReturns.status = true; objMessageReturns.message = "LF Data saved"; ;// ex.Message.ToString(); //string jsonstring = JsonConvert.SerializeObject(objMessageReturns);//, Newtonsoft.Json.Formatting.Indented); return objMessageReturns; } else { objMessageReturns.status = false; objMessageReturns.message = "Web Service Object is null";// ex.Message.ToString(); // string jsonstring = JsonConvert.SerializeObject(objMessageReturns);//, Newtonsoft.Json.Formatting.Indented); return objMessageReturns; } } catch (Exception ex) { var lineNumber = 0; const string lineSearch = ":line "; var index = ex.StackTrace.LastIndexOf(lineSearch); if (index != -1) { var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length); if (int.TryParse(lineNumberText, out lineNumber)) { } } objMessageReturns.status = false; objMessageReturns.message = "My Exception : " + ex.Message.ToString() + "line number-" + lineNumber; //string jsonstring = JsonConvert.SerializeObject(objMessageReturns);//, Newtonsoft.Json.Formatting.Indented); return objMessageReturns; } finally { if (session != null) { session.LogOut(); } session.Discard(); session.Close(); //WagnerRFQTemplate = null; } } private static FolderInfo CreateHelper(string path, Session s, string lfvolume) { string strmainpath = path; EntryInfo entry = null; Stack toCreate = new Stack(); if (!path.StartsWith("\\")) path = "\\" + path; // This is equivalent to the recursion in the other // solution, but with an explicit stack instead of relying // on the call stack for state. while (entry == null) { toCreate.Push(path.Split('\\').Last()); path = path.Substring(0, path.LastIndexOf('\\')); if (path == "") path = "\\"; entry = Entry.TryGetEntryInfo(path, s); } FolderInfo folder = entry as FolderInfo; if (folder == null) { throw new DuplicateObjectException( "A parent of the requested folder is a non-folder entry."); } // Walk back up the stack as if returning from recursive // calls. FolderInfo parent = null; try { while (toCreate.Count > 0) { if (parent != null) parent.Dispose(); parent = folder; folder = new FolderInfo(s); path = toCreate.Pop(); // folder.Create(parent, path, "DEFAULT", EntryNameOption.None); folder.Create(parent, path, lfvolume, EntryNameOption.None); } } catch (Exception) { // Guarantee that folder does not leak if an Exception // is thrown. if (folder != null) folder.Dispose(); throw; } finally { // Guarantee that parent does not leak. if (parent != null) parent.Dispose(); } return folder; } // PUT api/values/5 public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 public void Delete(int id) { } } }