File extraction and encoding  Topic is solved

Discussions relating to the REST API of Jiwa 7.

File extraction and encoding

Postby SBarnes » Tue Mar 30, 2021 4:17 pm

We have a customer for whom we have established a custom route that outputs a file with fixed length fields in it and a row per record.

The formatting is created by the following code on the response class

Code: Select all
   #region "Response Classes"   
     [Serializable]   
   [DataContract]
      public class DebtorOPEAFileExportResponse
      {
         [DataMember]
         public string CompanyID {get; set;}
         [DataMember]
         public DateTime EffectiveDate {get; set;}         
         [DataMember]
         public string PartNo {get; set;}
         [DataMember]
           public string Description {get; set;}
         [DataMember]
         public double Price {get; set;}
         [DataMember]
         public bool GST {get; set;}
         [DataMember]
         public double RRPrice {get; set;}
         [DataMember]
         public string DiscountCode {get; set;}         
         [DataMember]
         public string SupersededPartNo {get; set;}         
         [DataMember]
         public bool Active {get; set;}

         
         [DataMember]
         public string StockingCode {get; set;}   
         
         
         [DataMember]
         public double MOQ {get; set;}   
         
         [DataMember]
         public string Class {get; set;}   
         
         [DataMember]
         public string Filler {get; set;}   
         
         public DebtorOPEAFileExportResponse()
         {
         
            CompanyID  = "";
            
            EffectiveDate  = DateTime.Now;         
            
            PartNo  = "";
            
              Description  = "";
            
            Price  = 0;
            
            GST  = false;
            
            RRPrice  = 0;
            
            DiscountCode  = "";         
            
            SupersededPartNo  = "";         
            
            Active  = false;
            
               
            
            StockingCode  = "";   
            
            
            
            MOQ  = 0;   
            
            
            Class  = "";   
            
            
            Filler  = "";               
         }
         
         public string Formated()
         {
            string result = "";
            
            result += CompanyID.OPEAPadRight(4);
            result += EffectiveDate.ToString("yyMMdd").OPEAPadRight(6);
            result += PartNo.OPEAPadRight(20);
            result += Description.OPEAPadRight(35);
            result += (Price *100).OPEARoundedString(0).OPEAPadLeft(10, '0');
            result += (RRPrice *100).OPEARoundedString(0).OPEAPadLeft(10, '0');
            result += DiscountCode.OPEAPadRight(2);
            result += SupersededPartNo.OPEAPadRight(20);
            result += Active ? "A" : "N";
            result += GST ? "1" : "0";
            result += StockingCode.OPEAPadRight(1);
            result += MOQ.OPEARoundedString(0).OPEAPadRight(4);
            result += Class.OPEAPadRight(4);
            result += Filler.OPEAPadRight(8);
            
            byte[] asciiString = System.Text.Encoding.ASCII.GetBytes(result);
            result = System.Text.Encoding.ASCII.GetString(asciiString);
            return result;
         }         
         
      }
      
   public static class ExtHelper
   {
      
      public static string  OPEAPadLeft(this string str, int TotalWidth, char PadChar = ' ')
      {
         return str.PadLeft(TotalWidth, PadChar).Substring(0, TotalWidth);
      }
      
      public static string  OPEAPadRight(this string str, int TotalWidth, char PadChar = ' ')
      {
         string strPadded = str.PadRight(TotalWidth, PadChar);
         return strPadded.Substring(strPadded.Length - TotalWidth, TotalWidth);
      }
      
      public static string  OPEARoundedString(this double val, int decimals)
      {
         
         return Math.Round(val, decimals).ToString();
      }      
      
   }


and the service code is


Code: Select all
[Authenticate]
         [AddHeader(ContentType = "text/plain")]
         [AddHeader(HttpHeaders.ContentDisposition,  "attachment;filename=CASOPEA.txt")]
         public byte[]  Get(DebtorOPEAFileFormattedExport request)
         {
            //System.Diagnostics.Debugger.Launch();
            List<DebtorOPEAFileExportResponse> iresults = this.Get( new DebtorOPEAFileExportRequest());
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            
            System.IO.StreamWriter sw = new System.IO.StreamWriter(ms,  System.Text.Encoding.ASCII );
            int icount = iresults.Count;
            int i = 1;
            foreach(DebtorOPEAFileExportResponse resp in iresults)
            {
               if(i < icount)
               {
                  sw.Write(resp.Formated() + System.Environment.NewLine);
               }
               else
               {
                  sw.Write(resp.Formated());
               }
               
               i++;
            }
            sw.Flush();
            return ms.ToArray();
         }



I have added the code to set the stream writer's encoding today as well forcing the formatted string to be encoded to ASCII and yet they claim within the one file they are getting data that is encoded as ASCII and some encoded as UTF-8 which I would have thought was impossible.

Any ideas on how to resolve this?
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: File extraction and encoding  Topic is solved

Postby Mike.Sheen » Tue Mar 30, 2021 4:25 pm

Mixed encodings I tripped over a little while ago - I devised a solution by stripping out the byte order marks, attached is the plugin which does this (which is a useful plugin in its own right) - of interest to you should be the GetEncodedStringWithoutBOM method.

Take a look and even if it doesn't fix your specific problem, it's a handy plugin to have. But if it doesn't help, post back here and I'll look deeper.
Attachments
Plugin Import Multiple Plugins.xml
(32.86 KiB) Downloaded 47 times
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: File extraction and encoding

Postby Mike.Sheen » Tue Mar 30, 2021 4:46 pm

Is that reference to OPEA the Outdoor Power Equipment Association?

I worked on an add-in for a DOS based accounting package in a previous life (early 90's) which was a TSR utility to open a product CSV file, let the user search for products and add selected products to an order as a non-stocked item. It was a way of being able to sell millions of items without cluttering the database with seldom used products.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: File extraction and encoding

Postby Scott.Pearce » Tue Mar 30, 2021 5:00 pm

They had computers in the early '90s?
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221

Re: File extraction and encoding

Postby SBarnes » Tue Mar 30, 2021 5:40 pm

Given this is related to our motorcycle friends I'd say there is a good chance it is the same thing I only know it as OPEA.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: File extraction and encoding

Postby SBarnes » Tue Mar 30, 2021 5:46 pm

They even came with a cupholder Scott

zsc08oo347o21.jpg
zsc08oo347o21.jpg (26.54 KiB) Viewed 586 times
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: File extraction and encoding

Postby Mike.Sheen » Tue Mar 30, 2021 6:20 pm

They also only had 11 keys on the keyboard.

RealProgrammers.png
RealProgrammers.png (258.68 KiB) Viewed 585 times
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to REST API

Who is online

Users browsing this forum: No registered users and 2 guests

cron