I am using opencsv to parse csv file data which has been uploaded using web and populating the read data in the bean (using HeaderColumnNameTranslateMappingStrategy) which is working fine.
But struggling to find best way to validate (as a first check) that if the file has all the headers before starting processing the data.
Opencsv still process the file and populate null values in the bean when the file does not have all the headers that have been passed as a columnsMapping map.
So given a CSV file you want to ensure that the header contains a set of required elements before processing.
I would create a utility class with a readHeader method that takes the file name and using the CSVReader read the header using the readNext() as an string array (instead of skipping over it) and returns it.
Then you can add a second method that takes that array and an array or list of required fields and then using something like the Apache Commons ArrayUtils make sure that each element in your required array is in the header array and return true if so, false otherwise.
Then if you want you can create a third method that combines the two to hide the complexity .
Hope that helps.
Related
While getting all fields for files is possible for list of files by using the method service.files().list().setFields("files"), setFields() method not working with parameter "files" for service.files().get(id) , it gives error invalid field.
So what should be used in setFields("") method to get all fields for service.files().get(id)??
We will have to set all the fields we want in the response file object in the method setFields() and those will be comma separated. It will do our work but I didn't find any single parameter that can do this work if we want all the fields.
Spring batch : I have a list of files which I need to process and split into different files and write. I followed the steps mentioned in
http://incomplete-code.blogspot.com/2013/07/spring-batch-looping-over-multiple-files.html#comment-form
In this we supply the resource property to the FlatFileItemWriter dynamically using
.
The first step works fine. However, from the next iteration onwards, instead of taking the new value of "input.file", it keeps using the same file (used in first iteration). Can anybody suggest if I can have a writer in each iteration which can take a new dynamic value of the file ?
I have solved the issue:
Here is a proper description of the problem :
Actually code is similar to the one in the posted link above.
Then only difference is that I am passing the "input.file" to FlatFileOutputWriter
instead of FlatFileItemWriter.
In this code the following is being done :
We pass the job parameter "input.files" as a comma seperated list of file names.
In the FileDecision class we retrieve the "input.files" parameter, split it
into a list of string and iterate over it. In each iteration, we put a new parameter
"input.file" in jobExecutionContext.
In the spring bean declaration file (xml file), we pass the value of this
"input.file" (in jobExecutionContext) to a FlatFileItemWriter (as the value of
resource property).
Then we pass this FlatFileItemReader to a job step (multipleFileProcess.step1)
which is being iterated over. We expect the in each iteration, we set a new value
for "input.file" (without 's' at the end of file), and we will be able to write
the content to new output file.
However, instead of writing in separate output
files, it was writing the contents of all the iteration to a single output
file (the output file which was set to the FlatFileItemWriter in the first iteration)
The solution I implemented:
Created a CustomItemWriter and injected it into job step (multipleFileProcess.step1)
which is being iterated over.
Injected the MultiResourceItemWriter to the CustomItemWriter.
Injected the FlatFileItemWrited to MultiResourceItemWriter with resource value="".
In the CustomItemWriter #BeforeStep method,
a. created a new Resource with file value retrieved from "input.file" (in jobExecutionContext).
b. set this new Resource to the MultiResourceItemWriter instance variable.
Now, I am able to write to separate output file in each iteration.
I will post the sample code also after the weekend.
Thanks
I have a Java Method, in a class called by a Java Servlet, that returns an Array of JSONObjects. For example, if I loop through the array in a standard for loop, each object is printed as you would expect a JSONObject to appear.
I am struggling to understand how to use the .getJSON() Jquery method to get this data. Somehow, I need to get these JSONObjects to be called by this method, so I can populate a DataTable (using AJAX).
{"lastName":"doe","requestVar":3,"name":" John","rqTime":"1402600668949"}
{"lastName":"doe","requestVar":4,"name":" Jane","rqTime":"1402677126117"}
Currently, the only answers I can find are when people use the .getJSON method on a .JSON file; is there any way for me to .getJSON from a .java file?
In the .getJSON method, you can call a URL. So make a servlet call in this URL. Make that servlet return the JSON Array as string. In Javascript, get the output and use it.
I have an enum which stores numerous xml strings like this
enum1("<?xml version='1.0'?><!DOCTYPE FOO><FOO><HEADER><MESSAGE-ID>1</MESSAGE-ID><MESSAGE-TYPE>RQ</MESSAGE-TYPE></HEADER>[...someXMLstructure...]</FOO>")
enum2("<?xml version='1.0'?><!DOCTYPE FOO><FOO><HEADER><MESSAGE-ID>1</MESSAGE-ID><MESSAGE-TYPE>Q</MESSAGE-TYPE></HEADER>[...someOTHERXMLstructure...]</FOO>")
the string is accessed via myEnum.getTag()
My application takes user input for some of the nodes represented in this string stored as a Map<String, String> with the key of the map being the element of the XML doc, and the value being the value.
Essentially i want to copy the XML from the enum and replace any corresponding elements found in the map, then send this message as a String over via some third party, unrelated interface. For example the user could pass in MESSAGE-ID=3 and USER-ID=360 so we would find the MESSAGE-ID element of the string and replace that, then find USER-ID in some unknown generic location and replace that. If USER-ID isn't found in the XML, then it is simply discarded.
EDIT:
If there is a better way to do this i.e. convert the XML into some data structure and modify this then convert back to string, I'd be happy to hear it, however I couldn't find much on doing this generically in my searches.
Thanks for your help
I have some domain classes and i want to init and fill those classes with sample hardcode data , is there any method which i can fill data with any framework ?
For Example : List<Customer> should be filled with some mock data
Consider maintaining your test data in a JSON structure, and use a framework (e.g. google-gson) to deserialize the data into value objects.
If you wish to auto-generate random data, you might want to look into something like Quickcheck, which seems to be Java's equivalent of the .NET framework Autofixture.
As #ipavlic wrote, you might make your constructor generate some random data when the object is created.
You may store the data in a DB or a simple text file and read it from there when you fill your list.
You may combine aproach 1 and 2 and store possible field values in a file or somewhere else and fill the Object fields with these randomly chosen predefined values.
If you want fill list of Customer, there is this method Collections.fill(java.util.List, T) to fill list. This method replace current objects in list. If list is empty it won't fill.
You can put your hard-coded data in a constructor.
If it's mocking frameworks that you are after (as you indicate in comments), then take a look at e.g. Mockito.