/src/mpv5/handler/SDBObjectGenerator.java
Java | 97 lines | 60 code | 12 blank | 25 comment | 1 complexity | 937e4c0d3f635d9d45f1e76021e3330c MD5 | raw file
1/* 2 * This file is part of YaBS. 3 * 4 * YaBS is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * YaBS is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with YaBS. If not, see <http://www.gnu.org/licenses/>. 16 */ 17package mpv5.handler; 18 19import java.lang.reflect.Method; 20import java.util.ArrayList; 21import java.util.List; 22import mpv5.compiler.RuntimeCompiler; 23import mpv5.db.common.Context; 24import mpv5.db.common.DatabaseObject; 25import mpv5.logging.Log; 26 27/** 28 * This class generates simplified Beans from DatabaseObjects which do only contain their getter/setter Methods 29 */ 30public abstract class SDBObjectGenerator { 31 32 33 /** 34 * Create an SimpleObject 35 * @param dos 36 * @param packagename 37 * @return 38 */ 39 public static SimpleDatabaseObject getObjectFrom(DatabaseObject dos, String packagename) { 40 41 ArrayList<Method> methods = new ArrayList<Method>(dos.setVars().values()); 42 43 String classTemplate = 44 "package " + packagename + ";\n" + 45 "import mpv5.db.common.Context;\n" + 46 "import mpv5.db.common.DatabaseObject;\n" + 47 "import mpv5.utils.images.MPIcon;\n" + 48 "import java.util.Date;\n" + 49 "\n" + 50 "\n" + 51 "public final class " + dos.getDbIdentity() + " implements mpv5.handler.SimpleDatabaseObject{\n\n"; 52 53 for (int i = 0; i < methods.size(); i++) { 54 Method method = methods.get(i); 55 classTemplate += 56 "private " + method.getParameterTypes()[0].getCanonicalName() + " " + method.getName().substring(3).toLowerCase() + ";\n" + 57 "public " + method.getParameterTypes()[0].getCanonicalName() + " get" + method.getName().substring(3) + "(){\n" + 58 "return " + "this." + method.getName().substring(3).toLowerCase() + ";\n" + 59 "}\n" + 60 "public boolean " + method.getName().toString() + "(" + method.getParameterTypes()[0].getCanonicalName() + " arg){\n" + 61 "this." + method.getName().substring(3).toLowerCase() + " = arg;\n" + 62 "return true;\n" + 63 "}" + 64 "\n\n"; 65 } 66 67 classTemplate += "" + 68 "public boolean persist() throws Exception {" + 69 "return DatabaseObject.parse(this).saveImport();" + 70 "}" + 71 72 "" + 73 "public boolean fetch(int id) throws Exception {" + 74 "DatabaseObject.getObject(Context.getMatchingContext(getContext()), id).inject(this);" + 75 "return true;" + 76 "}" + 77 78 "" + 79 "public boolean fetch(String cname) throws Exception {" + 80 "DatabaseObject.getObject(Context.getMatchingContext(getContext()), cname).inject(this);" + 81 "return true;" + 82 "}" + 83 84 "" + 85 "public String getContext(){" + 86 "return getClass().getSimpleName();" + 87 "}" + 88 89 "}\n"; 90 try { 91 return (SimpleDatabaseObject) RuntimeCompiler.getObjectFor(RuntimeCompiler.getClassFor(dos.getDbIdentity(), classTemplate, packagename)); 92 } catch (Exception ex) { 93 Log.Debug(ex); 94 return null; 95 } 96 } 97}