[591] | 1 | package net.oni2.aeinstaller.backend.depot;
|
---|
| 2 |
|
---|
| 3 | import java.io.FileInputStream;
|
---|
| 4 | import java.io.FileNotFoundException;
|
---|
| 5 | import java.io.FileOutputStream;
|
---|
| 6 | import java.io.IOException;
|
---|
[621] | 7 | import java.io.UnsupportedEncodingException;
|
---|
| 8 | import java.net.UnknownHostException;
|
---|
[591] | 9 | import java.util.HashMap;
|
---|
| 10 | import java.util.HashSet;
|
---|
| 11 | import java.util.Vector;
|
---|
| 12 |
|
---|
| 13 | import net.oni2.aeinstaller.backend.Settings;
|
---|
| 14 | import net.oni2.aeinstaller.backend.Settings.Platform;
|
---|
| 15 | import net.oni2.aeinstaller.backend.depot.model.File;
|
---|
| 16 | import net.oni2.aeinstaller.backend.depot.model.Node;
|
---|
| 17 | import net.oni2.aeinstaller.backend.depot.model.NodeField_Body;
|
---|
| 18 | import net.oni2.aeinstaller.backend.depot.model.NodeField_Upload;
|
---|
| 19 | import net.oni2.aeinstaller.backend.depot.model.NodeMod;
|
---|
| 20 | import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
|
---|
| 21 | import net.oni2.aeinstaller.backend.depot.model.TaxonomyVocabulary;
|
---|
[600] | 22 | import net.oni2.aeinstaller.backend.mods.ECompatiblePlatform;
|
---|
[591] | 23 | import net.oni2.aeinstaller.backend.network.DrupalJSONQuery;
|
---|
| 24 |
|
---|
[621] | 25 | import org.apache.http.HttpResponse;
|
---|
| 26 | import org.apache.http.client.methods.HttpGet;
|
---|
| 27 | import org.apache.http.client.methods.HttpRequestBase;
|
---|
| 28 | import org.apache.http.impl.client.DefaultHttpClient;
|
---|
[591] | 29 | import org.json.JSONArray;
|
---|
| 30 | import org.json.JSONException;
|
---|
| 31 | import org.json.JSONObject;
|
---|
| 32 |
|
---|
| 33 | import com.thoughtworks.xstream.XStream;
|
---|
| 34 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * @author Christian Illy
|
---|
| 38 | */
|
---|
| 39 | public class DepotManager {
|
---|
| 40 | private static DepotManager instance = new DepotManager();
|
---|
| 41 |
|
---|
| 42 | private HashMap<Integer, TaxonomyVocabulary> taxonomyVocabulary = new HashMap<Integer, TaxonomyVocabulary>();
|
---|
| 43 | private HashMap<Integer, TaxonomyTerm> taxonomyTerms = new HashMap<Integer, TaxonomyTerm>();
|
---|
| 44 |
|
---|
| 45 | private HashMap<Integer, Node> nodes = new HashMap<Integer, Node>();
|
---|
| 46 | private HashMap<String, HashMap<Integer, Node>> nodesByType = new HashMap<String, HashMap<Integer, Node>>();
|
---|
| 47 |
|
---|
| 48 | private HashMap<Integer, File> files = new HashMap<Integer, File>();
|
---|
| 49 |
|
---|
| 50 | private int vocabId_type = -1;
|
---|
| 51 | private int vocabId_platform = -1;
|
---|
| 52 | private int vocabId_instmethod = -1;
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * @return Singleton instance
|
---|
| 56 | */
|
---|
| 57 | public static DepotManager getInstance() {
|
---|
| 58 | return instance;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Update local Depot information cache
|
---|
| 63 | *
|
---|
| 64 | * @param forceRefreshAll
|
---|
| 65 | * Force refreshing all data, even if it seems to be cached
|
---|
| 66 | * @param listener
|
---|
| 67 | * Listener for update status
|
---|
| 68 | */
|
---|
| 69 | public void updateInformation(boolean forceRefreshAll,
|
---|
| 70 | DepotCacheUpdateProgressListener listener) {
|
---|
| 71 | taxonomyTerms.clear();
|
---|
| 72 | taxonomyVocabulary.clear();
|
---|
| 73 |
|
---|
| 74 | nodes = new HashMap<Integer, Node>();
|
---|
| 75 | nodesByType = new HashMap<String, HashMap<Integer, Node>>();
|
---|
| 76 | files = new HashMap<Integer, File>();
|
---|
| 77 |
|
---|
| 78 | try {
|
---|
| 79 | JSONArray ja;
|
---|
| 80 | JSONObject jo;
|
---|
| 81 |
|
---|
[633] | 82 | ja = DrupalJSONQuery
|
---|
| 83 | .executeQuery("http://mods.oni2.net/jsoncache/vocabulary.json");
|
---|
| 84 | for (int i = 0; i < ja.length(); i++) {
|
---|
| 85 | jo = ja.getJSONObject(i);
|
---|
| 86 | TaxonomyVocabulary tv = new TaxonomyVocabulary(jo);
|
---|
| 87 | taxonomyVocabulary.put(tv.getVid(), tv);
|
---|
| 88 | }
|
---|
[591] | 89 |
|
---|
[633] | 90 | ja = DrupalJSONQuery
|
---|
| 91 | .executeQuery("http://mods.oni2.net/jsoncache/terms.json");
|
---|
| 92 | for (int i = 0; i < ja.length(); i++) {
|
---|
| 93 | jo = ja.getJSONObject(i);
|
---|
| 94 | TaxonomyTerm tt = new TaxonomyTerm(jo);
|
---|
| 95 | taxonomyTerms.put(tt.getTid(), tt);
|
---|
| 96 | }
|
---|
[591] | 97 |
|
---|
[633] | 98 | ja = DrupalJSONQuery
|
---|
| 99 | .executeQuery("http://mods.oni2.net/jsoncache/nodes.json");
|
---|
| 100 | for (int i = 0; i < ja.length(); i++) {
|
---|
| 101 | jo = ja.getJSONObject(i);
|
---|
[591] | 102 |
|
---|
[633] | 103 | int nid = jo.getInt("nid");
|
---|
[591] | 104 | String type = jo.getString("type");
|
---|
| 105 |
|
---|
| 106 | Node n = null;
|
---|
[592] | 107 | if (type.equalsIgnoreCase(DepotConfig.getNodeType_Mod()))
|
---|
[591] | 108 | n = new NodeMod(jo);
|
---|
| 109 | else
|
---|
| 110 | n = new Node(jo);
|
---|
| 111 |
|
---|
| 112 | nodes.put(nid, n);
|
---|
| 113 | if (!nodesByType.containsKey(type))
|
---|
| 114 | nodesByType.put(type, new HashMap<Integer, Node>());
|
---|
| 115 | nodesByType.get(type).put(nid, n);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[633] | 118 | ja = DrupalJSONQuery
|
---|
| 119 | .executeQuery("http://mods.oni2.net/jsoncache/files.json");
|
---|
| 120 | for (int i = 0; i < ja.length(); i++) {
|
---|
| 121 | jo = ja.getJSONObject(i);
|
---|
[591] | 122 |
|
---|
[633] | 123 | int fid = jo.getInt("fid");
|
---|
[591] | 124 |
|
---|
| 125 | File f = new File(jo);
|
---|
| 126 | files.put(fid, f);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[592] | 129 | vocabId_type = getVocabulary(
|
---|
| 130 | DepotConfig.getVocabularyName_ModType()).getVid();
|
---|
| 131 | vocabId_platform = getVocabulary(
|
---|
| 132 | DepotConfig.getVocabularyName_Platform()).getVid();
|
---|
| 133 | vocabId_instmethod = getVocabulary(
|
---|
| 134 | DepotConfig.getVocabularyName_InstallType()).getVid();
|
---|
[591] | 135 | } catch (JSONException e) {
|
---|
| 136 | e.printStackTrace();
|
---|
| 137 | } catch (Exception e) {
|
---|
| 138 | System.err.println(e.getMessage());
|
---|
| 139 | e.printStackTrace();
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
[621] | 144 | * @return Can we connect to the Depot?
|
---|
| 145 | */
|
---|
| 146 | public boolean checkConnection() {
|
---|
| 147 | HttpRequestBase httpQuery = null;
|
---|
| 148 |
|
---|
| 149 | try {
|
---|
| 150 | DefaultHttpClient httpclient = new DefaultHttpClient();
|
---|
| 151 | httpQuery = new HttpGet(DepotConfig.getDepotUrl());
|
---|
| 152 |
|
---|
| 153 | HttpResponse response = httpclient.execute(httpQuery);
|
---|
| 154 |
|
---|
| 155 | int code = response.getStatusLine().getStatusCode();
|
---|
| 156 |
|
---|
| 157 | return (code >= 200) && (code <= 299);
|
---|
| 158 | } catch (UnknownHostException e) {
|
---|
| 159 | } catch (UnsupportedEncodingException e) {
|
---|
| 160 | e.printStackTrace();
|
---|
| 161 | } catch (IOException e) {
|
---|
| 162 | e.printStackTrace();
|
---|
| 163 | } finally {
|
---|
| 164 | if (httpQuery != null)
|
---|
| 165 | httpQuery.releaseConnection();
|
---|
| 166 | }
|
---|
| 167 | return false;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
[591] | 171 | * @return All TaxVocabs
|
---|
| 172 | */
|
---|
| 173 | public Vector<TaxonomyVocabulary> getVocabulary() {
|
---|
| 174 | return new Vector<TaxonomyVocabulary>(taxonomyVocabulary.values());
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * @param id
|
---|
| 179 | * Get taxonomy vocabulary by given ID
|
---|
| 180 | * @return TaxVocab
|
---|
| 181 | */
|
---|
| 182 | public TaxonomyVocabulary getVocabulary(int id) {
|
---|
| 183 | return taxonomyVocabulary.get(id);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * @param name
|
---|
| 188 | * Get taxonomy vocabulary by given name
|
---|
| 189 | * @return TaxVocab
|
---|
| 190 | */
|
---|
| 191 | public TaxonomyVocabulary getVocabulary(String name) {
|
---|
| 192 | for (TaxonomyVocabulary v : taxonomyVocabulary.values()) {
|
---|
| 193 | if (v.getName().equalsIgnoreCase(name))
|
---|
| 194 | return v;
|
---|
| 195 | }
|
---|
| 196 | return null;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * @param vocabId
|
---|
| 201 | * Get all taxonomy terms of a given vocabulary
|
---|
| 202 | * @return TaxTerms
|
---|
| 203 | */
|
---|
| 204 | public Vector<TaxonomyTerm> getTaxonomyTermsByVocabulary(int vocabId) {
|
---|
| 205 | Vector<TaxonomyTerm> res = new Vector<TaxonomyTerm>();
|
---|
| 206 | for (TaxonomyTerm t : taxonomyTerms.values()) {
|
---|
| 207 | if (t.getVid() == vocabId)
|
---|
| 208 | res.add(t);
|
---|
| 209 | }
|
---|
| 210 | return res;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /**
|
---|
| 214 | * @param id
|
---|
| 215 | * Get taxonomy term by given ID
|
---|
| 216 | * @return TaxTerm
|
---|
| 217 | */
|
---|
| 218 | public TaxonomyTerm getTaxonomyTerm(int id) {
|
---|
| 219 | return taxonomyTerms.get(id);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /**
|
---|
| 223 | * @param name
|
---|
| 224 | * Get taxonomy term by given name
|
---|
| 225 | * @return TaxTerm
|
---|
| 226 | */
|
---|
| 227 | public TaxonomyTerm getTaxonomyTerm(String name) {
|
---|
| 228 | for (TaxonomyTerm t : taxonomyTerms.values()) {
|
---|
| 229 | if (t.getName().equalsIgnoreCase(name))
|
---|
| 230 | return t;
|
---|
| 231 | }
|
---|
| 232 | return null;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
| 236 | * Get all nodes of given node type
|
---|
| 237 | *
|
---|
| 238 | * @param nodeType
|
---|
| 239 | * Node type
|
---|
| 240 | * @return Nodes of type nodeType
|
---|
| 241 | */
|
---|
| 242 | public Vector<Node> getNodesByType(String nodeType) {
|
---|
| 243 | return new Vector<Node>(nodesByType.get(nodeType).values());
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
[600] | 247 | * Get a node by node id
|
---|
| 248 | *
|
---|
| 249 | * @param id
|
---|
| 250 | * Node id
|
---|
| 251 | * @return Node
|
---|
| 252 | */
|
---|
| 253 | public Node getNodeById(int id) {
|
---|
| 254 | return nodes.get(id);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /**
|
---|
| 258 | * Get a Mod-Node by a given package number
|
---|
| 259 | *
|
---|
| 260 | * @param packageNumber
|
---|
| 261 | * Package number to find
|
---|
| 262 | * @return The Mod-Node or null
|
---|
| 263 | */
|
---|
| 264 | public NodeMod getNodeByPackageNumber(int packageNumber) {
|
---|
| 265 | Vector<Node> files = getNodesByType(DepotConfig.getNodeType_Mod());
|
---|
| 266 | for (Node n : files) {
|
---|
| 267 | if (n instanceof NodeMod) {
|
---|
| 268 | NodeMod nm = (NodeMod) n;
|
---|
| 269 | if (nm.getPackageNumber() == packageNumber)
|
---|
| 270 | return nm;
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | return null;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /**
|
---|
[591] | 277 | * @return Mod-Nodes
|
---|
| 278 | */
|
---|
| 279 | public Vector<NodeMod> getModPackageNodes() {
|
---|
[592] | 280 | Vector<NodeMod> result = new Vector<NodeMod>();
|
---|
[600] | 281 | String instMethName = DepotConfig.getTaxonomyName_InstallType_Package();
|
---|
[591] | 282 |
|
---|
[592] | 283 | Vector<Node> files = getNodesByType(DepotConfig.getNodeType_Mod());
|
---|
[591] | 284 | for (Node n : files) {
|
---|
| 285 | if (n instanceof NodeMod) {
|
---|
| 286 | NodeMod nm = (NodeMod) n;
|
---|
[600] | 287 | if (nm.getInstallMethod().getName()
|
---|
| 288 | .equalsIgnoreCase(instMethName))
|
---|
[591] | 289 | result.add(nm);
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 | return result;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | /**
|
---|
| 296 | * @param node
|
---|
| 297 | * Node to check validity on
|
---|
| 298 | * @param platform
|
---|
| 299 | * Platform to check against
|
---|
| 300 | * @return True if valid on platform
|
---|
| 301 | */
|
---|
| 302 | public boolean isModValidOnPlatform(NodeMod node, Settings.Platform platform) {
|
---|
[600] | 303 | ECompatiblePlatform plat = node.getPlatform();
|
---|
| 304 | switch (plat) {
|
---|
| 305 | case BOTH:
|
---|
| 306 | return true;
|
---|
| 307 | case WIN:
|
---|
| 308 | return (platform == Platform.WIN)
|
---|
| 309 | || (platform == Platform.LINUX);
|
---|
| 310 | case MACOS:
|
---|
| 311 | return (platform == Platform.MACOS);
|
---|
| 312 | }
|
---|
| 313 | return false;
|
---|
[591] | 314 | }
|
---|
| 315 |
|
---|
| 316 | /**
|
---|
| 317 | * Checks if the given mod-node is of the given mod-type(s)
|
---|
| 318 | *
|
---|
| 319 | * @param node
|
---|
| 320 | * Node to check
|
---|
| 321 | * @param type
|
---|
| 322 | * Type(s) to check
|
---|
| 323 | * @param or
|
---|
| 324 | * If false check if all given types are included in node. If
|
---|
| 325 | * true checks if either of the given types is included.
|
---|
| 326 | * @return True if of given type(s)
|
---|
| 327 | */
|
---|
| 328 | public boolean isModOfType(NodeMod node, HashSet<Integer> type, boolean or) {
|
---|
[600] | 329 | boolean matching = !or;
|
---|
| 330 | HashSet<TaxonomyTerm> terms = node.getTypes();
|
---|
[591] | 331 |
|
---|
| 332 | for (int t : type) {
|
---|
| 333 | if (or)
|
---|
[600] | 334 | matching |= terms.contains(t);
|
---|
[591] | 335 | else
|
---|
[600] | 336 | matching &= terms.contains(t);
|
---|
[591] | 337 | }
|
---|
| 338 | return matching;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /**
|
---|
[600] | 342 | * @return VocabId of Platform vocabulary
|
---|
| 343 | */
|
---|
| 344 | public int getVocabIdPlatform() {
|
---|
| 345 | return vocabId_platform;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | /**
|
---|
| 349 | * @return VocabId of Install method vocabulary
|
---|
| 350 | */
|
---|
| 351 | public int getVocabIdInstMethod() {
|
---|
| 352 | return vocabId_instmethod;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | /**
|
---|
| 356 | * @return VocabId of Type vocabulary
|
---|
| 357 | */
|
---|
| 358 | public int getVocabIdType() {
|
---|
| 359 | return vocabId_type;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | /**
|
---|
| 363 | * @param id
|
---|
| 364 | * ID of file to get
|
---|
| 365 | * @return the file
|
---|
| 366 | */
|
---|
| 367 | public File getFile(int id) {
|
---|
| 368 | return files.get(id);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | /**
|
---|
[591] | 372 | * Print stats about nodes and files
|
---|
| 373 | */
|
---|
| 374 | public void printStats() {
|
---|
| 375 | System.out.println("Nodes by type:");
|
---|
| 376 | for (String t : nodesByType.keySet()) {
|
---|
| 377 | System.out.println(" " + t + ": " + nodesByType.get(t).size());
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | System.out.println("Files: " + files.size());
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | private XStream getXStream() {
|
---|
| 384 | XStream xs = new XStream(new StaxDriver());
|
---|
| 385 | xs.alias("Depot", DepotManager.class);
|
---|
| 386 | xs.alias("File", net.oni2.aeinstaller.backend.depot.model.File.class);
|
---|
| 387 | xs.alias("Node", Node.class);
|
---|
| 388 | xs.alias("NodeField_Body", NodeField_Body.class);
|
---|
| 389 | xs.alias("NodeField_Upload", NodeField_Upload.class);
|
---|
| 390 | xs.alias("NodeMod", NodeMod.class);
|
---|
| 391 | xs.alias("TaxonomyTerm", TaxonomyTerm.class);
|
---|
| 392 | xs.alias("TaxonomyVocabulary", TaxonomyVocabulary.class);
|
---|
| 393 | return xs;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | /**
|
---|
| 397 | * Save Depot cache instance to file
|
---|
| 398 | *
|
---|
| 399 | * @param f
|
---|
| 400 | * File to write to
|
---|
| 401 | */
|
---|
| 402 | public void saveToFile(java.io.File f) {
|
---|
| 403 | try {
|
---|
| 404 | FileOutputStream fos = new FileOutputStream(f);
|
---|
| 405 | XStream xs = getXStream();
|
---|
| 406 | xs.toXML(this, fos);
|
---|
| 407 | fos.close();
|
---|
| 408 | } catch (FileNotFoundException e) {
|
---|
| 409 | e.printStackTrace();
|
---|
| 410 | } catch (IOException e) {
|
---|
| 411 | e.printStackTrace();
|
---|
| 412 | }
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | /**
|
---|
| 416 | * Load Depot cache instance from file
|
---|
| 417 | *
|
---|
| 418 | * @param f
|
---|
| 419 | * File to read from
|
---|
| 420 | */
|
---|
| 421 | public void loadFromFile(java.io.File f) {
|
---|
| 422 | try {
|
---|
| 423 | FileInputStream fis = new FileInputStream(f);
|
---|
| 424 | XStream xs = getXStream();
|
---|
| 425 | Object obj = xs.fromXML(fis);
|
---|
| 426 | if (obj instanceof DepotManager)
|
---|
| 427 | instance = (DepotManager) obj;
|
---|
| 428 | fis.close();
|
---|
| 429 | } catch (FileNotFoundException e) {
|
---|
| 430 | } catch (IOException e) {
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
| 433 | }
|
---|