Easy FIrebase library for JavaFX

Hi everyone! This post is for people who already know the basics of Java and JavaFX and that could be looking for a way to easily working with Java Classes and Firebase without all the hassle that following the official guides for Firebase Admin SDK for Java provides.

Wait, wait, wait! Please, I don’t know what are you talking about!

Oh, sorry. I forgot it’s important to always mention and talk about the context since there could be newcomers. I don’t want to scare people out of their pants if they’re just trying to start developing a Server-side (or even to use as a Client!) app for Firebase Administration since the comfort of your desktop JavaFX app.

Well. There’s not a lot to be said. Basically, Firebase is a backend service that allows us to create easy and, at the same time, complex apps that need to use a Real Time Database for saving and sharing data or to authenticate their users, for naming some of the functions Firebase manages.

A Real Time Database?

Yes, a Real Time Database! This Firebase’s product allows you to set up an easy way to save and query data from multiple devices at the same time! There are a lot of things that Firebase offers. If you don’t know a thing about it, please read the Firebase Products page. There’s a lot you can do with your new web or mobile apps! (Or even desktop apps, as we’ll see with Firebase Admin SDK library for Java).

So… I already know what you’re talking about. I just want you to show me what this OOPFirebase is about already.

Firebase already offers libraries for the Java platform in the form of Firebase Admin SDK. However, Java’s library is still a bit too loooong to use if you want to simply create a new object instance from a Java class and save it directly to your online database. (You need many lines of code that you’ll be easily repeating, just like in the Javascript library for Clients) .

It’s not hard, as you can read in their Admin SDK guides but this can easily be improved by creating your own library, since Java is an Object Oriented Programming language; or by using my library, which I’ve called OOPFirebase, just for referencing the way it’s more object oriented than the usual way of saving data by using directly the official SDK library.

Let’s get started.

This library isn’t very well documented so far, since I’ve just began working on it, but no fear, since I’ll work it to be as commented as possible so that everything is easy to do, even for people who doesn’t get to read my posts.

So, now that we are all set, let’s go!

What are we doing today?

We are going to create a new connection to Firebase and write data to it directly from instanced Java classes. We’ll not be covering data retrieving for now, that will come in the next post. Also, this library needs JavaFX! Since I used many of the nice ObjectProperty classes that only JavaFX provides.

I’m couldn’t add my project to Maven repositories, yet, so my library is only available as an Ant compiled JAR right now. You still can import it to your Maven projects via editing the POM file. More details about it in this guide.

What do you need before starting?

  • You should create a new service account and get the JSON file that contains the private key. This will allow our JavaFX app to connect to our Firebase’s RealTime Database. Read this for getting it.
  • Add the latest version of the Maven library of the Firebase Admin Java SDK to your Java app, and add the OOPFirebase’s JAR to your project. (I’ll try to add the project to Maven repositories, but it’ll be later, If you don’t know how to add a local JAR to your maven project, follow this guide).
Ready, set, go!
It’s really easy to start a Firebase connection with the OOPFirebase library. All you need to do is instance the FirebaseConnection object passing two parameters, and then call the initApp() method:

Code:
FirebaseConnection conn=
         new FirebaseConnection
       ("/com/olmectron/oopfirebasetest/private-key-file.json",
                 "https://project-id.firebaseio.com/");
 conn.initApp();

The first parameter is the inner path (inside your source directory) to the JSON file. In this example, I have a package with the path /com/olmectron/oopfirebasetest and the JSON file is inside it along with my Main.java class. The second parameter is the database url that you can find easily in the RealTime Database section of the Firebase console.

Now, pushing an object to firebase is even easier! For know, let’s just see some generic objects, that means, we won’t create classes for them, but instead instance the FirebaseGenericObject class and pass it some values. Just like this:

Code:
FirebaseGenericObject person=new FirebaseGenericObject() {
                    @Override
                    public DatabaseReference retrieveFirebaseReference() {
                        return FirebaseConnection.getReference("people");
                    }
                };

 person.set("name", "Olmectron");
 person.set("age", 24);

The FirebaseGenericObjecet class has an abstract method that should return the reference we’ll use as the path for our object to be pushed to. In this example, we returned the reference for the path “people”, so our generic objects (persons) will be pushed to a list under that path.

Then, for adding fields to it, is as easy as calling the set(String field,Object value) method. Since it can receive any type of Object, you could pass even instances of your own custom classes, and it’ll work, but let’s save the details about custom classes for the next entry, alright?

If you want the data to save as a number, just pass the value without the quotation marks. This way you can pass int, double, short boolean and more type of primitive values and they’ll be retained like that when pushed to the database!

So far, we have just created our object and set it some fields. Now, let’s push it!

Code:
//You can use this for actually pushing with a new unique id generated by firebase
object.push(new FirebaseInsertCallback<Map<String,Object>>(){
               @Override
               public void onObjectInserted(String string, Map<String,Object> t) {

                        String logMessage="Object pushed with key "
                                +string+", name: "+t.get("name")
                                +", age:"+t.get("age");

                        System.out.println(logMessage);

                    }
                });

//OR use this for adding the object automatically with an autoincrement ID.
//A 'object-ref-counter' like path will be created automatically in your database.

obejct.insertToFirebase();

/*Unfortunately I haven't added a callback for this, yet. I'll be working on it
for the next post*/

That was fast! Yep, just call push and add it a callback for knowing when the data is actually saved to the database and that’s it. Since this is a generic object, we’ll get a Map back from firebase, which is the object that was just pushed. We can get the data back from it for GUI updating purposes.

Also, you can simply call the pushToFirebase() method so you don’t need to put a callback if you don’t want to.

And that’s it for pushing generic objects!

Next post will be about creating your own classes with setters and getters and push them easily!

Here’s a JavaFX example with a simple GUI for testing the code! I doesn’t include my project’s private key file, of course.

Keep coding!
  • Like
Reactions: 1 person

Comments

Didn't know how to put those codes in quotes, I just looked about Xenforo quotation codes and fixed it.
 

Blog entry information

Author
Olmectron
Views
333
Comments
4
Last update

More entries in Personal Blogs

  • 4: Reddit
    Finally, number 4! Never thought this day would come, did you? Uhh...
  • books
    1. I am cool as hell, have one million dollars 2. I am banned from...
  • Syncthing is fun!
    Having been kinda active in an Android forum I quickly got sick about...
  • Feeling at home here
    Not much to say this time. I'm depressed. Like almost always. Trying to...
  • I'll start, rate mine 1-10
    It's a very mixed bag, some rock, some rap, some video game music, a...

More entries from Olmectron

Share this entry

General chit-chat
Help Users
  • No one is chatting at the moment.
  • K3Nv2 @ K3Nv2:
    Don't know burnt pizza can be pretty bad
  • Xdqwerty @ Xdqwerty:
    gonna download sonic generations to my ps3
  • BigOnYa @ BigOnYa:
    How do you put games on there? Does it have like a game store on the ps3?
  • K3Nv2 @ K3Nv2:
    Black market store
    +1
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, download a pkg file, put it in a usb or a sd card, put the usb or sd card to ps3, turn on ps3hen, go to install game, install the game
    +1
  • BigOnYa @ BigOnYa:
    Ok yea that's same with me, was just curious. Coolio
    +1
  • Xdqwerty @ Xdqwerty:
    good night
  • BigOnYa @ BigOnYa:
    Nighty night
  • K3Nv2 @ K3Nv2:
    Lmao my keyboard decided to randomly die
  • BigOnYa @ BigOnYa:
    Its prob your new router interfering w it
  • K3Nv2 @ K3Nv2:
    How when my old one worked fine
  • BigOnYa @ BigOnYa:
    You figure out ring and all smart devices? And POE
  • K3Nv2 @ K3Nv2:
    Yeah but gave up on PPPoE
    +1
  • Sicklyboy @ Sicklyboy:
    did you fix it ken?
  • K3Nv2 @ K3Nv2:
    Bob the builder
  • K3Nv2 @ K3Nv2:
    Uck
  • BakerMan @ BakerMan:
    you
  • K3Nv2 @ K3Nv2:
    Deeze nuts
  • BakerMan @ BakerMan:
    $21 bill dropped

    iu
  • K3Nv2 @ K3Nv2:
    Wtf are you doing with that $20 cutting up meth
  • BigOnYa @ BigOnYa:
    Going to jail if he tries to spend it
  • K3Nv2 @ K3Nv2:
    Try to buy something with it
    K3Nv2 @ K3Nv2: Try to buy something with it