avatar

956

Better integration between JBidwatcher and My JBidwatcher.

by mrs, 17 Jun, 2009 01:25 AM
921 956  
613613     MQFactory.getConcrete("Swing").enqueue("Done Getting Selling Items for " + userId);
614614   }
615615 
616   //  TODO -- Wouldn't it be nice if this took an item number, and looked it up, instead of keeping a reference to the actual (potentially duplicated) object?
616617   private class SnipeListener implements MessageQueue.Listener {
617618     public void messageAction(Object deQ) {
618619       AuctionQObject ac = (AuctionQObject) deQ;
955 956  
118118   }
119119 
120120   public String recognizeBidpage(String identifier, StringBuffer page) {
121     Parameters p = new Parameters();
122     if(identifier != null) p.put("item", identifier);
123     p.put("body", page);
124     String url = url("my.jbidwatcher.com/services/recognize");
125     return http().postTo(url, p);
121     if(canParse()) {
122       Parameters p = new Parameters();
123       if (identifier != null) p.put("item", identifier);
124       p.put("body", page);
125       String url = url("my.jbidwatcher.com/services/recognize");
126       return http().postTo(url, p);
127     } else {
128       return null;
129     }
126130   }
127131 
128132   public String reportException(String sb) {
------
138142   }
139143 
140144   public void postXML(String queue, XMLSerialize ae) {
141     XMLElement xmlWrapper = new XMLElement("message");
142     XMLElement user = new XMLElement("user");
143     XMLElement access_key = new XMLElement("key");
144     user.setContents(JConfig.queryConfiguration("my.jbidwatcher.id"));
145     access_key.setContents(JConfig.queryConfiguration("my.jbidwatcher.key"));
146     xmlWrapper.addChild(user);
147     xmlWrapper.addChild(access_key);
148     xmlWrapper.addChild(ae.toXML());
149     String aucXML = xmlWrapper.toString();
145     if(canSync()) {
146       XMLElement xmlWrapper = new XMLElement("message");
147       XMLElement user = new XMLElement("user");
148       XMLElement access_key = new XMLElement("key");
149       user.setContents(JConfig.queryConfiguration("my.jbidwatcher.id"));
150       access_key.setContents(JConfig.queryConfiguration("my.jbidwatcher.key"));
151       xmlWrapper.addChild(user);
152       xmlWrapper.addChild(access_key);
153       xmlWrapper.addChild(ae.toXML());
154       String aucXML = xmlWrapper.toString();
150155 
151     if(queue != null) http().putTo(queue, aucXML);
156       if (queue != null) http().putTo(queue, aucXML);
157     }
152158   }
153159 
154160   private MyJBidwatcher() {
------
169175         if(JConfig.queryConfiguration("my.jbidwatcher.id") != null) {
170176           AuctionEntry ae = EntryCorral.getInstance().takeForRead((String) deQ);
171177           postXML(mSyncQueueURL, ae);
172           if(JConfig.queryConfiguration("my.jbidwatcher.uploadhtml", "false").equals("true")) {
173             uploadAuctionHTML(ae, "uploadhtml");
174           }
178           uploadAuctionHTML(ae, "uploadhtml");
175179         }
176180       }
177181     });
------
200204   }
201205 
202206   private void uploadAuctionHTML(AuctionEntry ae, String uploadType) {
203     String s3Result = sendFile(ae.getContentFile(), url(ITEM_UPLOAD_URL), JConfig.queryConfiguration("my.jbidwatcher.id"), ae.getLastStatus());
204     XMLElement root = new XMLElement(uploadType);
205     XMLElement s3Key = new XMLElement("s3");
206     s3Key.setContents(s3Result);
207     root.addChild(ae.toXML());
208     postXML(mReportQueueURL, root);
207     if(canUploadHTML()) {
208       String s3Result = sendFile(ae.getContentFile(), url(ITEM_UPLOAD_URL), JConfig.queryConfiguration("my.jbidwatcher.id"), ae.getLastStatus());
209       XMLElement root = new XMLElement(uploadType);
210       XMLElement s3Key = new XMLElement("s3");
211       s3Key.setContents(s3Result);
212       root.addChild(ae.toXML());
213       postXML(mReportQueueURL, root);
214     }
209215   }
210216 
217   private boolean canUploadHTML() { return allow("uploadhtml"); }
218   private boolean canSync() { return allow("sync"); }
219   private boolean canParse() { return allow("parser"); }
220   private boolean canGetSnipes() { return allow("snipes"); }
221   private boolean canSendSnipeToGixen() { return allow("gixen"); }
222 
223   private boolean allow(String type) {
224     return JConfig.queryConfiguration("my.jbidwatcher.allow." + type, "false").equals("true") &&
225            JConfig.queryConfiguration("my.jbidwatcher." + type, "false").equals("true");
226   }
227 
211228   public boolean getAccountInfo() {
212229     StringBuffer sb = http().get("https://my.jbidwatcher.com/services/account");
213230     if(sb == null) return false;
------
234251       }
235252     }
236253 
254     JConfig.setConfiguration("my.jbidwatcher.allow.listings", listingsRemaining.getContents());
255     JConfig.setConfiguration("my.jbidwatcher.allow.categories", categoriesRemaining.getContents());
256 
237257     mSyncQueueURL = sync == null ? null : sync.getContents();
258     JConfig.setConfiguration("my.jbidwatcher.allow.sync", Boolean.toString(mSyncQueueURL != null));
238259     mReportQueueURL = reporting == null ? null : reporting.getContents();
239260     mUseSSL = getBoolean(ssl);
261     JConfig.setConfiguration("my.jbidwatcher.allow.ssl", Boolean.toString(mUseSSL));
240262     mReadSnipesFromServer = getBoolean(snipesListen);
263     JConfig.setConfiguration("my.jbidwatcher.allow.snipes", Boolean.toString(mReadSnipesFromServer));
241264     mUploadHTML = getBoolean(uploadHTML);
265     JConfig.setConfiguration("my.jbidwatcher.allow.uploadhtml", Boolean.toString(mUploadHTML));
242266     mUseServerParser = getBoolean(serverParser);
267     JConfig.setConfiguration("my.jbidwatcher.allow.parser", Boolean.toString(mUseServerParser));
243268     mGixen = getBoolean(gixen);
269     JConfig.setConfiguration("my.jbidwatcher.allow.gixen", Boolean.toString(mGixen));
244270 
245271     return mSyncQueueURL != null && mReportQueueURL != null;
246272   }
862 956  
139139     }
140140     allTabs.add(new JConfigFirewallTab());
141141     allTabs.add(new JConfigSnipeTab());
142     if(JConfig.queryConfiguration("allow.my_jbidwatcher", "false").equals("true")) allTabs.add(new JConfigMyJBidwatcherTab());
142 //    if(JConfig.queryConfiguration("allow.my_jbidwatcher", "false").equals("true"))
143       allTabs.add(new JConfigMyJBidwatcherTab());
143144     allTabs.add(new JConfigFilePathTab());
144145     allTabs.add(new JConfigWebserverTab());
145146     allTabs.add(new JConfigDatabaseTab());
942 956  
1313 import java.awt.*;
1414 import java.awt.event.ActionListener;
1515 import java.awt.event.ActionEvent;
16 import java.util.Map;
17 import java.util.HashMap;
1618 import javax.swing.*;
1719 
1820 public class JConfigMyJBidwatcherTab extends JConfigTab {
------
2022   private JTextField mEmail;
2123   private JTextField mPassword;
2224   private JButton mCreateOrUpdate;
25   private Map<JCheckBox,String> mConfigurationMap = new HashMap<JCheckBox,String>();
26   private Map<JCheckBox,String> mEnabledMap = new HashMap<JCheckBox,String>();
27   private JLabel mListingStats;
28   private JLabel mCategoryStats;
29   private JLabel mSSLEnabled;
2330 
2431   public String getTabName() { return "My JBidwatcher"; }
2532   public void cancel() { }
2633 
2734   public boolean apply() {
28     boolean enabled = mEnable.isSelected();
29     JConfig.setConfiguration("my.jbidwatcher.enabled", Boolean.toString(enabled));
30 
3135     String email = mEmail.getText();
3236     String password = mPassword.getText();
3337 
------
3842       }
3943     }
4044 
45     for(JCheckBox cb : mConfigurationMap.keySet()) {
46       String cfg = mConfigurationMap.get(cb);
47       JConfig.setConfiguration(cfg, Boolean.toString(cb.isSelected()));
48     }
4149     return true;
4250   }
4351 
4452   public void updateValues() {
4553     String email = JConfig.queryConfiguration("my.jbidwatcher.id", "");
4654     String pass = JConfig.queryConfiguration("my.jbidwatcher.key", "");
47     boolean enabled = JConfig.queryConfiguration("my.jbidwatcher.enabled", "false").equals("true");
48 //    String id = JConfig.queryConfiguration("my.jbidwatcher.id");
4955 
56     mEnable.setSelected(JConfig.queryConfiguration("my.jbidwatcher.enabled", "false").equals("true"));
57 
58     for (JCheckBox cb : mEnabledMap.keySet()) {
59       String cfg = mEnabledMap.get(cb);
60       cb.setEnabled(mEnable.isSelected() && JConfig.queryConfiguration(cfg, "false").equals("true"));
61     }
62 
5063     mEmail.setText(email);
5164     mPassword.setText(pass);
52     mEnable.setSelected(enabled);
5365 
66     mListingStats.setText(left("listings"));
67     mCategoryStats.setText(left("categories"));
68     mSSLEnabled.setText(sslState());
69 
5470     for(ActionListener al : mEnable.getActionListeners()) {
5571       al.actionPerformed(new ActionEvent(mEnable, ActionEvent.ACTION_PERFORMED, "Redraw"));
5672     }
------
104120         if(MyJBidwatcher.getInstance().getAccountInfo()) {
105121           statusLabel.setIcon(successIcon);
106122           statusLabel.setText("success!");
123           updateValues();
107124         } else {
108125           statusLabel.setIcon(failIcon);
109126           statusLabel.setText("failed.");
------
134151         mPassword.setEditable(selected);
135152 
136153         mCreateOrUpdate.setEnabled(selected);
154         for(JCheckBox cb : mEnabledMap.keySet()) {
155           cb.setEnabled(selected && JConfig.queryConfiguration(mEnabledMap.get(cb), "false").equals("true"));
156         }
137157       }
138158     });
139159     JLabel enableLabel = new JLabel("Enable My JBidwatcher");
140160     enableLabel.setLabelFor(mEnable);
161     mConfigurationMap.put(mEnable, "my.jbidwatcher.enabled");
141162 
142163     jp.add(makeLine(mEnable, enableLabel), BorderLayout.NORTH);
143164     jp.add(innerPanel, BorderLayout.CENTER);
------
151172     JPanel jp = new JPanel();
152173     jp.setLayout(new BorderLayout());
153174     jp.add(buildUserSettings(), BorderLayout.NORTH);
154 //    jp.add(buildExtraSettings(), BorderLayout.SOUTH);
175     jp.add(buildExtraSettings(), BorderLayout.SOUTH);
155176     this.add(panelPack(jp), BorderLayout.NORTH);
156177     updateValues();
157178   }
179 
180   private JPanel buildExtraSettings() {
181     JPanel jp = new JPanel(new BorderLayout());
182     jp.setBorder(BorderFactory.createTitledBorder("My JBidwatcher Settings"));
183 
184     JPanel innerPanel = new JPanel();
185     innerPanel.setLayout(new SpringLayout());
186 
187     innerPanel.add(createSettingsCheckbox("Upload item info to My JBidwatcher", "sync"));
188     innerPanel.add(createSettingsCheckbox("Upload item HTML", "uploadhtml"));
189 //    innerPanel.add(createSettingsCheckbox("Upload snipes to Gixen", "gixen"));
190     innerPanel.add(createSettingsCheckbox("Allow setting snipes in My JBidwatcher", "snipes"));
191     innerPanel.add(createSettingsCheckbox("Use My JBidwatcher as a fallback parser", "parser"));
192 
193     mListingStats = new JLabel(left("listings"));
194     mListingStats.setFont(mListingStats.getFont().deriveFont(Font.BOLD));
195     mCategoryStats = new JLabel(left("categories"));
196     mCategoryStats.setFont(mCategoryStats.getFont().deriveFont(Font.BOLD));
197 
198     innerPanel.add(mListingStats);
199     innerPanel.add(mCategoryStats);
200     innerPanel.add(new JLabel(""));
201     mSSLEnabled = new JLabel();
202     mSSLEnabled.setText(sslState());
203     mSSLEnabled.setFont(mSSLEnabled.getFont().deriveFont(Font.BOLD | Font.ITALIC));
204     JPanel tmp = new JPanel(new BorderLayout());
205     tmp.add(mSSLEnabled, BorderLayout.EAST);
206     innerPanel.add(tmp);
207     SpringUtilities.makeCompactGrid(innerPanel, 4, 2, 6, 6, 6, 1);
208 
209     jp.add(innerPanel, BorderLayout.CENTER);
210     return jp;
211   }
212 
213   private String sslState() {return JConfig.queryConfiguration("my.jbidwatcher.allow.ssl", "false").equals("true") ? "SSL Enabled" : "SSL Disabled";}
214 
215   private JCheckBox createSettingsCheckbox(String text, String identifier) {
216     String cfgAllowed = "my.jbidwatcher.allow." + identifier;
217     String cfgSetting = "my.jbidwatcher." + identifier;
218     final JCheckBox cb = new JCheckBox(text);
219     cb.setEnabled(JConfig.queryConfiguration(cfgAllowed, "false").equals("true"));
220     mConfigurationMap.put(cb, cfgSetting);
221     mEnabledMap.put(cb, cfgAllowed);
222     return cb;
223   }
224 
225   private String left(String type) {
226     String listings = JConfig.queryConfiguration("my.jbidwatcher.allow." + type, "unknown");
227     if(listings.equals("-1")) listings = "unlimited";
228     return  listings + " " + type + " remaining";
229   }
158230 }