Created
October 30, 2012 05:51
-
-
Save m-nori/3978537 to your computer and use it in GitHub Desktop.
テンプレートエンジンTenjinのサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package #{@base_package}.client.dto.#{@meta_id}; | |
import java.io.Serializable; | |
<?rb if @group.fields.select{|field| (field.type != "String") && (field.multi) }.size != 0 ?> | |
import java.util.List; | |
import java.util.ArrayList; | |
<?rb end ?> | |
import org.apache.commons.lang.StringUtils; | |
import org.apache.commons.lang.builder.EqualsBuilder; | |
import org.apache.commons.lang.builder.HashCodeBuilder; | |
import org.apache.commons.lang.builder.StandardToStringStyle; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
/** | |
* #{@group.logic_name.force_encoding('ASCII-8BIT')}のDTO | |
* | |
* @generated | |
*/ | |
public class #{@class_name} implements Serializable { | |
/** | |
* serialVersionUID | |
* | |
* @generated | |
* @ordered | |
*/ | |
private static final long serialVersionUID = 1L; | |
<?rb @group.fields.each do |field| ?> | |
<?rb case ?> | |
<?rb when (field.type == "String") && (!field.multi) ?> | |
<?rb type = "String" ?> | |
<?rb init = "StringUtils.EMPTY" ?> | |
<?rb when (field.type == "String") && (field.multi) ?> | |
<?rb type = "String[]" ?> | |
<?rb init = "new String[0]" ?> | |
<?rb when (field.type != "String") && (!field.multi) ?> | |
<?rb type = to_camelcase(field.type) + "DTO" ?> | |
<?rb init = "new #{type}()" ?> | |
<?rb when (field.type != "String") && (field.multi) ?> | |
<?rb type = %|List<#{to_camelcase(field.type)}DTO>| ?> | |
<?rb init = "new Array#{type}()" ?> | |
<?rb end ?> | |
<?rb logic_name = field.logic_name.force_encoding('ASCII-8BIT') ?> | |
<?rb property_name = to_camelcase_l(field.name) ?> | |
/** | |
* #{logic_name} | |
* | |
* @generated | |
* @ordered | |
*/ | |
private #{type} #{property_name} = #{init}; | |
/** | |
* #{logic_name}を返却する. | |
* | |
* @return #{logic_name} | |
* @generated | |
*/ | |
public #{type} get#{to_camelcase(field.name)}() { | |
return #{property_name}; | |
} | |
/** | |
* #{logic_name}を設定する. | |
* | |
* @param #{property_name} | |
* #{logic_name} | |
* @generated | |
*/ | |
public void set#{to_camelcase(field.name)}(#{type} #{property_name}) { | |
this.#{property_name} = #{property_name}; | |
} | |
<?rb end ?> | |
/** | |
* @param other | |
* the reference object with which to compare. | |
* @return <code>true</code> if this object is the same as the obj argument; | |
* <code>false</code> otherwise. | |
* @see java.lang.Object#equals(Object other) | |
* @modifiable | |
*/ | |
public boolean equals(Object other) { | |
// begin-user-code | |
if (this == other) { | |
return true; | |
} | |
if (!(other instanceof #{@class_name})) { | |
return false; | |
} | |
return EqualsBuilder.reflectionEquals(this, other, false); | |
// end-user-code | |
} | |
/** | |
* @return a hash code value for this object. | |
* @see java.lang.Object#hashCode() | |
* @modifiable | |
*/ | |
public int hashCode() { | |
// begin-user-code | |
return HashCodeBuilder.reflectionHashCode(this, false); | |
// end-user-code | |
} | |
/** | |
* @return a string representation of the object. | |
* @see java.lang.Object#toString() | |
* @modifiable | |
*/ | |
public String toString() { | |
// begin-user-code | |
StandardToStringStyle style = new StandardToStringStyle(); | |
style.setUseShortClassName(true); | |
style.setUseIdentityHashCode(false); | |
return ToStringBuilder.reflectionToString(this, style, false); | |
// end-user-code | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DtoCreator | |
def create(conf, meta_id, rows, out_dir) | |
groups = create_model(meta_id, conf, rows).reverse.each_with_index do |group, i| | |
controller = DTOController.new(conf, meta_id, group) | |
if i == 0 | |
result = controller.create("template/RootDTO.rbjava") | |
else | |
result = controller.create("template/DTO.rbjava") | |
end | |
Dir::mkdir(%|#{out_dir}/dto/#{meta_id}|) unless File.exists?(%|#{out_dir}/dto/#{meta_id}|) | |
File.open(%|#{out_dir}/dto/#{meta_id}/#{to_camelcase(group.name)}DTO.java|, "w:utf-8") do |f| | |
f.puts result.force_encoding('utf-8') | |
end | |
end | |
end | |
class DTOController | |
include Common | |
include Tenjin::ContextHelper | |
@@engine = Tenjin::Engine.new() | |
def initialize(conf, meta_id, group) | |
@class_name = %|#{to_camelcase(group.name)}DTO| | |
@meta_id = meta_id | |
@group = group | |
@base_package = conf["java_base_package"] | |
end | |
def create(template) | |
@@engine.render(template, self) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package #{@base_package}.client.dto.#{@meta_id}; | |
import java.io.Serializable; | |
import java.util.Date; | |
import jp.co.nttdata.da.client.dto.DaDTO; | |
<?rb if @group.fields.select{|field| (field.type != "String") && (field.multi) }.size != 0 ?> | |
import java.util.List; | |
import java.util.ArrayList; | |
<?rb end ?> | |
import org.apache.commons.lang.StringUtils; | |
import org.apache.commons.lang.builder.EqualsBuilder; | |
import org.apache.commons.lang.builder.HashCodeBuilder; | |
import org.apache.commons.lang.builder.StandardToStringStyle; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
/** | |
* #{@group.logic_name.force_encoding('ASCII-8BIT')}のDTO | |
* | |
* @generated | |
*/ | |
public class #{@class_name} implements Serializable, DaDTO { | |
/** | |
* serialVersionUID | |
* | |
* @generated | |
* @ordered | |
*/ | |
private static final long serialVersionUID = 1L; | |
/** | |
* アイテムのバージョン. | |
*/ | |
private Long itemVersion; | |
/** | |
* アイテムバージョンを取得する. | |
* @return the itemVersion | |
*/ | |
public Long getItemVersion() { | |
return itemVersion; | |
} | |
/** | |
* アイテムバージョンを設定する. | |
* @param itemVersion the itemVersion to set | |
*/ | |
public void setItemVersion(Long itemVersion) { | |
this.itemVersion = itemVersion; | |
} | |
/** | |
* 削除フラグ. | |
*/ | |
private boolean deleteFlg; | |
/** | |
* 削除フラグ済みかどうかを取得する. | |
* @return | |
*/ | |
public boolean isDeleted() { | |
return deleteFlg; | |
} | |
/** | |
* 削除フラグを設定する. | |
* @param itemId | |
*/ | |
public void setDeleteFlg(boolean deleteFlg) { | |
this.deleteFlg = deleteFlg; | |
} | |
/** | |
* 公開済みフラグ. | |
*/ | |
private boolean openFlg; | |
/** | |
* 公開済みかどうかを取得する. | |
* @return | |
*/ | |
public boolean isOpend() { | |
return openFlg; | |
} | |
/** | |
* 貸出期限. | |
*/ | |
private Date lendLimit; | |
/** | |
* 貸出期限を返却する. | |
* @return the lendLimit | |
*/ | |
public Date getLendLimit() { | |
return lendLimit; | |
} | |
/** | |
* 貸出期限を設定する. | |
* @param lendLimit the lendLimit to set | |
*/ | |
public void setLendLimit(Date lendLimit) { | |
this.lendLimit = lendLimit; | |
} | |
/** | |
* 貸出状態. | |
*/ | |
private String lendStatus; | |
/** | |
* 貸出状態を返却する. | |
* @return the lendStatus | |
*/ | |
public String getLendStatus() { | |
return lendStatus; | |
} | |
/** | |
* 貸出期限を設定する. | |
* @param lendStatus the lendStatus to set | |
*/ | |
public void setLendStatus(String lendStatus) { | |
this.lendStatus = lendStatus; | |
} | |
/** | |
* 削除フラグを設定する. | |
* @param itemId | |
*/ | |
public void setOpenFlg(boolean openFlg) { | |
this.openFlg = openFlg; | |
} | |
<?rb @group.fields.each do |field| ?> | |
<?rb case ?> | |
<?rb when (field.type == "String") && (!field.multi) ?> | |
<?rb type = "String" ?> | |
<?rb init = "StringUtils.EMPTY" ?> | |
<?rb when (field.type == "String") && (field.multi) ?> | |
<?rb type = "String[]" ?> | |
<?rb init = "new String[0]" ?> | |
<?rb when (field.type != "String") && (!field.multi) ?> | |
<?rb type = to_camelcase(field.type) + "DTO" ?> | |
<?rb init = "new #{type}()" ?> | |
<?rb when (field.type != "String") && (field.multi) ?> | |
<?rb type = %|List<#{to_camelcase(field.type)}DTO>| ?> | |
<?rb init = "new Array#{type}()" ?> | |
<?rb end ?> | |
<?rb logic_name = field.logic_name.force_encoding('ASCII-8BIT') ?> | |
<?rb property_name = to_camelcase_l(field.name) ?> | |
/** | |
* #{logic_name} | |
* | |
* @generated | |
* @ordered | |
*/ | |
private #{type} #{property_name} = #{init}; | |
/** | |
* #{logic_name}を返却します。 | |
* | |
* @return #{logic_name} | |
* @generated | |
*/ | |
public #{type} get#{to_camelcase(field.name)}() { | |
return #{property_name}; | |
} | |
/** | |
* #{logic_name}を設定します。 | |
* | |
* @param #{property_name} | |
* #{logic_name} | |
* @generated | |
*/ | |
public void set#{to_camelcase(field.name)}(#{type} #{property_name}) { | |
this.#{property_name} = #{property_name}; | |
} | |
<?rb end ?> | |
/** | |
* @param other | |
* the reference object with which to compare. | |
* @return <code>true</code> if this object is the same as the obj argument; | |
* <code>false</code> otherwise. | |
* @see java.lang.Object#equals(Object other) | |
* @modifiable | |
*/ | |
public boolean equals(Object other) { | |
// begin-user-code | |
if (this == other) { | |
return true; | |
} | |
if (!(other instanceof #{@class_name})) { | |
return false; | |
} | |
return EqualsBuilder.reflectionEquals(this, other, false); | |
// end-user-code | |
} | |
/** | |
* @return a hash code value for this object. | |
* @see java.lang.Object#hashCode() | |
* @modifiable | |
*/ | |
public int hashCode() { | |
// begin-user-code | |
return HashCodeBuilder.reflectionHashCode(this, false); | |
// end-user-code | |
} | |
/** | |
* @return a string representation of the object. | |
* @see java.lang.Object#toString() | |
* @modifiable | |
*/ | |
public String toString() { | |
// begin-user-code | |
StandardToStringStyle style = new StandardToStringStyle(); | |
style.setUseShortClassName(true); | |
style.setUseIdentityHashCode(false); | |
return ToStringBuilder.reflectionToString(this, style, false); | |
// end-user-code | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment