- [Narrator] Alright, so we ended the previous video
by talking about, the need to add a method to get
the albums for an artist.
And so we're going to return a list of album objects.
So let's go ahead and write that query albums
for artist ID method.
So I'm just going to start
I'll actually start below the query artist method.
So,
we'll start with the method signatures,
so public, it's going to be a list of album objects
is what we're going to return.
And it's query artists,
query album,
or artist ID.
And we get past the ID, which is the actual record
in sequa-lite.
So I'll start with a try,
then we're going to do a query, albums by artist ID,
dot set it to one,
or part of the first field basically,
first column in the data base and ID being the parameter
that's past in this method.
Then we want results set, results is equal to query,
albums by artist ID,
dot execute query,
and we're getting this here at the moment
because we haven't handled the exception
which we'll be doing once we get to the end of the try.
List,
album,
albums,
equal to your A list,
all results dot next,
and we're going through the data base now,
one record at a time,
brand new album object,
album equals new album,
album dot set ID,
the results dot get it and be the first row
that's returned first column rather,
album dot set name.
That's gonna be results dot get string too,
then album dot set,
artist ID, that's gonna be ID,
albums dot at,
to our albums or album rather we'll just retrieve
from the database.
Then we gotta return from this method
the albums, the list that we've just created
and then let's also add the catch
to get rid of all those zeroes now,
catch seeker exception, E and we'll just print out a message
if we do get a sequel file there for some reason,
query filed plus E dot get message,
so we see something on the screen and then you'll turn null.
Alright so that's our query album for artist,
that should be albums rather,
query albums for artist ID method.
And a way of saying there isn't really new,
there's nothing really new there,
we set in the artist ID and the prepared statement
up here on line 227 and then we're running it
and we had looped through the results
and we're studying the artist ID and the album instance
one thing that's slightly different is here on line 235
what we're doing is were setting the artist ID
and the album instance by using the Vel-ly path
to the method rather than getting it from the results
because it's the same, we know it's the same artist
to here because we're querying a specific artist
there's no need for us to do that extra retrieval
from the database so we're probably saving a bit of time.
Alright so at this point we can now get a list of albums
based on an artist ID but now we need a way to display them.
Now what we're going to do is reuse the existing table
to do this and only display the album name.
Now fortunately the artist class stores the artist name
in a variable called names, let's go back and have a look.
We've got album, we've got a name there for our album
and then for our artist we're also using name,
so we're using the same name for both,
so the reason I'm mentioning that is the data binding
will work for both types of data.
Now this is probably a bit of a hack
so we wouldn't really do this in a real world application
we'd probably get around that by having multiple tables
each on a tab or perhaps have multiple tables
but only one visible at a time.
Another way of doing it could be to use a combo box,
like the sequel browser application does,
and display the selected table.
So there's many ways we could handle this
but since this is at a UI lecture
we're gonna go down the easiest route.
Now we could refactor the effects ID and the artist table
variable names but we're going to leave them
as they are as well.
Now what we do need though is an event handler
that will respond when the user selects an artist
and clicks the show albums artist button
in our GUI interface.
Now that's going to be very similar to the one displaying
the lists of artist but since we're only going to call up
when the buttons pressed, we can go ahead and use
an anonymous task for this.
So let's go back to our controller,
so were going to go ahead and write the method now,
the event handler for the query albums by artist button,
so we're going to do public, void,
list albums,
for artist,
and we'll annotate that,
fix it all,
now the actual code is going too,
we're going to start off
by getting the selected items,
so final artists,
artist is equal to artists,
artist table dot get selection model,
dot get selected item,
if artist is not a good to know,
so it's valid somethings been selected,
actually rather, what we wanna do is we wanna test
if it is null.
So if artist is null,
then we want to know about it at that point
we're gonna put no artist selected,
so that would indicate as a problem,
we're gonna return,
because we need that valid artist to be able to take this
process further.
Alright, so we're going to go ahead now and use
an anonymous task for this.
So task,
and we want an observable list,
of album objects,
and collect task is equal to new task,
and it's observable list again
of album objects.
And then we want to override, a cool method there,
so I'm just going to select that,
and we're not going to return null,
in fact we're going to return is effects collections
dot, it's observable array list.
It's going to be data source, dot an instance,
dot and our method query albums
for artist ID
and it's going to be artist dot get ID,
the ID that we need to past-a this method
that we've just retrieved.
Like so.
And we also put a semi colon there to make that valid,
so that's giving us the list of albums for the artist
and we then want a need to do artist table,
dot items property, dot bind,
task, dot value property,
and new thread,
task dot start.
Now we need to fix this up as well
and what we need to do first of all
we're going to go back in and add a button
to our FXML but then we also after that
need to go through and make the same changes to album
that we have previously for artist,
in other words making the, over here,
we need to use these simple, these properties,
in our album object.
So we'll do that shortly,
but we'll start with the button,
so we go back to the main FXML
and what we want to do is add for the show albums
artist button, up here we want to change this button
to little bit, we're going to,
leave the max with the pneumonic pausing
and the text should be before.
And at the start we just want to add on action
equals, and it's going to be equal to our method
we just wrote in that controller, so list,
albums for artist.
Okay, so it knows which code needs to be executed.
Alright now that we've added that,
we then need to swing back over as I mentioned
to our album class and make those changes as well.
So we're going to start by changing these three entries
the ID the name and the artist ID to the properties,
different properties so it's going to be simple,
integer property for our ID
and also for our artist ID.
Simple,
integer property and then instead of a string
it's going to be simple string property.
For the name, we also need a constructor now,
constructor and we're going to select none there.
Because you want an empty constructor
so that we can initialise these three objects now.
So we're going to start with this dot ID equals new.
Simple integer property,
this dot name is equal to new,
simple string property,
and this dot artist ID equals new, simple integer property.
Alright so that's them initialised.
And then our getters and setters we just need to change
code slightly, so that's going to be ID dot get
instead of just ID.
So we're still returning the primitive type here
into this case, and then luck was for the setters.
We're gonna change that slot listed ID dot set.
ID.
And for the name, we're gonna return name dot get.
It's dot name dot set.
Name, and then for the artist ID return artist ID
dot get and this dot artist dot set,
alright, so that should be it.
So we're going back to controller,
alright so we've still got this error
and if we just have a look at that error again
you can see at the moment
that the property cannot be applied
from the sample dot model dot artist to the java effects
dot beans dot property dot read only object property
and then it's looking for album.
That's because of this definition up here on line 15,
because we made it specific to an artist
so as I said this is a little bit of a hack
but to get around it, we just remove that type
so we just basically being a bit more generic
with what we're saying in the table view contains,
the error then disappears
and again because we've got the same
property name in album, name here which we're gonna be
displaying on the screen and also for artist,
this will then work.
So we go back and now run this,
we should firstly find the list of artist comes up correctly
so we can see that's working okay.
But now if we select a particular artist,
say AC/DC for example,
and click on show albums for the artist,
we get a list of in this case two entries or AC/DC
for the albums so clearly that's working okay.
Now obviously we haven't implemented the code here
for list artist and when I click on that nothing happens
so let's work on getting that operational in the next video.