Open Source and i The best things in life are free Jon Paris Jon.Paris @ Partner400.com www.partner400.com www.systemideveloper.com Notes This presentation may contain small code examples that are furnished as simple examples to provide an illustration. These examples have not been thoroughly tested under all conditions. We therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All code examples contained herein are provided to you "as is". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. The author, Jon Paris, is co-founder of Partner400, a firm specializing in customized education and mentoring services for AS/400 and iseries developers. With his partner, Susan Gantner, Jon authors regular technical articles for a number of technical publications including iseries 400 Experts Journal, IBM Systems Magazine, i5 edition, and its companion electronic newsletter, i5 EXTRA and their weekly idevelop blog. You may view articles in current and past issues and/or subscribe to the free newsletter or the magazine at: http://www.ibmsystemsmag.com Susan and Jon are also partners in SystemiDeveloper, a company that hosts the RPG & DB2 Summit conferences. See SystemiDeveloper.com for more details. Feel free to contact the author at: jon.paris @ partner400.com or visit the Partner400 web site at http://www.partner400.com Partner400, 2016 Lecture Notes: Page 1 of 12
Agenda What do you want to do Today? Other than have a day off work that is! Whatever it is... There's probably a (free) App for That! But... every time I give this presentation someone tells me I forgot something Note that not all the software mentioned in this session is strictly "Open Source" Some is freeware distributed as source code and some is distributed in object form. So What Do You Want to Do? Build new RPG-based modern web applications? Including generation, consumption and testing of web services Send email from your RPG programs? Or even use RPG to process the content of received emails! Create, update and read business documents? Spreadsheets, graphs, charts and reports Generate and Process XML or JSON? Create XML documents Process XML documents Transform XML into database and database into XML Know How To...? Use System APIs Use C-style functions Perform common programming tasks Invoke PC applications from green screens? We will do our best to show you where you can find code to do all of these Partner400, 2016 Lecture Notes: Page 2 of 12
Let's Start With Creating Web Applications CGIDEV2 - the original and still a great place to start Easy to use template driven set of APIs to simplify web programming www.easy400.net Not actually Open Source but... The Renaissance Framework From CoralTree Systems in the UK Originally based on CGIDEV2 but now goes far beyond it Very sophisticated Web 2.0 framework that extends the CGIDEV2 tool set www.renaissanceframework.com powerext Also based originally on CGIDEV2 A Web 2.0 oriented RPG framework that also includes excellent XML tooling "Pay for" version available with extended features and application generation tooling powerext.com Not Open Source But Free These products are the exception in that no source code is supplied They are free-to-use products A model for commercial software that is new to the IBM i community CNX's Valence RPG oriented commercial product now offering a free community edition www.cnxcorp.com/valence System & Method's IceBreak Community Edition (CE) www.system-method.com/download/icebreakce Modelled after the Microsoft's ASP approach Web site features some great "try it and see" features www.icebreak.org/programs2.htm Icebreak is unusual in that it also supports COBOL Partner400, 2016 Lecture Notes: Page 3 of 12
Consume Web Services From RPG IBM's Web Services Client Requires that you have the C compiler installed and is limited to SOAP We prefer Scott's Klement's HTTP API An easier and more powerful approach www.scottklement.com/httpapi A free open source tool that acts as an HTTP client (i.e. a "Browser") Limited documentation But excellent demonstration programs Presentation handout on Scott's web site Many articles at iprodeveloper.com Very supportive user community Access the list via Scott's web site Formal support contracts available vie Profound Logic Thomas Raddatz offers WSDL2RPG An easy way of generating programs to consume web services Builds on Scott's HTTPAPI www.tools400.de/english/english.html Simple HTTPAPI Example - GeoIP SOAP = '<soapenv:envelope + xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" + xmlns:web="http://www.webservicex.net"> + <soapenv:header/><soapenv:body> + <web:getgeoip> + <web:ipaddress>' + IPADDR + '</web:ipaddress> + </web:getgeoip></soapenv:body></soapenv:envelope>'; http_setccsids( 1208: 0 ); http_debug( *ON ); rc = http_post_xml( 'http://www.webservicex.net/geoipservice.asmx' : %addr(soap) + VARYINGDATAOFFSET : %len(soap) : *NULL : %paddr(mapxmldata) : %addr(countryname) : HTTP_TIMEOUT : HTTP_USERAGENT : 'text/xml; charset=utf-8' : '"http://www.webservicex.net/getgeoip"' ); if (rc<>1); http_crash(); endif; dsply CountryName ' ' wait; *inlr = *on; Partner400, 2016 Lecture Notes: Page 4 of 12
Send email from your RPG programs Aaron Bartell's RPGMail2 A Custom Java interface to the Java Mail APIs Keeps the RPG simpler so there is no need to create your string objects etc. Download it from www.mowyourlawn.com Premium "for fee" version available with support services /copy qsource,rpgmailcp /copy qsource,errorcp D email s o class(*java: RPGMail) /Free RPGMail_init(); email = RPGMail_new(); RPGMail_addAddress(email: 'FROM': 'myname@mycompany.com': ' '); RPGMail_addAddress(email: 'TO': 'myname@mycompany.com': ' '); RPGMail_setSubject(email: 'RPGMail Test'); RPGMail_setBodyText(email: 'The text for my first rpg email message!' : 'text/plain'); RPGMail_setSMTPHost(email: 'smtp.mycompany.com'); RPGMail_setSMTPPort(email: '25'); RPGMail_send(email); Send email from your RPG programs Giovanni Perotti's MMail (Mime and MAIL) A completely RPG Solution Download it from www.easy400.net We wrote an article on it for IBM Systems Magazine in 2004 www.ibmsystemsmag.com/ibmi/tipstechniques/applicationdevelopment/you-ve- Got-Mail! Provides green screen, Windows, and Browser interfaces Web interface allows for sending and receiving email More complex to use than RPGMail2 But offers broader functionality Uses a templating system for the body of the email - Very similar in action to CGIDEV2 HTML templating Partner400, 2016 Lecture Notes: Page 5 of 12
Create, update and read Excel Spreadsheets Scott Klement has written extensively on the subject His RPG code wrappers the Jakata POI Java classes Full details in Scott's presentation on his web site www.scottklement.com/presentations/excel Spreadsheets from RPG with POI.pdf Presentation provides references to the many articles he has written The associated code can be downloaded from SystemiNetwork.com Scott's Service Programs form the foundation for many other utilities For example, Giovanni Perotti at Easy400 uses them extensively Giovanni's Excel Utilities include: XLSCGI Creates Excel spreadsheets in XML format using CGIDEV2 templates Templates are defined using a web interface HSSFCGI Based on Scott's HSSFR4 to simplify Excel spreadsheet generation Uses its own XLSGEN "language" - a simplified XML format for defining spreadsheets XLPARSE2 Uses Scott's XMLPARSER4 to directly convert Excel spreadsheets into database Want to Generate XML? Use CGIDEV2 as a templating system Originally designed to produce web pages But XML and HTML are very similar The XML template is external to the program just like DDS Change the template and you have changed the XML - Not even a recompile needed Larry Ducie's XMLi Toolkit (xmli.sourceforge.net) Designed for XML from the ground up Two "flavors" of operation API driven and template driven Henrik Rützou's powerext (powerext.com) Developed as part of the powerext web development framework API driven approach that can handle HTML, JSON, XML and more XML facilities are part of the free Core features www.powerext.com/pextdoc_cgi.htm Partner400, 2016 Lecture Notes: Page 6 of 12
CGIDEV2 Simple XML Template This example use the default section identifier "/$" Notice that there are three sections (think record formats) The first (Header) will be output at the start of the file The second (Customer) is used for the repeating customer elements The third (Trailer) wraps up the document The whole template can be placed in a source physical file or in an IFS file /$Header <?xml version='1.0'?> <Customers> /$Customer <Customer> <Contact>/%Contact%/</Contact> <Company>/%Company%/</Company> <Address> <Street>/%Street%/</Street> <City>/%City%/</City> <State>/%State%/</State> <Zip>/%Zip%/</Zip> </Address> </Customer> /$Trailer </Customers> CGIDEV2 Code to Process the Template GetHTML loads the template into memory UpdHTMLVar supplies the data for the substitution variables Note that %Char (or %EditC/W) must be used for all numeric fields WrtSection actually merges the skeleton with the variable content A substitution variable can appear multiple times just as a DDS variable can WrtHtmlToStmf writes the buffered XML to the IFS The second parameter identifies the code page of the file to be created // Load HTML from source file and write out the Header Section gethtml('htmlsrc': '*LIBL': 'CUSTXML'); WrtSection('Header') // Update substitution variables for each record & write it out UpdHTMLVar('Contact': ContactName); UpdHTMLVar('Company': CoName); UpdHTMLVar('Street' : Address1); This portion is UpdHTMLVar('City' : City); repeated for each UpdHTMLVar('State' : StateCode); record to be output UpdHTMLVar('Zip' : %EditC(ZipCode: 'X')); WrtSection('Customer'); // When all records processed write the trailer and output to the IFS WrtSection('Trailer'); WrtHtmlToStmf('/xml/Customers.xml':819); Partner400, 2016 Lecture Notes: Page 7 of 12
Using powerext - An API Driven Approach Exec SQL declare customercursor cursor for select * from QIWS.QCUSTCDT; Exec SQL open customercursor; DoU SQLSTATE = endofdata; Exec SQL fetch next from customercursor into :customer; If SQLSTATE <> endofdata; xmlnode('customer':'id="'+ %char(cusnum) + '"'); xmlnode('name':'':lstnam); xmlnode('address'); xmlnode('street':'':street); xmlnode('city':'':city); xmlnode('state':'':state); xmlnode('zip':'':%editc(zipcod:'x')); xmlendnode(); xmlendnode(); EndIf; EndDo; Exec SQL close customercursor; xmlendnode(); echotostmf(xmlfilename:1208); Note: No element name needed Starts element and adds attribute value XMLi Toolkit - Template Option XMLi has both template and API options The API option is very similar to that of powerext <xmli:template xmlns:xmli="http://www.sourceforge.net/xmli" ccsid="1208"> <Customers> <xmli:sql name="custcount" statement="select count(*) from mcustomers"> <xmli:for-each> <RecordCount><xmli:value-of select="custcount.1" /></RecordCount> </xmli:for-each> <xmli:sql name="custrow" statement="select * from mcustomers"> <xmli:for-each> <Customer ID="${custRow.1}"> <Company><xmli:value-of select="custrow.2" /></Company> <Address> <Street><xmli:value-of select="custrow.3" /></Street> <City><xmli:value-of select="custrow.4" /></City> <State><xmli:value-of select="custrow.5" /></State> <Zip><xmli:value-of select="custrow.6" /></Zip> </Address> </Customer> </xmli:for-each> </Customers> <xmli:write-to-file path="'/partner400/redbook/customers.xml'" /> </xmli:template> Partner400, 2016 Lecture Notes: Page 8 of 12
Want to Generate and Consume JSON? YAJL - An Open Source project ported to IBM i by Scott Klement Scott has added a set of subprocedures to simplify the interface More information at scottklement.com/yajl/ Process is similar to the XML generation APIs of powerext and XMLi i.e. You add object definitions and then add elements to them, including arrays etc. yajl_genopen(*on ); // Set up to generate in "pretty" format yajl_beginobj(); // Start Object build yajl_beginarray('customers'); // Start Customer array yajl_beginobj(); yajl_addnum( 'ID': cusnum ); yajl_addchar( 'Name': LSTNAM ); yajl_addchar( 'Street': street ); yajl_addchar( 'City': city); yajl_addchar( 'State': state); yajl_addnum( 'Zip': zipcod )); yajl_endobj(); yajl_endarray(); // Close the array yajl_endobj(); // and wrap up the object before writing to IFS yajl_savebuf('/partner400/json/customers.json': errmsg); yajl_genclose(); // All done This section is repeated for each customer Notes The JSON generated from the script shown would look like this: { "Customers": [ { "ID": 12345, "Name": "Paris ", "Street": "Main Street ", "City": "Jasontown ", "State": "GA", "Zip": 30236 } ] } The formatting is the result of using the formatting option "*On" on the yajl_genopen(*on) call. This is useful during testing to enable you to easily see if the correct levels are being generated, but for production should be set to *Off. That would have resulted in this far more compact output: {"Customers":[{"ID":12345,"Name":"Paris ","City":"Jasontown ","State":"GA","Zip":30236}]} ","Street":"Main Street We would also normally make the results even more compact by using %TrimR on the source fields producing this: {"Customers":[{"ID": 12345,"Name":"Paris","Street":"Main Street","City":"Jasontown","State":"GA","Zip":30236}]} Partner400, 2016 Lecture Notes: Page 9 of 12
www.easy400.net Originally distributed just CGIDEV2, related tutorials, samples, etc. CGIDEV2 is the only software on the site that is not Open Source! IBM have to date refused to release it as OS - The version IBM distribute is out-of-date and the easy400 version is a better choice - Still being enhanced by the original developer But there is much more to find than CGIDEV2 Antoon van Os' DB2XTools and WebSql www.easy400.net/db2xtools/html/db2xtools.htm Converts a table to an IFS stream file in one of four formats: CSV, HTML, TXT, and XML Web or green screen interface WebSql adds the ability to dynamically define SQL queries The results are then fed into DB2XTools for output Places to find "Useful Stuff" David Morris' iseries Toolkit http://iseries-toolkit.sourceforge.net Leif Guldbrand's Think400 www.think400.dk There's a LOT of stuff including many API usage examples Also links to many other useful sites Henrik Krebs www.hkrebs.dk/sw.html A collection of free utilities including spool file cleanup and more Craig Rutledge www.jcrcmds.com Many utility programs including a group that provide sophisticated conversion of fixed-form RPG to /Free format Martin Rowe's DBG/400 www.dbg400.net Original DBG400 utility was to assist in setting up a complete test environment Several other tools including spool file manipulation added since Partner400, 2016 Lecture Notes: Page 10 of 12
Places to find "Useful Stuff" Peter Bishop's AS400Pro.com (No longer updated but useful) A great resource for finding articles on IBM i tech topics Home page lists article references added in the last 30 days The last time I checked there were over 100 listings Covers CL, DDS, RPG, SQL, and other topics of interest to i folk "Tips" page lists topic areas and links to detailed lists for each topic - as400pro.com/viewtips.php Albert York http://www.albertyork.yolasite.com/samples.php Includes a reformatting utility for /Free RPG Source Also the famous (or at least it should be) TNAPI It allows you to control an interactive program in batch! Help400 http://www.help400.de/software.html Sadly this page is no longer available in English but Google Translate is your friend and all utilities identified with a Union Jack are in English Links to lots of Open Source and Freeware utilities and samples Places to find "Useful Stuff" Jean-Paul Lamontre jplamontre.free.fr/jpltools.htm Collection of tools for everything from creating emails, PDFs and Excel spreadsheets to handling QSYSOPR message queues Also links to many of Jean-Paul's magazine articles Brian Dietz bryandietz.us/iseries.html Variety of communications related tools Port scanners, and Netstat style tools Spool file scanner and the PHP WRKSPLF sample script homepage1.nifty.com/uzaemon Don't let the Japanese text put you off Free 5250 emulation software tn5250j.sourceforge.net/index.html Full 5250 support Highly configurable Partner400, 2016 Lecture Notes: Page 11 of 12
Places to find "Useful Stuff" Peter Levy www.peterlevy.com Includes Peter's Dynamic Arrays utility And references to the System i Network articles that describe them Assorted other sites www.qpratools.com Utility functions for everything from spool files to date conversions slic-systems.org/index.php/downloads File utilities and editor, Source code formatters, etc. isphere - RDi Extensions Fabulous set of plug-in features for IBM's RDi development tool New features regularly added Look out for our article in IBM Systems Mag's Sept 2014 Extra sourceforge.net/projects/isphere/ Partner400, 2016 Lecture Notes: Page 12 of 12