1
+ import java .io .IOException ;
2
+ import java .io .InputStream ;
3
+ import java .net .URL ;
4
+ import java .nio .file .Files ;
5
+ import java .nio .file .Paths ;
6
+ import java .sql .ResultSet ;
7
+ import java .sql .SQLException ;
8
+
9
+ class Item {
10
+
11
+ private static String [] match = {"/" , "& " , "&" , "-" , "," };
12
+
13
+ static void getItems (ResultSet resultSet , String type ) throws SQLException {
14
+
15
+ System .out .println ("Displaying all items" );
16
+
17
+ if ( type .equals ("wheels" ) ) {
18
+ while ( resultSet .next () )
19
+ System .out .println ( downloadWheels ( resultSet ) );
20
+
21
+ } else if ( type .equals ("tires" ) ) {
22
+ while ( resultSet .next () )
23
+ System .out .println ( downloadTires ( resultSet ) );
24
+ }
25
+
26
+ System .out .println ("Finished" );
27
+ }
28
+
29
+ private static String downloadWheels (ResultSet resultSet ) throws SQLException {
30
+ String item_number = resultSet .getString ( "item_number" );
31
+ String brand = resultSet .getString ( "brand" );
32
+ String model = resultSet .getString ( "model" );
33
+ String finish = resultSet .getString ( "finish" );
34
+
35
+ brand = sanitizeData ( brand );
36
+ model = sanitizeData ( model );
37
+ finish = sanitizeData ( finish );
38
+
39
+ String image_location = "http://website.com/version_2/img/wheels/" + brand + "/" + model + "_" + finish + ".jpg" ;
40
+
41
+ try (InputStream in = new URL ( image_location ).openStream ()){
42
+ Files .copy (in , Paths .get ("D:/DB/FCWT/applications/eci-website-images/wheels/" + item_number + ".jpg" ));
43
+ } catch (IOException e ) {
44
+ //try with png
45
+ try (InputStream in = new URL ( image_location .replace ( ".jpg" , ".png" ) ).openStream ()) {
46
+ Files .copy (in , Paths .get ("D:/DB/FCWT/applications/eci-website-images/wheels/" + item_number + ".jpg" ));
47
+ } catch ( IOException ignored ) {}
48
+ }
49
+
50
+ return image_location ;
51
+ }
52
+
53
+ private static String downloadTires (ResultSet resultSet ) throws SQLException {
54
+ String item_number = resultSet .getString ( "item_number" );
55
+ String brand = resultSet .getString ( "brand" );
56
+ String model = resultSet .getString ( "model" );
57
+
58
+ brand = sanitizeData ( brand );
59
+ model = sanitizeData ( model );
60
+
61
+ String image_location = "http://website.com/version_2/img/tires/" + brand + "/" + model + ".jpg" ;
62
+
63
+ try (InputStream in = new URL ( image_location ).openStream ()){
64
+ Files .copy (in , Paths .get ("D:/DB/FCWT/applications/eci-website-images/tires/" + item_number + ".jpg" ));
65
+ } catch (IOException e ) {
66
+ //try with png
67
+ try (InputStream in = new URL ( image_location .replace ( ".jpg" , ".png" ) ).openStream ()) {
68
+ Files .copy (in , Paths .get ("D:/DB/FCWT/applications/eci-website-images/tires/" + item_number + ".jpg" ));
69
+ } catch ( IOException ignored ) {}
70
+ }
71
+
72
+ return image_location ;
73
+ }
74
+
75
+ private static String sanitizeData ( String str ) {
76
+ str = str .toLowerCase ();
77
+
78
+ for ( String replacement : match )
79
+ str = str .replace ( replacement , "" );
80
+
81
+ str = str .replace (" " , "_" );
82
+
83
+ return str ;
84
+ }
85
+ }
0 commit comments