-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.java
More file actions
510 lines (504 loc) · 22.3 KB
/
Utility.java
File metadata and controls
510 lines (504 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import java.io.File;
import java.util.*;
import java.util.Scanner;
import java.util.Map.Entry;
import java.io.IOException;
import java.io.FileWriter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;
import java.io.FileInputStream;
class Utility
{
public static HashMap< ArrayList<String>,ArrayList<String> >map_notfound=null;
public static HashMap< ArrayList<String>,ArrayList<String> >map_modified=null;
public static HashMap< ArrayList<String>,ArrayList<String> >map_found=null;
public static void writeFile(String FileName,String arg,boolean parameter)
{
try
{
FileWriter writer = new FileWriter(FileName, parameter);
writer.write(arg.toString());
writer.write("\r\n"); // write new line
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static String getFileExtension(String fileName)
{
fileName = fileName.toString();
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
return fileName.substring(fileName.lastIndexOf(".")+1);
else return "";
}
public static ArrayList<String>getMainFolders(String path)
{
ArrayList<String>mainFolders=new ArrayList<String>();
File root = new File( path );
File[] list = root.listFiles();
if(list==null)
return null;
for(File f:list)
{
mainFolders.add(f.toString());
}
return mainFolders;
}
public static void getPath(String path,ArrayList<String>pathsXlsx,ArrayList<String>pathsXls)
{
//System.out.println(path.toString());
File root = new File( path );
File[] list = root.listFiles();
// System.out.println(path+list.length);
if(list==null)
return;
for ( File f : list )
{
if ( f.isDirectory() )
{
getPath( f.getAbsolutePath().toString(),pathsXlsx,pathsXls);
}
else
{
String s=f.getAbsolutePath();
String fileExtn = Utility.getFileExtension(s);
if(fileExtn.equals("xlsx") )
{
pathsXlsx.add(s);
}
else if(fileExtn.equals("xls"))
{
pathsXls.add(s);
}
}
}
}
public static String stripExtension(final String path)
{
return path != null && path.lastIndexOf(".") > 0 ? path.substring(0, path.lastIndexOf(".")) : path;
}
public static boolean match(ArrayList<String> tempExcelLinked, ArrayList<String>Utility )
{
if(!(stripExtension(tempExcelLinked.get(tempExcelLinked.size()-1)).equals(stripExtension(Utility.get(Utility.size()-1)))))
return false;
for(int i=0;i<tempExcelLinked.size()-1;i++)
{
if(!(tempExcelLinked.get(i).toLowerCase().equals(Utility.get(i).toLowerCase())))
{
return false;
}
}
return true;
}
public static void readExcel_xlsx(String mainFolder,String absolutePathExtracted, ArrayList<String>relPathExtracted)
{
try
{
File excel=new File(absolutePathExtracted);
FileInputStream file = new FileInputStream(excel);
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
if(sheet==null)
{
workbook.close();
return;
}
Row row = sheet.getRow(19);
if(row==null)
{
workbook.close();
return;
}
Iterator<Cell> cellIterator = row.cellIterator();
int colNo=0;
boolean flag=false;
String excelLinked=new String();
while (cellIterator!=null && cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
excelLinked=cell.getStringCellValue();
if(excelLinked.contentEquals("EXCEL TO BE LINKED"))
{
colNo=(cell.getColumnIndex());
flag=true;
break;
}
break;
case Cell.CELL_TYPE_NUMERIC:
break;
default:
break;
}
if(flag)
break;
}
for (int rowIndex = 20; flag==true && rowIndex <= sheet.getLastRowNum(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(colNo);
if (cell != null && cell.getCellType()==Cell.CELL_TYPE_STRING && row.getCell(colNo).getStringCellValue().length()!=0)
{
boolean flag1=false;
excelLinked=(row.getCell(colNo).getStringCellValue()).toString();
StringTokenizer st = new StringTokenizer(excelLinked.toString(),"\\//");
ArrayList<String>listExcelLinked=new ArrayList<String>();
while (st.hasMoreTokens())
{
listExcelLinked.add(st.nextToken());
}
int size=listExcelLinked.size();
if(size==0)
continue;
StringTokenizer st1=null;
ArrayList<String>list1=new ArrayList<String>();
if((Utility.map_found).containsKey(listExcelLinked))
{
writeFile("CorrectPaths.txt",absolutePathExtracted+" - "+excelLinked.toString()+"\n",true);
System.out.println("CorrectPath"+" - "+excelLinked);
continue;
}
else if((Utility.map_modified).containsKey(listExcelLinked))
{
list1=map_modified.get(listExcelLinked);
String updatedExcelLinked=excelLinked;
for(int i=0;i<size;i++)
{
if(!(listExcelLinked.get(i).equals(list1.get(i))))
{
updatedExcelLinked=excelLinked.replace(listExcelLinked.get(i), list1.get(i));
}
}
cell.setCellValue(updatedExcelLinked);
FileOutputStream os=new FileOutputStream(excel);
workbook.write(os);
os.close();
writeFile("LinksModified.txt",absolutePathExtracted+" - "+excelLinked+" - "+updatedExcelLinked.toString()+"\n",true);
System.out.println(excelLinked.toString()+"-"+updatedExcelLinked.toString());
System.out.println();
}
else if((Utility.map_notfound).containsKey(listExcelLinked))
{
Utility.writeFile("FilesNotFound.txt",absolutePathExtracted+" - "+excelLinked.toString(),true);
System.out.println("File not found - "+excelLinked.toString()+" - in the folder - "+mainFolder);
}
else
{
for(int i=0;i<relPathExtracted.size();i++)
{
ArrayList<String>listRelPathExtracted=new ArrayList<String>();
ArrayList<String>tempListRelPathExtracted=new ArrayList<String>();
st1 = new StringTokenizer(relPathExtracted.get(i).toString(),"\\//");
while (st1.hasMoreTokens())
{
listRelPathExtracted.add(st1.nextToken());
}
for(int j=listRelPathExtracted.size()-size;j<=listRelPathExtracted.size()-1 && j>=0;j++)
{
tempListRelPathExtracted.add(listRelPathExtracted.get(j));
}
flag1=match(listExcelLinked,tempListRelPathExtracted);
if(flag1)
{
list1=tempListRelPathExtracted;
break;
}
}
if(flag1)
{
boolean flag2=false;
String updatedExcelLinked=excelLinked;
String s=null;
if(!((Utility.getFileExtension(listExcelLinked.get(size-1))).equals("xls")||(Utility.getFileExtension(listExcelLinked.get(size-1))).equals("xlsx")))
{
s=stripExtension(list1.get(size-1));
list1.add(size-1,s);
}
for(int i=0;i<size;i++)
{
if(!(listExcelLinked.get(i).equals(list1.get(i))))
{
flag2=true;
updatedExcelLinked=excelLinked.replace(listExcelLinked.get(i), list1.get(i));
}
}
if(flag2)
{
map_modified.put(listExcelLinked,list1);
cell.setCellValue(updatedExcelLinked);
FileOutputStream os=new FileOutputStream(excel);
workbook.write(os);
os.close();
writeFile("LinksModified.txt",absolutePathExtracted+" - "+excelLinked+" - "+updatedExcelLinked.toString()+"\n",true);
System.out.println(excelLinked.toString()+"-"+updatedExcelLinked.toString());
System.out.println();
}
else
{
map_found.put(listExcelLinked,listExcelLinked);
writeFile("CorrectPaths.txt",absolutePathExtracted+" - "+excelLinked.toString()+"\n",true);
System.out.println("CorrectPath"+" - "+excelLinked);
continue;
}
}
else
{
map_notfound.put(listExcelLinked,listExcelLinked);
Utility.writeFile("FilesNotFound.txt",absolutePathExtracted+" - "+excelLinked.toString(),true);
System.out.println("File not found - "+excelLinked.toString()+" - in the folder - "+mainFolder);
}
cell=null;
}
}
}
}
workbook.close();
file.close();
}
catch (Exception e)
{
writeFile("Exceptions_file.txt",e+" - \t\t\t - "+absolutePathExtracted,true);
System.out.println(e+" - file read - "+absolutePathExtracted);
System.out.println(e.getStackTrace());
}
}
public static void readExcel_xls(String mainFolder,String absolutePathExtracted, ArrayList<String>relPathExtracted)
{
try
{
File excel=new File(absolutePathExtracted);
FileInputStream file = new FileInputStream(excel);
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
if(sheet==null)
{
workbook.close();
return;
}
Row row = sheet.getRow(19);
if(row==null)
{
workbook.close();
return;
}
Iterator<Cell> cellIterator = row.cellIterator();
int colNo=0;
boolean flag=false;
String excelLinked=new String();
while (cellIterator!=null && cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
excelLinked=cell.getStringCellValue();
if(excelLinked.contentEquals("EXCEL TO BE LINKED"))
{
colNo=(cell.getColumnIndex());
flag=true;
break;
}
break;
case Cell.CELL_TYPE_NUMERIC:
break;
default:
break;
}
if(flag)
break;
}
for (int rowIndex = 20; flag==true && rowIndex <= sheet.getLastRowNum(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(colNo);
if (cell != null && cell.getCellType()==Cell.CELL_TYPE_STRING && row.getCell(colNo).getStringCellValue().length()!=0)
{
boolean flag1=false;
excelLinked=(row.getCell(colNo).getStringCellValue()).toString();
StringTokenizer st = new StringTokenizer(excelLinked.toString(),"\\//");
ArrayList<String>listExcelLinked=new ArrayList<String>();
while (st.hasMoreTokens())
{
listExcelLinked.add(st.nextToken());
}
int size=listExcelLinked.size();
if(size==0)
continue;
StringTokenizer st1=null;
ArrayList<String>list1=new ArrayList<String>();
if((Utility.map_found).containsKey(listExcelLinked))
{
writeFile("CorrectPaths.txt",absolutePathExtracted+" - "+excelLinked.toString()+"\n",true);
System.out.println("CorrectPath"+" - "+excelLinked);
continue;
}
else if((Utility.map_modified).containsKey(listExcelLinked))
{
list1=map_modified.get(listExcelLinked);
String updatedExcelLinked=excelLinked;
for(int i=0;i<size;i++)
{
if(!(listExcelLinked.get(i).equals(list1.get(i))))
{
updatedExcelLinked=excelLinked.replace(listExcelLinked.get(i), list1.get(i));
}
}
cell.setCellValue(updatedExcelLinked);
FileOutputStream os=new FileOutputStream(excel);
workbook.write(os);
os.close();
writeFile("LinksModified.txt",absolutePathExtracted+" - "+excelLinked+" - "+updatedExcelLinked.toString()+"\n",true);
System.out.println(excelLinked.toString()+"-"+updatedExcelLinked.toString());
System.out.println();
}
else if((Utility.map_notfound).containsKey(listExcelLinked))
{
Utility.writeFile("FilesNotFound.txt",absolutePathExtracted+" - "+excelLinked.toString(),true);
System.out.println("File not found - "+excelLinked.toString()+" - \t\tin the folder - "+mainFolder);
}
else
{
for(int i=0;i<relPathExtracted.size();i++)
{
ArrayList<String>listRelPathExtracted=new ArrayList<String>();
ArrayList<String>tempListRelPathExtracted=new ArrayList<String>();
st1 = new StringTokenizer(relPathExtracted.get(i).toString(),"\\//");
while (st1.hasMoreTokens())
{
listRelPathExtracted.add(st1.nextToken());
}
for(int j=listRelPathExtracted.size()-size;j<=listRelPathExtracted.size()-1 && j>=0;j++)
{
tempListRelPathExtracted.add(listRelPathExtracted.get(j));
}
flag1=match(listExcelLinked,tempListRelPathExtracted);
if(flag1)
{
list1=tempListRelPathExtracted;
break;
}
}
if(flag1)
{
boolean flag2=false;
String updatedExcelLinked=excelLinked;
String s=null;
if(!((Utility.getFileExtension(listExcelLinked.get(size-1))).equals("xls")||(Utility.getFileExtension(listExcelLinked.get(size-1))).equals("xlsx")))
{
s=stripExtension(list1.get(size-1));
list1.add(size-1,s);
}
for(int i=0;i<size;i++)
{
if(!(listExcelLinked.get(i).equals(list1.get(i))))
{
flag2=true;
updatedExcelLinked=excelLinked.replace(listExcelLinked.get(i), list1.get(i));
}
}
if(flag2)
{
map_modified.put(listExcelLinked,list1);
cell.setCellValue(updatedExcelLinked);
FileOutputStream os=new FileOutputStream(excel);
workbook.write(os);
os.close();
writeFile("LinksModified.txt",absolutePathExtracted+" - "+excelLinked+" - "+updatedExcelLinked.toString()+"\n",true);
System.out.println(excelLinked.toString()+"-"+updatedExcelLinked.toString());
System.out.println();
}
else
{
map_found.put(listExcelLinked,listExcelLinked);
writeFile("CorrectPaths.txt",absolutePathExtracted+" - "+excelLinked.toString()+"\n",true);
System.out.println("CorrectPath"+" - "+excelLinked);
continue;
}
}
else
{
map_notfound.put(listExcelLinked,listExcelLinked);
Utility.writeFile("FilesNotFound.txt",absolutePathExtracted+" - "+excelLinked.toString(),true);
System.out.println("File not found - "+excelLinked.toString()+" - \t\tin the folder - "+mainFolder);
}
cell=null;
}
}
}
}
workbook.close();
file.close();
}
catch (Exception e)
{
writeFile("Exceptions_file.txt",e+" - file read - "+absolutePathExtracted,true);
System.out.println(e+" - \t\t\t - "+absolutePathExtracted);
System.out.println(e.getStackTrace());
}
}
public static void main(String[] args)
{
try
{
map_notfound=new HashMap<ArrayList<String>,ArrayList<String> >();
map_found=new HashMap<ArrayList<String>,ArrayList<String> >();
map_modified=new HashMap<ArrayList<String>,ArrayList<String> >();
writeFile("Exceptions_file.txt"," \t\tException Name \t\t\t \t\t\t\t\t\t\t\t\t- file read ",false);
writeFile("CorrectPaths.txt","\t\tExcel File Read\t\t\t\t-\t\t\t\t\tCorrectPaths (Files Found)\n\n\n",false);
writeFile("FilesNotFound.txt","\t\tExcel File Read\t\t\t\t-\t\t\t\twrong path written in the given excel file (Files Not Found)\n\n\tEither Spelling Mistakes or File Doesn't Exist\n\n\n",false);
writeFile("LinksModified.txt","\t\tExcel File Read\t\t\t-\t\t\t\t\t\tOldLink\t\t\t\t-\t\t\t\t\t\tNewLink\n\n\n",false);
int i;
ArrayList<String>Folders=new ArrayList<String>();
System.out.println("Enter the path(ex: D:\\\\DMS\\\\Automation ):");
Scanner sc = new Scanner(System.in);
String pathInput = sc. nextLine();
ArrayList<String>mainFolders=new ArrayList<String>();
Folders=Utility.getMainFolders(pathInput);
for(i=0;i<Folders.size();i++)
{
ArrayList<String>arr=new ArrayList<String>();
StringTokenizer st = new StringTokenizer(Folders.get(i).toString());
while (st.hasMoreTokens())
{
arr.add(st.nextToken("\\"));
}
mainFolders.add(arr.get(arr.size()-1));
}
for(i=0;i<mainFolders.size() && mainFolders!=null;i++)
{
map_notfound=new HashMap<ArrayList<String>,ArrayList<String> >();
map_found=new HashMap<ArrayList<String>,ArrayList<String> >();
map_modified=new HashMap<ArrayList<String>,ArrayList<String> >();
ArrayList<String>path_xlsx=new ArrayList<String>();
ArrayList<String>path_xls=new ArrayList<String>();
getPath(pathInput+"\\"+mainFolders.get(i),path_xlsx,path_xls);
//System.out.println(path_xls.size());
for(int j=0;j<path_xlsx.size()&&path_xlsx!=null;j++)
{
Utility.readExcel_xlsx(mainFolders.get(i),path_xlsx.get(j).toString(),path_xlsx);
}
for(int j=0;j<path_xls.size()&&path_xls!=null;j++)
{
Utility.readExcel_xls(mainFolders.get(i),path_xls.get(j).toString(),path_xls);
}
}
System.out.println("\n\n\n\t\t\t\t\tProgram Executed Successfully");
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
}