aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird GUI start up how to get all Rooms? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird GUI start up how to get all Rooms?" Watch "URLyBird GUI start up how to get all Rooms?" New topic
Author

URLyBird GUI start up how to get all Rooms?

Leah Knowles
Greenhorn

Joined: Sep 25, 2009
Posts: 8
Hi

I'm sorry if this seems a basic or silly question but I am getting a real mental blank about it. For URLyBird I am working on GUI start up I want to get all Rooms from the data file to populate in the GUI table.

There is a find(String [] criteria) method which if the array parameter is null should return all records, but problem is it only returns the int recNos. I need to get the entire details of all the records niot just the recNo.

I suppose I could create a new method similar to find that returns String [] array or even a Room object but is there a problem with this, as my understanding is we should only use the methods in the Data.java interface supplied in the assignment and not introduce new ones. I dont want to maintain maps or data in the GUI package as I believe this is not good practice.

How did others do this?

Thanks
Leah
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Hi Leah,

First of all welcome to the JavaRanch and good luck with your assignment.

as my understanding is we should only use the methods in the Data.java interface supplied in the assignment and not introduce new ones.

That's a misunderstanding: I created my own interface (extending sun's interface of course), defined some extra new methods (also an extra method to return Map of recNo and String[]). My Data class implements my own interface (and because it extends sun's required interface it also implements sun's interface).

So creating your own method is 1 possibility. Another possibility would be to call read-method for each recNo returned by the find-method.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2264
    
    3

Howdy, y'all.

Roel De Nijs wrote:So creating your own method is 1 possibility. Another possibility would be to call read-method for each recNo returned by the find-method.


Hum... I'd go with the first option. Calling the read() method for each record number returned by the find() method would generate too much network traffic. In my case, I also created a new interface, which extends Sun's interface, where I added a method called getAllRecords(), which returns a Map<Integer, Room>.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Alex Hartman
Greenhorn

Joined: Oct 11, 2009
Posts: 7
Hey guys,

I have noticed that a lot of you utilize the approach where you read the db file into a map and store the db file in cache. So do you write the records back to file every time you update or change them, or how do you ensure that two different users see the same records ? When you access the db file directly, you do it in a synchronized context, ensuring that no other user accesses it at the same time. But what happens if you read all records into a map, and five minutes later another user does the same. So I am assuming that every time a record changes the db file is updated accordingly?

Kind regards,

Alex
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Hi Alex,

The principle of a record cache is very easy:
  • when the application (standalone or server) starts the db file is read and all records are stored in a map (the record cache)
  • every action on a record (find, read, update,...) is executed on the map (so totally no file I/O), so the map contains all records. when an update happens the according record in the map is updated
  • at the end of the application the record cache is written back to the db file


  • Kind regards,
    Roel
    Fernando Franzini
    Ranch Hand

    Joined: Jan 09, 2009
    Posts: 486
        
        2

    Hi Leah

    I am working on GUI start up I want to get all Rooms from the data file to populate in the GUI table.

    I'm doing URLyBird and I dont remenber such requirement...that you "must" populated main gui in startup. See your assignment....if was your assunption dont forget to write in choices.txt. Anyway,..... you are free to do it.

    There is a find(String [] criteria) method which if the array parameter is null should return all records, but problem is it only returns the int recNos. I need to get the entire details of all the records niot just the recNo......
    I dont want to maintain maps or data in the GUI package as I believe this is not good practice.

    In this case we have solved this design create a layer between pesistence and gui layer. Instead of: GUI acess-> DB acess->FILE ....do GUI acess->Another Layer acess-> DB -> acess File. Some guys have use a bussines layer approach and another have use a simple GUI Controller.


    I suppose I could create a new method similar to find that returns String [] array or even a Room object but is there a problem with this, as my understanding is we should only use the methods in the Data.java interface supplied in the assignment and not introduce new ones.

    You can't change assignment inteface but you can extends another yours....this ways you are free to define how many methods you want.

    Regards


    Fernando Franzini - Java Blog
    Alex Hartman
    Greenhorn

    Joined: Oct 11, 2009
    Posts: 7
    Hello Roel,

    Thank you for the explanation. If the map is not written back until the user exits the application won't that cause problems in a multi-user environment. What would happen for example if user 1 signs into the application and populates the map. Shortly thereafter, user 2 signs in and does the same. Then, user 1 books a room and user 2 books exactly the same room. Won't that cause problems, or am I thinking along the wrong line of thought here?

    Kind regards,

    Alex
    Nicolas Kal
    Ranch Hand

    Joined: Sep 09, 2009
    Posts: 69
    As far as it concerns with my approach I didn't followed the cached methodology.
    What happens if the VM crushes suddenly and the application is exited without saving the file? That means all work is lost. Furthermore what happens if Client A caches data from DB_FILE_A while Client B has allready cached the same data. Both clients operate on the same file, which Client's data should we save? Should the data be merged? I know that this can be easily resolved while working over the network but what happens if you are working standalone?
    My approach opens a stream, update the file, close the stream. Also whenever a user chooses to perform an update on an existing record the record is first synchronized and then the refreshed data are prompted to the user for update.

    As far as it concerns for retrieving all the data i have introduced another method in the data class that retrieves all the records. It returns an ArrayList<String []> and the back-end business logic translates it to a Business Object and sends it back to the client, a simplified java class with accessor method that matches the database schema.

    Hope I helped..


    SCJP 6.0 , SBCD (Preparing..),
    MSc Enterprise Systems Development, BSc Computer Science
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5201
        
      12

    Hi Alex and Nicolas,

    you have 2 possible modes: network mode (with a client application and a server running the database) and standalone (local) mode (where the client and the database is running in the same VM). The first one should be able to handle multiple clients, the second one is just 1 client.
    So the server is started and the records are cached, this happens just once, not for every client that's signing into the application. So I think you are thinking alone the wrong line. The record cache is on the server, not on the clients. And for the standalone application there is no problem, because there is just 1 application, so records are also cached just once.

    I think you both didn't read the instructions very well (unless they have changed them recently), but my instructions clearly say:
    You may assume that at any moment, at most one program is accessing the database file

    There will be just 1 application access the database file at any given moment: that could be the server application (in networked mode) or the client application (in standalone mode), but you don't have to handle a situation where 2 clients (both standalone, 1 server application + 1 standalone client,...) access the same file at the same moment.

    It's true if the VM crashes you will loose all your data, so that's a drawback of this approach, which I mentioned in my choices.txt together with some possible solutions (for example: create a seperate thread that will save your record cache every hour to the database file). As far as I know using a record cache together with a singleton Data class with all synchronized methods is the easiest and most simple approach (I don't have to handle IOException in my read, update, delete,... methods). And because easy, simple, understandable code (for a junior programmer) is preferred, I followed this approach. But of course I pointed out possible drawbacks in my choices.txt and also mentioned possible solutions for each drawback.

    Hope it helps.
    Kind regards,
    Roel
    Roberto Perillo
    Bartender

    Joined: Dec 28, 2007
    Posts: 2264
        
        3

    Just to complement the words of the Great King of Belgium, the good thing of this approach is that you also avoid the I/O overhead that you would have if you kept dealing with the .db file directly. The other good thing is that you don't have to deal with I/O exceptions.

    Another thing to remember is, even though there is only one client when working in standalone mode, the locking mechanism must work properly as well.

    The Great King of Belgium (Roel) wrote:As far as I know using a record cache together with a singleton Data class with all synchronized methods is the easiest and most simple approach...


    That was exactly my approach!
    Nicolas Kal
    Ranch Hand

    Joined: Sep 09, 2009
    Posts: 69
    Hi Roel,

    Taking into account the two followings from the instructions

    In the future, URLyBird wants to move into Internet-based marketing, and hopes to be able to accept bookings direct from customers over the web.


    Your user interface should be designed with the expectation of future functionality enhancements, and it should establish a framework that will support this with minimal disruption to the users when this occurs.


    You may assume that at any moment, at most one program is accessing the database file


    A clear design, such as will be readily understood by junior programmers, will be preferred to a complex one, even if the complex one is a little more efficient.


    Yes it is assumed that at the moment the database file is accessed by a single application.It mentions also that in the future this system will be implemented as a web application.
    According to my opinion and taking the above issues in consideration I believe that the best approach is to follow a design so that when changes occur they should disrupt only the part they are taking place. For isntance if we decide to move to a web application, throw away the GUI part but keep the bussiness logic and the server functionality intact. If changes occur to database and a new relational will be introduced alter the data access class but the client must not be affected.

    My desing addresses all the above issues in a simplistic way. Instead of throwing IOExceptions to the client in order to catch the error I have created a custom CommunicationException in order to be safe when enhancements will come. Furthermore the developer it self does not have to do anything to display the error message since I have created an ExceptionHandler class which handles and displays the appropriate error message to the user. In my design the client does not know anything regarding the Data class. The client is exposed to an interface, DataSimulator, with the following methods



    My data class is also a singleton but I have not declared not even one method as synchronized, why? My data class is accessed only through the DataSimulator implementations. when the client accesses one of the interfaces methods a syncronization block takes place holding the lock for the Data singleton class. I've tested it and debug it a lot and it works perfectly. Using a thread or a timer class to flush data on a fixed ammount of time I believe that is also acquiring resources from the CPU and you are also facing the possibility of loosing your data. Furthermore if you change to a relational DB you will still be flushing data on fixed ammount of time? Of course not, because when this applications moves to the web you want your clients to see the actual data on the data file

    To summarise I don't say that your approach is not correct, on the contrary I believe that every simplistic design which is build according to specs and with a clearly defined choices.txt explaining all the issues you fced is not a candidate to fail. After all I believe that the puprose of this assignment is to let us explore the possible issues that may arise and how we will resolve them.

    Thanks and good luck to all of us
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5201
        
      12

    Hi Nicolas,

    First of all: when you use a code-tag try to limit the number of characters (80 for example) on 1 line, because it messes up the complete thread and reduces readibility. Thanks!

    You use a DataSimulator, I use a BusinessService. And of course my GUI can easily be replaced by another view (web-application for example) without having to change business logic and server functionality. Or you can easily get rid of the database file and use a RDBMS (like MySql for example). Or you can easily add a similar Data class to handle a file containing the customers too.

    After all I believe that the puprose of this assignment is to let us explore the possible issues that may arise and how we will resolve them.

    Can't agree more!

    Kind regards,
    Roel
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5201
        
      12

    Roberto Perillo wrote:Just to complement the words of the Great King of Belgium

    The last time I checked: it was Albert the 2nd
     
    Consider Paul's rocket mass heater.
     
    subject: URLyBird GUI start up how to get all Rooms?