parent
56c3bfb9cb
commit
92c056ec0b
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 YAMJ Members
|
||||||
|
* http://code.google.com/p/moviejukebox/people/list
|
||||||
|
*
|
||||||
|
* Web: http://code.google.com/p/moviejukebox/
|
||||||
|
*
|
||||||
|
* This software is licensed under a Creative Commons License
|
||||||
|
* See this page: http://code.google.com/p/moviejukebox/wiki/License
|
||||||
|
*
|
||||||
|
* For any reuse or distribution, you must make clear to others the
|
||||||
|
* license terms of this work.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.moviejukebox.themoviedb.model;
|
||||||
|
|
||||||
|
public class Filmography {
|
||||||
|
private String url;
|
||||||
|
private String name;
|
||||||
|
private String department;
|
||||||
|
private String character;
|
||||||
|
private String job;
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDepartment() {
|
||||||
|
return department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepartment(String department) {
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCharacter() {
|
||||||
|
return character;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCharacter(String character) {
|
||||||
|
this.character = character;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getJob() {
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJob(String job) {
|
||||||
|
this.job = job;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 YAMJ Members
|
||||||
|
* http://code.google.com/p/moviejukebox/people/list
|
||||||
|
*
|
||||||
|
* Web: http://code.google.com/p/moviejukebox/
|
||||||
|
*
|
||||||
|
* This software is licensed under a Creative Commons License
|
||||||
|
* See this page: http://code.google.com/p/moviejukebox/wiki/License
|
||||||
|
*
|
||||||
|
* For any reuse or distribution, you must make clear to others the
|
||||||
|
* license terms of this work.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.moviejukebox.themoviedb.tools;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic set of routines to process the DOM model data
|
||||||
|
* @author Stuart
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DOMHelper {
|
||||||
|
/**
|
||||||
|
* Gets the string value of the tag element name passed
|
||||||
|
* @param element
|
||||||
|
* @param tagName
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getValueFromElement(Element element, String tagName) {
|
||||||
|
String returnValue = "";
|
||||||
|
|
||||||
|
try {
|
||||||
|
NodeList elementNodeList = element.getElementsByTagName(tagName);
|
||||||
|
Element tagElement = (Element) elementNodeList.item(0);
|
||||||
|
NodeList tagNodeList = tagElement.getChildNodes();
|
||||||
|
returnValue = ((Node) tagNodeList.item(0)).getNodeValue();
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
return returnValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a DOM document from the supplied URL
|
||||||
|
* @param url
|
||||||
|
* @return
|
||||||
|
* @throws MalformedURLException
|
||||||
|
* @throws IOException
|
||||||
|
* @throws ParserConfigurationException
|
||||||
|
* @throws SAXException
|
||||||
|
*/
|
||||||
|
public static Document getEventDocFromUrl(String url) throws MalformedURLException, IOException, ParserConfigurationException, SAXException {
|
||||||
|
InputStream in = (new URL(url)).openStream();
|
||||||
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||||
|
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||||
|
Document doc = db.parse(in);
|
||||||
|
doc.getDocumentElement().normalize();
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,276 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 YAMJ Members
|
||||||
|
* http://code.google.com/p/moviejukebox/people/list
|
||||||
|
*
|
||||||
|
* Web: http://code.google.com/p/moviejukebox/
|
||||||
|
*
|
||||||
|
* This software is licensed under a Creative Commons License
|
||||||
|
* See this page: http://code.google.com/p/moviejukebox/wiki/License
|
||||||
|
*
|
||||||
|
* For any reuse or distribution, you must make clear to others the
|
||||||
|
* license terms of this work.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.moviejukebox.themoviedb.tools;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
import com.moviejukebox.themoviedb.TheMovieDb;
|
||||||
|
import com.moviejukebox.themoviedb.model.Artwork;
|
||||||
|
import com.moviejukebox.themoviedb.model.Category;
|
||||||
|
import com.moviejukebox.themoviedb.model.Country;
|
||||||
|
import com.moviejukebox.themoviedb.model.Filmography;
|
||||||
|
import com.moviejukebox.themoviedb.model.MovieDB;
|
||||||
|
import com.moviejukebox.themoviedb.model.Person;
|
||||||
|
|
||||||
|
public class DOMParser {
|
||||||
|
static Logger logger = TheMovieDb.getLogger();
|
||||||
|
|
||||||
|
public static MovieDB parseMovieInfo(Document doc) {
|
||||||
|
// Borrowed from http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html
|
||||||
|
MovieDB movie = null;
|
||||||
|
NodeList movieNodeList, subNodeList;
|
||||||
|
Node movieNode, subNode;
|
||||||
|
Element movieElement, subElement;
|
||||||
|
|
||||||
|
try {
|
||||||
|
movie = new MovieDB();
|
||||||
|
movieNodeList = doc.getElementsByTagName("movie");
|
||||||
|
|
||||||
|
// Only get the first movie from the list
|
||||||
|
movieNode = movieNodeList.item(0);
|
||||||
|
|
||||||
|
if (movieNode == null) {
|
||||||
|
logger.finest("Movie not found");
|
||||||
|
return movie;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (movieNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
movieElement = (Element) movieNode;
|
||||||
|
|
||||||
|
movie.setTitle(DOMHelper.getValueFromElement(movieElement, "name"));
|
||||||
|
movie.setPopularity(DOMHelper.getValueFromElement(movieElement, "popularity"));
|
||||||
|
movie.setType(DOMHelper.getValueFromElement(movieElement, "type"));
|
||||||
|
movie.setId(DOMHelper.getValueFromElement(movieElement, "id"));
|
||||||
|
movie.setImdb(DOMHelper.getValueFromElement(movieElement, "imdb_id"));
|
||||||
|
movie.setUrl(DOMHelper.getValueFromElement(movieElement, "url"));
|
||||||
|
movie.setOverview(DOMHelper.getValueFromElement(movieElement, "overview"));
|
||||||
|
movie.setRating(DOMHelper.getValueFromElement(movieElement, "rating"));
|
||||||
|
movie.setReleaseDate(DOMHelper.getValueFromElement(movieElement, "released"));
|
||||||
|
movie.setRuntime(DOMHelper.getValueFromElement(movieElement, "runtime"));
|
||||||
|
movie.setBudget(DOMHelper.getValueFromElement(movieElement, "budget"));
|
||||||
|
movie.setRevenue(DOMHelper.getValueFromElement(movieElement, "revenue"));
|
||||||
|
movie.setHomepage(DOMHelper.getValueFromElement(movieElement, "homepage"));
|
||||||
|
movie.setTrailer(DOMHelper.getValueFromElement(movieElement, "trailer"));
|
||||||
|
|
||||||
|
// Process the "categories"
|
||||||
|
subNodeList = doc.getElementsByTagName("categories");
|
||||||
|
|
||||||
|
for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) {
|
||||||
|
subNode = subNodeList.item(nodeLoop);
|
||||||
|
if (subNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
subElement = (Element) subNode;
|
||||||
|
Category category = new Category();
|
||||||
|
|
||||||
|
category.setType(DOMHelper.getValueFromElement(subElement, "type"));
|
||||||
|
category.setUrl(DOMHelper.getValueFromElement(subElement, "url"));
|
||||||
|
category.setName(DOMHelper.getValueFromElement(subElement, "name"));
|
||||||
|
|
||||||
|
movie.addCategory(category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the "countries"
|
||||||
|
subNodeList = doc.getElementsByTagName("countries");
|
||||||
|
|
||||||
|
for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) {
|
||||||
|
subNode = subNodeList.item(nodeLoop);
|
||||||
|
if (subNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
subElement = (Element) subNode;
|
||||||
|
Country country = new Country();
|
||||||
|
|
||||||
|
country.setCode(DOMHelper.getValueFromElement(subElement, "code"));
|
||||||
|
country.setUrl(DOMHelper.getValueFromElement(subElement, "url"));
|
||||||
|
country.setName(DOMHelper.getValueFromElement(subElement, "name"));
|
||||||
|
|
||||||
|
movie.addProductionCountry(country);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the "cast"
|
||||||
|
subNodeList = doc.getElementsByTagName("cast");
|
||||||
|
|
||||||
|
for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) {
|
||||||
|
subNode = subNodeList.item(nodeLoop);
|
||||||
|
if (subNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
subElement = (Element) subNode;
|
||||||
|
Person person = new Person();
|
||||||
|
|
||||||
|
person.setUrl(DOMHelper.getValueFromElement(subElement, "url"));
|
||||||
|
person.setName(DOMHelper.getValueFromElement(subElement, "name"));
|
||||||
|
person.setJob(DOMHelper.getValueFromElement(subElement, "job"));
|
||||||
|
person.setCharacter(DOMHelper.getValueFromElement(subElement, "character"));
|
||||||
|
person.setId(DOMHelper.getValueFromElement(subElement, "id"));
|
||||||
|
|
||||||
|
movie.addPerson(person);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This processes the image elements. There are two formats to deal with:
|
||||||
|
* Movie.imdbLookup, Movie.getInfo & Movie.search:
|
||||||
|
* <images>
|
||||||
|
* <image type="poster" size="original" url="http://images.themoviedb.org/posters/60366/Fight_Club.jpg" id="60366"/>
|
||||||
|
* <image type="backdrop" size="original" url="http://images.themoviedb.org/backdrops/56444/bscap00144.jpg" id="56444"/>
|
||||||
|
* </images>
|
||||||
|
*
|
||||||
|
* Movie.getImages:
|
||||||
|
* <images>
|
||||||
|
* <poster id="17066">
|
||||||
|
* <image url="http://images.themoviedb.org/posters/17066/Fight_Club.jpg" size="original"/>
|
||||||
|
* <image url="http://images.themoviedb.org/posters/17066/Fight_Club_thumb.jpg" size="thumb"/>
|
||||||
|
* <image url="http://images.themoviedb.org/posters/17066/Fight_Club_cover.jpg" size="cover"/>
|
||||||
|
* <image url="http://images.themoviedb.org/posters/17066/Fight_Club_mid.jpg" size="mid"/>
|
||||||
|
* </poster>
|
||||||
|
* <backdrop id="18593">
|
||||||
|
* <image url="http://images.themoviedb.org/backdrops/18593/Fight_Club_on_the_street1.jpg" size="original"/>
|
||||||
|
* <image url="http://images.themoviedb.org/backdrops/18593/Fight_Club_on_the_street1_poster.jpg" size="poster"/>
|
||||||
|
* <image url="http://images.themoviedb.org/backdrops/18593/Fight_Club_on_the_street1_thumb.jpg" size="thumb"/>
|
||||||
|
* </backdrop>
|
||||||
|
* </images>
|
||||||
|
*/
|
||||||
|
subNodeList = doc.getElementsByTagName("images");
|
||||||
|
|
||||||
|
for (int nodeLoop = 0; nodeLoop < subNodeList.getLength(); nodeLoop++) {
|
||||||
|
subNode = subNodeList.item(nodeLoop);
|
||||||
|
|
||||||
|
if (subNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
|
||||||
|
NodeList artworkNodeList = subNode.getChildNodes();
|
||||||
|
for (int artworkLoop = 0; artworkLoop < artworkNodeList.getLength(); artworkLoop++) {
|
||||||
|
Node artworkNode = artworkNodeList.item(artworkLoop);
|
||||||
|
if (artworkNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
subElement = (Element) artworkNode;
|
||||||
|
|
||||||
|
if (subElement.getNodeName().equalsIgnoreCase("image")) {
|
||||||
|
// This is the format used in Movie.imdbLookup, Movie.getInfo & Movie.search
|
||||||
|
Artwork artwork = new Artwork();
|
||||||
|
artwork.setType(subElement.getAttribute("type"));
|
||||||
|
artwork.setSize(subElement.getAttribute("size"));
|
||||||
|
artwork.setUrl(subElement.getAttribute("url"));
|
||||||
|
artwork.setId(subElement.getAttribute("id"));
|
||||||
|
movie.addArtwork(artwork);
|
||||||
|
} else if (subElement.getNodeName().equalsIgnoreCase("backdrop") ||
|
||||||
|
subElement.getNodeName().equalsIgnoreCase("poster")) {
|
||||||
|
// This is the format used in Movie.getImages
|
||||||
|
String artworkId = subElement.getAttribute("id");
|
||||||
|
String artworkType = subElement.getNodeName();
|
||||||
|
|
||||||
|
// We need to decode and loop round the child nodes to get the data
|
||||||
|
NodeList imageNodeList = subElement.getChildNodes();
|
||||||
|
for (int imageLoop = 0; imageLoop < imageNodeList.getLength(); imageLoop++) {
|
||||||
|
Node imageNode = imageNodeList.item(imageLoop);
|
||||||
|
if (imageNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element imageElement = (Element) imageNode;
|
||||||
|
Artwork artwork = new Artwork();
|
||||||
|
artwork.setId(artworkId);
|
||||||
|
artwork.setType(artworkType);
|
||||||
|
artwork.setUrl(imageElement.getAttribute("url"));
|
||||||
|
artwork.setSize(imageElement.getAttribute("size"));
|
||||||
|
movie.addArtwork(artwork);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// This is a classic, it should never happen error
|
||||||
|
logger.severe("UNKNOWN Image type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception error) {
|
||||||
|
logger.severe("ERROR: " + error.getMessage());
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
return movie;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Person parsePersonInfo(Document doc) {
|
||||||
|
Person person = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
person = new Person();
|
||||||
|
NodeList personNodeList = doc.getElementsByTagName("person");
|
||||||
|
|
||||||
|
// Only get the first movie from the list
|
||||||
|
Node personNode = personNodeList.item(0);
|
||||||
|
|
||||||
|
if (personNode == null) {
|
||||||
|
logger.finest("Person not found");
|
||||||
|
return person;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (personNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element personElement = (Element) personNode;
|
||||||
|
|
||||||
|
person.setName(DOMHelper.getValueFromElement(personElement, "name"));
|
||||||
|
person.setId(DOMHelper.getValueFromElement(personElement, "id"));
|
||||||
|
person.setBiography(DOMHelper.getValueFromElement(personElement, "biography"));
|
||||||
|
person.setKnownMovies(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "known_movies")));
|
||||||
|
person.setBirthday(DOMHelper.getValueFromElement(personElement, "birthday"));
|
||||||
|
person.setBirthPlace(DOMHelper.getValueFromElement(personElement, "birthplace"));
|
||||||
|
person.setUrl(DOMHelper.getValueFromElement(personElement, "url"));
|
||||||
|
person.setVersion(Integer.parseInt(DOMHelper.getValueFromElement(personElement, "version")));
|
||||||
|
person.setLastModifiedAt(DOMHelper.getValueFromElement(personElement, "last_modified_at"));
|
||||||
|
|
||||||
|
NodeList artworkNodeList = doc.getElementsByTagName("image");
|
||||||
|
for (int nodeLoop = 0; nodeLoop < artworkNodeList.getLength(); nodeLoop++) {
|
||||||
|
Node artworkNode = artworkNodeList.item(nodeLoop);
|
||||||
|
if (artworkNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element artworkElement = (Element) artworkNode;
|
||||||
|
Artwork artwork = new Artwork();
|
||||||
|
artwork.setType(artworkElement.getAttribute("type"));
|
||||||
|
artwork.setUrl(artworkElement.getAttribute("url"));
|
||||||
|
artwork.setSize(artworkElement.getAttribute("size"));
|
||||||
|
artwork.setId(artworkElement.getAttribute("id"));
|
||||||
|
person.addArtwork(artwork);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeList filmNodeList = doc.getElementsByTagName("movie");
|
||||||
|
for (int nodeLoop = 0; nodeLoop < filmNodeList.getLength(); nodeLoop++) {
|
||||||
|
Node filmNode = filmNodeList.item(nodeLoop);
|
||||||
|
if (filmNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element filmElement = (Element) filmNode;
|
||||||
|
Filmography film = new Filmography();
|
||||||
|
|
||||||
|
film.setCharacter(filmElement.getAttribute("character"));
|
||||||
|
film.setDepartment(filmElement.getAttribute("department"));
|
||||||
|
film.setId(filmElement.getAttribute("id"));
|
||||||
|
film.setJob(filmElement.getAttribute("job"));
|
||||||
|
film.setName(filmElement.getAttribute("name"));
|
||||||
|
film.setUrl(filmElement.getAttribute("url"));
|
||||||
|
|
||||||
|
person.addFilm(film);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception error) {
|
||||||
|
logger.severe("ERROR: " + error.getMessage());
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return person;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Person parsePersonGetVersion(Document doc) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,176 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 YAMJ Members
|
||||||
|
* http://code.google.com/p/moviejukebox/people/list
|
||||||
|
*
|
||||||
|
* Web: http://code.google.com/p/moviejukebox/
|
||||||
|
*
|
||||||
|
* This software is licensed under a Creative Commons License
|
||||||
|
* See this page: http://code.google.com/p/moviejukebox/wiki/License
|
||||||
|
*
|
||||||
|
* For any reuse or distribution, you must make clear to others the
|
||||||
|
* license terms of this work.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.moviejukebox.themoviedb.tools;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.moviejukebox.themoviedb.model.Artwork;
|
||||||
|
|
||||||
|
public class ModelTools {
|
||||||
|
private List<Artwork> artwork = new ArrayList<Artwork>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a piece of artwork to the artwork array
|
||||||
|
* @param artworkType must be one of Artwork.ARTWORK_TYPES
|
||||||
|
* @param artworkSize must be one of Artwork.ARTWORK_SIZES
|
||||||
|
* @param artworkUrl
|
||||||
|
* @param posterId
|
||||||
|
*/
|
||||||
|
public void addArtwork(String artworkType, String artworkSize, String artworkUrl, String artworkId) {
|
||||||
|
if (validateElement(Artwork.ARTWORK_TYPES, artworkType) && validateElement(Artwork.ARTWORK_SIZES, artworkSize)) {
|
||||||
|
Artwork newArtwork = new Artwork();
|
||||||
|
|
||||||
|
newArtwork.setType(artworkType);
|
||||||
|
newArtwork.setSize(artworkSize);
|
||||||
|
newArtwork.setUrl(artworkUrl);
|
||||||
|
newArtwork.setId(artworkId);
|
||||||
|
|
||||||
|
artwork.add(newArtwork);
|
||||||
|
Collections.sort(artwork);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a piece of artwork to the artwork array
|
||||||
|
* @param newArtwork an Artwork object to add to the array
|
||||||
|
*/
|
||||||
|
public void addArtwork(Artwork newArtwork) {
|
||||||
|
if (validateElement(Artwork.ARTWORK_TYPES, newArtwork.getType()) && validateElement(Artwork.ARTWORK_SIZES, newArtwork.getSize())) {
|
||||||
|
artwork.add(newArtwork);
|
||||||
|
Collections.sort(artwork);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first artwork that matches the Type and Size
|
||||||
|
* @param artworkType
|
||||||
|
* @param artworkSize
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Artwork getFirstArtwork(String artworkType, String artworkSize) {
|
||||||
|
return getArtwork(artworkType, artworkSize, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check to see if element is contained in elementArray
|
||||||
|
* @param elementArray
|
||||||
|
* @param element
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean validateElement(String[] elementArray, String element) {
|
||||||
|
boolean valid = false;
|
||||||
|
|
||||||
|
for (String arrayEntry : elementArray) {
|
||||||
|
if (arrayEntry.equalsIgnoreCase(element)) {
|
||||||
|
valid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all the artwork for a movie
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<Artwork> getArtwork() {
|
||||||
|
return artwork;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the artwork of a specific type
|
||||||
|
* @param artworkType
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<Artwork> getArtwork(String artworkType) {
|
||||||
|
// Validate the Type and Size arguments
|
||||||
|
if (!validateElement(Artwork.ARTWORK_TYPES, artworkType)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Artwork> artworkList = new ArrayList<Artwork>();
|
||||||
|
|
||||||
|
for (Artwork singleArtwork : artwork) {
|
||||||
|
if (singleArtwork.getType().equalsIgnoreCase(artworkType)) {
|
||||||
|
artworkList.add(singleArtwork);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return artworkList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all artwork of a specific Type and Size
|
||||||
|
* @param artworkType
|
||||||
|
* @param artworkSize
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<Artwork> getArtwork(String artworkType, String artworkSize) {
|
||||||
|
List<Artwork> artworkList = new ArrayList<Artwork>();
|
||||||
|
// Validate the Type and Size arguments
|
||||||
|
if (!validateElement(Artwork.ARTWORK_TYPES, artworkType) && !validateElement(Artwork.ARTWORK_SIZES, artworkSize)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Artwork singleArtwork : artwork) {
|
||||||
|
if (singleArtwork.getType().equalsIgnoreCase(artworkType) && singleArtwork.getSize().equalsIgnoreCase(artworkSize)) {
|
||||||
|
artworkList.add(singleArtwork);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return artworkList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a specific artwork entry for a Type & Size
|
||||||
|
* @param artworkType
|
||||||
|
* @param artworkSize
|
||||||
|
* @param artworkNumber
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Artwork getArtwork(String artworkType, String artworkSize, int artworkNumber) {
|
||||||
|
// Validate the Type and Size arguments
|
||||||
|
if (!validateElement(Artwork.ARTWORK_TYPES, artworkType) && !validateElement(Artwork.ARTWORK_SIZES, artworkSize)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the number
|
||||||
|
if (artworkNumber <= 0) {
|
||||||
|
artworkNumber = 0;
|
||||||
|
} else {
|
||||||
|
// Artwork elements start at 0 (Zero)
|
||||||
|
artworkNumber -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Artwork> artworkList = getArtwork(artworkType, artworkSize);
|
||||||
|
|
||||||
|
int artworkCount = artworkList.size();
|
||||||
|
if (artworkCount < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the number requested is greater than the array size, loop around until it's within scope
|
||||||
|
while (artworkNumber > artworkCount) {
|
||||||
|
artworkNumber = artworkNumber - artworkCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return artworkList.get(artworkNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue