Source Code Cross Referenced for Trade.java in  » 6.0-JDK-Modules » java-3d » com » db » server » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » java 3d » com.db.server 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2001 Silvere Martin-Michiellot All Rights Reserved.
003:         *
004:         * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005:         * royalty free, license to use, modify and redistribute this
006:         * software in source and binary code form,
007:         * provided that i) this copyright notice and license appear on all copies of
008:         * the software; and ii) Licensee does not utilize the software in a manner
009:         * which is disparaging to Silvere Martin-Michiellot.
010:         *
011:         * This software is provided "AS IS," without a warranty of any kind. ALL
012:         * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013:         * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014:         * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015:         * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016:         * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017:         * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018:         * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019:         * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020:         * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021:         * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022:         * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023:         * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024:         *
025:         * This software is not designed or intended for use in on-line control of
026:         * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027:         * the design, construction, operation or maintenance of any nuclear
028:         * facility. Licensee represents and warrants that it will not use or
029:         * redistribute the Software for such purposes.
030:         *
031:         * @Author: Silvere Martin-Michiellot
032:         *
033:         */
034:
035:        package com.db.server;
036:
037:        import javax.jdo.*;
038:        import java.util.*;
039:
040:        /**
041:         * This is a simple banking system. It is currently not as safe as a professionnal banking system.
042:         */
043:
044:        //Many different things can be sold: land, backgrounds, objects, avatars skins, service, pseudo-cash (an object on which is imprinted the value of its price but not secure
045:        //since someone can build up objects with an imprinted value different from the price).
046:        //
047:        //Buying and selling can either occur directly between two participants putting money against the other's services. It can also occur with a group of
048:        //participants in a merchant exchange in which people pool and draw objects with the existing agreement of some other one doing the same.
049:        //
050:        //Credit card can be used. Public key system is here to enforce security as well as receipts and certificates.
051:        //PLEASE NOTE:
052:        //Dates are computed using the Java System time NOT the UniverseServer time (which is basically the same apart from an offset)
053:        public class Trade extends Object {
054:
055:            public final static String EURO = "Euro";
056:            public final static String US_DOLLAR = "US Dollar";
057:            public final static String CAN_DOLLAR = "Canadian Dollar";
058:            public final static String AUS_DOLLAR = "Australian Dollar";
059:            public final static String YEN = "YEN";
060:
061:            private UniverseServer universeServer;
062:            private Vector currenciesIdentifiers;
063:            private double[][] currencies;
064:
065:            private Trade() {
066:
067:                throw new java.lang.IllegalArgumentException(
068:                        "A world spot must have at least a universeServer, currenciesIdentifiers and currencies.");
069:
070:            }
071:
072:            //not everything is checked (positive values...)
073:            public Trade(UniverseServer universeServer,
074:                    Vector currenciesIdentifiers, double[][] currencies) {
075:
076:                int i;
077:                boolean found;
078:
079:                if (universeServer != null) {
080:
081:                    this .universeServer = universeServer;
082:
083:                    //be sure to have a n*n matrix
084:                    i = 0;
085:                    found = false;
086:
087:                    while ((i < currencies.length) && (!found)) {
088:                        found = (currencies[i].length != currencies.length);
089:                        i++;
090:                    }
091:
092:                    if ((found)
093:                            || (currenciesIdentifiers.size() != currenciesIdentifiers
094:                                    .size())) {
095:                        throw new IllegalArgumentException(
096:                                "Arrays don't have matching length.");
097:                    } else {
098:                        if ((currenciesIdentifiers.size() == 0)
099:                                || (((String) currenciesIdentifiers.get(0))
100:                                        .length() == 0)) {
101:                            throw new IllegalArgumentException(
102:                                    "There must be at least a valid currency.");
103:                        } else {
104:                            this .currenciesIdentifiers = currenciesIdentifiers;
105:                            this .currencies = currencies;
106:                        }
107:                    }
108:
109:                } else {
110:                    throw new IllegalArgumentException(
111:                            "A trade must have a valid universeServer.");
112:                }
113:
114:            }
115:
116:            public PersistenceManager getPersistenceManager() {
117:
118:                return this .universeServer.getPersistenceManager();
119:
120:            }
121:
122:            //returns the ratio aCurrency/anotherCurrency
123:            public final double convertCurrency(String aCurrency,
124:                    String anotherCurrency) {
125:
126:                if ((currenciesIdentifiers.contains(aCurrency))
127:                        && (currenciesIdentifiers.contains(anotherCurrency))) {
128:                    return this .currencies[currenciesIdentifiers
129:                            .indexOf(aCurrency)][currenciesIdentifiers
130:                            .indexOf(aCurrency)];
131:                } else {
132:                    return 0;
133:                }
134:
135:            }
136:
137:            public Vector getCurrenciesIdentifiers() {
138:
139:                return this .currenciesIdentifiers;
140:
141:            }
142:
143:            public double[][] getCurrencies() {
144:
145:                return this .currencies;
146:
147:            }
148:
149:            //NO check at all is made
150:            //but you should nesure there is always at least conversion to and from Trade.US_DOLLAR
151:            protected void setCurrencies(Vector currenciesIdentifiers,
152:                    double[][] currencies) {
153:
154:                //currencies array should be filled with proper values
155:                this .currenciesIdentifiers = currenciesIdentifiers;
156:                this .currencies = currencies;
157:
158:            }
159:
160:            //devaluate money
161:            //please use setCurrencies(Vector currenciesIdentifiers, double[][] currencies);
162:
163:            //returns the converted price using aCurrency/anotherCurrency
164:            public final double convertPrice(String aCurrency,
165:                    String anotherCurrency, double price) {
166:
167:                return convertCurrency(aCurrency, anotherCurrency) * price;
168:
169:            }
170:
171:            //each avatar must own its respective objects
172:            //no price here and no ticket
173:            public final void exchange(Avatar aAvatar,
174:                    VirtualElement[] someVirtualElements, Avatar anotherAvatar,
175:                    VirtualElement[] someOthersVirtualElements) {
176:
177:                Transaction transaction;
178:                int i;
179:                boolean found;
180:
181:                i = 0;
182:                found = false;
183:
184:                if ((aAvatar != null) && (anotherAvatar != null)) {
185:
186:                    while ((i < someVirtualElements.length) && (!found)) {
187:                        found = (someVirtualElements[i].getUserOwner() != aAvatar);
188:                        i++;
189:                    }
190:
191:                    i = 0;
192:                    while ((i < someVirtualElements.length) && (!found)) {
193:                        found = (someVirtualElements[i].getUserOwner() != anotherAvatar);
194:                        i++;
195:                    }
196:
197:                    if (!found) {
198:
199:                        for (i = 0; i < someVirtualElements.length; i++) {
200:                            someVirtualElements[i].setUserOwner(anotherAvatar);
201:                        }
202:
203:                        for (i = 0; i < someOthersVirtualElements.length; i++) {
204:                            someOthersVirtualElements[i].setUserOwner(aAvatar);
205:                        }
206:
207:                        //update database
208:
209:                        try {
210:                            transaction = this .getPersistenceManager()
211:                                    .currentTransaction();
212:                            transaction.begin();
213:
214:                            this .getPersistenceManager().refreshAll(
215:                                    someVirtualElements);
216:                            this .getPersistenceManager().refreshAll(
217:                                    someOthersVirtualElements);
218:
219:                            transaction.commit();
220:
221:                        } catch (Exception e) {
222:                        }
223:                    }
224:
225:                }
226:
227:            }
228:
229:            //allows a bargaining system
230:            //as well as a balancing system (substract both prices)
231:            //"real" prices and currencies of VirtualElements are not used here since this is considered to be a lot sale
232:            //adjust individual prices of the newly acquired items at your convenience after sale
233:            //v-cash-card of avatars are used to make the transaction
234:            //please use currencies identifiers used in the constructor for currencies
235:            public final void buyAndSell(Avatar aAvatar,
236:                    VirtualElement[] someVirtualElements, double aPrice,
237:                    String aCurrency, Avatar anotherAvatar,
238:                    VirtualElement[] someOthersVirtualElements,
239:                    double anotherPrice, String anotherCurrency) {
240:
241:                Transaction transaction;
242:                int i;
243:                boolean found;
244:                Ticket[] tickets;
245:                double basicPrice;
246:
247:                if ((aAvatar != null) && (anotherAvatar != null)) {
248:
249:                    if ((aPrice >= 0) && (aCurrency != null)
250:                            && (aCurrency.length() > 0) && (anotherPrice >= 0)
251:                            && (anotherCurrency != null)
252:                            && (anotherCurrency.length() > 0)) {
253:
254:                        i = 0;
255:                        found = false;
256:
257:                        while ((i < someVirtualElements.length) && (!found)) {
258:                            found = (someVirtualElements[i].getUserOwner() != aAvatar);
259:                            i++;
260:                        }
261:
262:                        i = 0;
263:                        while ((i < someOthersVirtualElements.length)
264:                                && (!found)) {
265:                            found = (someOthersVirtualElements[i]
266:                                    .getUserOwner() != anotherAvatar);
267:                            i++;
268:                        }
269:
270:                        if (!found) {
271:
272:                            basicPrice = this .convertPrice(aCurrency,
273:                                    anotherCurrency, aPrice)
274:                                    / someVirtualElements.length;
275:                            for (i = 0; i < someVirtualElements.length; i++) {
276:                                someVirtualElements[i]
277:                                        .setUserOwner(anotherAvatar);
278:                                tickets = this .generateTransaction(aAvatar,
279:                                        anotherAvatar, someVirtualElements[i],
280:                                        basicPrice, anotherCurrency);
281:                            }
282:
283:                            basicPrice = this .convertPrice(anotherCurrency,
284:                                    aCurrency, aPrice)
285:                                    / someOthersVirtualElements.length;
286:                            for (i = 0; i < someOthersVirtualElements.length; i++) {
287:                                someOthersVirtualElements[i]
288:                                        .setUserOwner(aAvatar);
289:                                tickets = this .generateTransaction(
290:                                        anotherAvatar, aAvatar,
291:                                        someOthersVirtualElements[i],
292:                                        basicPrice, aCurrency);
293:                            }
294:
295:                            //update database
296:
297:                            try {
298:                                transaction = this .getPersistenceManager()
299:                                        .currentTransaction();
300:                                transaction.begin();
301:
302:                                this .getPersistenceManager().refreshAll(
303:                                        someVirtualElements);
304:                                this .getPersistenceManager().refreshAll(
305:                                        someOthersVirtualElements);
306:
307:                                transaction.commit();
308:
309:                            }
310:
311:                            catch (Exception e) {
312:                            }
313:
314:                        }
315:
316:                    }
317:
318:                }
319:
320:            }
321:
322:            //see buyAndSell
323:            //actual prices of objects are used here
324:            public final void buyAndSell(Avatar aAvatar,
325:                    VirtualElement[] someVirtualElements, Avatar anotherAvatar,
326:                    VirtualElement[] someOthersVirtualElements) {
327:
328:                Transaction transaction;
329:                int i;
330:                boolean found;
331:                Ticket[] tickets;
332:                double basicPrice;
333:
334:                if ((aAvatar != null) && (anotherAvatar != null)) {
335:
336:                    i = 0;
337:                    found = false;
338:
339:                    while ((i < someVirtualElements.length) && (!found)) {
340:                        found = (someVirtualElements[i].getUserOwner() != aAvatar);
341:                        i++;
342:                    }
343:
344:                    i = 0;
345:                    while ((i < someOthersVirtualElements.length) && (!found)) {
346:                        found = (someOthersVirtualElements[i].getUserOwner() != anotherAvatar);
347:                        i++;
348:                    }
349:
350:                    if (!found) {
351:
352:                        for (i = 0; i < someVirtualElements.length; i++) {
353:                            someVirtualElements[i].setUserOwner(anotherAvatar);
354:                            tickets = this .generateTransaction(aAvatar,
355:                                    anotherAvatar, someVirtualElements[i],
356:                                    someVirtualElements[i].getPrice(),
357:                                    someVirtualElements[i].getCurrency());
358:                        }
359:
360:                        for (i = 0; i < someOthersVirtualElements.length; i++) {
361:                            someOthersVirtualElements[i].setUserOwner(aAvatar);
362:                            tickets = this .generateTransaction(anotherAvatar,
363:                                    aAvatar, someOthersVirtualElements[i],
364:                                    someOthersVirtualElements[i].getPrice(),
365:                                    someOthersVirtualElements[i].getCurrency());
366:                        }
367:
368:                        //update database
369:
370:                        try {
371:                            transaction = this .getPersistenceManager()
372:                                    .currentTransaction();
373:                            transaction.begin();
374:
375:                            this .getPersistenceManager().refreshAll(
376:                                    someVirtualElements);
377:                            this .getPersistenceManager().refreshAll(
378:                                    someOthersVirtualElements);
379:
380:                            transaction.commit();
381:
382:                        }
383:
384:                        catch (Exception e) {
385:                        }
386:
387:                    }
388:
389:                }
390:
391:            }
392:
393:            //see generateTransaction
394:            //uses values from virtual element (which can't be null)
395:            public final Ticket[] generateTransaction(Avatar seller,
396:                    Avatar buyer, VirtualElement virtualElement) {
397:
398:                if (virtualElement != null) {
399:                    return this .generateTransaction(seller, buyer,
400:                            virtualElement, virtualElement.getPrice(),
401:                            virtualElement.getCurrency());
402:                } else {
403:                    return null;
404:                }
405:
406:            }
407:
408:            //returns the Ticket and Receipt
409:            //this is the fundamental transaction to which each merchant system must comply
410:            //virtualElement can be null in case a service is paid
411:            //price must be >=0
412:            //currency value is checked and defaults to Trade.US_DOLLAR if null or of length 0
413:            //seller must own the VirtualElement he sells if any
414:            //uses v-cash-card
415:            public final Ticket[] generateTransaction(Avatar seller,
416:                    Avatar buyer, VirtualElement virtualElement, double price,
417:                    String currency) {
418:
419:                Transaction transaction;
420:                Ticket[] tickets;
421:                Ticket ticket;
422:                Ticket receipt;
423:                long id;
424:                Date date;
425:
426:                if ((currency == null) || (currency.length() == 0)) {
427:                    currency = Trade.US_DOLLAR;
428:                }
429:
430:                if ((buyer != null) && (seller != null) && (price >= 0)) {
431:
432:                    tickets = new Ticket[2];
433:
434:                    if ((virtualElement != null)
435:                            && (virtualElement.getUserOwner() == seller)) {
436:                        virtualElement.setUserOwner(buyer);
437:                        virtualElement.doBuy(buyer);
438:                    }
439:
440:                    id = this .getTransactionID();
441:                    date = new Date(System.currentTimeMillis());
442:
443:                    //field access on seller and buyer
444:                    ticket = new Ticket(id, Ticket.TICKET, virtualElement,
445:                            price, currency, date, seller.getLoginInformation()
446:                                    .getPublicKey());
447:                    receipt = new Ticket(id, Ticket.RECEIPT, virtualElement,
448:                            price, currency, date, buyer.getLoginInformation()
449:                                    .getPublicKey());
450:
451:                    tickets[0] = ticket;
452:                    tickets[1] = receipt;
453:
454:                    buyer.addTicketOrReceipt(ticket);
455:                    seller.addTicketOrReceipt(receipt);
456:
457:                    try {
458:                        transaction = this .getPersistenceManager()
459:                                .currentTransaction();
460:                        transaction.begin();
461:
462:                        this .getPersistenceManager().makePersistentAll(tickets);
463:
464:                        transaction.commit();
465:
466:                    }
467:
468:                    catch (Exception e) {
469:                    }
470:
471:                    this .payService(buyer, seller, price, currency);
472:
473:                    return tickets;
474:
475:                } else {
476:
477:                    return null;
478:
479:                }
480:
481:            }
482:
483:            //this is an optional system based on the assumption that people have an electronic wallet or account
484:            //it involves tools such as v-cash-card (you get v-cash you never see but from which you can exchange services)
485:            //it is different from pseudo cash which is objectWorld on which is textured a value that is believed to be the price by everyone using it
486:            //pseudo cash is not implemented as we believe it is unsafe
487:            //v-cash-card uses money from your real credit card or v-cash
488:            //when money is drawn from your cash card, it is converted in v-cash with the corresponding v-currency
489:            //banks should eventually do the opposite someday, that is transfer v-cash into "real" money on your cash card
490:            //you can run a world without credit cards but with v-cash anyway
491:            //just note that there won't be any conversion between your v-currency and any other currency
492:
493:            //can be only called by ruler
494:            //not needed if to use input from real Cash
495:            //returned is the wallet of the ruler updated with the required amount (which should be in its inventory)
496:            //no check is made to know if avatar actually owns the money he tranfers
497:            public final void createVCash(Avatar avatar, double value,
498:                    String currency) {
499:
500:                //Not Yet implemented
501:                //should also be synchronized
502:
503:                Transaction transaction;
504:                Extent extent;
505:                Query query;
506:                String filter;
507:                Collection collection;
508:                //should be a sub class of ObjectWorld
509:                SecuredVCashWallet securedVCashWallet;
510:
511:                if ((avatar != null) && (Avatar.isRuler(avatar))) {
512:
513:                    try {
514:                        transaction = this .getPersistenceManager()
515:                                .currentTransaction();
516:                        transaction.begin();
517:
518:                        // Execute a query
519:                        extent = this .getPersistenceManager().getExtent(
520:                                ObjectWorld.class, true);
521:                        filter = new String(
522:                                "SELECT ObjectWorld FROM ObjectWorld o WHERE o.getClass()==SecuredVCashWallet.class AND o.getUserOwner()==avatar");
523:                        query = this .getPersistenceManager().newQuery(
524:                                ObjectWorld.class, extent, filter);
525:                        collection = (Collection) query.execute();
526:
527:                        transaction.commit();
528:
529:                        //find the first wallet
530:                        if (collection.size() > 0) {
531:                            securedVCashWallet = (SecuredVCashWallet) collection
532:                                    .iterator().next();
533:                            securedVCashWallet.add(value, currency, this );
534:                            //we should may be call for a database refresh
535:                        } else {
536:                            securedVCashWallet = new SecuredVCashWallet(
537:                                    this .universeServer, avatar);
538:                            securedVCashWallet.add(value, currency, this );
539:                            try {
540:                                transaction = this .getPersistenceManager()
541:                                        .currentTransaction();
542:                                transaction.begin();
543:
544:                                this .getPersistenceManager().makePersistent(
545:                                        securedVCashWallet);
546:
547:                                transaction.commit();
548:
549:                            }
550:
551:                            catch (Exception e) {
552:                            }
553:
554:                            avatar.addInventoryEntry(securedVCashWallet);
555:                            //we should may be call for a database refresh
556:                        }
557:
558:                    }
559:
560:                    catch (Exception e) {
561:                    }
562:
563:                }
564:
565:            }
566:
567:            //no check is made to know if avatar actually owns the money he tranfers
568:            public final void fromCashCardToVCashCard(Avatar avatar,
569:                    double value, String currency) {
570:
571:                //Not Yet implemented
572:                //should use Java wallet
573:                //should also be synchronized
574:
575:                Transaction transaction;
576:                Extent extent;
577:                Query query;
578:                String filter;
579:                Collection collection;
580:                //should be a sub class of ObjectWorld
581:                SecuredVCashWallet securedVCashWallet;
582:
583:                if (avatar != null) {
584:
585:                    try {
586:                        transaction = this .getPersistenceManager()
587:                                .currentTransaction();
588:                        transaction.begin();
589:
590:                        // Execute a query
591:                        extent = this .getPersistenceManager().getExtent(
592:                                ObjectWorld.class, true);
593:                        filter = new String(
594:                                "SELECT ObjectWorld FROM ObjectWorld o WHERE o.getClass()==SecuredVCashWallet.class AND o.getUserOwner()==avatar");
595:                        query = this .getPersistenceManager().newQuery(
596:                                ObjectWorld.class, extent, filter);
597:                        collection = (Collection) query.execute();
598:
599:                        transaction.commit();
600:
601:                        //find the first wallet
602:                        if (collection.size() > 0) {
603:                            securedVCashWallet = (SecuredVCashWallet) collection
604:                                    .iterator().next();
605:                            securedVCashWallet.add(value, currency, this );
606:                            //we should may be call for a database refresh
607:                        } else {
608:                            securedVCashWallet = new SecuredVCashWallet(
609:                                    this .universeServer, avatar);
610:                            securedVCashWallet.add(value, currency, this );
611:                            try {
612:                                transaction = this .getPersistenceManager()
613:                                        .currentTransaction();
614:                                transaction.begin();
615:
616:                                this .getPersistenceManager().makePersistent(
617:                                        securedVCashWallet);
618:
619:                                transaction.commit();
620:
621:                            }
622:
623:                            catch (Exception e) {
624:                            }
625:
626:                            avatar.addInventoryEntry(securedVCashWallet);
627:                            //we should may be call for a database refresh
628:                        }
629:
630:                    }
631:
632:                    catch (Exception e) {
633:                    }
634:
635:                }
636:
637:                //remove the same amount of money from the Java wallet
638:
639:            }
640:
641:            //no check is made to know if avatar actually owns the money he transfers
642:            public final void fromVCashCardToCashCard(Avatar avatar,
643:                    double value, String currency) {
644:
645:                //Not Yet implemented
646:                //should use Java wallet
647:                //should also be synchronized
648:
649:                Transaction transaction;
650:                Extent extent;
651:                Query query;
652:                String filter;
653:                Collection collection;
654:                //should be a sub class of ObjectWorld
655:                SecuredVCashWallet securedVCashWallet;
656:
657:                if (avatar != null) {
658:
659:                    try {
660:                        transaction = this .getPersistenceManager()
661:                                .currentTransaction();
662:                        transaction.begin();
663:
664:                        // Execute a query
665:                        extent = this .getPersistenceManager().getExtent(
666:                                ObjectWorld.class, true);
667:                        filter = new String(
668:                                "SELECT ObjectWorld FROM ObjectWorld o WHERE o.getClass()==SecuredVCashWallet.class AND o.getUserOwner()==avatar");
669:                        query = this .getPersistenceManager().newQuery(
670:                                ObjectWorld.class, extent, filter);
671:                        collection = (Collection) query.execute();
672:
673:                        transaction.commit();
674:
675:                        //find the first wallet
676:                        if (collection.size() > 0) {
677:                            securedVCashWallet = (SecuredVCashWallet) collection
678:                                    .iterator().next();
679:                            securedVCashWallet.subtract(value, currency, this );
680:                            //we should may be call for a database refresh
681:                        } else {
682:                            securedVCashWallet = new SecuredVCashWallet(
683:                                    this .universeServer, avatar);
684:                            securedVCashWallet.subtract(value, currency, this );
685:                            try {
686:                                transaction = this .getPersistenceManager()
687:                                        .currentTransaction();
688:                                transaction.begin();
689:
690:                                this .getPersistenceManager().makePersistent(
691:                                        securedVCashWallet);
692:
693:                                transaction.commit();
694:
695:                            }
696:
697:                            catch (Exception e) {
698:                            }
699:
700:                            avatar.addInventoryEntry(securedVCashWallet);
701:                            //we should may be call for a database refresh
702:                        }
703:
704:                    }
705:
706:                    catch (Exception e) {
707:                    }
708:
709:                }
710:
711:                //add the same amount of money from the Java wallet
712:
713:            }
714:
715:            //uses the v-cash-card (which are built up if needed and added to inventory)
716:            //value is substracted from aAvatar and credited to anotherAvatar
717:            //no check is made to know if aAvatar actually owns the money
718:            // aAvatar, anotherAvatar, value, currency are non null values
719:            private final void payService(Avatar aAvatar, Avatar anotherAvatar,
720:                    double value, String currency) {
721:
722:                //Not Yet implemented
723:                //should also be synchronized
724:
725:                Transaction transaction;
726:                Extent extent;
727:                Query query;
728:                String filter;
729:                Collection collection1;
730:                Collection collection2;
731:                //should be a sub class of ObjectWorld
732:                SecuredVCashWallet securedVCashWallet1;
733:                SecuredVCashWallet securedVCashWallet2;
734:
735:                try {
736:                    transaction = this .getPersistenceManager()
737:                            .currentTransaction();
738:                    transaction.begin();
739:
740:                    // Execute a query
741:                    extent = this .getPersistenceManager().getExtent(
742:                            ObjectWorld.class, true);
743:                    filter = new String(
744:                            "SELECT ObjectWorld FROM ObjectWorld o WHERE o.getClass()==SecuredVCashWallet.class AND o.getUserOwner()==aAvatar");
745:                    query = this .getPersistenceManager().newQuery(
746:                            ObjectWorld.class, extent, filter);
747:                    collection1 = (Collection) query.execute();
748:
749:                    transaction.commit();
750:
751:                    try {
752:
753:                        transaction = this .getPersistenceManager()
754:                                .currentTransaction();
755:                        transaction.begin();
756:
757:                        // Execute a query
758:                        extent = this .getPersistenceManager().getExtent(
759:                                ObjectWorld.class, true);
760:                        filter = new String(
761:                                "SELECT ObjectWorld FROM ObjectWorld o WHERE o.getClass()==SecuredVCashWallet.class AND o.getUserOwner()==anotherAvatar");
762:                        query = this .getPersistenceManager().newQuery(
763:                                ObjectWorld.class, extent, filter);
764:                        collection2 = (Collection) query.execute();
765:
766:                        transaction.commit();
767:
768:                        //find the first wallet
769:                        if (collection1.size() > 0) {
770:                            securedVCashWallet1 = (SecuredVCashWallet) collection1
771:                                    .iterator().next();
772:                            securedVCashWallet1.subtract(value, currency, this );
773:                            //we should may be call for a database refresh
774:                        } else {
775:                            securedVCashWallet1 = new SecuredVCashWallet(
776:                                    this .universeServer, aAvatar);
777:                            securedVCashWallet1.subtract(value, currency, this );
778:                            try {
779:                                transaction = this .getPersistenceManager()
780:                                        .currentTransaction();
781:                                transaction.begin();
782:
783:                                this .getPersistenceManager().makePersistent(
784:                                        securedVCashWallet1);
785:
786:                                transaction.commit();
787:
788:                            }
789:
790:                            catch (Exception e) {
791:                            }
792:
793:                            aAvatar.addInventoryEntry(securedVCashWallet1);
794:                            //we should may be call for a database refresh
795:                        }
796:
797:                        //find the first wallet
798:                        if (collection2.size() > 0) {
799:                            securedVCashWallet2 = (SecuredVCashWallet) collection2
800:                                    .iterator().next();
801:                            securedVCashWallet2.add(value, currency, this );
802:                            //we should may be call for a database refresh
803:                        } else {
804:                            securedVCashWallet2 = new SecuredVCashWallet(
805:                                    this .universeServer, anotherAvatar);
806:                            securedVCashWallet2.add(value, currency, this );
807:                            try {
808:                                transaction = this .getPersistenceManager()
809:                                        .currentTransaction();
810:                                transaction.begin();
811:
812:                                this .getPersistenceManager().makePersistent(
813:                                        securedVCashWallet2);
814:
815:                                transaction.commit();
816:
817:                            }
818:
819:                            catch (Exception e) {
820:                            }
821:
822:                            anotherAvatar
823:                                    .addInventoryEntry(securedVCashWallet2);
824:                            //we should may be call for a database refresh
825:                        }
826:
827:                    }
828:
829:                    catch (Exception e) {
830:                    }
831:
832:                }
833:
834:                catch (Exception e) {
835:                }
836:
837:            }
838:
839:            public static double promoteFloat(float aFloat) {
840:
841:                return new Double(aFloat).floatValue();
842:
843:            }
844:
845:            private static long getTransactionID() {
846:                //generates a unique number everytime it is called
847:                //ID generation should use the system time so that there is never the same ID even between virtual worlds (we hope)
848:
849:                return new Double(Math.random() * Double.MAX_VALUE).longValue();
850:
851:            }
852:
853:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.