avatar

206

More integration of the database into the objects. So far we have AuctionEntry, AuctionInfo, and Category as DB-backed classes, and Seller as a partially DB-backed object. (It needs to be refactored to be fully HashBacked.)

There are evidently still problems with updates nuking info. :(

by mrs, 08 Oct, 2007 10:43 AM
205 206  
66  */
77 
88 import com.jbidwatcher.Constants;
9 import com.jbidwatcher.search.Category;
910 import com.jbidwatcher.auction.server.AuctionServer;
1011 import com.jbidwatcher.auction.server.AuctionServerManager;
1112 import com.jbidwatcher.config.JConfig;
1213 import com.jbidwatcher.queue.AuctionQObject;
1314 import com.jbidwatcher.queue.MQFactory;
1415 import com.jbidwatcher.util.*;
16 import com.jbidwatcher.util.db.DBRecord;
17 import com.jbidwatcher.util.db.AuctionDB;
1518 import com.jbidwatcher.xml.XMLElement;
1619 
1720 import java.io.FileNotFoundException;
------
4447   public static final int SUCC_HIGHBID=0, SUCC_OUTBID=1, FAIL_ENDED=2;
4548   public static final int FAIL_CONNECT=3, FAIL_PARSE=4, FAIL_BADMULTI=5;
4649 
50   private static AuctionDB sDB;
51   
4752   /**
4853    * @brief Set a status message, and mark that the connection is currently invalid.
4954    */
------
528533    * not set, return a standard 30 seconds.
529534    */
530535   private static long getGlobalSnipeTime() {
531     String strConfigSnipeAt;
532536     long snipeTime;
533537 
534     strConfigSnipeAt = JConfig.queryConfiguration("snipemilliseconds");
538     String strConfigSnipeAt = JConfig.queryConfiguration("snipemilliseconds");
535539     if(strConfigSnipeAt != null) {
536540       snipeTime = Long.parseLong(strConfigSnipeAt);
537541     } else {
------
554558    */
555559   private void checkConfigurationSnipeTime() {
556560     mDefaultSnipeAt = getGlobalSnipeTime();
561     if(sDB == null) sDB = setTable("entries");
562     setDB(sDB);
557563   }
558564 
559565   /**
------
668674         }
669675       } else {
670676         if(!isDutch()) {
671           String localUserId;
672 
673           localUserId = mServer.getUserId().trim();
677           String localUserId = mServer.getUserId().trim();
674678           mHighBidder = localUserId.equalsIgnoreCase(getHighBidder());
675679         }
676680       }
------
781785    * we effectively delete the comment.
782786    */
783787   public void setComment(String newComment) {
784     if(newComment.trim().equals(""))
788     if(newComment.trim().length() == 0)
785789       mComment = null;
786790     else
787791       mComment = newComment;
------
921925    */
922926   public XMLElement toXML() {
923927     XMLElement xmlResult = new XMLElement("auction");
924     XMLElement xbid, xsnipe, xcomplete, xinvalid, xcomment, xlog, xmulti, xshipping, xcategory;
925928 
926929     xmlResult.setProperty("id", getIdentifier());
927930     xmlResult.addChild(mAuction.toXML());
928931 
929932     if(isBidOn()) {
930       xbid = new XMLElement("bid");
933       XMLElement xbid = new XMLElement("bid");
931934       xbid.setEmpty();
932935       xbid.setProperty("quantity", Integer.toString(mBidQuantity));
933936       xbid.setProperty("currency", mBid.fullCurrencyName());
------
939942     }
940943 
941944     if(isSniped()) {
942       xsnipe = new XMLElement("snipe");
945       XMLElement xsnipe = new XMLElement("snipe");
943946       xsnipe.setEmpty();
944947       xsnipe.setProperty("quantity", Integer.toString(mSnipeQuantity));
945948       xsnipe.setProperty("currency", getSnipe().fullCurrencyName());
------
951954     if(isMultiSniped()) {
952955       MultiSnipe outMS = getMultiSnipe();
953956 
954       xmulti = new XMLElement("multisnipe");
957       XMLElement xmulti = new XMLElement("multisnipe");
955958       xmulti.setEmpty();
956959       xmulti.setProperty("subtractshipping", Boolean.toString(outMS.subtractShipping()));
957960       xmulti.setProperty("color", outMS.getColorString());
------
961964     }
962965 
963966     if(isComplete()) {
964       xcomplete = new XMLElement("complete");
967       XMLElement xcomplete = new XMLElement("complete");
965968       xcomplete.setEmpty();
966969       xmlResult.addChild(xcomplete);
967970     }
968971 
969972     if(isInvalid()) {
970       xinvalid = new XMLElement("invalid");
973       XMLElement xinvalid = new XMLElement("invalid");
971974       xinvalid.setEmpty();
972975       xmlResult.addChild(xinvalid);
973976     }
974977 
975978     if(getComment() != null) {
976       xcomment = new XMLElement("comment");
979       XMLElement xcomment = new XMLElement("comment");
977980       xcomment.setContents(getComment());
978981       xmlResult.addChild(xcomment);
979982     }
980983 
981984     if(getCategory() != null) {
982       xcategory = new XMLElement("category");
985       XMLElement xcategory = new XMLElement("category");
983986       xcategory.setContents(getCategory());
984987       xcategory.setProperty("sticky", isSticky() ?"true":"false");
985988       xmlResult.addChild(xcategory);
986989     }
987990 
988991     if(getShipping() != null) {
989       xshipping = new XMLElement("shipping");
992       XMLElement xshipping = new XMLElement("shipping");
990993       xshipping.setEmpty();
991994       xshipping.setProperty("currency", getShipping().fullCurrencyName());
992995       xshipping.setProperty("price", Double.toString(getShipping().getValue()));
993996       xmlResult.addChild(xshipping);
994997     }
995998 
996     xlog = mEntryEvents.toXML();
999     XMLElement xlog = mEntryEvents.toXML();
9971000     if(xlog != null) {
9981001       xmlResult.addChild(xlog);
9991002     }
------
10741077     boolean shouldSnipe = false;
10751078 
10761079     if(isSniped()) {
1077       long endDate, curDate, adjustedDate;
1080       long endDate = mAuction.getEndDate().getTime();
1081       long curDate = mServer.getAdjustedTime();
10781082 
1079       endDate = mAuction.getEndDate().getTime();
1080       curDate = mServer.getAdjustedTime();
1081 
10821083       //  If the auction hasn't ended already...
10831084       if(endDate > curDate) {
10841085         //  mSnipeAt / 1000 seconds before the end of the auction.
1086         long adjustedDate;
10851087         if(hasDefaultSnipeTime()) {
10861088           adjustedDate = curDate + mDefaultSnipeAt;
10871089         } else {
------
12881290    */
12891291   public void forceUpdate() { mForceUpdate = true; mDontUpdate = 0; mNeedsUpdate = true; }
12901292 
1293   Category mCategory;
1294 
12911295   /**
12921296    * @brief Get the category this belongs in, usually used for tab names, and fitting in search results.
12931297    *
12941298    * @return - A category, or null if none has been assigned.
12951299    */
1296   public String getCategory() { return getString("category"); }
1300   public String getCategory() { return mCategory.getName(); }
12971301 
12981302   /**
12991303    * @brief Set the category associated with the auction entry.  If the
------
13021306    * @param newCategory - The new category to associate this item with.
13031307    */
13041308   public void setCategory(String newCategory) {
1305     setString("category", newCategory);
1309     mCategory = Category.findCategory(newCategory);
1310     setInteger("category_id", mCategory.getId());
13061311     if(isComplete()) setSticky(true);
13071312   }
13081313 
------
16391644   protected void setSnipe(Currency snipe) {
16401645     mSnipe = snipe;
16411646   }
1647 
1648   public DBRecord getMap() {
1649     setInteger("auction_id", mAuction.getId());
1650     return getBacking();
1651   }
16421652 }
203 206  
1616 import com.jbidwatcher.config.JConfig;
1717 import com.jbidwatcher.util.*;
1818 import com.jbidwatcher.util.db.DBRecord;
19 import com.jbidwatcher.util.db.AuctionDB;
1920 import com.jbidwatcher.xml.XMLElement;
2021 
2122 import java.io.File;
------
7071   protected static final sun.misc.BASE64Encoder b64enc = new sun.misc.BASE64Encoder();
7172   protected GZip _loadedPage = null;
7273 
74   private static AuctionDB sDB = null;
7375   /**
7476    * @brief Empty constructor, for XML parsing.
7577    *
7678    */
77   AuctionInfo() {
79   protected AuctionInfo() {
7880     setTranslationTable(mKeys);
81     if(sDB == null) sDB = setTable("auctions");
82     setDB(sDB);
7983   }
8084 
8185   /** 
------
9094    * @param auctionEnd - The end time for the auction.
9195    * @param auctionBidCount - The number of bids that have been placed so far.
9296    */
93   AuctionInfo(String auctionTitle, String auctionSeller, String auctionHighBidder,
97   protected AuctionInfo(String auctionTitle, String auctionSeller, String auctionHighBidder,
9498                      Currency auctionCurBid, Date auctionStart, Date auctionEnd, int auctionBidCount) {
9599     setTranslationTable(mKeys);
100     setTable("auctions");
96101     setTitle(auctionTitle.trim());
97102     setHighBidder(auctionHighBidder.trim());
98103     _seller = Seller.makeSeller(auctionSeller.trim());
------
191196 
192197   public XMLElement toXML() {
193198     XMLElement xmlResult = new XMLElement("info");
194     XMLElement xseller, xbidcount, xstart, xend, xdutch, xinsurance;
195199 
196200     addStringChild(xmlResult, "title");
197201 
198     xseller = _seller.toXML();
202     XMLElement xseller = _seller.toXML();
199203     xmlResult.addChild(xseller);
200204 
201     xstart = new XMLElement("start");
205     XMLElement xstart = new XMLElement("start");
202206     xstart.setContents(Long.toString(getStart().getTime()));
203207     xmlResult.addChild(xstart);
204208 
205     xend = new XMLElement("end");
209     XMLElement xend = new XMLElement("end");
206210     xend.setContents(Long.toString(getEnd().getTime()));
207211     xmlResult.addChild(xend);
208212 
209     xbidcount = new XMLElement("bidcount");
213     XMLElement xbidcount = new XMLElement("bidcount");
210214     xbidcount.setContents(Integer.toString(getNumBids()));
211215     xmlResult.addChild(xbidcount);
212216 
213     xinsurance = addCurrencyChild(xmlResult, "insurance");
217     XMLElement xinsurance = addCurrencyChild(xmlResult, "insurance");
214218     if(xinsurance != null) xinsurance.setProperty("optional", isInsuranceOptional() ?"true":"false");
215219 
216220     if(getCurBid().getCurrencyType() != Currency.US_DOLLAR) {
------
222226     addCurrencyChild(xmlResult, "buynow");
223227     addCurrencyChild(xmlResult, "minimum");
224228 
225     xdutch = addBooleanChild(xmlResult, "dutch");
229     XMLElement xdutch = addBooleanChild(xmlResult, "dutch");
226230     if(xdutch != null) xdutch.setProperty("quantity", Integer.toString(getQuantity()));
227231 
228232     XMLElement xreserve = addBooleanChild(xmlResult, "reserve");
205 206  
11 package com.jbidwatcher.auction;
22 
3 import com.jbidwatcher.util.ErrorManagement;
43 import com.jbidwatcher.util.db.DBRecord;
54 import com.jbidwatcher.util.db.AuctionDB;
65 import com.jbidwatcher.xml.XMLElement;
------
98 import java.util.Map;
109 
1110 /**
12  * Created by IntelliJ IDEA.
13 * User: Morgan
14 * Date: Sep 29, 2007
15 * Time: 7:27:30 PM
16 * To change this template use File | Settings | File Templates.
17 */
11  * User: Morgan
12  * Date: Sep 29, 2007
13  * Time: 7:27:30 PM
14  * To change this template use File | Settings | File Templates.
15  */
1816 public class Seller {
19   static Map<String,Seller> mSellerMap = new HashMap<String,Seller>();
17   static Map<String, Seller> mSellerMap = new HashMap<String,Seller>();
2018   String mSeller;
2119   String mPositivePercentage;
2220   int mFeedback;
------
2725     establishSellerDatabase();
2826   }
2927 
28   private Seller(DBRecord record) {
29     mSeller = record.get("seller");
30     try { mFeedback = Integer.parseInt(record.get("feedback")); } catch(Exception e) { mFeedback = 0; }
31     mPositivePercentage = record.get("feedback_percentage");
32   }
33 
3034   private static void establishSellerDatabase() {
3135     if (sDB == null) {
3236       try {
------
4145     establishSellerDatabase();
4246     Seller existing_seller = mSellerMap.get(sellerName);
4347     if(existing_seller == null) {
44     //  This should look up the seller in the database, so there's only one instance of any given seller.
45       Seller rval = new Seller();
46       rval.setSeller(sellerName);
48       DBRecord existing = sDB.findByColumn("seller", sellerName);
49       Seller rval;
4750 
51       if(existing != null) {
52         rval = new Seller(existing);
53       } else {
54         //  This should look up the seller in the database, so there's only one instance of any given seller.
55         rval = new Seller();
56         rval.setSeller(sellerName);
57       }
58 
4859       mSellerMap.put(sellerName, rval);
4960       return rval;
5061     }
------
5465 
5566   public Integer getId() {
5667     if(mId == null) {
57       String id = sDB.storeMap(getMap());
58       if(id != null && id.length() != 0) {
59         mId = Integer.parseInt(id);
60       } else {
61         mId = null;
62       }
68       create();
6369     }
6470     return mId;
6571   }
6672 
73   private void create() {
74     String id = sDB.storeMap(getMap());
75     if(id != null && id.length() != 0) {
76       mId = Integer.parseInt(id);
77     } else {
78       mId = null;
79     }
80   }
81 
6782   public DBRecord getMap() {
6883     DBRecord values = new DBRecord();
6984 
205 206  
124124 
125125         ae.setServer(newServer);
126126         ae.fromXML(perEntry);
127         mDB.storeMap(ae.getMap());
127         ae.create();
128128         if (sEntryManager != null) {
129129           sEntryManager.addEntry(ae);
130130         }
205 206  
33 import com.jbidwatcher.xml.XMLElement;
44 import com.jbidwatcher.xml.XMLSerializeSimple;
55 import com.jbidwatcher.util.db.DBRecord;
6 import com.jbidwatcher.util.db.AuctionDB;
67 
78 import java.util.Date;
89 import java.util.Map;
910 import java.util.TimeZone;
1011 import java.text.SimpleDateFormat;
12 import java.sql.SQLException;
1113 
1214 /**
13  * Created by IntelliJ IDEA.
1415  * User: Morgan
1516  * Date: Sep 30, 2007
1617  * Time: 1:54:43 PM
------
2223   private SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2324   private Map<String, String> mTranslationTable;
2425   private String mDefaultCurrency;
26   protected Integer mId;
27   protected AuctionDB mDB = null;
2528 
26   public HashBacked() {
29   protected void setDB(AuctionDB db) { mDB = db; }
30   protected AuctionDB setTable(String tableName) {
31     if(mDB == null) try {
32       mDB = new AuctionDB(tableName);
33     } catch (Exception e) {
34       e.printStackTrace();
35     }
36 
37     return mDB;
38   }
39 
40   protected HashBacked() {
2741     mDateFormat.setTimeZone(TimeZone.getDefault());
2842     mBacking = new DBRecord();
2943     mDefaultCurrency = Currency.getCurrency("$1.00").fullCurrencyName();
------
198212     return xadd;
199213   }
200214 
215   public Integer getId() {
216     if(mId == null) {
217       create();
218     }
219     return mId;
220   }
221 
222   public void create() {
223     String id = mDB.storeMap(getBacking());
224     if(id != null && id.length() != 0) {
225       mId = Integer.parseInt(id);
226     } else {
227       mId = null;
228     }
229   }
230 
201231   protected DBRecord getBacking() { return mBacking; }
232   protected void setBacking(DBRecord newRecord) { mBacking = newRecord; }
202233 }
197 206  
1414  * Wrap the auction information up in a database.
1515  */
1616 public class AuctionDB {
17   private class TypeColumn {
17   private static class TypeColumn {
1818     private String mType;
1919     private Integer mIndex;
2020 
------
123123   }
124124 
125125   public String updateMap(String tableName, String columnKey, String value, DBRecord newRow) {
126     DBRecord oldRow = getOldRow(tableName, columnKey, value);
126     DBRecord oldRow = getOldRow(tableName, columnKey, value, true);
127127     if(oldRow == null) return storeMap(newRow);
128128 
129129     String sql = createPreparedUpdate(tableName, oldRow, newRow);
------
136136       for(String key: newRow.keySet()) {
137137         if (newRow.get(key) != null) {
138138           if(!setColumn(ps, column++, key, newRow.get(key))) {
139             System.err.println("Error from columns: (" + column + "," + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")");
139             ErrorManagement.logMessage("Error from columns: (" + column + "," + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")");
140140           }
141141         }
142142       }
------
149149     return null;
150150   }
151151 
152   private DBRecord getOldRow(String tableName, String columnKey, String value) {
152   public DBRecord findByColumn(String columnKey, String value) {
153     return findByColumn(columnKey, value, false);
154   }
155 
156   public DBRecord findByColumn(String columnKey, String value, boolean forUpdate) {
157     return getOldRow(mTableName, columnKey, value, forUpdate);
158   }
159 
160   private DBRecord getOldRow(String tableName, String columnKey, String value, boolean forUpdate) {
153161     DBRecord oldRow = null;
154162     try {
155       ResultSet rs = mS.executeQuery("SELECT * FROM " + tableName + " FOR UPDATE WHERE " + columnKey + "=" + value);
163       String statement = "SELECT * FROM " + tableName;
164       if(forUpdate) statement += " FOR UPDATE";
165       statement += " WHERE " + columnKey + " = ?";
166       PreparedStatement ps = mDB.prepare(statement);
167       setColumn(ps, 1, columnKey, value);
168       ResultSet rs = ps.executeQuery();
156169       oldRow = getFirstResult(rs);
157170     } catch (SQLException e) {
158       ErrorManagement.handleException("Can't get row for update.", e);
171       ErrorManagement.handleException("Can't get row" + (forUpdate? " for update":"") + " (columnKey = '" + value +"'.", e);
159172     }
160173     return oldRow;
161174   }
------
171184       for(String key: newRow.keySet()) {
172185         if (newRow.get(key) != null) {
173186           if(!setColumn(ps, column++, key, newRow.get(key))) {
174             System.err.println("Error from columns: (" + column + ", " + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")");
187             ErrorManagement.logDebug("Error from columns: (" + column + ", " + key + ", " + mColumnMap.get(key).getType() + ", " + newRow.get(key) + ")");
175188           }
176189         }
177190       }
------
274287           }
275288         }
276289       } else {
277         System.err.println("WTF?!?!");
290         ErrorManagement.logDebug("WTF?!?!");
278291       }
279292     } catch (SQLException e) {
280293       e.printStackTrace();