create changelog entry
[debian/openrocket] / android / src / net / sf / openrocket / android / thrustcurve / TCMissingMotorDownloadAction.java
1 package net.sf.openrocket.android.thrustcurve;\r
2 \r
3 import java.util.List;\r
4 import java.util.Set;\r
5 import java.util.regex.Matcher;\r
6 import java.util.regex.Pattern;\r
7 \r
8 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
9 import net.sf.openrocket.motor.ThrustCurveMotor;\r
10 import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;\r
11 \r
12 public class TCMissingMotorDownloadAction extends TCQueryAction {\r
13 \r
14         private final static String DESIGNATION_REGEX_STRING = "(Micro Maxx|Micro Maxx II|1/4A|1/2A|[A-O][0-9]*)";\r
15         \r
16         public static TCMissingMotorDownloadAction newInstance( Set<ThrustCurveMotorPlaceholder> missingMotors ) {\r
17                 TCMissingMotorDownloadAction frag = new TCMissingMotorDownloadAction();\r
18                 frag.task = frag.new Downloader(missingMotors);\r
19                 return frag;\r
20         }\r
21 \r
22         private class Downloader extends TCQueryAction.TCQueryTask {\r
23 \r
24                 private Set<ThrustCurveMotorPlaceholder> missingMotors;\r
25                 private Pattern designation_pattern = null;\r
26                 \r
27                 private Downloader( Set<ThrustCurveMotorPlaceholder> missingMotors ) {\r
28                         this.missingMotors = missingMotors;\r
29                         try {\r
30                                 designation_pattern = Pattern.compile(DESIGNATION_REGEX_STRING);\r
31                         } catch ( Exception ex ) {\r
32                                 AndroidLogWrapper.e(TCMissingMotorDownloadAction.class, "Exception in pattern compile {}", ex);\r
33                         }\r
34                 }\r
35                 \r
36                 private void downloadMissingMotor( ThrustCurveMotorPlaceholder motor ) {\r
37                         try {\r
38                                         \r
39                                 SearchRequest request = new SearchRequest();\r
40                                 request.setManufacturer(motor.getManufacturer());\r
41                                 String designation = motor.getDesignation();\r
42                                 if ( designation_pattern != null ) {\r
43                                         Matcher m = designation_pattern.matcher(designation);\r
44                                         if ( m.find() ) {\r
45                                                 designation = m.group();\r
46                                         }\r
47                                 }\r
48                                 AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "using designation {}", designation);\r
49                                 request.setCommon_name(designation);\r
50 \r
51                                 handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));\r
52 \r
53                                 SearchResponse res = ThrustCurveAPI.doSearch(request);\r
54 \r
55                                 int total = res.getResults().size();\r
56                                 int count = 1;\r
57                                 for( TCMotor mi : res.getResults() ) {\r
58                                         StringBuilder message = new StringBuilder();\r
59                                         message.append("Downloading details ");\r
60                                         if ( total > 1 ) {\r
61                                                 message.append(count);\r
62                                                 message.append(" of " );\r
63                                                 message.append(total);\r
64                                                 message.append("\n");\r
65                                         }\r
66                                         message.append(mi.getManufacturer());\r
67                                         message.append(" ");\r
68                                         message.append(mi.getCommon_name());\r
69                                         handler.post(new UpdateMessage(message.toString()));\r
70                                         count++;\r
71                                         if ( mi.getData_files() == null || mi.getData_files().intValue() == 0 ) {\r
72                                                 continue;\r
73                                         }\r
74 \r
75                                         AndroidLogWrapper.d(TCQueryAction.class, mi.toString());\r
76 \r
77                                         List<MotorBurnFile> listOfMotors = ThrustCurveAPI.downloadData(mi.getMotor_id());\r
78 \r
79                                         ThrustCurveMotor bestMatch = ThrustCurveAPI.findBestMatch(motor, listOfMotors);\r
80                                         writeMotor( mi, bestMatch);\r
81 \r
82                                 }\r
83                         }\r
84                         catch( Exception ex){\r
85                                 AndroidLogWrapper.d(TCQueryAction.class,ex.toString());\r
86                                 handler.post( new UpdateMessage("Failed") );\r
87 \r
88                         }\r
89 \r
90                 }\r
91 \r
92                 @Override\r
93                 protected String doInBackground(Void... arg0) {\r
94                         \r
95                         for ( ThrustCurveMotorPlaceholder motor : missingMotors ) {\r
96                                 AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "Motor: {}", motor);\r
97                                 downloadMissingMotor(motor);\r
98                         }\r
99                         return null;\r
100                 }\r
101 \r
102         }\r
103 \r
104 }\r