| 1 |
package net.oni2.aeinstaller.backend.depot.model; |
| 2 |
|
| 3 |
import java.util.HashMap; |
| 4 |
import java.util.HashSet; |
| 5 |
import java.util.Vector; |
| 6 |
|
| 7 |
import net.oni2.aeinstaller.backend.depot.DepotConfig; |
| 8 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
| 9 |
import net.oni2.aeinstaller.backend.mods.ECompatiblePlatform; |
| 10 |
|
| 11 |
import org.json.JSONArray; |
| 12 |
import org.json.JSONException; |
| 13 |
import org.json.JSONObject; |
| 14 |
|
| 15 |
/** |
| 16 |
* @author Christian Illy |
| 17 |
*/ |
| 18 |
public class NodeMod extends Node { |
| 19 |
private Vector<NodeField_Upload> uploads = new Vector<NodeField_Upload>(); |
| 20 |
private HashMap<Integer, HashSet<TaxonomyTerm>> taxonomyTerms = new HashMap<Integer, HashSet<TaxonomyTerm>>(); |
| 21 |
private HashMap<String, String> fields = new HashMap<String, String>(); |
| 22 |
|
| 23 |
/** |
| 24 |
* @param json |
| 25 |
* JSON object of Mod-node to parse |
| 26 |
* @throws JSONException |
| 27 |
* On key not found / wrong type |
| 28 |
*/ |
| 29 |
public NodeMod(JSONObject json) throws JSONException { |
| 30 |
super(json); |
| 31 |
|
| 32 |
Object uploadObj = json.get("upload"); |
| 33 |
if (uploadObj instanceof JSONObject) { |
| 34 |
JSONArray jUploads = ((JSONObject) uploadObj).getJSONArray("und"); |
| 35 |
for (int i = 0; i < jUploads.length(); i++) { |
| 36 |
NodeField_Upload up = new NodeField_Upload( |
| 37 |
jUploads.getJSONObject(i)); |
| 38 |
if (up.getDisplay() != 0) { |
| 39 |
uploads.add(up); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
for (Object key : json.keySet()) { |
| 45 |
String keyS = ((String) key).toLowerCase(); |
| 46 |
Object val = json.get(keyS); |
| 47 |
if (keyS.startsWith("field_")) { |
| 48 |
String fName = keyS.substring(keyS.indexOf("_") + 1); |
| 49 |
String value = ""; |
| 50 |
if (val instanceof JSONObject) { |
| 51 |
value = ((JSONObject) val).getJSONArray("und") |
| 52 |
.getJSONObject(0).getString("value"); |
| 53 |
} |
| 54 |
fields.put(fName, value); |
| 55 |
} |
| 56 |
|
| 57 |
if (keyS.startsWith("taxonomy_vocabulary_")) { |
| 58 |
int vid = Integer |
| 59 |
.parseInt(keyS.substring(keyS.lastIndexOf("_") + 1)); |
| 60 |
HashSet<TaxonomyTerm> values = new HashSet<TaxonomyTerm>(); |
| 61 |
if (val instanceof JSONObject) { |
| 62 |
JSONArray ja = ((JSONObject) val).getJSONArray("und"); |
| 63 |
for (int i = 0; i < ja.length(); i++) { |
| 64 |
values.add(DepotManager.getInstance().getTaxonomyTerm( |
| 65 |
ja.getJSONObject(i).getInt("tid"))); |
| 66 |
} |
| 67 |
} |
| 68 |
taxonomyTerms.put(vid, values); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @return the uploads |
| 75 |
*/ |
| 76 |
public Vector<NodeField_Upload> getUploads() { |
| 77 |
return uploads; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return Types |
| 82 |
*/ |
| 83 |
public HashSet<TaxonomyTerm> getTypes() { |
| 84 |
return taxonomyTerms.get(DepotManager.getInstance().getVocabIdType()); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return Install method |
| 89 |
*/ |
| 90 |
public TaxonomyTerm getInstallMethod() { |
| 91 |
return taxonomyTerms |
| 92 |
.get(DepotManager.getInstance().getVocabIdInstMethod()) |
| 93 |
.iterator().next(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return Compatible platform |
| 98 |
*/ |
| 99 |
public ECompatiblePlatform getPlatform() { |
| 100 |
TaxonomyTerm term = taxonomyTerms |
| 101 |
.get(DepotManager.getInstance().getVocabIdPlatform()) |
| 102 |
.iterator().next(); |
| 103 |
|
| 104 |
String validPlatform = term.getName(); |
| 105 |
if (validPlatform.equalsIgnoreCase(DepotConfig |
| 106 |
.getTaxonomyName_Platform_Both())) |
| 107 |
return ECompatiblePlatform.BOTH; |
| 108 |
if (validPlatform.equalsIgnoreCase(DepotConfig |
| 109 |
.getTaxonomyName_Platform_Win())) |
| 110 |
return ECompatiblePlatform.WIN; |
| 111 |
if (validPlatform.equalsIgnoreCase(DepotConfig |
| 112 |
.getTaxonomyName_Platform_Mac())) |
| 113 |
return ECompatiblePlatform.MACOS; |
| 114 |
|
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @return Creator of mod |
| 120 |
*/ |
| 121 |
public String getCreator() { |
| 122 |
if (fields.get("creator") != null) |
| 123 |
return fields.get("creator"); |
| 124 |
else |
| 125 |
return ""; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @return Version of mod |
| 130 |
*/ |
| 131 |
public String getVersion() { |
| 132 |
if (fields.get("version") != null) |
| 133 |
return fields.get("version"); |
| 134 |
else |
| 135 |
return ""; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @return Package number |
| 140 |
*/ |
| 141 |
public int getPackageNumber() { |
| 142 |
return Integer.parseInt(fields.get("package_number")); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return Is this mod a tool? |
| 147 |
*/ |
| 148 |
public boolean isTool() { |
| 149 |
HashSet<TaxonomyTerm> types = getTypes(); |
| 150 |
for (String s : DepotConfig.getTaxonomyName_ModType_Tool()) { |
| 151 |
for (TaxonomyTerm tt : types) |
| 152 |
if (tt.getName().equalsIgnoreCase(s)) |
| 153 |
return true; |
| 154 |
} |
| 155 |
return false; |
| 156 |
} |
| 157 |
} |