Need some guidance on storing parsed info from CSV files

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Need some guidance on storing parsed info from CSV files

cjhorton
Hi Everyone,

I am having difficulty with how to keep parsed/validated information from an uploaded CSV file around so I can display it for review to the customer before it is put into the db.

I am using the importAddressMatchMap java service(called from request map) as a reference to parse and validate the user uploaded CSV file.  I don't want to put the information into the db after parsing/validation, but want to allow the user to verify the information first before the order is submitted.  

Where can I store the information(records or map) so I can call it from a .ftl in the next step or two?

Any guidance would be greatly appreciated!

-CJ

I have included the OOTB java method below for reference.

    public static Map<String, Object> importAddressMatchMapCsv(DispatchContext dctx, Map<String, ? extends Object> context) {
        GenericDelegator delegator = dctx.getDelegator();
        ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
        String encoding = System.getProperty("file.encoding");
        String csvFile = Charset.forName(encoding).decode(fileBytes).toString();
        csvFile = csvFile.replaceAll("\\r", "");
        String[] records = csvFile.split("\\n");

        for (int i = 0; i < records.length; i++) {
            if (records[i] != null) {
                String str = records[i].trim();
                String[] map = str.split(",");
                if (map.length != 2 && map.length != 3) {
                    return ServiceUtil.returnError("Invalid format for CSV (key,value,sequence)");
                } else {
                    GenericValue addrMap = delegator.makeValue("AddressMatchMap");
                    addrMap.put("mapKey", map[0].trim().toUpperCase());
                    addrMap.put("mapValue", map[1].trim().toUpperCase());
                    int seq = i + 1;
                    if (map.length == 3) {
                        char[] chars = map[2].toCharArray();
                        boolean isNumber = true;
                        for (char c: chars) {
                            if (!Character.isDigit(c)) {
                                isNumber = false;
                            }
                        }
                        if (isNumber) {
                            try {
                                seq = Integer.parseInt(map[2]);
                            } catch (Throwable t) {
                                Debug.logWarning(t, "Unable to parse number", module);
                            }
                        }
                    }

                    addrMap.put("sequenceNum", Long.valueOf(seq));
                    Debug.log("Creating map entry: " + addrMap, module);
                    try {
                        delegator.create(addrMap);
                    } catch (GenericEntityException e) {
                        Debug.logError(e, module);
                        return ServiceUtil.returnError(e.getMessage());                        
                    }
                }
            } else {
                return ServiceUtil.returnError("No records found in file");
            }
        }

        return ServiceUtil.returnSuccess();
    }
Reply | Threaded
Open this post in threaded view
|

Re: Need some guidance on storing parsed info from CSV files

BJ Freeman
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

in the svn trunk
importAddressMatchMapCsv is part of the Partyservices.java.
if you notice the java importAddressMatchMapCsv() returns a map.
to the service called by the controller.xml
so if you return you data in that map
this would be called out in the out parameters, in your service as
importdata java.util.List
you controller would on return go to a view in you widget/ftl that would
display the map.

There are examples of this in ofbiz.



cjhorton sent the following on 1/27/2009 12:04 PM:

> Hi Everyone,
>
> I am having difficulty with how to keep parsed/validated information from an
> uploaded CSV file around so I can display it for review to the customer
> before it is put into the db.
>
> I am using the importAddressMatchMap java service(called from request map)
> as a reference to parse and validate the user uploaded CSV file.  I don't
> want to put the information into the db after parsing/validation, but want
> to allow the user to verify the information first before the order is
> submitted.  
>
> Where can I store the information(records or map) so I can call it from a
> .ftl in the next step or two?
>
> Any guidance would be greatly appreciated!
>
> -CJ
>
> I have included the OOTB java method below for reference.
>
>     public static Map<String, Object>
> importAddressMatchMapCsv(DispatchContext dctx, Map<String, ? extends Object>
> context) {
>         GenericDelegator delegator = dctx.getDelegator();
>         ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
>         String encoding = System.getProperty("file.encoding");
>         String csvFile =
> Charset.forName(encoding).decode(fileBytes).toString();
>         csvFile = csvFile.replaceAll("\\r", "");
>         String[] records = csvFile.split("\\n");
>
>         for (int i = 0; i < records.length; i++) {
>             if (records[i] != null) {
>                 String str = records[i].trim();
>                 String[] map = str.split(",");
//this will fail since you may have a comma inside a double quote.

>                 if (map.length != 2 && map.length != 3) {
>                     return ServiceUtil.returnError("Invalid format for CSV
> (key,value,sequence)");
>                 } else {
>                     GenericValue addrMap =
> delegator.makeValue("AddressMatchMap");
>                     addrMap.put("mapKey", map[0].trim().toUpperCase());
>                     addrMap.put("mapValue", map[1].trim().toUpperCase());
>                     int seq = i + 1;
>                     if (map.length == 3) {
>                         char[] chars = map[2].toCharArray();
>                         boolean isNumber = true;
>                         for (char c: chars) {
>                             if (!Character.isDigit(c)) {
>                                 isNumber = false;
>                             }
>                         }
>                         if (isNumber) {
>                             try {
>                                 seq = Integer.parseInt(map[2]);
>                             } catch (Throwable t) {
>                                 Debug.logWarning(t, "Unable to parse
> number", module);
>                             }
>                         }
>                     }
>
>                     addrMap.put("sequenceNum", Long.valueOf(seq));
>                     Debug.log("Creating map entry: " + addrMap, module);
>                     try {
>                         delegator.create(addrMap);
>                     } catch (GenericEntityException e) {
>                         Debug.logError(e, module);
>                         return ServiceUtil.returnError(e.getMessage());                        
>                     }
>                 }
>             } else {
>                 return ServiceUtil.returnError("No records found in file");
>             }
>         }
>
>         return ServiceUtil.returnSuccess();
>     }
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJf4pirP3NbaWWqE4RAgJsAJ49rP6HWc23+WCx16Gm/eWgyENcDgCgkbSb
+FRNrOw/htAAAaBSY9/3qlM=
=NW2M
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|

Re: Need some guidance on storing parsed info from CSV files

cjhorton
Thanks BJ.

I need to add something similar to:

Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("parsedfileinfo", parsedfileinfo);
return result;

And add an out attribute for "parsedfileinfo" in the service definition.

-CJ





BJ Freeman wrote
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

in the svn trunk
importAddressMatchMapCsv is part of the Partyservices.java.
if you notice the java importAddressMatchMapCsv() returns a map.
to the service called by the controller.xml
so if you return you data in that map
this would be called out in the out parameters, in your service as
importdata java.util.List
you controller would on return go to a view in you widget/ftl that would
display the map.

There are examples of this in ofbiz.