Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- file validation code with code to retrieve json data from db.
- import oracle.jdbc.OracleConnection;
- import oracle.jdbc.pool.OracleDataSource;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Iterator;
- import org.apache.commons.io.FilenameUtils;
- import org.apache.tika.Tika;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.sql.Blob;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class FetchRows {
- //private static final int BUFFER_SIZE = 4096;
- private static String s;
- private static String extvalstatus;
- private static String typevalstatus;
- private static String namevalstatus;
- private static ArrayList<String> valstatus = new ArrayList();
- public String fetchJSON() throws FileNotFoundException, IOException {
- //public static void main(String[] args) throws IOException {
- try {
- OracleDataSource ods = new OracleDataSource();
- ods.setURL("jdbc:oracle:thin:system/admin@localhost:1521/xe");
- // jdbc:oracle:thin:@localhost:1521:xe
- // connect to the database and turn off auto commit
- OracleConnection ocon = (OracleConnection)ods.getConnection();
- ocon.setAutoCommit(false);
- // create the statement and execute the query
- Statement stmt = ocon.createStatement();
- ResultSet rset=stmt.executeQuery("select JSON_DATA from table007");
- // print out the results
- while(rset.next()) {
- Blob lob =rset.getBlob("JSON_DATA");
- byte[] bd = lob.getBytes(1,(int)lob.length());
- // System.out.println(bd);
- s = new String(bd);
- ocon.close();
- }} catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return s;
- // System.out.println(s);
- }
- public void fileValidations(String excelFilePath) throws IOException, ParseException {
- fetchJSON();
- //System.out.println(s);
- String jsonp=s;
- JSONParser parser = new JSONParser();
- Object obj = parser.parse(new String(jsonp));
- JSONObject jsonObject = (JSONObject) obj;
- // file object
- JSONArray fileExtensionList = (JSONArray) jsonObject.get("fileExtension");
- String fileTypeInMaster = (String) jsonObject.get("fileType");
- String fileNameInMaster = (String) jsonObject.get("fileName");
- //checking if file extension matches metadata file extension
- @SuppressWarnings("unchecked")
- Iterator<String> fileExtensionListIterator = fileExtensionList.iterator();
- while (fileExtensionListIterator.hasNext()) {
- String[] fileExt = { fileExtensionListIterator.next() };
- String fileExtension = FilenameUtils.getExtension(excelFilePath);
- if (Arrays.asList(fileExt).contains(fileExtension)) {
- extvalstatus="file extension valid";
- //System.out.println(extvalstatus);
- }
- else {
- extvalstatus ="file extension invalid";
- // System.out.println(extvalstatus);
- }
- valstatus.add(extvalstatus);
- }
- //validating file type
- Tika tika = new Tika();
- String fileType = tika.detect(excelFilePath);
- // System.out.println("input file type: " +fileType);
- if (fileType.equals(fileTypeInMaster)) {
- typevalstatus = "valid file type";
- //System.out.println(typevalstatus);
- }
- else {
- typevalstatus = "invalid file type";
- //System.out.println(typevalstatus);
- }
- valstatus.add(typevalstatus);
- //validating file name
- String filename = FilenameUtils.getBaseName(excelFilePath);
- // System.out.println("input file name: " +filename);
- if (filename.equals(fileNameInMaster)) {
- namevalstatus = "filename valid";
- //System.out.println(namevalstatus);
- }
- else {
- namevalstatus ="filename invalid";
- }
- valstatus.add(namevalstatus);
- //worksheetname validation
- int worksheetCount = Integer.parseInt((String) jsonObject.get("worksheetCount"));
- //System.out.println(worksheetCount);
- JSONArray worksheetListArray = (JSONArray) jsonObject.get("worksheetList");
- for (String i:valstatus) {
- System.out.println("file validation status:" +i);
- }
- }
- }
- //methods for file vals
- /* public static String fileTypeValidation(String excelFilePath)
- throws FileNotFoundException, IOException, ParseException {
- // getting file type in master template
- fetchJSON();
- String jsonp = s;
- JSONParser parser = new JSONParser();
- Object obj = parser.parse(new FileReader(jsonp));
- JSONObject jsonObject = (JSONObject) obj;
- String fileTypeInMaster = (String) jsonObject.get("fileType");
- System.out.println("expected file type: " +fileTypeInMaster);
- // validating if input file is valid file type
- Tika tika = new Tika();
- String fileType = tika.detect(excelFilePath);
- // System.out.println("input file type: " +fileType);
- if (fileType.equals(fileTypeInMaster)) {
- typevalstatus = "valid file type";
- //System.out.println(typevalstatus);
- }
- else {
- typevalstatus = "invalid file type";
- //System.out.println(typevalstatus);
- }
- return (typevalstatus);
- }
- /* public String fileNameValidation(String excelFilePath)
- throws FileNotFoundException, IOException, ParseException {
- fetchJSON();
- String jsonp = s;
- JSONParser parser = new JSONParser();
- Object obj = parser.parse(new FileReader(jsonp));
- JSONObject jsonObject = (JSONObject) obj;
- String fileNameInMaster = (String) jsonObject.get("fileName");
- // file name validation
- // System.out.println("expected file name: " +fileNameInMaster);
- String filename = FilenameUtils.getBaseName(excelFilePath);
- // System.out.println("input file name: " +filename);
- if (filename.equals(fileNameInMaster)) {
- namevalstatus = "filename valid";
- //System.out.println(namevalstatus);
- }
- else {
- namevalstatus ="filename invalid";
- }
- return (namevalstatus);
- }
- */
- //main class
- import java.io.IOException;
- import org.json.simple.parser.ParseException;
- public class MainClass {
- public static void main(String[] args) throws IOException {
- FetchRows f = new FetchRows();
- String excelFilePath = "C:\\Commback\\CreditSuisse\\Dummy123.xlsx";
- try {
- f.fileValidations(excelFilePath);
- // f.fileTypeValidation(excelFilePath);
- // f.fileNameValidation(excelFilePath);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment