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