PageRenderTime 397ms CodeModel.GetById 103ms RepoModel.GetById 2ms app.codeStats 0ms

/src/mpv5/handler/SDBObjectGenerator.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 97 lines | 60 code | 12 blank | 25 comment | 1 complexity | 937e4c0d3f635d9d45f1e76021e3330c MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. */
  17. package mpv5.handler;
  18. import java.lang.reflect.Method;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import mpv5.compiler.RuntimeCompiler;
  22. import mpv5.db.common.Context;
  23. import mpv5.db.common.DatabaseObject;
  24. import mpv5.logging.Log;
  25. /**
  26. * This class generates simplified Beans from DatabaseObjects which do only contain their getter/setter Methods
  27. */
  28. public abstract class SDBObjectGenerator {
  29. /**
  30. * Create an SimpleObject
  31. * @param dos
  32. * @param packagename
  33. * @return
  34. */
  35. public static SimpleDatabaseObject getObjectFrom(DatabaseObject dos, String packagename) {
  36. ArrayList<Method> methods = new ArrayList<Method>(dos.setVars().values());
  37. String classTemplate =
  38. "package " + packagename + ";\n" +
  39. "import mpv5.db.common.Context;\n" +
  40. "import mpv5.db.common.DatabaseObject;\n" +
  41. "import mpv5.utils.images.MPIcon;\n" +
  42. "import java.util.Date;\n" +
  43. "\n" +
  44. "\n" +
  45. "public final class " + dos.getDbIdentity() + " implements mpv5.handler.SimpleDatabaseObject{\n\n";
  46. for (int i = 0; i < methods.size(); i++) {
  47. Method method = methods.get(i);
  48. classTemplate +=
  49. "private " + method.getParameterTypes()[0].getCanonicalName() + " " + method.getName().substring(3).toLowerCase() + ";\n" +
  50. "public " + method.getParameterTypes()[0].getCanonicalName() + " get" + method.getName().substring(3) + "(){\n" +
  51. "return " + "this." + method.getName().substring(3).toLowerCase() + ";\n" +
  52. "}\n" +
  53. "public boolean " + method.getName().toString() + "(" + method.getParameterTypes()[0].getCanonicalName() + " arg){\n" +
  54. "this." + method.getName().substring(3).toLowerCase() + " = arg;\n" +
  55. "return true;\n" +
  56. "}" +
  57. "\n\n";
  58. }
  59. classTemplate += "" +
  60. "public boolean persist() throws Exception {" +
  61. "return DatabaseObject.parse(this).saveImport();" +
  62. "}" +
  63. "" +
  64. "public boolean fetch(int id) throws Exception {" +
  65. "DatabaseObject.getObject(Context.getMatchingContext(getContext()), id).inject(this);" +
  66. "return true;" +
  67. "}" +
  68. "" +
  69. "public boolean fetch(String cname) throws Exception {" +
  70. "DatabaseObject.getObject(Context.getMatchingContext(getContext()), cname).inject(this);" +
  71. "return true;" +
  72. "}" +
  73. "" +
  74. "public String getContext(){" +
  75. "return getClass().getSimpleName();" +
  76. "}" +
  77. "}\n";
  78. try {
  79. return (SimpleDatabaseObject) RuntimeCompiler.getObjectFor(RuntimeCompiler.getClassFor(dos.getDbIdentity(), classTemplate, packagename));
  80. } catch (Exception ex) {
  81. Log.Debug(ex);
  82. return null;
  83. }
  84. }
  85. }