aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/query/IO.java
blob: 6fe9390ee12bcce3580f51b1d8c2e90e077c401e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package eu.equalparts.cardbase.query;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import eu.equalparts.cardbase.data.FullCardSet;
import eu.equalparts.cardbase.data.Cardbase;
import eu.equalparts.cardbase.data.CardSet;

/**
 * Class responsible for all I/O operations, such as fetching content from remote servers, reading from
 * and writing to files.
 * <br>
 * All relevant methods here are static, this class should not be instantiated.
 * 
 * @author Eduardo Pedroni
 */
public class IO {
	
	/**
	 * The base URL from where the information is fetched.
	 */
	private static final String BASE_URL = "http://mtgjson.com/json/";
	/**
	 * The URL where the complete list of sets is fetched.
	 */
	private static final String SETS_URL = BASE_URL + "SetList.json";
	/**
	 * The Jackson ObjectMapper which parses fetched JSON files.
	 */
	private static final ObjectMapper mapper = createMapper();

	/**
	 * Private constructor, this class is not to be instantiated.
	 */
	private IO() {}
	
	/**
	 * Instantiate and configure Jackson mapper statically. 
	 * 
	 * @return the {@code ObjectMapper}, ready to use.
	 */
	private static ObjectMapper createMapper() {
		ObjectMapper objectMapper = new ObjectMapper();
		// TODO decide what to do about this
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		return objectMapper;
	}

	/**
	 * Fetches a complete set by code, where the code is a short string determined by WotC.
	 * The full list of valid codes can be acquired with {@code IO.getCardSetList()}.
	 * 
	 * @param setCode the code of the set to be fetched.
	 * @return the complete specified set in a {@code FullCardSet} object.
	 * 
	 * @throws JsonParseException if underlying input contains invalid content of type JsonParser supports (JSON for default case).
	 * @throws JsonMappingException if the input JSON structure does not match structure expected for result type (or has other mismatch issues).
	 * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
	 */
	public static FullCardSet getCardSet(String setCode) throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(new URL(BASE_URL + setCode + ".json"), FullCardSet.class);
	}
	
	/**
	 * @return a list of all card sets in the form of {@code CardSet} objects.
	 * 
	 * @throws JsonParseException if underlying input contains invalid content of type JsonParser supports (JSON for default case).
	 * @throws JsonMappingException if the input JSON structure does not match structure expected for result type (or has other mismatch issues).
	 * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
	 */
	public static ArrayList<CardSet> getCardSetList() throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(new URL(SETS_URL), new TypeReference<ArrayList<CardSet>>() {});
	}
	
	/**
	 * Attemps to the read the specified file into a {@code Cardbase.} Exceptions are thrown as outlined below.
	 * 
	 * @param file the file to read.
	 * @return a {@code Cardbase} object equivalent to the given file.
	 * 
	 * @throws JsonParseException if underlying input contains invalid content of type JsonParser supports (JSON for default case).
	 * @throws JsonMappingException if the input JSON structure does not match structure expected for result type (or has other mismatch issues).
	 * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
	 */
	public static Cardbase readCardbase(File file) throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(file, Cardbase.class);
	}
	
	/**
	 * Writes the provided {@code Cardbase} to the provided file in JSON format.
	 * 
	 * @param file the file to which to write the {@code Cardbase}.
	 * @param cardbase the {@code Cardbase} to write out.
	 * 
	 * @throws JsonGenerationException if the data structure given does not generate valid JSON.
	 * @throws JsonMappingException if the data structure given does not generate valid JSON as well?
	 * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
	 */
	public static void writeCardbase(File file, Cardbase cardbase) throws JsonGenerationException, JsonMappingException, IOException {
		mapper.writeValue(file, cardbase);
	}
	
}