Clarity

Expand all | Collapse all

Looking for Gel Script to create a tab separated file and move file on MFTP server

  • 1.  Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 04:36 PM
      |   view attached

    Hi ,

     

    We are currently using the java program to move the output file from Clarity Application server to MFTP server. Below is the attached sample code of java program.

     

    package com.abc.xyz.clarity.z;

    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;
    import java.util.logging.FileHandler;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.logging.SimpleFormatter;

    import com.niku.union.interfaces.SchedulerListener;
    import com.niku.union.interfaces.JobSchedulerContext;

    import jcifs.smb.NtlmPasswordAuthentication;
    import jcifs.smb.SmbFile;

    public class MFTFileCopyZ implements SchedulerListener

    {
    //@Override
    public void scheduledEventFired(JobSchedulerContext jobContext_) throws Exception {
    if(jobContext_ == null) {
    throw new Exception("Invalid JobContext");
    }
    try {
    final String args[] = {};
    main(args);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws IOException {
    copyFiles();
    }
    public static void copyFiles() {


    final Logger logger = Logger.getLogger(MFTFileCopyZ.class.getName());
    FileHandler fh = null;
    try {
    fh=new FileHandler("loggerZ.log", false);
    } catch (SecurityException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    } catch (IOException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    fh.setFormatter(new SimpleFormatter());
    logger.addHandler(fh);
    logger.setLevel(Level.CONFIG);
    boolean successful = false;

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh_mm_ss");
    df.setTimeZone(TimeZone.getTimeZone("PST"));
    String today=df.format(new Date());

    String recipients[] = new String [2];
    recipients[0]=UtilInterfaces.getClearPropertyValue("FAILURE_EMAIL_NOTIFY1");
    recipients[1]=UtilInterfaces.getClearPropertyValue("FAILURE_EMAIL_NOTIFY2");
    final String MFT_SOURCE_FOLDER = UtilInterfaces.getClearPropertyValue("MFT_SOURCE_FOLDER");//location on Shared drive
    final String MFT_TARGET_FOLDER = UtilInterfaces.getClearPropertyValue("MFT_TARGET_FOLDER");//location on MFT drive specified as SMB(MFT) share
    final String MFT_ARCH_TARGET_FOLDER = UtilInterfaces.getClearPropertyValue("MFT_ARCH_TARGET_FOLDER");//archive location on local drive specified as SMB(MFT) share
    final String SOURCE_FILE_NAME = UtilInterfaces.getClearPropertyValue("SOURCE_FILE_NAME");//wildcard search for MFT file
    String DOMAIN = UtilInterfaces.getEncryptedPropertyValue("mft_domain");
    String USER_NAME = UtilInterfaces.getEncryptedPropertyValue("mft_user_name");
    String PASSWORD = UtilInterfaces.getEncryptedPropertyValue("mft_password");

    try{

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(DOMAIN, USER_NAME, PASSWORD);

    SmbFile srcdir = new SmbFile(MFT_SOURCE_FOLDER, auth);
    final SmbFile[] wildcard = srcdir.listFiles(SOURCE_FILE_NAME);
    String NEWSOURCE=wildcard[0].toString();
    SmbFile MFTlocalfile = new SmbFile(NEWSOURCE, auth);
    final String arch_target= MFT_ARCH_TARGET_FOLDER+SOURCE_FILE_NAME.toString().substring(0,16)+"-"+today+".dat";
    final String MFTtargetfile= MFT_TARGET_FOLDER+SOURCE_FILE_NAME;
    SmbFile archSMBfile= new SmbFile(arch_target,auth);
    SmbFile workingSMBfile = new SmbFile (MFTtargetfile,auth);
    //copy working file MFT from local working file/folder to MFT share folder
    workingSMBfile.createNewFile();
    MFTlocalfile.copyTo(workingSMBfile);
    //copy MFT working file to local archive folder
    archSMBfile.createNewFile();
    MFTlocalfile.copyTo(archSMBfile);
    //Delete MFT file from location only if file has been copied to local archive and working file has been copied to MFT share
    if ( workingSMBfile.exists() && archSMBfile.exists() ) {
    MFTlocalfile.delete();
    System.out.println("delete() invoked");
    successful = true;
    }

    else{
    logger.log(Level.WARNING,"Files not processed");
    String message = "File Actions Failed";
    UtilInterfaces.postMail("File Actions Failed", message, recipients, null);
    }
    if (successful == false){
    logger.log(Level.WARNING,"Main Program Error");
    //send email to Clarity Team in case of failure
    String message = "File Actions Failed";
    UtilInterfaces.postMail("File Actions Failed", message, recipients, null);
    }

    } catch (Exception e) {
    }

    }
    }

     

    I am looking for a gel sample to use the attached java code to transfer the file on mftp server or other gel code which will export the data in tab separated file and transfer on MFTP server.

     

     

     

    Thanks,

    Shal.

    Attachment(s)

    zip
    new 7.java.zip   1 KB 1 version


  • 2.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 05:01 PM

    I am assuming that MFT_SOURCE_FOLDER is a shared folder containing the file to copy.  Is MFT_SOURCE_FOLDER located physically on the Clarity Application server?

     

    Does the MFTP server have the ability to access a FTP request?

     

    If yes to both questions, you could just FTP the file from the  Clarity Application server to the MFTP server.

     

    https://communities.ca.com/thread/241746988

     

    You appear to be on premise give this is a custom job. Are you trying to move to GEL to eliminate this custom job?

     

    V/r,
    Gene



  • 3.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 05:57 PM

    Thanks for quick response.

     

    Question 1 answer is yes however I tried to use ftp open tag in gel and it gives the below error. Not sure if that is because MFTP has no ability  to access a FTP request. 

    Sample code used in gel;

    </file:writeFile>

    <ftp:open hostName="\\ftpservername\ClarityApp_Share" port="21" user="username"
    password="password">

    <ftp:put fileName="abc_monthly_data.txt"
    localDir="d://xyz//"
    remoteDir="//ftpservername/ClarityApp_Share/QA/inbound/"
    /> <!---remoteDir is MFTP server folder and localDir is Application server directory-->
    </ftp:open>
    </gel:script>

     

     

    Here is the error.

     



  • 4.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 05:36 PM

    I might have misunderstood what you were asking.

     

    Are you trying to use the code above?  The code above is a PPM job (public class MFTFileCopyZ implements SchedulerListener), so if you compile it and drop the jar into the lib directory (check docs) you should be able to see it in the available jobs listing for scheduling it.

     

    The code above doesn't appear to create a create a tab separated file.  I looks like it just copies one from a given share (Samba share?) to the MFT_TARGET_FOLDER location.

     

    V/r,

    Gene



  • 5.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 06:02 PM

    The java code is currently used as a scheduled job to move the files from application server to MFTP server. It is working fine for several ERP based interfaces. However I am looking for possibility if I can use the same MFTP server in Gel code to move the tab separated file or Adhoc files which has been created via gel on need basis.

     

    Thanks.



  • 6.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 06:06 PM

    here is test gel code:

    <gel:script xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:core="jelly:core"
    xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    xmlns:ftp="jelly:com.niku.union.gel.FTPTagLibrary"
    xmlns:mail="jelly:email"
    xmlns:sql="jelly:sql">

    <gel:formatDate format="yyyy-MM-ddHH-mm" stringVar="rundate"/>
    <gel:parameter default="d://xyz//" var="filePath"/>
    <gel:parameter default="abc_monthly_data_${rundate}.txt" var="infile"/>

    <gel:setDataSource dbId="niku" var="a"/>

    <sql:query dataSource="${a}" var="result">
    SELECT Prj_ID Prj_ID,
    Prj_Name Prj_Name,
    Prj_Type Prj_Type,
    FROM MONTHLY_PORTLET_DATA
    WHERE MONTH='Nov 2016'

    </sql:query>

    <!-- Write a file on application server -->

    <file:writeFile fileName="${filePath}/${infile}" delimiter="&#0009;" embedded="false">

    <!-- Add a comment header to the file -->

    <file:comment value="PROJECT_CODE,PROJECT_NAME,PROJECT_TYPE"/>

    <!-- Add a Detail record to the file -->

    <core:forEach trim="true" items="${result.rowsByIndex}" var="row">
    <file:line>
    <file:column value="${row[0]}"/>
    <file:column value="${row[1]}"/>
    <file:column value="${row[2]}"/>
    </file:line>

    </core:forEach>

    </file:writeFile>

    <ftp:open hostName="\\ftpservername\ClarityApp_Share" port="21" user="username"
    password="password">

    <ftp:put fileName="abc_monthly_data.txt"
    localDir="d://xyz//"
    remoteDir="//ftpservername/ClarityApp_Share/QA/inbound/"
    /> <!---remoteDir is MFTP server folder and localDir is Application server directory-->
    </ftp:open>
    </gel:script>



  • 7.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 06:21 PM

    To check if the MFTP has ftp, just type ftp://MFTP into a browser address bar.  It will either show you a login /set of file or error out.  You will need the real name for the MFTP server (look in the UtilInterfaces class if you don't have it).

     

    V/r,

    Gene



  • 8.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 06:40 PM

    when I tried ftp://MFTP server name, I received the error.



  • 9.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 06:28 PM

    I think it should look more like this:

     

    <gel:script xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:core="jelly:core"
    xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    xmlns:ftp="jelly:com.niku.union.gel.FTPTagLibrary"
    xmlns:mail="jelly:email"
    xmlns:sql="jelly:sql">


         <gel:formatDate format="yyyy-MM-ddHH-mm" stringVar="rundate"/>
         <gel:parameter default="d://xyz//" var="filePath"/>
         <gel:parameter default="abc_monthly_data_${rundate}.txt" var="infile"/>

         <gel:setDataSource dbId="niku" var="a"/>

         <sql:query dataSource="${a}" var="result">
    SELECT Prj_ID Prj_ID,
    Prj_Name Prj_Name,
    Prj_Type Prj_Type,
    FROM MONTHLY_PORTLET_DATA
    WHERE MONTH='Nov 2016'
         </sql:query>

         <!-- Write a file on application server -->
         <file:writeFile fileName="${filePath}/${infile}" delimiter="&#0009;" embedded="false">

              <!-- Add a comment header to the file -->
              <file:comment value="PROJECT_CODE,PROJECT_NAME,PROJECT_TYPE"/>

              <!-- Add a Detail record to the file -->
              <core:forEach trim="true" items="${result.rowsByIndex}" var="row">
                   <file:line>
                        <file:column value="${row[0]}"/>
                        <file:column value="${row[1]}"/>
                        <file:column value="${row[2]}"/>
                   </file:line>

              </core:forEach>
         </file:writeFile>

         <ftp:open hostName="MFTP server name goes here" port="21" user="username" password="password">
              <ftp:put fileName="${infile}" localDir="${filePath}" remoteDir="A Directory on MFTP" />
         </ftp:open>
    </gel:script>

     

    ftp:open need the MFTP host name, port 21 is for ftp 22 is for sftp.

    ftp:put needs where to store the file recieved via ftp.  It also needs to be a folder that "username" has write access to.

     

    V/r,

    Gene



  • 10.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server
    Best Answer

    Posted Jan 25, 2017 06:39 PM

    Just sitting here thinking about it you could save the abc_monthly_data_${rundate}.txt file based on the setting in the static UtilInterfaces class then create a new MFTFileCopyZ object and invoke the copyFiles menthod.

     

    V/r,

    Gene



  • 11.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 07:21 PM

    Thanks Gene.

     

    Thinking in same direction.



  • 12.  Re: Looking for Gel Script to create a tab separated file and move file on MFTP server

    Posted Jan 25, 2017 07:53 PM

    Another thing to try is just saving the abc_monthly directly to MFTP.

     

    Update the first parameter to the correct location on MFTP.

     

    <gel:parameter default="\\\\MFTP\\c$\\temp" var="filePath"/>
    <gel:parameter default="abc_monthly_data_${rundate}.txt" var="infile"/>
    <file:writeFile fileName="${filePath}\\${infile}" delimiter="&#0009;" embedded="false">

     

    V/r,

    Gene