BowlerKernel
Purchasing.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.vitamins;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.concurrent.TimeUnit;
9 
10 import com.neuronrobotics.imageprovider.NativeResource;
11 import com.neuronrobotics.sdk.common.Log;
12 
13 import eu.mihosoft.vrl.v3d.CSG;
14 import eu.mihosoft.vrl.v3d.STL;
15 import eu.mihosoft.vrl.v3d.parametrics.StringParameter;
16 
17 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
18 import com.neuronrobotics.bowlerstudio.vitamins.Purchasing;
19 
20 import javafx.scene.paint.Color;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.lang.reflect.Type;
27 import java.net.HttpURLConnection;
28 import java.net.InetSocketAddress;
29 import java.net.Socket;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.io.IOUtils;
36 import org.eclipse.jgit.api.errors.GitAPIException;
37 import org.eclipse.jgit.api.errors.InvalidRemoteException;
38 import org.eclipse.jgit.api.errors.TransportException;
39 import org.kohsuke.github.GHMyself;
40 import org.kohsuke.github.GHRepository;
41 
42 import com.google.gson.Gson;
43 import com.google.gson.GsonBuilder;
44 import com.google.gson.reflect.TypeToken;
45 
46 public class Purchasing {
47 
48  private static String jsonRootDir = "json/";
49  private static final Map<String, CSG> fileLastLoaded = new HashMap<String, CSG>();
50  private static final Map<String, HashMap<String, HashMap<String, PurchasingData>>> databaseSet =
51  new HashMap<String, HashMap<String, HashMap<String, PurchasingData>>>();
52  private static final String defaultgitRpoDatabase = "https://github.com/CommonWealthRobotics/Hardware-Purchasing.git";
53  private static String gitRpoDatabase = defaultgitRpoDatabase;
54  //Create the type, this tells GSON what datatypes to instantiate when parsing and saving the json
55  private static Type TT_mapStringString = new TypeToken<HashMap<String, HashMap<String, PurchasingData>>>() {
56  }.getType();
57  //chreat the gson object, this is the parsing factory
58  private static Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
59  private static boolean checked;
60 
61 
62  public static HashMap<String, PurchasingData> getConfiguration(String type, String id) {
63  HashMap<String, HashMap<String, PurchasingData>> database = getDatabase(type);
64  if (database.get(id) == null) {
65  database.put(id, new HashMap<String, PurchasingData>());
66  }
67 
68  return database.get(id);
69  }
70 
71  public static void saveDatabase(String type) throws Exception {
72 
73  // Save contents and publish them
74  String jsonString = makeJson(type);
75  try {
77  getGitRpoDatabase(),// git repo, change this if you fork this demo
79  getRootFolder() + type + ".json", // local path to the file in git
80  jsonString, // content of the file
81  "Pushing changed Database");//commit message
82 
83  } catch (org.eclipse.jgit.api.errors.TransportException ex) {
84  System.out.println("You need to fork " + getGitRpoDatabase() + " to have permission to save");
85  System.out.println(
86  "You do not have permission to push to this repo, change the GIT repo to your fork with setGitRpoDatabase(String gitRpoDatabase) ");
87  throw ex;
88  }
89 
90  }
91 
92  public static String makeJson(String type) {
93  return gson.toJson(getDatabase(type), TT_mapStringString);
94  }
95 
96  public static void newVitamin(String type, String id) throws Exception {
97  HashMap<String, HashMap<String, PurchasingData>> database = getDatabase(type);
98  if (database.keySet().size() > 0) {
99  String exampleKey = null;
100  for (String key : database.keySet()) {
101  if (!key.contains("meta")) {
102  exampleKey = key;
103  }
104  }
105  if (exampleKey != null) {
106  // this database has examples, load an example
107  HashMap<String, PurchasingData> exampleConfiguration = getConfiguration(type, exampleKey);
108  HashMap<String, PurchasingData> newConfig = getConfiguration(type, id);
109  for (String key : exampleConfiguration.keySet()) {
110  newConfig.put(key, exampleConfiguration.get(key));
111  }
112  }
113  }
114 
115  getConfiguration(type, id);
116  //saveDatabase(type);
117 
118  }
119 
120  public static void setParameter(String type, String id, String parameterName,
121  PurchasingData parameter) throws Exception {
122 
123  HashMap<String, PurchasingData> config = getConfiguration(type, id);
124  config.put(parameterName, parameter);
125  //saveDatabase(type);
126  }
127 
128  public static HashMap<String, HashMap<String, PurchasingData>> getDatabase(String type) {
129  if (databaseSet.get(type) == null) {
130  // we are using the default vitamins configuration
131  //https://github.com/madhephaestus/Hardware-Dimensions.git
132 
133  // create some variables, including our database
134  String jsonString;
135  InputStream inPut = null;
136 
137  // attempt to load the JSON file from the GIt Repo and pars the JSON string
138  File f;
139  try {
140  f = ScriptingEngine
141  .fileFromGit(
142  getGitRpoDatabase(),// git repo, change this if you fork this demo
143  getRootFolder() + type + ".json"// File from within the Git repo
144  );
145  inPut = FileUtils.openInputStream(f);
146 
147  jsonString = IOUtils.toString(inPut);
148  // perfoem the GSON parse
149  HashMap<String, HashMap<String, PurchasingData>> database = gson
150  .fromJson(jsonString, TT_mapStringString);
151  if (database == null) {
152  throw new RuntimeException("create a new one");
153  }
154  databaseSet.put(type, database);
155 
156  } catch (Exception e) {
157  databaseSet.put(type, new HashMap<String, HashMap<String, PurchasingData>>());
158  }
159  }
160  return databaseSet.get(type);
161 
162  }
163 
164  private static String getRootFolder() {
165  return getJsonRootDir();
166  }
167 
168  public static ArrayList<String> listVitaminTypes() {
169 
170  ArrayList<String> types = new ArrayList<String>();
171  File folder;
172  try {
173  folder = ScriptingEngine
174  .fileFromGit(
175  getGitRpoDatabase(),// git repo, change this if you fork this demo
176  getRootFolder() + "capScrew.json"
177  );
178  File[] listOfFiles = folder.getParentFile().listFiles();
179 
180  for (File f : listOfFiles) {
181  if (!f.isDirectory() && f.getName().endsWith(".json")) {
182  types.add(f.getName().substring(0, f.getName().indexOf(".json")));
183  }
184  }
185 
186  } catch (Exception e) {
187  // TODO Auto-generated catch block
188  e.printStackTrace();
189  }
190  return types;
191  }
192 
193  public static ArrayList<String> listVitaminSizes(String type) {
194 
195  ArrayList<String> types = new ArrayList<String>();
196  HashMap<String, HashMap<String, PurchasingData>> database = getDatabase(type);
197  Set<String> keys = database.keySet();
198  for (String s : keys) {
199  if (!s.contains("meta")) {
200  types.add(s);
201  }
202  }
203 
204  return types;
205  }
206 
207  public static ArrayList<String> listVitaminVariants(String type, String size) {
208 
209  ArrayList<String> types = new ArrayList<String>();
210  HashMap<String, PurchasingData> database = getDatabase(type).get(size);
211  Set<String> keys = database.keySet();
212 
213  for (String variant : keys) {
214  PurchasingData pd = database.get(variant);
215  if (!variant.endsWith("variant-1"))// exclude the stub generated purhcasing data
216  {
217  try {
218  URL u = new URL(pd.getAPIUrl());
219  HttpURLConnection huc = (HttpURLConnection) u.openConnection();
220  huc.setRequestMethod("GET"); //OR huc.setRequestMethod ("HEAD");
221  huc.connect();
222  huc.getResponseCode();
223  huc.disconnect();
224  u = new URL(pd.getCartUrl());
225  huc = (HttpURLConnection) u.openConnection();
226  huc.setRequestMethod("GET"); //OR huc.setRequestMethod ("HEAD");
227  huc.connect();
228  huc.getResponseCode();
229  huc.disconnect();
230  types.add(variant);
231  } catch (java.net.ConnectException ce) {
232  // server or cart is not availible, reject vitamin
233  } catch (Exception ex) {
234  ex.printStackTrace();
235  }
236  }
237 
238  }
239 
240  return types;
241  }
242 
243  public static PurchasingData get(String type, String size, String variant) {
244  try {
245  return getDatabase(type).get(size).get(variant);
246  } catch (NullPointerException ex) {
247  throw new RuntimeException(
248  "Vitamin " + type + " " + size + " " + variant + " does not exist");
249  }
250  }
251 
252 
253  public static String getGitRpoDatabase() throws IOException {
254  return gitRpoDatabase;
255  }
256 
257  public static void setGitRpoDatabase(String gitRpoDatabase) {
259  }
260 
261  public static String getJsonRootDir() {
262  return jsonRootDir;
263  }
264 
265  public static void setJsonRootDir(String jsonRootDir) {
267  }
268 
269 
270 }
static void pushCodeToGit(String id, String branch, String FileName, String content, String commitMessage)
static File fileFromGit(String remoteURI, String fileInRepo)
static void newVitamin(String type, String id)
Definition: Purchasing.java:96
static final Map< String, HashMap< String, HashMap< String, PurchasingData > > > databaseSet
Definition: Purchasing.java:50
static HashMap< String, PurchasingData > getConfiguration(String type, String id)
Definition: Purchasing.java:62
static void setParameter(String type, String id, String parameterName, PurchasingData parameter)
static ArrayList< String > listVitaminSizes(String type)
static void setGitRpoDatabase(String gitRpoDatabase)
static HashMap< String, HashMap< String, PurchasingData > > getDatabase(String type)
static ArrayList< String > listVitaminVariants(String type, String size)
static void setJsonRootDir(String jsonRootDir)
static final Map< String, CSG > fileLastLoaded
Definition: Purchasing.java:49