Skip to Content
avatar image
Former Member

Adding Header in Appended File

Hello All,

   I have build a scenario SuccessFactors to File. Since i am sending the 200 MB of xml data to the file i have choosen append mode.

   Here i have a challenge of adding header line to appended file.

   I am planning to develop 2 interfaces. one is for appending the file without header line. This is done.

   in Second interface i am planning to add header line to the appended file.

   Can anyone help me to build the second interface to add header line to the output appended file which is present in FTP folder.

   Header will be like below  

  "NOTACTIVE","STUD_ID","FNAME","LNAME","MI","GENDER","JP_ID","JP_DESC","JOB_TITLE","ORG_ID"

   If anyone has any other approach kindly in terms of design kindly let me know.

   I am on PI 7.0 version.

Thanks,

Bhaskar

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    avatar image
    Former Member
    Dec 02, 2015 at 05:09 AM

    Hello All,

      Can anyone help me with the solution.

    Thanks,

    Bhaskar

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Bhaskar,

                        It has taken a lot of time to answer this query as it involved lot of trial and error. Try this code. I have assumed that last line of original file has no newline at the end. In case it has just initialise value of count variable to -1 instead of zero.

      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.util.Calendar;
      import java.util.Date;
      
      
      import com.sap.aii.mapping.api.AbstractTransformation;
      import com.sap.aii.mapping.api.StreamTransformationException;
      import com.sap.aii.mapping.api.TransformationInput;
      import com.sap.aii.mapping.api.TransformationOutput;
      
      
      
      
      public class RemoveExtraElement  extends AbstractTransformation{
      
      
        /**
        * @param args
        * @throws FileNotFoundException
        * @throws StreamTransformationException
        */
      
      
      
        @Override
        public void transform(TransformationInput arg0, TransformationOutput arg1)
        throws StreamTransformationException {
        // TODO Auto-generated method stub
        execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());
        }
          public void execute(InputStream in, OutputStream out) throws StreamTransformationException
          {
          try
          {
          StringBuilder s = new StringBuilder();
             byte[] b=new byte[in.available()];
             if(in.read(b) >= 0) {
              s = new StringBuilder(s.toString()+new String(b));
             }
            
             String header="\"NOTACTIVE\",\"STUD_ID\",\"FNAME\",\"LNAME\",\"MI\",\"GENDER\",\"JP_ID\",\"JP_DESC\",\"JOB_TITLE\",\"ORG_ID\"!##!\n";
             s= new StringBuilder(s.insert(0,header));
             java.util.Date date= new Date();
             Calendar cal = Calendar.getInstance();
             cal.setTime(date);
            
             int month = cal.get(Calendar.MONTH)+1;
           
             int year=cal.get(Calendar.YEAR);
             int day=cal.get(Calendar.DAY_OF_MONTH);
             cal.set(Calendar.YEAR,year);
             cal.set(Calendar.MONTH,month);
             cal.set(Calendar.DATE,day);
             String runDate1=""+year+"/";
             if(month<10)
             {
              runDate1=runDate1+"0";
             }
             runDate1=runDate1+month+"/";
           
             String runDate2=runDate1+cal.getActualMaximum(Calendar.DAY_OF_MONTH);
             runDate1+="01";
           
             int len=s.length();
             int count=0;
             for(int i=0;i<len;++i)
             {
                        if(s.charAt(i)=='\n')
                        {
                       count++;
                        }
             }
             String footer="\nTB TA TC "+runDate1+" "+runDate2+" "+count;
             s=s.append(footer);
             System.out.println(s);
             out.write(s.toString().getBytes("UTF-8"));
             in.close();
             out.close();
          }   
          catch(Exception e)
          {
          e.printStackTrace();
          throw new StreamTransformationException(e.getMessage());
          }
          }
      
      }
      
      

      Regards

      Anupam

  • Dec 07, 2015 at 05:33 PM

    Anupam,

    Just need a clarification. When you say using Java mapping we can get the record count, are you thinking of bringing the whole 200 MB (less or more) file back into PI through the second  interface and do the record count?

    Thanks,

    Justin.

    Add comment
    10|10000 characters needed characters exceeded

    • Thank you bhaskar for providing the update and marking my answer as correct answer.

      Thank you again for posting a question like this. While providing a solution, I learnt a lot of new things.

      Special thanks to Justin for providing all the valuable inputs to this thread. All your posts are very helpful indeed.

      Regards

      Anupam.

  • Dec 07, 2015 at 08:27 PM

    Bhaskar.

    I'm thinking the below design will work for Header only files. Still thinking through the Files that has footers. But I don't like the idea of bringing in the whole file back into PI , which later  might lead to memory/heap issues.

    Anupam, has given a perfect solution, but I'm just only concerned with the volume of file. Think through this and see if it make sense.

    @Anupam and/all experts- Please feel free to chip in and let me know if there is any flaws you can see in the above design.

    Thank you,

    Justin.

    Add comment
    10|10000 characters needed characters exceeded