/src/mpv5/server/XMLRPCHandler.java
Java | 93 lines | 79 code | 10 blank | 4 comment | 6 complexity | d7663b9997e3d71dbe68576fabfeaf80 MD5 | raw file
1package mpv5.server; 2 3import java.util.ArrayList; 4import java.util.HashMap; 5import java.util.List; 6import mpv5.db.common.Context; 7import mpv5.db.common.DatabaseObject; 8import mpv5.db.common.NodataFoundException; 9import mpv5.logging.Log; 10import mpv5.usermanagement.MPSecurityManager; 11 12/** 13 * 14 * @author andreasw 15 */ 16public class XMLRPCHandler { 17 18 public String[] getAvailableContexts() { 19 ArrayList<Context> c = Context.getImportableContexts(); 20 String[] s = new String[c.size()]; 21 for (int i = 0; i < c.size(); i++) { 22 s[i] = c.get(i).getDbIdentity(); 23 } 24 return s; 25 } 26 27 public boolean login(String user, String passw) throws Exception { 28 Log.Debug(this, "Login " + user); 29 return MPSecurityManager.checkAuth(user, passw) != null; 30 } 31 32 public HashMap<String, Object> getObjects(String context, int startid, int endid) throws Exception { 33 Log.Debug(this, "Getting " + context + " ids: " + startid + " to " + endid); 34 HashMap<String, Object> m = new HashMap<String, Object>(); 35 for (int i = startid; i <= endid; i++) { 36 try { 37 DatabaseObject d = DatabaseObject.getObject(Context.getMatchingContext(context), i); 38 HashMap<String, Object> m2 = new HashMap<String, Object>(); 39 List<Object[]> l = d.getValues2(); 40 for (Object[] objects : l) { 41 m2.put(objects[0].toString(), objects[1]); 42 } 43 m.put(context + " [" + i + "]", m2); 44 } catch (NodataFoundException nodataFoundException) { 45 Log.Debug(this, nodataFoundException); 46 } 47 } 48 return m; 49 } 50 51 public HashMap<String, Object> getObject(String context, int id) throws Exception { 52 Log.Debug(this, "Getting " + context + " id: " + id); 53 DatabaseObject d = DatabaseObject.getObject(Context.getMatchingContext(context), id); 54 HashMap<String, Object> m = new HashMap<String, Object>(); 55 List<Object[]> l = d.getValues2(); 56 for (Object[] objects : l) { 57 m.put(objects[0].toString(), objects[1]); 58 } 59 return m; 60 } 61 62 public HashMap<String, Object> getObject(String context, String cname) throws Exception { 63 Log.Debug(this, "Getting " + context + " cname: " + cname); 64 DatabaseObject d = DatabaseObject.getObject(Context.getMatchingContext(context), cname); 65 HashMap<String, Object> m = new HashMap<String, Object>(); 66 List<Object[]> l = d.getValues2(); 67 for (Object[] objects : l) { 68 m.put(objects[0].toString(), objects[1]); 69 } 70 return m; 71 } 72 73 public boolean addObject(String context, HashMap<String, Object> data) throws Exception { 74 Log.Debug(this, "Adding " + context + " data: " + data); 75 DatabaseObject d = DatabaseObject.getObject(Context.getMatchingContext(context)); 76 d.parse(data); 77 d.setIDS(-1); 78 return d.saveImport(); 79 } 80 81 public boolean updateObject(String context, int id, HashMap<String, Object> data) throws Exception { 82 Log.Debug(this, "Updating " + context + " id: " + id + " data: " + data); 83 DatabaseObject d = DatabaseObject.getObject(Context.getMatchingContext(context), id); 84 d.parse(data); 85 d.setIDS(id); 86 return d.save(); 87 } 88 89 public boolean deleteObject(String context, int id) throws Exception { 90 Log.Debug(this, "Deleting " + context + " id: " + id); 91 return DatabaseObject.getObject(Context.getMatchingContext(context), id).delete(); 92 } 93}