|
12 | 12 |
|
13 | 13 | import com.google.gson.*; |
14 | 14 |
|
| 15 | +import java.util.Locale; |
| 16 | + |
15 | 17 | import javax.swing.table.DefaultTableModel; |
16 | 18 | import java.lang.reflect.Array; |
17 | 19 |
|
@@ -109,6 +111,94 @@ public MusicBrainzCDStub[] getCDStubs() { |
109 | 111 | }, new MusicBrainzCDStub[0]); |
110 | 112 | } |
111 | 113 |
|
| 114 | + /** |
| 115 | + * Gets Artists from the JSON. |
| 116 | + * @return An array of the artists. |
| 117 | + */ |
| 118 | + public MusicBrainzArtist[] getArtists() { |
| 119 | + return parseJsonArray("artists", jsonObject -> { |
| 120 | + String name = null; |
| 121 | + if (jsonHasAndIsNotNull(jsonObject, "name")) { |
| 122 | + JsonElement nameElement = jsonObject.get("name"); |
| 123 | + if (nameElement != null && !nameElement.isJsonNull()) { |
| 124 | + name = nameElement.getAsString(); |
| 125 | + } |
| 126 | + } |
| 127 | + String type = jsonHasAndIsNotNull(jsonObject, "type") ? jsonObject.get("type").getAsString() : null; |
| 128 | + |
| 129 | + // THE CODE BELOW MAY CAUSE FUTURE BUGS, THOUGH UNLIKELY. |
| 130 | + // Keep in mind that the life-span.begin JSON for artists is |
| 131 | + // either the date organized or the birthdate, depending on if |
| 132 | + // the artist is a group or a person. |
| 133 | + String birthDate; |
| 134 | + String organizedDate; |
| 135 | + if (jsonHasAndIsNotNull(jsonObject, "life-span")) { |
| 136 | + JsonObject lifeSpanObject = jsonObject.getAsJsonObject("life-span"); |
| 137 | + if("Group".equals(type)) { |
| 138 | + organizedDate = jsonHasAndIsNotNull(lifeSpanObject, "begin") ? lifeSpanObject.get("begin").getAsString() : null; |
| 139 | + birthDate = null; |
| 140 | + } else if ("Person".equals(type)) { |
| 141 | + birthDate = jsonHasAndIsNotNull(lifeSpanObject, "begin") ? lifeSpanObject.get("begin").getAsString() : null; |
| 142 | + organizedDate = null; |
| 143 | + } else { |
| 144 | + birthDate = null; |
| 145 | + organizedDate = null; |
| 146 | + } |
| 147 | + } else { |
| 148 | + birthDate = null; |
| 149 | + organizedDate = null; |
| 150 | + } |
| 151 | + |
| 152 | + String id = jsonHasAndIsNotNull(jsonObject, "id") ? jsonObject.get("id").getAsString() : null; |
| 153 | + String sortName = jsonHasAndIsNotNull(jsonObject, "sort-name") ? jsonObject.get("sort-name").getAsString() : null; |
| 154 | + // Groups do not have genders in this API. |
| 155 | + Gender gender = Gender.fromString(jsonHasAndIsNotNull(jsonObject, "gender") ? jsonObject.get("gender").getAsString() : null); |
| 156 | + |
| 157 | + String disambiguation; |
| 158 | + // The code below MAY also be problematic, although VERY unlikely. |
| 159 | + if (jsonHasAndIsNotNull(jsonObject, "tags")) { |
| 160 | + JsonArray tagsArray = jsonObject.getAsJsonArray("tags"); |
| 161 | + if (!tagsArray.isEmpty()) { |
| 162 | + JsonObject firstTagObject = tagsArray.get(0).getAsJsonObject(); |
| 163 | + disambiguation = jsonHasAndIsNotNull(firstTagObject, "name") ? firstTagObject.get("name").getAsString() : null; |
| 164 | + int highestCount = 0; |
| 165 | + for (JsonElement tagElement : tagsArray) { |
| 166 | + JsonObject tagObject = tagElement.getAsJsonObject(); |
| 167 | + int count = jsonHasAndIsNotNull(tagObject, "count") ? tagObject.get("count").getAsInt() : -1; |
| 168 | + if (count > highestCount) { |
| 169 | + highestCount = count; |
| 170 | + disambiguation = jsonHasAndIsNotNull(tagObject, "name") ? tagObject.get("name").getAsString() : null; |
| 171 | + } |
| 172 | + } |
| 173 | + } else { |
| 174 | + disambiguation = null; |
| 175 | + } |
| 176 | + } else { |
| 177 | + disambiguation = null; |
| 178 | + } |
| 179 | + |
| 180 | + String lifeSpan; |
| 181 | + if (jsonHasAndIsNotNull(jsonObject, "life-span")) { |
| 182 | + JsonObject lifeSpanObject = jsonObject.getAsJsonObject("life-span"); |
| 183 | + if (jsonHasAndIsNotNull(lifeSpanObject, "begin") && jsonHasAndIsNotNull(lifeSpanObject, "end")) { |
| 184 | + lifeSpan = lifeSpanObject.get("begin").getAsString() + " - " + lifeSpanObject.get("end").getAsString(); |
| 185 | + } else { |
| 186 | + lifeSpan = null; |
| 187 | + } |
| 188 | + } else { |
| 189 | + lifeSpan = null; |
| 190 | + } |
| 191 | + |
| 192 | + String country = jsonHasAndIsNotNull(jsonObject, "country") ? jsonObject.get("country").getAsString() : null; |
| 193 | + if (country != null) { |
| 194 | + Locale locale = new Locale.Builder().setRegion(country).build(); |
| 195 | + country = locale.getDisplayCountry(); |
| 196 | + } |
| 197 | + |
| 198 | + return new MusicBrainzArtist(name, organizedDate, birthDate, id, sortName, gender, type, disambiguation, lifeSpan, country); |
| 199 | + }, new MusicBrainzArtist[0]); |
| 200 | + } |
| 201 | + |
112 | 202 | /** |
113 | 203 | * Creates a table model from an array of items. |
114 | 204 | * @param items The array of items. Usually a MusicBrainzRelease, MusicBrainzCDStub, etc. |
@@ -166,6 +256,30 @@ public DefaultTableModel getCDStubsAsTableModel(MusicBrainzCDStub[] cdStubArray) |
166 | 256 | }); |
167 | 257 | } |
168 | 258 |
|
| 259 | + /** |
| 260 | + * Gets the artists as a table model. |
| 261 | + * @param artistArray The array of artists. |
| 262 | + * @return The table model. |
| 263 | + */ |
| 264 | + public DefaultTableModel getArtistsAsTableModel(MusicBrainzArtist[] artistArray) { |
| 265 | + String[] columnNames = {"Name", "Date Organised", "Birthdate", "Sort Name", "Gender", "Type", "Disambiguation", "Life Span", "Country", ""}; |
| 266 | + return createTableModel(artistArray, columnNames, item -> { |
| 267 | + MusicBrainzArtist artist = (MusicBrainzArtist) item; |
| 268 | + return new String[] { |
| 269 | + getOrDefault(artist.getName()), |
| 270 | + getOrDefault(artist.getDateOrganized()), |
| 271 | + getOrDefault(artist.getBirthDate()), |
| 272 | + getOrDefault(artist.getSortName()), |
| 273 | + getOrDefault(artist.getGender().toString().toLowerCase(Locale.ROOT)), |
| 274 | + getOrDefault(artist.getType()), |
| 275 | + getOrDefault(artist.getDisambiguation()), |
| 276 | + getOrDefault(artist.getLifeSpan()), |
| 277 | + getOrDefault(artist.getCountry()), |
| 278 | + "" |
| 279 | + }; |
| 280 | + }); |
| 281 | + } |
| 282 | + |
169 | 283 | /** |
170 | 284 | * Functional interface for extracting data from an item. |
171 | 285 | */ |
@@ -199,7 +313,7 @@ private boolean jsonHasAndIsNotNull(JsonObject jsonObject, String memberName) { |
199 | 313 | * @return The value or "n/a" if the value is null. |
200 | 314 | */ |
201 | 315 | private String getOrDefault(String value) { |
202 | | - return value != null ? value : "n/a"; |
| 316 | + return value != null ? value : ""; |
203 | 317 | } |
204 | 318 |
|
205 | 319 | @Override |
|
0 commit comments