i need upload data 2 database tables(factory , factorytype) using below form isnt working can take :
factory table:factoryid,factoryname factorytype table: factorytype,factorytypeid factoryconf table: factoryid,factorytypeid we using hibernate database operations.
model:
@entity @table(name = "factoryconf", uniqueconstraints = { @uniqueconstraint(columnnames = { "factoryid" } ) }) public class factoryconf { @id long factoryid; @onetoone @joincolumn(name = "factoryid", insertable = false, updatable = false) factory factory; @manytoone(optional = false) @joincolumn(name = "factorytypeid") factorytype factorytype; public factoryconf() { super(); } public factoryconf(long factoryid, factorytype factorytype) { super(); this.factorytype = factorytype; this.factoryid = factoryid; } public factory getfactory() { return factory; } public void setfactory(factory factory) { this.factory = factory; } public factorytype getfactorytype() { return factorytype; } public void setfactorytype(factorytype factorytype) { this.factorytype = factorytype; } public long getfactoryid() { return factoryid; } public void setfactoryid(long factoryid) { this.factoryid = factoryid; } public factorytype getfactorytypebyfactoryid(long factoryid){ return factorytype; } } bean class:
/** * bean defined parse each record csv file. * records mapped instances of bean class. * */ public class factorycsvfileinputbean { private string upload_id; private string file_name; private byte file_data; private long id; private string name; private string type; //getter setters } csv parsing:
/** * class defined following actions * 1. validate input csv file format, header columns. * 2. parse csv file list of beans. * 3. validate input records missing data , prepare valid factory list processed. * */ public class factorycsvutil { private static log log = logfactory.getlog(factorycsvutil.class); private static final list<string> fileheaderfields = new arraylist<string>(); private static final string utf8charset = "utf-8"; static{ (field f : factorycsvfileinputbean.class.getdeclaredfields()) { fileheaderfields.add(f.getname()); } } public static list<factorycsvfileinputbean> getcsvinputlist(inputstream inputstream){ csvreader reader = null; list<factorycsvfileinputbean> csvlist = null; factorycsvfileinputbean inputrecord = null; string[] header = null; string[] row = null; try { reader = new csvreader(new inputstreamreader(inputstream,utf8charset)); csvlist = new arraylist<factorycsvfileinputbean>(); header = reader.readnext(); boolean isemptyline = true; while ((row = reader.readnext()) != null) { isemptyline = true; if(!(row.length==1 && stringutils.isblank(row[0]))){//not empty line, not containing ',' inputrecord = new factorycsvfileinputbean(); isemptyline = populatefields(inputrecord, header, row); if(row.length != header.length) //inputrecord.setuploadstatus("not loaded - missing or invalid data"); if(!isemptyline) csvlist.add(inputrecord); } } } catch (ioexception e) { log.debug("ioexception while accessing factorycsvfileinputbean: " + e); return null; } catch (illegalaccessexception e) { log.debug("illegalaccessexception while accessing factorycsvfileinputbean: " + e); return null; } catch (invocationtargetexception e) { log.debug("invocationtargetexception while copying factorycsvfileinputbean properties: " + e); return null; } catch (exception e) { log.debug("exception while parsing csv file: " + e); return null; }finally{ try{ if(reader!=null) reader.close(); }catch(ioexception ioe){} } return csvlist; } protected static boolean populatefields(factorycsvfileinputbean inputrecord,string[] header, string[] row) throws illegalaccessexception, invocationtargetexception { boolean isemptyline = true; (int = 0; < row.length; i++) { string val = row[i]; if(!stringutils.isblank(val)){ beanutilsbean.getinstance().copyproperty(inputrecord, header[i], val); isemptyline = false; } else { //inputrecord.setuploadstatus(string.format("not loaded - missing or invalid data for:%s",header[i])); } } return isemptyline; } public static void validateinputfile(commonsmultipartfile csvfile, model model){ inputstream inputstream = null; csvreader reader = null; string filename = csvfile.getoriginalfilename(); string fileextension = filename.substring(filename.lastindexof('.') + 1); if(fileextension.touppercase().equals("csv")){ try{ inputstream = csvfile.getinputstream(); reader = new csvreader(new inputstreamreader(inputstream,utf8charset)); string[] header = reader.readnext(); if(header!=null){ (int = 0; < header.length; i++) { if(!header[i].equals("") && !fileheaderfields.contains(header[i])){ log.debug("invalid column found in upload file: " + header[i]); model.addattribute("failuremsg", "invalid column found in upload file: " + header[i]); break; } } for(csvheaderfieldsenum field : csvheaderfieldsenum.values()){ if(!arrays.aslist(header).contains(field.getvalue())){ log.debug("missing column in upload file: " + field.getvalue()); model.addattribute("failuremsg", "missing column in upload file: " + field.getvalue()); break; } } }else{ model.addattribute("failuremsg", "file empty - please select valid file"); } string[] data = reader.readnext(); if(data==null){ log.debug("empty file header - no data found"); model.addattribute("failuremsg", "empty file header - no data found"); } }catch(ioexception e){ log.debug("ioexception in reading csv file: " + e); model.addattribute("failuremsg", "exception in reading csv file"); }finally{ if(reader!=null) try{ reader.close(); }catch(ioexception e){ log.debug("ioexception in closing reader of csv file: " + e);} } } else{ model.addattribute("failuremsg", "invalid file format - please select csv file"); } } } model
public class factoryuploadform { private commonsmultipartfile filedata; private string uploadcomment; /** * @return filedata */ public commonsmultipartfile getfiledata() { return filedata; } /** * @param filedata filedata set */ public void setfiledata(commonsmultipartfile filedata) { this.filedata = filedata; } /** * @return uploadcomment */ public string getuploadcomment() { return uploadcomment; } /** * @param uploadcomment uploadcomment set */ public void setuploadcomment(string uploadcomment) { this.uploadcomment = uploadcomment; } public string tostring(){ return " csvfilename: " + getfiledata().getoriginalfilename() + "; upload comment: " + uploadcomment; } } controller
@controller public class factoryuploaddownloadcontroller {
private static final log logger = logfactory.getlog(factoryuploaddownloadcontroller.class); @resource service service; @resource factoryuploadrepository repository; @requestmapping(value = "/submituploadfactoryform") public string uploadfactory(factoryuploadform uploadform, httpservletrequest request, model model, bindingresult result) { logger.debug("===================================================================="); list<factorycsvfileinputbean> csvlist = null; list<factorytype> factorytypes = service.getfactorytypes(); try { commonsmultipartfile file = uploadform.getfiledata(); // parse csv file list csvlist = factorycsvutil.getcsvinputlist(file.getinputstream()); if (csvlist == null) { model.addattribute("failuremsg","error in file parsing - please verify file"); logger.debug("---------------------------------------------------------"); return "sucess"; } } catch (exception e) { logger.debug("sorry isn't working you"); } try { commonsmultipartfile file = uploadform.getfiledata(); (factorycsvfileinputbean inputrecord : csvlist) { factory factoryentity = new factory(); factoryentity.setid(inputrecord.getid()); factoryentity.setname(inputrecord.getname()); factoryentity = this.service.savefactory(factoryentity); factoryconf factoryconf = new factoryconf(); factoryconf.setfactory(factoryentity); factoryconf.setfactorytype(pickfactorytype(factorytypes,inputrecord.gettype())); model.addattribute("factoryconf", factoryconf); this.service.savefactorycfg(factoryconf); } } catch (exception e) { logger.debug("sorry isnt working you"); } return "success"; } private factorytype pickfactorytype(list<factorytype> types, string typename) { (factorytype type : types) { if (type.getfactorytype().equalsignorecase(typename)) return type; } throw new runtimeexception(string.format("factory type invalid :%s", typename)); } }
from question, understand not able parse data csv file. here sample code similar task. think should help.
Comments
Post a Comment