home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / petstore.js < prev    next >
Encoding:
Text File  |  2004-07-12  |  18.3 KB  |  575 lines

  1. /*
  2.  * Copyright 1999-2004 The Apache Software Foundation.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. // Page Flow for PetStore Application
  18.  
  19. // load CForms support
  20. cocoon.load("resource://org/apache/cocoon/forms/flow/javascript/Form.js");
  21.  
  22. var MAX_RESULTS = 5;
  23.  
  24. var VIEW = "jexl";
  25. var EXT = ".jexl";
  26.  
  27. // Utility to format numbers and dates (for use by Velocity templates)
  28.  
  29. function Formatter() {
  30. }
  31.  
  32. Formatter.prototype.formatNumber = function(num, format) {
  33.     return new java.text.DecimalFormat(format).format(num);
  34. }
  35.  
  36. Formatter.prototype.formatDate = function(date, format) {
  37.     return new java.text.SimpleDateFormat(format).format(date);
  38. }
  39.  
  40. var formatter = new Formatter();
  41. var petStore = null;
  42. var accountForm = null;
  43. var cartForm = null;
  44. var categoryList = null;
  45.  
  46.  
  47. function main(funName) {
  48.     var fun = this[funName];
  49.     var args = new Array(arguments.length -1);
  50.     for (var i = 1; i < arguments.length; i++) {
  51.         args[i-1] = arguments[i];
  52.     }
  53.     getPetStore();
  54.     fun.apply(args);
  55.  
  56. }
  57.  
  58. function getPetStore() {
  59.     if (petStore == null) {
  60.         this.petStore = new PetStore("hsql");
  61.         this.cartForm = new CartForm();
  62.         this.accountForm = new AccountForm();
  63.         this.categoryList = getPetStore().getCategoryList();
  64.     }
  65.     return petStore;
  66. }
  67.  
  68. function setView() {
  69.     VIEW = cocoon.request.get("view");
  70.     print("setView: VIEW="+VIEW);
  71.     if (VIEW == "velocity") {
  72.         EXT = ".vm";
  73.     } else if (VIEW == "xsp") {
  74.         EXT = ".xsp";
  75.     } else if (VIEW == "jexl") {
  76.         EXT = ".jexl";
  77.     } else if (VIEW == "jxpath") {
  78.         EXT = ".jxpath";
  79.     }
  80.     print("EXT="+EXT);
  81. }
  82.  
  83. // Index page
  84.  
  85. function index() {
  86.     setView();
  87.     getPetStore();
  88.     cocoon.sendPage("view/index" + EXT, {
  89.             accountForm: accountForm,
  90.             categoryList: categoryList,
  91.     });
  92. }
  93.  
  94. // Cart page
  95.  
  96. function viewCart() {
  97.     var cartItems = [];
  98.     for (var i in cartForm.cart.cartItems) {
  99.         var cartItem = cartForm.cart.cartItems[i];
  100.         cartItems.push(cartItem);
  101.     }
  102.     cocoon.sendPage("view/Cart" + EXT, {
  103.             accountForm: accountForm,
  104.             cartForm: cartForm,
  105.             fmt: formatter,
  106.             cartItems: cartItems,
  107.             label: "Shopping Cart"
  108.     });
  109. }
  110.  
  111. function removeItemFromCart() {
  112.     var itemId = cocoon.request.getParameter("workingItemId");
  113.     var item = getPetStore().getItem(itemId);
  114.     cartForm.cart.removeItem(item);
  115.     var cartItems = [];
  116.     for (var i in cartForm.cart.cartItems) {
  117.         var cartItem = cartForm.cart.cartItems[i];
  118.         cartItems.push(cartItem);
  119.     }
  120.     cocoon.sendPage("view/Cart" + EXT, {
  121.             fmt: formatter,
  122.             accountForm: accountForm,
  123.             cartForm: cartForm,
  124.             cartItems: cartItems,
  125.             label: "Shopping Cart"
  126.     });
  127. }
  128.  
  129. function updateCartQuantities() {
  130.     var cartItems = [];
  131.     for (var i in cartForm.cart.cartItems) {
  132.         var cartItem = cartForm.cart.cartItems[i];
  133.         var itemId = cartItem.item.itemId;
  134.         var quantity = parseInt(cocoon.request.get(itemId));
  135.         cartItem.updateQuantity(quantity);
  136.         cartItems.push(cartItem);
  137.     }
  138.     cocoon.sendPage("view/Cart" + EXT, {
  139.             fmt: formatter,
  140.             accountForm: accountForm,
  141.             cartForm:cartForm,
  142.             cartItems: cartItems,
  143.             label: "Shopping Cart"
  144.     });
  145. }
  146.  
  147. function addItemToCart() {
  148.     var itemId = cocoon.request.getParameter("itemId");
  149.     var item = getPetStore().getItem(itemId);
  150.     cartForm.cart.addItem(item);
  151.     var cartItems = [];
  152.     for (var i in cartForm.cart.cartItems) {
  153.         var cartItem = cartForm.cart.cartItems[i];
  154.         cartItems.push(cartItem);
  155.     }
  156.     cocoon.sendPage("view/Cart" + EXT, {
  157.             fmt: formatter,
  158.             accountForm: accountForm,
  159.             cartForm: cartForm,
  160.             cartItems: cartItems,
  161.             label: "Shopping Cart"
  162.     });
  163. }
  164.  
  165. // Category page
  166.  
  167. function viewCategory() {
  168.     var categoryId = cocoon.request.get("categoryId");
  169.     var category = getPetStore().getCategory(categoryId);
  170.     var maxResults = MAX_RESULTS;
  171.     /* page local variable to keep track of pagination */
  172.     var local = cocoon.createPageLocal();
  173.     local.skipResults = 0;
  174.     while (true) {
  175.         var productList =
  176.             getPetStore().getProductListByCategory(categoryId,
  177.                                                    local.skipResults,
  178.                                                    maxResults);
  179.         local.lastPage = !productList.isLimitedByMaxRows;
  180.         local.rowCount = productList.rowCount;
  181.         var contextData = {
  182.             accountForm: accountForm,
  183.             productList: productList.rows,
  184.             category: category,
  185.             firstPage: local.skipResults == 0,
  186.             lastPage: local.lastPage
  187.         };
  188.         cocoon.sendPageAndWait("view/Category" + EXT, 
  189.                                contextData, 
  190.                                function () {
  191.                                    /* release contextData and productList */
  192.                                    contextData = null;
  193.                                    productList = null;
  194.                                });
  195.         var page = cocoon.request.get("page");
  196.         if (page == "previous") {
  197.             if (local.skipResults != 0) {
  198.                 local.skipResults -= maxResults;
  199.             }
  200.         } else if (page == "next") {
  201.             if (!local.lastPage) {
  202.                 local.skipResults += local.rowCount;
  203.             }
  204.         }
  205.     }
  206. }
  207.  
  208. // Product page
  209.  
  210. function viewProduct() {
  211.     var productId = cocoon.request.get("productId");
  212.     var product = getPetStore().getProduct(productId);
  213.     var maxResults = MAX_RESULTS;
  214.     /* page local variable to handle pagination */
  215.     var local = cocoon.createPageLocal();
  216.     local.skipResults = 0;
  217.     while (true) {
  218.         var itemList =
  219.             getPetStore().getItemListByProduct(productId,
  220.                                                local.skipResults,
  221.                                                maxResults);
  222.         local.lastPage = !itemList.isLimitedByMaxRows;
  223.         local.rowCount = itemList.rowCount;
  224.         var contextData = {
  225.             accountForm: accountForm,
  226.             fmt: formatter,
  227.             product: product,
  228.             firstPage: local.skipResults == 0,
  229.             lastPage: local.lastPage,
  230.             itemList: itemList.rows
  231.         };
  232.         cocoon.sendPageAndWait("view/Product" + EXT, 
  233.                                contextData,
  234.                                function() {
  235.                                    /* release contextData and itemList */
  236.                                    contextData = null;
  237.                                    itemList = null;
  238.                                });
  239.         var page = cocoon.request.get("page");
  240.         if (page == "previous") {
  241.             if (local.skipResults != 0) {
  242.                 local.skipResults -= maxResults;
  243.             }
  244.         } else if (page == "next") {
  245.             if (!local.lastPage) {
  246.                 local.skipResults += local.rowCount;
  247.             }
  248.         }
  249.     }
  250. }
  251.  
  252. // Item page
  253.  
  254. function viewItem() {
  255.     var itemId = cocoon.request.getParameter("itemId");
  256.     var item = getPetStore().getItem(itemId);
  257.     cocoon.sendPage("view/Item" + EXT, {
  258.              accountForm: accountForm,
  259.              cartForm: cartForm,
  260.              item: item,
  261.              quantity: getPetStore().getItemRowCountByProduct(item.productId),
  262.              product: item.product,
  263.              fmt: formatter
  264.     });
  265. }
  266.  
  267. // Sign-on page
  268.  
  269. function signonForm() {
  270.     signOn();
  271.     index();
  272. }
  273.  
  274. function signOn(process) {
  275.     if (cocoon.request.get("signoff") != null) {
  276.         accountForm = new AccountForm();
  277.         cartForm = new CartForm();
  278.     } else {
  279.         var message = "";
  280.         var registerType;
  281.         if (process) {
  282.             registerType = process;
  283.         } else {
  284.             registerType = "new";
  285.         }
  286.         while (true) {
  287.             cocoon.sendPageAndWait("view/SignonForm" + EXT, {
  288.                             accountForm: accountForm,
  289.                             message: message,
  290.                             registerType: registerType
  291.             });
  292.             var username = cocoon.request.get("username");
  293.             var password = cocoon.request.get("password");
  294.             print("getting account: " + username);
  295.             var account = getPetStore().getAccount(username, password);
  296.             if (account == null) {
  297.                 message = "Invalid username or password";
  298.             } else {
  299.                 accountForm = new AccountForm(username, password);
  300.                 accountForm.account = account;
  301.                 accountForm.signOn = false;
  302.                 break;
  303.             }
  304.         }
  305.     }
  306. }
  307.  
  308. // Account Forms
  309.  
  310. function editAccount() {
  311.     editAccountData();
  312.     cocoon.sendPage("index.do");
  313.  
  314. }
  315.  
  316. function newAccount() {
  317.     newAccountData();
  318.     cocoon.sendPage("index.do");
  319. }
  320.  
  321. function instantAccount() {
  322.     newAccountData();
  323.     cocoon.sendPage("checkout.do");
  324. }
  325.  
  326. function editAccountData() {
  327.     var editAccountDataForm = new Form("view/forms/editAccountForm_d.xml");
  328.     var model = editAccountDataForm.getModel();
  329.     model.message = "";
  330.     model.username = accountForm.username;
  331.     model.changePwdOption = false;
  332.     model.password = "";
  333.     model.retypepassword = "";
  334.     model.firstname = accountForm.account.firstname;
  335.     model.lastname = accountForm.account.lastname;
  336.     model.email = accountForm.account.email;
  337.     model.phone= accountForm.account.phone;
  338.     model.addr1 = accountForm.account.addr1;
  339.     model.addr2 = accountForm.account.addr2;
  340.     model.city = accountForm.account.city;
  341.     model.state = accountForm.account.state;
  342.     model.zip = accountForm.account.zip;
  343.     model.country = accountForm.account.country;
  344.     model.langpref = accountForm.account.langpref;
  345.     model.favcategory = accountForm.account.favcategory;
  346.     model.mylistopt = accountForm.account.mylistopt;
  347.     model.banneropt = accountForm.account.banneropt;
  348.  
  349.     editAccountDataForm.showForm("view/editAccountForm.cforms");
  350.     while (model.changePwdOption && (model.password != model.retypepassword || model.password == null)) {
  351.         model.message = "Passwords don't match!";
  352.         editAccountDataForm.showForm("view/editAccountForm.cforms");
  353.     }
  354.  
  355.     if (!accountForm.signOn) {
  356.         var update = getPetStore().updateAccount(model);
  357.     } else {
  358.         var insert = getPetStore().insertAccount(model);
  359.         accountForm.signOn = false;
  360.     }
  361.  
  362.     if (model.changePwdOption) {
  363.         var chPwd = getPetStore().updateSignon(accountForm.username, model.password);
  364.         accountForm.password = model.password;
  365.     }
  366.     accountForm.account = getPetStore().getAccount(accountForm.username, accountForm.password);
  367. }
  368.  
  369. function newAccountData() {
  370.     var newAccountDataForm = new Form("view/forms/newAccountForm_d.xml");
  371.     var model = newAccountDataForm.getModel();
  372.  
  373.     model.message = "";
  374.     model.username = "";
  375.     model.password = "";
  376.     model.retypepassword = "";
  377.  
  378.     newAccountDataForm.showForm("view/newAccountForm.cforms");
  379.     while (getPetStore().testDuplicateLogin(model.username) > 0) {
  380.         model.message = "Username already in use. Please choose another username.";
  381.         newAccountDataForm.showForm("view/newAccountForm.cforms");
  382.     }
  383.     var insertNewUser = getPetStore().insertNewUser(model);
  384.     print("insertNewUser: "+insertNewUser);
  385.     accountForm = new AccountForm(model.username, model.password);
  386.     accountForm.account = new Account();
  387.     editAccountData();
  388.  
  389. }
  390.  
  391. // Search
  392.  
  393. function empty(str) {
  394.     return str == null ||
  395.       (str instanceof java.lang.String ?
  396.            str.length() == 0 :
  397.                   str.length == 0);
  398. }
  399.  
  400. function searchProducts() {
  401.     var keyword = cocoon.request.get("keyword");
  402.     if (empty(keyword)) {
  403.         cocoon.sendPage("view/Error" + EXT, {
  404.             accountForm: accountForm,
  405.             message: "Please enter a keyword to search for, then press the search button"
  406.         });
  407.         return;
  408.     }
  409.     /* page local variable to manage pagination */
  410.     var local = cocoon.createPageLocal();
  411.     local.skipSearchResults = 0;
  412.     var maxSearchResults = 3;
  413.     while (true) {
  414.         var result =
  415.             getPetStore().searchProductList(keyword, 
  416.                                             local.skipSearchResults,
  417.                                             maxSearchResults);
  418.         local.lastPage = !result.isLimitedByMaxRows;
  419.         local.rowCount = result.rowCount;
  420.         var contextData = {
  421.             accountForm: accountForm,
  422.             searchResultsProductList: result.rows,
  423.             firstPage: local.skipSearchResults == 0,
  424.             lastPage: local.lastPage
  425.         };
  426.         cocoon.sendPageAndWait("view/SearchProducts" + EXT, 
  427.                                contextData, 
  428.                                function() {
  429.                                    /* release contextData and result */
  430.                                    contextData = null;
  431.                                    result = null;
  432.                                });
  433.         var page = cocoon.request.get("page");
  434.         if (page == "previous") {
  435.             if (local.skipSearchResults != 0) {
  436.                 local.skipSearchResults -= maxSearchResults;
  437.             }
  438.         } else if (page == "next") {
  439.             if (!local.lastPage) {
  440.                 local.skipSearchResults += local.rowCount;
  441.             }
  442.         }
  443.     }
  444. }
  445.  
  446. function billingForm(order) {
  447.     var billingForm = new Form("view/forms/newOrderForm_d.xml");
  448.     var model = billingForm.getModel();
  449.     model.cardType = order.cardType;
  450.     model.creditCard = order.creditCard;
  451.     model.expiryDate = order.expiryDate;
  452.     model.billToFirstName = order.billToFirstName;
  453.     model.billToLastName = order.billToLastName;
  454.     model.billAddress1 = order.billAddress1;
  455.     model.billAddress2 = order.billAddress2;
  456.     model.billCity = order.billCity;
  457.     model.billState = order.billState;
  458.     model.billZip = order.billZip;
  459.     model.billCountry = order.billCountry;
  460.     model.shippingAddressRequired = false;
  461.     billingForm.showForm("view/newOrderForm.cforms");
  462.     return model;
  463. }
  464.  
  465. function shippingForm(order) {
  466.     var shippingForm = new Form("view/forms/newShippingForm_d.xml");
  467.     var model = shippingForm.getModel();
  468.     model.shipToFirstName = order.shipToFirstName;
  469.     model.shipToLastName = order.shipToLastName;
  470.     model.shipAddress1 = order.shipAddress1;
  471.     model.shipAddress2 = order.shipAddress2;
  472.     model.shipCity= order.shipCity;
  473.     model.shipState= order.shipState;
  474.     model.shipZip= order.shipZip;
  475.     model.shipCountry= order.shipCountry;
  476.     shippingForm.showForm("view/newShippingForm.cforms");
  477.     return model;
  478. }
  479.  
  480. // Checkout
  481.  
  482. function checkout() {
  483.     var cartItems = [];
  484.     for (var i in cartForm.cart.cartItems) {
  485.         var cartItem = cartForm.cart.cartItems[i];
  486.         cartItems.push(cartItem);
  487.     }
  488.     cocoon.sendPageAndWait("view/Cart" + EXT, {
  489.                     accountForm: accountForm,
  490.                     cartForm: cartForm,
  491.                     fmt: formatter,
  492.                     cartItems: cartItems,
  493.                     label: "Checkout Summary"
  494.     });
  495.     if (accountForm.signOn) {
  496.         signOn("instant");
  497.     }
  498.     var orderForm = new OrderForm();
  499.     orderForm.initOrder(accountForm, cartForm);
  500.  
  501.     var model = billingForm(orderForm.order);
  502.     orderForm.order.billToFirstName = model.billToFirstName;
  503.     orderForm.order.billToLastName = model.billToLastName;
  504.     orderForm.order.billAddress1 = model.billAddress1;
  505.     orderForm.order.billAddress2 = model.billAddress2;
  506.     orderForm.order.billCity = model.billCity;
  507.     orderForm.order.billState = model.billState;
  508.     orderForm.order.billZip = model.billZip;
  509.     orderForm.order.billCountry = model.billCountry;
  510.     orderForm.order.cardType = model.cardType;
  511.     orderForm.order.creditCard = model.creditCard;
  512.     orderForm.order.expiryDate = model.expiryDate;
  513.     orderForm.shippingAddressRequired = model.shippingAddressRequired;
  514.     if (orderForm.shippingAddressRequired == true) {
  515.         model = shippingForm(orderForm.order);
  516.         orderForm.order.shipToFirstName = model.shipToFirstName;
  517.         orderForm.order.shipToLastName = model.shipToLastName;
  518.         orderForm.order.shipAddress1 = model.shipAddress1;
  519.         orderForm.order.shipAddress2 = model.shipAddress2;
  520.         orderForm.order.shipCity = model.shipCity;
  521.         orderForm.order.shipState = model.shipState;
  522.         orderForm.order.shipZip = model.shipZip;
  523.         orderForm.order.shipCountry = model.shipCountry;
  524.     }
  525.     
  526.     cocoon.sendPageAndWait("view/ConfirmOrder" + EXT,
  527.                     {accountForm: accountForm,
  528.                     order: orderForm.order,
  529.                     fmt: formatter});
  530.  
  531.     orderForm.confirmed = 
  532.         cocoon.request.getParameter("confirmed") == "true";
  533.  
  534.     if (cartForm.cart.numberOfItems > 0 && orderForm.confirmed) {
  535.         var lastOID = getPetStore().insertOrder(orderForm.order, accountForm.username);
  536.         cartForm = new CartForm();
  537. //        cocoon.sendPage("viewOrder.do?orderId=" + lastOID);
  538.         viewOrder(lastOID);
  539.     }
  540.     else {
  541.         cocoon.sendPage("index.do");
  542.     }
  543. }
  544.  
  545. function listOrders() {
  546.     var orderList = getPetStore().getOrderList(accountForm.username);
  547.     cocoon.sendPage("view/ListOrders" + EXT, {
  548.             accountForm: accountForm,
  549.             fmt: formatter,
  550.             orderList: orderList
  551.     });
  552. }
  553.  
  554. function viewOrder(lastOID) {
  555.     var orderId;
  556.     var message;
  557.     if (lastOID != null) {
  558.         orderId = lastOID;
  559.         message = "Thank you, your order has been submitted.";
  560.     } else {
  561.         orderId = cocoon.request.getParameter("orderId");
  562.     }
  563.     var archivedOrder = getPetStore().getOrder(orderId, accountForm.username);
  564.     var lineItemList = getPetStore().getLineItems(orderId);
  565.     cocoon.sendPage("view/ViewOrder" + EXT, {
  566.             accountForm: accountForm,
  567.             fmt: formatter,
  568.             message: message,
  569.             archivedOrder: archivedOrder,
  570.             lineItemList: lineItemList.rows,
  571.             process: {label: "Ordered Items:",
  572.                        id: "checkout"}
  573.     });
  574. }
  575.