Package org.xulfaces.rubis.db

Source Code of org.xulfaces.rubis.db.DatasScriptsBuilder

package org.xulfaces.rubis.db;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.xulfaces.rubis.model.Category;
import org.xulfaces.rubis.model.Item;
import org.xulfaces.rubis.model.User;

public class DatasScriptsBuilder {

  private Map<Integer,Item> items = new HashMap<Integer,Item>();
 
  private Category[] categories = {
     
  new Category(1,"Antiques & Art"),
  new Category(2,"Books"),
  new Category(3,"Business, Office & Industrial"),
  new Category(4,"Clothing & Accessories"),
  new Category(5,"Coins"),
  new Category(6,"Collectibles"),
  new Category(7,"Computers"),
  new Category(8,"Consumer Electronics"),
  new Category(9,"Dolls & Bears"),
  new Category(10,"Home & Garden"),
  new Category(11,"Jewelry, Gems & Watches"),
  new Category(12,"Movies & Television"),
  new Category(13,"Music"),
  new Category(14,"Photo"),
  new Category(15,"Pottery & Glass"),
  new Category(16,"Sports"),
  new Category(17,"Stamps"),
  new Category(18,"Tickets & Travel"),
  new Category(19,"Toys & Hobbies"),
  new Category(20,"Everything Else")};

  private User[] users = {
     
      new User("Արամ Խաչատրյան"),
      new User("Johann Strauß"),
      new User("Vaqif Səmədoğlu"),
      new User("সুকুমার রায়"),
      new User("章子怡"),
      new User("عبدالحليم حافظ"),
      new User("ኃይሌ ገብረሥላሴ"),
      new User("⠇⠕⠥⠊⠎⠀⠃⠗⠁⠊⠇⠇⠑"),
      new User("Jürgen Klinsmann"),
      new User("Γιώργος Νταλάρας"),
      new User("András Sütő"),
      new User("माधुरी दीक्षित"),
      new User("久保田    利伸"),
      new User("林原 めぐみ"),
      new User("宮崎 駿"),
      new User("テクス・テクサン"),
      new User("안성기"),
      new User("Садриддин Айнӣ"),
      new User("சிவாஜி கனேசன்"),
      new User("ธงไชย แม็คอินไตย์"),
      new User("Ніна Матвієнко"),
      new User("Солижон Шарипов"
  };
 
 
 
  /**
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    DatasScriptsBuilder datasScriptsBuilder = new DatasScriptsBuilder();
    datasScriptsBuilder.buildUsersDataScript();
    datasScriptsBuilder.buildItemsDataScript();
    datasScriptsBuilder.buildBidsDataScript();   
  }

  protected void buildUsersDataScript() throws IOException
    int userId = 0;
    FileWriter fileWriter = new FileWriter("users.sql");
    fileWriter.append("DELETE FROM USERS\n");
    fileWriter.flush();
    for(int i=0; i < 500; i++,userId++){
      StringBuffer sqlBuffer = new StringBuffer("INSERT INTO USERS (ID,FIRSTNAME,LASTNAME,NICKNAME,PASSWORD,EMAIL,RATING,BALANCE,CREATION_DATE,REGION) VALUES (");
      sqlBuffer.append(userId);
      sqlBuffer.append(",");
      sqlBuffer.append("'User #");
      sqlBuffer.append(userId);
      sqlBuffer.append(" first name',");
      sqlBuffer.append("'User #");
      sqlBuffer.append(userId);
      sqlBuffer.append(" last name',");
      int nicknameId= generateUserNicknameId();
      sqlBuffer.append("'");
      sqlBuffer.append(this.users[nicknameId].getNickname());
      sqlBuffer.append("',");
      sqlBuffer.append("'user#");
      sqlBuffer.append(userId);
      sqlBuffer.append("',");
      sqlBuffer.append("'user#");
      sqlBuffer.append(userId);
      sqlBuffer.append("@xulfaces.org',");
      sqlBuffer.append("0,100,'2006-04-29 18:23:55',");       
      sqlBuffer.append(generateRegionId());
      sqlBuffer.append(")\n");     
      fileWriter.append(sqlBuffer);
      fileWriter.flush();
    }
    fileWriter.close();
  }
 
  protected void buildItemsDataScript() throws IOException{   
    int itemId = 0;
    FileWriter fileWriter = new FileWriter("items.sql");
    fileWriter.append("DELETE FROM ITEMS\n");
    fileWriter.flush();
    for(int i=0; i < 5000; i++,itemId++){
      StringBuffer sqlBuffer = new StringBuffer("INSERT INTO ITEMS (ID,NAME,DESCRIPTION,INITIAL_PRICE,QUANTITY,RESERVE_PRICE,BUY_NOW,NB_OF_BIDS,MAX_BID,START_DATE,END_DATE,SELLER,CATEGORY) VALUES (");     
      Item item = new Item();
      int categoryId = generateCategoryId();
      item.setId(new Integer(itemId));
      this.items.put(item.getId(),item);
      sqlBuffer.append(itemId);
      sqlBuffer.append(",");
      Category category = this.categories[categoryId-1];
      sqlBuffer.append("'" + category.getName() + " #");
      sqlBuffer.append(itemId);
      sqlBuffer.append("','This incredible item is exactly what you need !',");

      // Initial price
      item.setInitialPrice((float) (Math.random()*15000));
      sqlBuffer.append((int)item.getInitialPrice());
     
      sqlBuffer.append(",");
      // Qty
      item.setQuantity((int)(Math.random()*10));
      sqlBuffer.append(item.getQuantity());
      sqlBuffer.append(",");
      // Reserved price
      item.setReservedPrice((float) (Math.random()*item.getInitialPrice()));
      sqlBuffer.append((int)item.getReservedPrice());
      sqlBuffer.append(",");
      sqlBuffer.append("0,0,0,'2006-04-01 18:23:55','2060-01-01 18:23:55',");       
      sqlBuffer.append(generateUserId());
      sqlBuffer.append(",");
      sqlBuffer.append(categoryId);
      sqlBuffer.append(")\n");
      fileWriter.append(sqlBuffer);
      fileWriter.flush();
    }
    fileWriter.close();
  }
 
 
  protected void buildBidsDataScript() throws IOException{   
    int bidId = 0;
    FileWriter fileWriter = new FileWriter("bids.sql");
    fileWriter.append("DELETE FROM BIDS\n");
    fileWriter.flush();   
    for(int i=0; i < 10000; i++,bidId++){
      StringBuffer sqlBuffer = new StringBuffer("INSERT INTO BIDS (ID,USER_ID,ITEM_ID,QTY,BID,MAX_BID,DATE) VALUES (");
      sqlBuffer.append(bidId);
      sqlBuffer.append(",");
      sqlBuffer.append(generateUserId());     
      sqlBuffer.append(",");
      int itemId = generateItemId();
      Item item = this.items.get(new Integer(itemId));
      sqlBuffer.append(itemId);
      sqlBuffer.append(",");
      sqlBuffer.append(1);
      sqlBuffer.append(",");
      sqlBuffer.append((int)item.getInitialPrice());
      sqlBuffer.append(",0,'2006-04-01 13:00:00')\n");
      fileWriter.append(sqlBuffer);
      fileWriter.flush();
    }
    fileWriter.close();
  }
 
 
  protected int generateUserNicknameId(){
    int userId = (int)(Math.random()*this.users.length);
    if(userId < 1){
      userId = 1;
    }
    return userId;
  }
 
  protected int generateUserId(){
    int userId = (int)(Math.random()*500);
    if(userId < 1){
      userId = 1;
    }
    return userId;
  }
 
  protected int generateItemId(){
    int itemId = (int)(Math.random()*5000);
    if(itemId < 1){
      itemId = 1;
    }
    return itemId;
  }
 
  protected int generateCategoryId(){
    int categoryId = (int)(Math.random()*20);
    if(categoryId < 1){
      categoryId = 1;
    }
    return categoryId;
  }
 
  protected int generateRegionId(){
    int regionId = (int)(Math.random()*62);
    if(regionId < 1){
      regionId = 1;
    }
    return regionId;
  }
}
TOP

Related Classes of org.xulfaces.rubis.db.DatasScriptsBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.