/[projects]/android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java
ViewVC logotype

Diff of /android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 951 by torben, Mon Jul 5 08:27:01 2010 UTC revision 1014 by torben, Thu Aug 12 09:25:52 2010 UTC
# Line 8  import java.util.concurrent.TimeUnit; Line 8  import java.util.concurrent.TimeUnit;
8  import java.util.logging.Logger;  import java.util.logging.Logger;
9    
10  import javax.servlet.ServletException;  import javax.servlet.ServletException;
11    import javax.servlet.annotation.WebServlet;
12  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServlet;
13  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;  import javax.servlet.http.HttpServletResponse;
# Line 19  import com.gc.android.market.api.model.M Line 20  import com.gc.android.market.api.model.M
20  import com.gc.android.market.api.model.Market.CommentsRequest;  import com.gc.android.market.api.model.Market.CommentsRequest;
21    
22    
23    @WebServlet(urlPatterns={"/ShowStats"})
24  public class ShowStats extends HttpServlet {  public class ShowStats extends HttpServlet {
25          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
26    
# Line 57  public class ShowStats extends HttpServl Line 58  public class ShowStats extends HttpServl
58                  memcache = null;                  memcache = null;
59          }          }
60    
61          protected AppBean doLookup(String query) throws IOException {          protected AppBean lookupApp(String query) throws IOException {
62                                    
63    
64                  String key = "marketstats:" + query.replace(' ', '_');                  String key = "marketstats:" + query.replace(' ', '_');
# Line 65  public class ShowStats extends HttpServl Line 66  public class ShowStats extends HttpServl
66                  AppBean response = (AppBean) memcache.get(key);                  AppBean response = (AppBean) memcache.get(key);
67    
68                  if (response == null) {                  if (response == null) {
69                          response = doLookupWorker(query);                          response = lookupAppWorker(query);
70                          if (response != null) {                          if (response != null) {
71                                  memcache.set(key, APP_TIMEOUT, response);                                  memcache.set(key, APP_TIMEOUT, response);
72                          }                          }
73                  }                  }
74                  return response;                  return response;
75          }          }
76    
77            protected AppBean lookupAppWorker(String query) {
78    
79                    MarketSession session = new MarketSession();
80                    session.login(login,password);
81    
82    
83                    AppsRequest appsRequest = AppsRequest.newBuilder()
84                    .setQuery(query)
85                    .setStartIndex(0)
86                    .setEntriesCount(1)
87                    .setWithExtendedInfo(true)
88                    .build();
89                    
90                    AppsCallback appsCb = new AppsCallback();
91                    session.append(appsRequest, appsCb);
92                    session.flush();
93                    
94                    return appsCb.getResult().get(0);
95            }      
96                    
97          CommentsRequest buildCommentRequest(String appId, int start, int count) {          CommentsRequest buildCommentRequest(String appId, int start, int count) {
98                  CommentsRequest commentsRequest = CommentsRequest.newBuilder()                  CommentsRequest commentsRequest = CommentsRequest.newBuilder()
# Line 103  public class ShowStats extends HttpServl Line 124  public class ShowStats extends HttpServl
124                                  count = Math.min(10, commentsCb.getEntryCount() );                                  count = Math.min(10, commentsCb.getEntryCount() );
125                                                    
126                          //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );                          //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
127                          CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);                                                for (int i=0; i<5;i++) {
128                          session.append(commentsRequest, commentsCb);                                  CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);                      
129                          start +=10;                                  session.append(commentsRequest, commentsCb);
130                                    start +=10;
131                            }
132                                                    
                         CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count);                        
                         session.append(commentsRequest2, commentsCb);  
133                          session.flush();                          session.flush();
134                                                                            
                         start +=10;  
                           
135                          if (start >= MAXCOMMENTS)                          if (start >= MAXCOMMENTS)
136                                  break; //emergency brake                                  break; //emergency brake
137                                                    
# Line 122  public class ShowStats extends HttpServl Line 141  public class ShowStats extends HttpServl
141          }          }
142    
143                    
   
         protected AppBean doLookupWorker(String query) {  
                 final StringBuilder sb = new StringBuilder();  
   
   
                 MarketSession session = new MarketSession();  
                 session.login(login,password);  
   
   
                 AppsRequest appsRequest = AppsRequest.newBuilder()  
                 .setQuery(query)  
                 .setStartIndex(0)  
                 .setEntriesCount(1)  
                 .setWithExtendedInfo(true)  
                 .build();  
                   
                 AppsCallback appsCb = new AppsCallback();  
                 session.append(appsRequest, appsCb);  
                 session.flush();  
                   
                 return appsCb.getResult();  
         }  
144                    
145            @SuppressWarnings("unchecked")
146          public ArrayList<CommentBean> getComments(AppBean app) {                          public ArrayList<CommentBean> getComments(AppBean app) {                
147                  String key = "marketstats:comments:" + app.getId();                  String key = "marketstats:comments:" + app.getId();
148                  ArrayList<CommentBean>  comments =  (ArrayList<CommentBean>) memcache.get(key);                  ArrayList<CommentBean>  comments =  (ArrayList<CommentBean>) memcache.get(key);
# Line 157  public class ShowStats extends HttpServl Line 155  public class ShowStats extends HttpServl
155          }          }
156    
157          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
158                  String query = request.getParameter("query");                  String query = "pname:" + request.getParameter("app");
159                                    
160                  AppBean app = doLookup(query);                  AppBean app = lookupApp(query);
161                  if (app != null) {                  if (app != null) {
162                          request.setAttribute("app", app);                          request.setAttribute("app", app);
163                                    

Legend:
Removed from v.951  
changed lines
  Added in v.1014

  ViewVC Help
Powered by ViewVC 1.1.20