Guest User

Untitled

a guest
Jan 7th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. file validation code with code to retrieve json data from db.
  2.  
  3.  
  4.  
  5. import oracle.jdbc.OracleConnection;
  6. import oracle.jdbc.pool.OracleDataSource;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.Iterator;
  11. import org.apache.commons.io.FilenameUtils;
  12. import org.apache.tika.Tika;
  13. import org.json.simple.JSONArray;
  14. import org.json.simple.JSONObject;
  15. import org.json.simple.parser.JSONParser;
  16. import org.json.simple.parser.ParseException;
  17. import java.io.FileNotFoundException;
  18. import java.io.FileReader;
  19. import java.io.IOException;
  20. import java.sql.Blob;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. public class FetchRows {
  24. //private static final int BUFFER_SIZE = 4096;
  25. private static String s;
  26. private static String extvalstatus;
  27. private static String typevalstatus;
  28. private static String namevalstatus;
  29. private static ArrayList<String> valstatus = new ArrayList();
  30. public String fetchJSON() throws FileNotFoundException, IOException {
  31. //public static void main(String[] args) throws IOException {
  32. try {
  33. OracleDataSource ods = new OracleDataSource();
  34. ods.setURL("jdbc:oracle:thin:system/admin@localhost:1521/xe");
  35. // jdbc:oracle:thin:@localhost:1521:xe
  36. // connect to the database and turn off auto commit
  37. OracleConnection ocon = (OracleConnection)ods.getConnection();
  38. ocon.setAutoCommit(false);
  39.  
  40. // create the statement and execute the query
  41. Statement stmt = ocon.createStatement();
  42. ResultSet rset=stmt.executeQuery("select JSON_DATA from table007");
  43.  
  44. // print out the results
  45. while(rset.next()) {
  46. Blob lob =rset.getBlob("JSON_DATA");
  47. byte[] bd = lob.getBytes(1,(int)lob.length());
  48. // System.out.println(bd);
  49. s = new String(bd);
  50. ocon.close();
  51.  
  52.  
  53. }} catch (SQLException e) {
  54. System.out.println(e.getMessage());
  55. }
  56. return s;
  57. // System.out.println(s);
  58. }
  59. public void fileValidations(String excelFilePath) throws IOException, ParseException {
  60. fetchJSON();
  61. //System.out.println(s);
  62. String jsonp=s;
  63. JSONParser parser = new JSONParser();
  64. Object obj = parser.parse(new String(jsonp));
  65. JSONObject jsonObject = (JSONObject) obj;
  66. // file object
  67. JSONArray fileExtensionList = (JSONArray) jsonObject.get("fileExtension");
  68. String fileTypeInMaster = (String) jsonObject.get("fileType");
  69. String fileNameInMaster = (String) jsonObject.get("fileName");
  70. //checking if file extension matches metadata file extension
  71. @SuppressWarnings("unchecked")
  72. Iterator<String> fileExtensionListIterator = fileExtensionList.iterator();
  73. while (fileExtensionListIterator.hasNext()) {
  74. String[] fileExt = { fileExtensionListIterator.next() };
  75. String fileExtension = FilenameUtils.getExtension(excelFilePath);
  76. if (Arrays.asList(fileExt).contains(fileExtension)) {
  77. extvalstatus="file extension valid";
  78. //System.out.println(extvalstatus);
  79. }
  80. else {
  81. extvalstatus ="file extension invalid";
  82. // System.out.println(extvalstatus);
  83. }
  84. valstatus.add(extvalstatus);
  85.  
  86.  
  87. }
  88.  
  89. //validating file type
  90. Tika tika = new Tika();
  91. String fileType = tika.detect(excelFilePath);
  92. // System.out.println("input file type: " +fileType);
  93. if (fileType.equals(fileTypeInMaster)) {
  94. typevalstatus = "valid file type";
  95. //System.out.println(typevalstatus);
  96. }
  97. else {
  98. typevalstatus = "invalid file type";
  99. //System.out.println(typevalstatus);
  100. }
  101. valstatus.add(typevalstatus);
  102. //validating file name
  103. String filename = FilenameUtils.getBaseName(excelFilePath);
  104. // System.out.println("input file name: " +filename);
  105. if (filename.equals(fileNameInMaster)) {
  106. namevalstatus = "filename valid";
  107. //System.out.println(namevalstatus);
  108. }
  109. else {
  110. namevalstatus ="filename invalid";
  111.  
  112. }
  113. valstatus.add(namevalstatus);
  114. //worksheetname validation
  115. int worksheetCount = Integer.parseInt((String) jsonObject.get("worksheetCount"));
  116. //System.out.println(worksheetCount);
  117. JSONArray worksheetListArray = (JSONArray) jsonObject.get("worksheetList");
  118.  
  119. for (String i:valstatus) {
  120. System.out.println("file validation status:" +i);
  121. }
  122. }
  123.  
  124. }
  125.  
  126.  
  127. //methods for file vals
  128. /* public static String fileTypeValidation(String excelFilePath)
  129. throws FileNotFoundException, IOException, ParseException {
  130. // getting file type in master template
  131. fetchJSON();
  132. String jsonp = s;
  133. JSONParser parser = new JSONParser();
  134. Object obj = parser.parse(new FileReader(jsonp));
  135. JSONObject jsonObject = (JSONObject) obj;
  136. String fileTypeInMaster = (String) jsonObject.get("fileType");
  137. System.out.println("expected file type: " +fileTypeInMaster);
  138.  
  139. // validating if input file is valid file type
  140. Tika tika = new Tika();
  141. String fileType = tika.detect(excelFilePath);
  142. // System.out.println("input file type: " +fileType);
  143. if (fileType.equals(fileTypeInMaster)) {
  144. typevalstatus = "valid file type";
  145. //System.out.println(typevalstatus);
  146. }
  147. else {
  148. typevalstatus = "invalid file type";
  149. //System.out.println(typevalstatus);
  150. }
  151. return (typevalstatus);
  152. }
  153.  
  154. /* public String fileNameValidation(String excelFilePath)
  155. throws FileNotFoundException, IOException, ParseException {
  156. fetchJSON();
  157. String jsonp = s;
  158. JSONParser parser = new JSONParser();
  159. Object obj = parser.parse(new FileReader(jsonp));
  160. JSONObject jsonObject = (JSONObject) obj;
  161. String fileNameInMaster = (String) jsonObject.get("fileName");
  162. // file name validation
  163. // System.out.println("expected file name: " +fileNameInMaster);
  164. String filename = FilenameUtils.getBaseName(excelFilePath);
  165. // System.out.println("input file name: " +filename);
  166. if (filename.equals(fileNameInMaster)) {
  167. namevalstatus = "filename valid";
  168. //System.out.println(namevalstatus);
  169. }
  170. else {
  171. namevalstatus ="filename invalid";
  172.  
  173. }
  174. return (namevalstatus);
  175. }
  176. */
  177.  
  178.  
  179. //main class
  180. import java.io.IOException;
  181.  
  182. import org.json.simple.parser.ParseException;
  183.  
  184. public class MainClass {
  185. public static void main(String[] args) throws IOException {
  186. FetchRows f = new FetchRows();
  187. String excelFilePath = "C:\\Commback\\CreditSuisse\\Dummy123.xlsx";
  188. try {
  189.  
  190. f.fileValidations(excelFilePath);
  191. // f.fileTypeValidation(excelFilePath);
  192. // f.fileNameValidation(excelFilePath);
  193.  
  194. } catch (ParseException e) {
  195. // TODO Auto-generated catch block
  196. e.printStackTrace();
  197. }
  198. }
  199. }
Add Comment
Please, Sign In to add comment