create changelog entry
[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         public static SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {\r
22                 \r
23                 String requestString = request.toString();\r
24                 \r
25                 AndroidLogWrapper.d(ThrustCurveAPI.class, "doSearch: " + requestString);\r
26                 // Froyo has troubles resolving URLS constructed with protocols.  Because of this\r
27                 // we need to do it in parts.\r
28                 URL url = new URL("http", "www.thrustcurve.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                 // Froyo has troubles resolving URLS constructed with protocols.  Because of this\r
62                 // we need to do it in parts.\r
63                 URL url = new URL("http", "www.thrustcurve.org", "/servlets/download");\r
64 \r
65                 OutputStream  stream;\r
66 \r
67                 URLConnection conn = url.openConnection();\r
68                 conn.setDoInput(true);\r
69                 conn.setDoOutput(true);\r
70                 conn.setUseCaches(false);\r
71 \r
72                 stream = conn.getOutputStream();\r
73 \r
74                 stream.write(requestString.getBytes());\r
75 \r
76                 InputStream is = conn.getInputStream();\r
77 \r
78                 DownloadResponse downloadResponse = DownloadResponseParser.parse(is);\r
79                 AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());\r
80 \r
81                 return downloadResponse.getData(motor_id);\r
82 \r
83     }\r
84         \r
85         /**\r
86          * look through the listOfMotors to find the one which best matches the motor requested.\r
87          * \r
88          * The algorithm uses a score based method.  Each entry in listOfMotors is assigned a score\r
89          * and the element with the highest score is returned.  The score is computed as follows:\r
90          * \r
91          * 1) if the element matches the digest of the requested motor eactly, score += 1000\r
92          * 1) if the element matches the designation in the requested motor exactly, score = 100\r
93          * 2) if the element is a RockSim file score += 10\r
94          * \r
95          * @param motor\r
96          * @param listOfMotors\r
97          * @return\r
98          */\r
99         public static ThrustCurveMotor findBestMatch( ThrustCurveMotorPlaceholder motor, List<MotorBurnFile> listOfMotors ) {\r
100                 \r
101                 ThrustCurveMotor bestMatch = null;\r
102                 int bestScore = -1;\r
103                 \r
104                 final String wantedDesignation = motor.getDesignation();\r
105                 final String wantedDigest = motor.getDigest();\r
106                 \r
107                 for ( MotorBurnFile entry : listOfMotors ) {\r
108                         int entryScore = 0;\r
109                         ThrustCurveMotor entryMotor = entry.getThrustCurveMotor();\r
110                         \r
111                         if ("RockSim".equals(entry.getFiletype()) ) {\r
112                                 entryScore += 10;\r
113                         }\r
114 \r
115                         if ( wantedDigest != null && wantedDigest.equals( entryMotor.getDigest() ) ) {\r
116                                 entryScore += 1000;\r
117                         }\r
118                         \r
119                         if ( wantedDesignation != null && wantedDesignation.equals(entryMotor.getDesignation())) {\r
120                                 entryScore += 100;\r
121                         }\r
122                         \r
123                         if ( entryScore > bestScore ) {\r
124                                 bestScore = entryScore;\r
125                                 bestMatch = entry.getThrustCurveMotor();\r
126                         }\r
127                         \r
128                 }\r
129                 \r
130                 return bestMatch;\r
131         }\r
132         \r
133         /**\r
134          * Extract all unique motors from the list based on designation.\r
135          * @param listOfMotors\r
136          * @return\r
137          */\r
138         public static List<ThrustCurveMotor> extractAllMotors( List<MotorBurnFile> listOfMotors ) {\r
139                 \r
140                 Map<String, ThrustCurveMotor> motorsByDesignation = new HashMap<String,ThrustCurveMotor>();\r
141                 \r
142                 for( MotorBurnFile entry : listOfMotors ) {\r
143                         ThrustCurveMotor motor = entry.getThrustCurveMotor();\r
144                         if ( motor != null ) {\r
145                                 motorsByDesignation.put( motor.getDesignation(), motor);\r
146                         }\r
147                 }\r
148                 \r
149                 return new ArrayList<ThrustCurveMotor>(motorsByDesignation.values());\r
150                 \r
151         }\r
152 }\r