Menu

[r3096]: / branches / phpintegration / ch.sahits.codegen.example / jet_templates / dbbeanConverter.javajet  Maximize  Restore  History

Download this file

131 lines (125 with data), 5.3 kB

<%@ 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.*" package="PAGAGE_NAME" class="CLASS_NAME" %>
<%
/*
 * This Emitter Template creates a database bean.
 * The bean methods are defined in the first interface.
 * The constructor takes the interface and uses its
 * getter and setter methods to initialize
 * the values of this object.
 * Thus a Object can be initialized through a common
 * interface eaven if the two classes are not in the
 * same hierarchy.
 */
%>
<% DataBaseTable dbt = (DataBaseTable) argument; %>
<%=ConvenientCodegen.toPackageDefinition(dbt.getPackageName()) 
%><% List importClasses = dbt.usedClasses(); 
     for (Iterator i = importClasses.iterator(); i.hasNext(); ){
         Class c = (Class)i.next();
         if (!ConvenientCodegen.isPrimitiveClass(c)){
%>import <%=c.getName() %>;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;

<% } // 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() %>;
<% }
} %>
/**
 * This class is a bean class that has a constructor that
 * can be initialized through hierarchical cloning of an
 * instance of the bean interface. Thus an initialisation
 * with an instance that lies not in the same hierarchy
 * but implements the same interface can be initialized.
 * The initialisation referres to the getter and setter
 * methods defined in the interface.
 */
<% 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 {
			  		%>, <% }
			  %><% } 
%><% } 
%> {

<% List fields = dbt.getFields();
   for (Iterator i = fields.iterator(); i.hasNext(); ){
       DataBaseTableField field = (DataBaseTableField)i.next();
%>
    /** <code><%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %></code> holds the value of the field <%=field.getFieldName() %> */ 
    private <%=ConvenientCodegen.getSimpleClassName( field.getFieldType()) %> <%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %>;
<% } %>
<% Class[] interfaces = dbt.getInterfaces(); %>
	/**
	 * This constructor initialized this instance by
	 * initializing the values from the interface.
	 * @param bean
	 */
	public <%=dbt.getClassName() %>(<%=ConvenientCodegen.getSimpleClassName(interfaces[0]) %> bean){
		this();
		Class c = bean.getClass();
		Class thisC = getClass();
		Method[] methods = c.getDeclaredMethods();
		for (int i = 0; i < methods.length; i++) {
			if (methods[i].getName().startsWith("get")){
				String setterMethodName = "set"+methods[i].getName().substring(3);
				try {
					Class<?>[] type = methods[i].getParameterTypes();
					Method setter = thisC.getMethod(setterMethodName, type);
					setter.invoke(this, methods[i].invoke(bean, new Class[]{}));
				} catch (SecurityException e) {
					throw new RuntimeException(e.getMessage());
				} catch (NoSuchMethodException e) {
					throw new RuntimeException(e.getMessage());
				} catch (IllegalArgumentException e) {
					throw new RuntimeException(e.getMessage());
				} catch (IllegalAccessException e) {
					throw new RuntimeException(e.getMessage());
				} catch (InvocationTargetException e) {
					throw new RuntimeException(e.getMessage());
				}
			}
		}
	}
	/**
	 * Hidden default constructor
	 */
	private <%=dbt.getClassName() %>(){
		// do nothing
	}

<% 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 <%=ConvenientCodegen.getSimpleClassName( field.getFieldType()) %> <%= ConvenientCodegen.toName( "get_"+field.getFieldName(),"_" ) %>(){
        return <%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %>;
    }
    
    /**
     * Set the value of the field <%=field.getFieldName() %>.
     * @param <%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %> value to be set for <%=field.getFieldName() %>
     */
    public void <%= ConvenientCodegen.toName( "set_"+field.getFieldName(),"_" ) %>(<%=ConvenientCodegen.getSimpleClassName( field.getFieldType()) %> <%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %>){
        this.<%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %> = <%= ConvenientCodegen.toName( field.getFieldName(),"_" ) %>;
    }
    
<% } %>
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.