Merge branches/froyo_12.03 to head.
[debian/openrocket] / android / src / net / sf / openrocket / android / thrustcurve / ThrustCurveAPI.java
1 package net.sf.openrocket.android.thrustcurve;\r
2 \r
3 import java.io.IOException;\r
4 import java.io.InputStream;\r
5 import java.io.OutputStream;\r
6 import java.net.MalformedURLException;\r
7 import java.net.URL;\r
8 import java.net.URLConnection;\r
9 import java.util.ArrayList;\r
10 import java.util.HashMap;\r
11 import java.util.List;\r
12 import java.util.Map;\r
13 \r
14 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
15 import net.sf.openrocket.motor.ThrustCurveMotor;\r
16 import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;\r
17 \r
18 \r
19 public abstract class ThrustCurveAPI {\r
20 \r
21         //private static String url_base = "http://www.thrustcurve.org/servlets/";\r
22         \r
23         public static SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {\r
24                 \r
25                 String requestString = request.toString();\r
26                 \r
27                 AndroidLogWrapper.d(ThrustCurveAPI.class, "doSearch: " + requestString);\r
28                 URL url = new URL("http", "www.thurustcurve.org", "servlets/search");\r
29 \r
30         OutputStream  stream;\r
31 \r
32         URLConnection conn = url.openConnection();\r
33         conn.setConnectTimeout(2000);\r
34         conn.setDoInput(true);\r
35         conn.setDoOutput(true);\r
36         conn.setUseCaches(false);\r
37 \r
38         stream = conn.getOutputStream();\r
39 \r
40         stream.write(requestString.getBytes());\r
41         \r
42         InputStream is = conn.getInputStream();\r
43 \r
44         SearchResponse result = SearchResponseParser.parse(is);\r
45         AndroidLogWrapper.d(ThrustCurveAPI.class,result.toString());\r
46         \r
47         return result;\r
48         }\r
49 \r
50         public static List<MotorBurnFile> downloadData( Integer motor_id ) throws MalformedURLException, IOException {\r
51 \r
52                 if ( motor_id == null ) {\r
53                         return null;\r
54                 }\r
55                 DownloadRequest dr = new DownloadRequest();\r
56                 dr.add(motor_id);\r
57 \r
58                 String requestString = dr.toString();\r
59 \r
60                 AndroidLogWrapper.d(ThrustCurveAPI.class, "downloadData: " + requestString);\r
61                 URL url = new URL("http", "www.thurustcurve.org", "servlets/download");\r
62 \r
63                 OutputStream  stream;\r
64 \r
65                 URLConnection conn = url.openConnection();\r
66                 conn.setDoInput(true);\r
67                 conn.setDoOutput(true);\r
68                 conn.setUseCaches(false);\r
69 \r
70                 stream = conn.getOutputStream();\r
71 \r
72                 stream.write(requestString.getBytes());\r
73 \r
74                 InputStream is = conn.getInputStream();\r
75 \r
76                 DownloadResponse downloadResponse = DownloadResponseParser.parse(is);\r
77                 AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());\r
78 \r
79                 return downloadResponse.getData(motor_id);\r
80 \r
81     }\r
82         \r
83         /**\r
84          * look through the listOfMotors to find the one which best matches the motor requested.\r
85          * \r
86          * The algorithm uses a score based method.  Each entry in listOfMotors is assigned a score\r
87          * and the element with the highest score is returned.  The score is computed as follows:\r
88          * \r
89          * 1) if the element matches the digest of the requested motor eactly, score += 1000\r
90          * 1) if the element matches the designation in the requested motor exactly, score = 100\r
91          * 2) if the element is a RockSim file score += 10\r
92          * \r
93          * @param motor\r
94          * @param listOfMotors\r
95          * @return\r
96          */\r
97         public static ThrustCurveMotor findBestMatch( ThrustCurveMotorPlaceholder motor, List<MotorBurnFile> listOfMotors ) {\r
98                 \r
99                 ThrustCurveMotor bestMatch = null;\r
100                 int bestScore = -1;\r
101                 \r
102                 final String wantedDesignation = motor.getDesignation();\r
103                 final String wantedDigest = motor.getDigest();\r
104                 \r
105                 for ( MotorBurnFile entry : listOfMotors ) {\r
106                         int entryScore = 0;\r
107                         ThrustCurveMotor entryMotor = entry.getThrustCurveMotor();\r
108                         \r
109                         if ("RockSim".equals(entry.getFiletype()) ) {\r
110                                 entryScore += 10;\r
111                         }\r
112 \r
113                         if ( wantedDigest != null && wantedDigest.equals( entryMotor.getDigest() ) ) {\r
114                                 entryScore += 1000;\r
115                         }\r
116                         \r
117                         if ( wantedDesignation != null && wantedDesignation.equals(entryMotor.getDesignation())) {\r
118                                 entryScore += 100;\r
119                         }\r
120                         \r
121                         if ( entryScore > bestScore ) {\r
122                                 bestScore = entryScore;\r
123                                 bestMatch = entry.getThrustCurveMotor();\r
124                         }\r
125                         \r
126                 }\r
127                 \r
128                 return bestMatch;\r
129         }\r
130         \r
131         /**\r
132          * Extract all unique motors from the list based on designation.\r
133          * @param listOfMotors\r
134          * @return\r
135          */\r
136         public static List<ThrustCurveMotor> extractAllMotors( List<MotorBurnFile> listOfMotors ) {\r
137                 \r
138                 Map<String, ThrustCurveMotor> motorsByDesignation = new HashMap<String,ThrustCurveMotor>();\r
139                 \r
140                 for( MotorBurnFile entry : listOfMotors ) {\r
141                         ThrustCurveMotor motor = entry.getThrustCurveMotor();\r
142                         if ( motor != null ) {\r
143                                 motorsByDesignation.put( motor.getDesignation(), motor);\r
144                         }\r
145                 }\r
146                 \r
147                 return new ArrayList<ThrustCurveMotor>(motorsByDesignation.values());\r
148                 \r
149         }\r
150 }\r