Code Generator for Eclipse Code
Brought to you by:
hotzst
<%@ jet imports="java.util.* ch.sahits.model.* ch.sahits.model.db.* ch.sahits.model.java.* ch.sahits.model.java.db.* ch.sahits.codegen.java.model.util.* java.lang.reflect.* " package="PAGAGE_NAME" class="CLASS_NAME" %> <% DataBaseTable dbt = (DataBaseTable) argument; %> <%=ConvenientCodegen.toPackageDefinition(dbt.getPackageName()) %><% Set importClasses = new HashSet(); importClasses.addAll(dbt.usedClasses()); if (dbt.hasSuperClass() && !dbt.superclasses().get(0).equals(Object.class)){ // Add interfaces from abstract methods of super class Class clazz = dbt.superclasses().get(0); MethodReflector mr = new MethodReflector(clazz); importClasses.addAll(mr.neededImportsOfAbstractMethods()); } if (!dbt. interfaces().isEmpty()){ Class[] interfaces = dbt.getInterfaces(); for (int i=0;i<interfaces.length; i++ ){ MethodReflector mr = new MethodReflector(interfaces[i]); importClasses.addAll(mr.needeImportsOfNonPrivateMethods()); } } for (Iterator i = importClasses.iterator(); i.hasNext(); ){ Class c = (Class)i.next(); if (!ConvenientCodegen.isPrimitiveClass(c)){ %>import <%=c.getName() %>; <% } // end if } %><% if (dbt.hasSuperClass() && !ConvenientCodegen.isPrimitiveClass((Class)dbt.superclasses().get(0))){ %> import <%=((Class)dbt.superclasses().get(0)).getName() %>;<% } %><% if (!dbt.interfaces().isEmpty()){ Class[] interfaces = dbt.getInterfaces(); for (int i=0;i<interfaces.length; i++ ){%> import <%=interfaces[i].getName() %>; <% } } %> import java.util.*; import java.sql.SQLException; /** * This class represents the database table * <%=dbt.getTableName() %>. */ <% if (dbt.isPublic()){ %> public <% } else if (dbt.isProtected()){ %> protected <% } else if (dbt.isPrivate()){ %> private <% } %><% if (dbt.isAbstract()) { %>abstract <% } %><% if (dbt.isFinal()) { %>final <% } %>class <%=dbt.getClassName() %><% if (dbt.hasSuperClass() && !dbt.superclasses().get(0).equals(Object.class)){ %> extends <%=ConvenientCodegen.getSimpleClassName((Class)dbt.superclasses().get(0)) %><% } %><% if (!dbt. interfaces().isEmpty()){ Class[] interfaces = dbt.getInterfaces(); %> implements <% for (int i=0;i<interfaces.length; i++ ){ %><%=ConvenientCodegen.getSimpleClassName((Class)interfaces[i]) %><% if (i==interfaces.length-1){ %> <% } else { %>, <% } %><% } %><% } %> { <% // My constructors %> /** * Konstruktor initialisiert Objekt mit Werten aus Hashtable */ public <%=dbt.getClassName() %>(){ } <% /* End of my constructors */ %> <% List fields = dbt.getFields(); for (Iterator i = fields.iterator(); i.hasNext(); ){ DataBaseTableField field = (DataBaseTableField)i.next(); %> /** * Retrieve the value of the field <%=field.getFieldName() %>. * @return the value of the field */ public String <%= ConvenientCodegen.toName( "get_"+field.getFieldName(),"_" ) %>(){ return super.get("<%= field.getFieldName() %>"); } /** * Set the value of the field <%=field.getFieldName() %>. * @param <%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %> value to be set for <%=field.getFieldName() %> */ void <%= ConvenientCodegen.toName( "set_"+field.getFieldName(),"_" ) %>(String <%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %>){ if (<%= ConvenientCodegen.toName( "get_"+field.getFieldName(),"_" ) %>()==null) throw new NullPointerException("<%=ConvenientCodegen.toName( field.getFieldName(),"_" ) %> may not be null"); super.put("<%=field.getFieldName() %>",<%=ConvenientCodegen.toName( field.getFieldName(),"_" ) %>); } <% } // End for loop fields %><% // START implementation of abstract methods of super class %> <% /* Abstract classes from the superclass */ %> @Override public void clear() { throw new RuntimeException("This operation is not supported"); } @Override public String get(Object key) { throw new RuntimeException("This operation is not supported"); } @Override public String put(String key, String value) { throw new RuntimeException("This operation is not supported"); } @Override public void putAll(Map<? extends String, ? extends String> m) { throw new RuntimeException("This operation is not supported"); } @Override public String remove(Object key) { throw new RuntimeException("This operation is not supported"); } @Override public Collection<String> values() { throw new RuntimeException("This operation is not supported"); } @Override public Object clone() { throw new RuntimeException("This operation is not supported"); } @Override public boolean containsKey(Object key) { throw new RuntimeException("This operation is not supported"); } @Override public boolean containsValue(Object value) { throw new RuntimeException("This operation is not supported"); } @Override public Set<java.util.Map.Entry<String, String>> entrySet() { throw new RuntimeException("This operation is not supported"); } @Override public boolean isEmpty() { throw new RuntimeException("This operation is not supported"); } @Override public Set<String> keySet() { throw new RuntimeException("This operation is not supported"); } @Override public int size() { throw new RuntimeException("This operation is not supported"); } /** * Fuegt das aktuelle Objekt persistent in die DB ein.<br> * Wenn das Objekt bereits vorhanden ist, wird eine entsprechende * Exception erzeugt. * @return int: Anzahl eingefügter Records * @throws SQLException */ public int insert() throws SQLException { return new <%=dbt.getClassName() %>DAO().insert(this); } /** * Speichert die Veränderungen des aktuellen Objekt persistent ab.<br> * Wenn das Objekt nicht vorhanden ist, wird eine entsprechende * Exception erzeugt. * @return int: Anzahl geänderter Records * @throws SQLException */ public int update() throws SQLException { return new <%=dbt.getClassName() %>DAO().update(this); } /** * Löscht das aktuelle Objekt dauerhaft.<br> * Wenn das Objekt nicht gelöscht werden konnte, wird eine entsprechende * Exception erzeugt.<br> * @return int: Anzahl entfernter Records * @throws SQLException */ public int remove() throws SQLException { return new <%=dbt.getClassName() %>DAO().remove(this); } /** * @return alle gefundenen Objekte * @throws SQLException */ public static List<<%=dbt.getClassName() %>> loadAll() throws SQLException{ return new <%=dbt.getClassName() %>DAO().loadAll(); } <% /* * Load methods for all unique Indexes */ List<DataBaseTableIndex> indices = dbt.uniqueKeys(); for (Iterator i = indices.iterator(); i.hasNext(); ){ // For-Loop loadInidecs DataBaseTableIndex index = (DataBaseTableIndex)i.next();%> /** * Load a data set by the unique index <%=index.getIndexName() %> <% List<DataBaseTableField> indexFields = index.getFields(); for (DataBaseTableField field : indexFields){ String fieldName = field.getFieldName(); %> * @param <%=fieldName %> value that is part of the index<% } /* end for loop javadoc param */%> * @return selected <%=dbt.getClassName() %> * @throws SQLException Error while extracting the data */ public <%=dbt.getClassName() %> selectBy<%=ConvenientCodegen.camelCase(index.getIndexName(),"_") %>(<% for (int i2=0;i2<indexFields.size();i2++){ // for loop i2 DataBaseTableField field = (DataBaseTableField)indexFields.get(i2); String fieldName = ConvenientCodegen.toLowerCaseFirst(ConvenientCodegen.camelCase(field.getFieldName(),"_")); DataBaseTableField f = dbt.findField(field.getFieldName());%>String <%= fieldName%><% if (i2<indexFields.size()-1){ // parameter separation %>, <% } // End parameter separation } /* End for loop i2 */ %>) throws SQLException{ return new <%=dbt.getClassName() %>DAO().selectBy<%=ConvenientCodegen.camelCase(index.getIndexName(),"_") %>(<% for (int i2=0;i2<indexFields.size();i2++){ // for loop i2 DataBaseTableField field = (DataBaseTableField)indexFields.get(i2); String fieldName = ConvenientCodegen.toLowerCaseFirst(ConvenientCodegen.camelCase(field.getFieldName(),"_")); DataBaseTableField f = dbt.findField(field.getFieldName());%><%= fieldName%><% if (i2<indexFields.size()-1){ // parameter separation %>, <% } // End parameter separation } /* End for loop i2 */ %>); } <% /* End if superclass*/ }%><% // END implementation of abstract methods of super class %> }