BowlerKernel
ConfigurationDatabase.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.assets;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.reflect.Type;
6 import java.util.HashMap;
7 import java.util.Map;
8 
9 import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
10 import org.kohsuke.github.GHMyself;
11 import org.kohsuke.github.GHRepository;
12 
13 import com.google.gson.Gson;
14 import com.google.gson.GsonBuilder;
15 import com.google.gson.reflect.TypeToken;
16 import com.neuronrobotics.bowlerstudio.IssueReportingExceptionHandler;
17 import com.neuronrobotics.bowlerstudio.scripting.IGithubLoginListener;
18 import com.neuronrobotics.bowlerstudio.scripting.PasswordManager;
19 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
20 
21 public class ConfigurationDatabase {
22 
23  private static final String repo = "BowlerStudioConfiguration";
24  private static final String HTTPS_GITHUB_COM_NEURON_ROBOTICS_BOWLER_STUDIO_CONFIGURATION_GIT = "https://github.com/CommonWealthRobotics/"
25  + repo + ".git";
26 
27  private static String gitSource = null; // madhephaestus
28  private static String dbFile = "database.json";
29  private static boolean checked;
30  private static HashMap<String, HashMap<String, Object>> database = null;
31  private static final Type TT_mapStringString = new TypeToken<HashMap<String, HashMap<String, Object>>>() {
32  }.getType();
33  // chreat the gson object, this is the parsing factory
34  private static Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
36  private static String loggedInAs = null;
37  static {
38 
39  }
40 
41  public static Object getObject(String paramsKey, String objectKey, Object defaultValue) {
42  if (getParamMap(paramsKey).get(objectKey) == null) {
43  System.err.println("Cant find: " + paramsKey + ":" + objectKey);
44  setObject(paramsKey, objectKey, defaultValue);
45  }
46  return getParamMap(paramsKey).get(objectKey);
47  }
48 
49  public static HashMap<String, Object> getParamMap(String paramsKey) {
50  if (getDatabase().get(paramsKey) == null) {
51  getDatabase().put(paramsKey, new HashMap<String, Object>());
52  }
53  return getDatabase().get(paramsKey);
54  }
55 
56  public static Object setObject(String paramsKey, String objectKey, Object value) {
57  return getParamMap(paramsKey).put(objectKey, value);
58  }
59 
60  public static Object removeObject(String paramsKey, String objectKey) {
61  return getParamMap(paramsKey).remove(objectKey);
62  }
63 
64  public static void save() {
65  if (loggedInAs == null)
66  return;
67  String writeOut = null;
68  getDatabase();
69  // synchronized(database){
70  writeOut = gson.toJson(database, TT_mapStringString);
71  // }
72 
73  for (int i = 0; i < 2; i++) {
74  try {
76  System.out.println("Not pushing changes the default branch");
77  return;
78  }
81  getDbFile(), writeOut, "Saving database");
82  return;
83  } catch (Exception e) {
84  e.printStackTrace();
85  try {
87  Thread.sleep(500);
88  } catch (Exception e1) {
89  e1.printStackTrace();
90  }
91  }
92 
93  }
94 
95  }
96 
97  @SuppressWarnings("unchecked")
98  public static HashMap<String, HashMap<String, Object>> getDatabase() {
99  if (database != null) {
100  return database;
101  }
102  HashMap<String, HashMap<String, Object>> existing = null;
103  try {
104  existing = (HashMap<String, HashMap<String, Object>>) ScriptingEngine.inlineFileScriptRun(loadFile(), null);
105 
107  database = (HashMap<String, HashMap<String, Object>>) ScriptingEngine.inlineFileScriptRun(loadFile(), null);
108  // new Exception().printStackTrace();
109  } catch (org.eclipse.jgit.api.errors.WrongRepositoryStateException e) {
110  // oignore and use new one
111  try {
113  return getDatabase() ;
114  } catch (Exception e1) {
115  // TODO Auto-generated catch block
116  e1.printStackTrace();
117  }
118  }catch (Exception ex) {
119  ex.printStackTrace();
120  }
121  if (database == null) {
122  database = new HashMap<String, HashMap<String, Object>>();
123  // new Exception().printStackTrace();
124  }
125  // Copy over the existing database values into the newly logged in user
126  syncOldDBToNew(existing);
127  return database;
128  }
129 
130  public static File loadFile() throws Exception {
131  return ScriptingEngine.fileFromGit(getGitSource(), // git repo, change
132  getDbFile());
133  }
134 
135  public static void loginEvent(String username) {
136  if (username == null) {
137  String loggedInAs2 = loggedInAs;
138  loggedInAs = null;
139  checked = false;
140  gitSource = null;
141  database = null;
142  System.out.println("\n\nLogout from "+loggedInAs2+"\n\n");
143  return;
144  }
145  if (loggedInAs != null)
146  if (username.contentEquals(loggedInAs))
147  return;
148  checked = false;
149  gitSource = null;
150  HashMap<String, HashMap<String, Object>> existing = database;
151  database = null;
152  try {
153  System.out.println("Setting Configurations repo to " + getGitSource());
154  } catch (Exception e) {
155  // TODO Auto-generated catch block
156  e.printStackTrace();
157  }
158  // Copy over the existing database values into the newly logged in user
159  syncOldDBToNew(existing);
160  loggedInAs = username;
161 
162  }
163 
164  private static void syncOldDBToNew(HashMap<String, HashMap<String, Object>> existing) {
165  if (existing != null)
166  for (String pkey : existing.keySet()) {
167  HashMap<String, Object> val = existing.get(pkey);
168 
169  for (String objectKey : val.keySet()) {
170  Object defaultValue = val.get(objectKey);
171  Object value = getObject(pkey, objectKey, defaultValue);
172  System.err.println("Setting " + pkey + ":" + objectKey + " to " + value + " with previous being "+ defaultValue);
173  }
174 
175  }
176  }
177 
178  public static String getGitSource() throws Exception {
179  if (!checked) {
181  org.kohsuke.github.GitHub github = PasswordManager.getGithub();
182  try {
183  GHRepository myConfigRepo = github.getRepository(PasswordManager.getLoginID() + "/" + repo);
184  setRepo(myConfigRepo);
185  checked = true;
186  } catch (Throwable t) {
187  if (gitSource == null) {
188  GHRepository defaultRep = github.getRepository("CommonWealthRobotics/" + repo);
189  GHRepository forkedRep = defaultRep.fork();
190  setRepo(forkedRep);
191  checked = true;
192  }
193  if (PasswordManager.getUsername() != null) {
194  setGitSource("https://github.com/" + PasswordManager.getUsername() + "/" + repo + ".git");
195  } else {
197  }
198 
199  }
200  } else if (ScriptingEngine.isLoginSuccess()) {
201  // no network but has logged in before
202  setGitSource("https://github.com/" + PasswordManager.getUsername() + "/" + repo + ".git");
203  }
204 
205  }
206  return gitSource;
207 
208  }
209 
210  private static void setRepo(GHRepository forkedRep) {
211  String myAssets = forkedRep.getGitTransportUrl().replaceAll("git://", "https://");
212  setGitSource(myAssets);
213  }
214 
215  public static void setGitSource(String myAssets) {
216  System.out.println("Using my version of configuration database: " + myAssets);
217  if (myAssets != null && gitSource != null && myAssets.contentEquals(gitSource))
218  return;
219  database = null;
220  // new Exception("Changing from "+gitSource+" to "+myAssets).printStackTrace();
221  checked = false;
222  gitSource = myAssets;
223  getDatabase();
224  }
225 
226  public static String getDbFile() {
227  return dbFile;
228  }
229 
230  public static void setDbFile(String dbFile) {
233  }
234 }
static HashMap< String, HashMap< String, Object > > database
static HashMap< String, Object > getParamMap(String paramsKey)
static HashMap< String, HashMap< String, Object > > getDatabase()
static Object removeObject(String paramsKey, String objectKey)
static Object getObject(String paramsKey, String objectKey, Object defaultValue)
static void syncOldDBToNew(HashMap< String, HashMap< String, Object >> existing)
static Object setObject(String paramsKey, String objectKey, Object value)
static void pushCodeToGit(String id, String branch, String FileName, String content, String commitMessage)
static Object inlineFileScriptRun(File f, ArrayList< Object > args)
static File fileFromGit(String remoteURI, String fileInRepo)
static void pull(String remoteURI, String branch)