-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathItem.java
85 lines (64 loc) · 3.18 KB
/
Item.java
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
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.SQLException;
class Item {
private static String[] match = {"/", "& ", "&", "-", ","};
static void getItems(ResultSet resultSet, String type) throws SQLException {
System.out.println("Displaying all items");
if ( type.equals("wheels") ) {
while( resultSet.next() )
System.out.println( downloadWheels( resultSet ) );
} else if ( type.equals("tires") ) {
while( resultSet.next() )
System.out.println( downloadTires( resultSet ) );
}
System.out.println("Finished");
}
private static String downloadWheels(ResultSet resultSet) throws SQLException {
String item_number = resultSet.getString( "item_number");
String brand = resultSet.getString( "brand" );
String model = resultSet.getString( "model" );
String finish = resultSet.getString( "finish" );
brand = sanitizeData( brand );
model = sanitizeData( model );
finish = sanitizeData( finish );
String image_location = "http://website.com/version_2/img/wheels/" + brand + "/" + model + "_" + finish + ".jpg";
try(InputStream in = new URL( image_location ).openStream()){
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/wheels/" + item_number + ".jpg"));
} catch (IOException e) {
//try with png
try(InputStream in = new URL( image_location.replace( ".jpg", ".png" ) ).openStream()) {
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/wheels/" + item_number + ".jpg"));
} catch ( IOException ignored ) {}
}
return image_location;
}
private static String downloadTires(ResultSet resultSet) throws SQLException {
String item_number = resultSet.getString( "item_number");
String brand = resultSet.getString( "brand" );
String model = resultSet.getString( "model" );
brand = sanitizeData( brand );
model = sanitizeData( model );
String image_location = "http://website.com/version_2/img/tires/" + brand + "/" + model + ".jpg";
try(InputStream in = new URL( image_location ).openStream()){
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/tires/" + item_number + ".jpg"));
} catch (IOException e) {
//try with png
try(InputStream in = new URL( image_location.replace( ".jpg", ".png" ) ).openStream()) {
Files.copy(in, Paths.get("D:/DB/FCWT/applications/eci-website-images/tires/" + item_number + ".jpg"));
} catch ( IOException ignored ) {}
}
return image_location;
}
private static String sanitizeData( String str ) {
str = str.toLowerCase();
for ( String replacement : match )
str = str.replace( replacement, "" );
str = str.replace(" ", "_");
return str;
}
}