rb.templates.model.erb Maven / Gradle / Ivy
<%#
# Copyright 2011 Rapleaf
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
%>
<%= autogenerated %>
package <%= root_package %>.models;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import <%= JACK_NAMESPACE %>.AssociationType;
import <%= JACK_NAMESPACE %>.AttributesWithId;
import <%= JACK_NAMESPACE %>.BelongsToAssociation;
import <%= JACK_NAMESPACE %>.DefaultAssociationMetadata;
import <%= JACK_NAMESPACE %>.HasManyAssociation;
import <%= JACK_NAMESPACE %>.HasOneAssociation;
import <%= JACK_NAMESPACE %>.IAssociationMetadata;
import <%= JACK_NAMESPACE %>.IModelAssociationMetadata;
import <%= JACK_NAMESPACE %>.ModelIdWrapper;
import <%= JACK_NAMESPACE %>.ModelWithId;
import <%= JACK_NAMESPACE %>.queries.AbstractTable;
import <%= JACK_NAMESPACE %>.queries.Column;
import <%= project_defn.databases_namespace %>.IDatabases;
import <%= JACK_NAMESPACE %>.util.JackUtility;
<% model_defn.associations.each do |a| %>
<% if a.assoc_model.database_defn != model_defn.database_defn %>
<%= a.assoc_model.import %>
<% end %>
<% end %>
public class <%=model_defn.model_name%> extends ModelWithId<<%= model_defn.model_name %>, IDatabases> implements Comparable<<%=model_defn.model_name%>>{
public static final long serialVersionUID = <%= model_defn.serial_version_uid %>L;
public static class Tbl extends AbstractTable<<%= model_defn.model_name %>.Attributes, <%= model_defn.model_name %>> {
public final Column ID;
<% model_defn.fields.each do |field_defn| %>
public final Column<<%= field_defn.java_type(true) %>> <%= field_defn.name.upcase %>;
<% end %>
private Tbl(String alias) {
super("<%= model_defn.table_name %>", alias, <%= model_defn.model_name %>.Attributes.class, <%= model_defn.model_name %>.class);
this.ID = Column.fromId(alias);
<% model_defn.fields.each do |field_defn| %>
this.<%= field_defn.name.upcase %> = <%= field_defn.column_defn %>
<% end %>
Collections.addAll(this.allColumns, ID<% model_defn.fields.each_with_index do |field_defn, index| %>, <%= field_defn.name.upcase %><% end %>);
}
public static Tbl as(String alias) {
return new Tbl(alias);
}
}
public static final Tbl TBL = new Tbl("<%= model_defn.table_name %>");
public static final Column ID = TBL.ID;
<% model_defn.fields.each do |field_defn| %>
public static final Column<<%= field_defn.java_type(true) %>> <%= field_defn.name.upcase %> = TBL.<%= field_defn.name.upcase %>;
<% end %>
private final Attributes attributes;
private transient <%=model_defn.id_wrapper_name%> cachedTypedId;
// Associations
<% model_defn.associations.each do |assoc| %>
private <%= assoc.java_type %> <%= assoc.field_name %>;
<% end %>
public enum _Fields {
<% model_defn.fields.each do |field_defn| %>
<%= field_defn.name %>,
<% end %>
}
@Override
public <%=model_defn.id_wrapper_name%> getTypedId() {
if (cachedTypedId == null) {
cachedTypedId = new <%=model_defn.id_wrapper_name%>(this.getId());
}
return cachedTypedId;
}
public <%= model_defn.model_name %>(long id<%= create_signature_full.empty? ? "" : ", " %><%= create_signature_full %>, IDatabases databases) {
super(databases);
attributes = new Attributes(id<%= model_defn.fields.map{|fd| ", #{fd.name}"}.join %>);
<% model_defn.associations.each do |assoc| %>
this.<%= assoc.field_name %> = <%= assoc.constructor_call %>;
<% end %>
}
public <%= model_defn.model_name %>(long id<%= create_signature_full.empty? ? "" : ", " %><%= create_signature_full %>) {
super(null);
attributes = new Attributes(id<%= model_defn.fields.map{|fd| ", #{fd.name}"}.join %>);
}
<% if model_defn.should_make_created_at_methods %>
public <%= model_defn.model_name %>(long id<%= model_defn.create_signature_full("created_at").empty? ? "" : ", " %><%= model_defn.create_signature_full("created_at") %>, IDatabases databases) {
super(databases);
attributes = new Attributes(id<%= model_defn.fields.select{|fd| fd.name != "created_at"}.map{|fd| ", #{fd.name}"}.join %>);
<% model_defn.associations.each do |assoc| %>
this.<%= assoc.field_name %> = <%= assoc.constructor_call %>;
<% end %>
}
public <%= model_defn.model_name %>(long id<%= model_defn.create_signature_full("created_at").empty? ? "" : ", " %><%= model_defn.create_signature_full("created_at") %>) {
super(null);
attributes = new Attributes(id<%= model_defn.fields.select{|fd| fd.name != "created_at"}.map{|fd| ", #{fd.name}"}.join %>);
}
<% end %>
<% if !create_signature_small.nil? %>
public <%= model_defn.model_name %>(long id<%= create_signature_small.empty? ? "" : ", " %><%= create_signature_small %>, IDatabases databases) {
super(databases);
attributes = new Attributes(id<%= model_defn.fields.select{|fd| !fd.nullable?}.map{|fd| ", #{fd.name}"}.join %>);
<% model_defn.associations.each do |assoc| %>
this.<%= assoc.field_name %> = <%= assoc.constructor_call %>;
<% end %>
}
public <%= model_defn.model_name %>(long id<%= create_signature_small.empty? ? "" : ", " %><%= create_signature_small %>) {
super(null);
attributes = new Attributes(id<%= model_defn.fields.select{|fd| !fd.nullable?}.map{|fd| ", #{fd.name}"}.join %>);
}
<% if model_defn.should_make_created_at_methods %>
public <%= model_defn.model_name %>(long id<%= model_defn.create_signature_small("created_at").empty? ? "" : ", " %><%= model_defn.create_signature_small("created_at") %>, IDatabases databases) {
super(databases);
attributes = new Attributes(id<%= model_defn.fields.select{|fd| !fd.nullable? && fd.name != "created_at"}.map{|fd| ", #{fd.name}"}.join %>);
<% model_defn.associations.each do |assoc| %>
this.<%= assoc.field_name %> = <%= assoc.constructor_call %>;
<% end %>
}
public <%= model_defn.model_name %>(long id<%= model_defn.create_signature_small("created_at").empty? ? "" : ", " %><%= model_defn.create_signature_small("created_at") %>) {
super(null);
attributes = new Attributes(id<%= model_defn.fields.select{|fd| !fd.nullable? && fd.name != "created_at"}.map{|fd| ", #{fd.name}"}.join %>);
}
<% end %>
<% end %>
public static <%= model_defn.model_name %> newDefaultInstance(long id) {
<% if create_argument_defaults.empty? %>
return new <%= model_defn.model_name %>(id);
<% else %>
return new <%= model_defn.model_name %>(id, <%= create_argument_defaults %>);
<% end %>
}
public <%= model_defn.model_name %>(Attributes attributes, IDatabases databases) {
super(databases);
this.attributes = attributes;
if (databases != null) {
<% model_defn.associations.each do |assoc| %>
this.<%= assoc.field_name %> = <%= assoc.constructor_call %>;
<% end %>
}
}
public <%= model_defn.model_name %>(Attributes attributes) {
this(attributes, (IDatabases) null);
}
public <%= model_defn.model_name %>(long id, Map fieldsMap) {
super(null);
attributes = new Attributes(id, fieldsMap);
}
public <%= model_defn.model_name %> (<%= model_defn.model_name %> other) {
this(other, (IDatabases)null);
}
public <%= model_defn.model_name %> (<%= model_defn.model_name %> other, IDatabases databases) {
super(databases);
attributes = new Attributes(other.getAttributes());
if (databases != null) {
<% model_defn.associations.each do |assoc| %>
this.<%= assoc.field_name %> = <%= assoc.constructor_call %>;
<% end %>
}
}
public Attributes getAttributes() {
return attributes;
}
<% model_defn.fields.each do |field_defn| %>
public <%= field_defn.java_type %> <%= field_defn.getter %> {
return attributes.<%= field_defn.getter %>;
}
public <%= model_defn.model_name %> set<%= field_defn.name.camelcase %>(<%= field_defn.java_type %> newval) {
attributes.set<%= field_defn.name.camelcase %>(newval);
<% if field_defn.association != nil and field_defn.association.type == "belongs_to" %>
if(<%= field_defn.association.field_name %> != null){
this.<%= field_defn.association.field_name %>.setOwnerId(newval);
}
<% end %>
cachedHashCode = 0;
return this;
}
<% end %>
public void setField(_Fields field, Object value) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
set<%= field_defn.name.camelcase %>((<%= field_defn.java_type(true) %>)value);
break;
<% end %>
default:
throw new IllegalStateException("Invalid field: " + field);
}
}
public void setField(String fieldName, Object value) {
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
set<%= field_defn.name.camelcase %>((<%= field_defn.java_type(true) %>) value);
return;
}
<% end %>
throw new IllegalStateException("Invalid field: " + fieldName);
}
public static Class getFieldType(_Fields field) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
return <%= field_defn.java_type %>.class;
<% end %>
default:
throw new IllegalStateException("Invalid field: " + field);
}
}
public static Class getFieldType(String fieldName) {
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
return <%= field_defn.java_type %>.class;
}
<% end %>
throw new IllegalStateException("Invalid field name: " + fieldName);
}
<% model_defn.associations.each do |a| %>
public <%= a.assoc_getter_type %> <%= a.assoc_getter %>() throws IOException {
return <%= a.field_name %>.get();
}
<% end %>
@Override
public Object getField(String fieldName) {
if (fieldName.equals("id")) {
return getId();
}
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
return <%= field_defn.getter %>;
}
<% end %>
throw new IllegalStateException("Invalid field name: " + fieldName);
}
public Object getField(_Fields field) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
return <%= field_defn.getter %>;
<% end %>
}
throw new IllegalStateException("Invalid field: " + field);
}
public boolean hasField(String fieldName) {
if (fieldName.equals("id")) {
return true;
}
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
return true;
}
<% end %>
return false;
}
public static Object getDefaultValue(_Fields field) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
return <%= field_defn.default_value.nil? ? "null" : field_defn.default_value %>;
<% end %>
}
throw new IllegalStateException("Invalid field: " + field);
}
@Override
public Set getFieldSet() {
Set set = EnumSet.allOf(_Fields.class);
return set;
}
@Override
public <%= model_defn.model_name %> getCopy() {
return getCopy(databases);
}
@Override
public <%= model_defn.model_name %> getCopy(IDatabases databases) {
return new <%= model_defn.model_name %>(this, databases);
}
@Override
public boolean save() throws IOException {
return databases.<%= model_defn.database_defn.getter %>.<%= model_defn.persistence_getter %>.save(this);
}
<% model_defn.associations.each do |association| %>
<% assoc_field = association.assoc_model.fields.reject{|field_defn| field_defn.name != association.foreign_key }.first %>
<% assoc_field_type = assoc_field.java_type rescue nil %>
<% association_name = association.type == "belongs_to" ? association.foreign_key.gsub("_id", "").camelcase : association.name.camelcase %>
<% other_model_field = model_defn.fields.reject{|field_defn| field_defn.name != (association.foreign_key) }.first %>
<% new_model_name = "new" + association.assoc_model_name %>
<% if association.type == "belongs_to" %>
<% signatures = [ association.assoc_model.create_signature(true, association.foreign_key), association.assoc_model.create_signature(false, association.foreign_key) ] %>
<% arguments = [ association.assoc_model.field_names_list(true, false), association.assoc_model.field_names_list(false, false) ] %>
<% if !signatures.include?("") %>
<% signatures << "" %>
<% arguments << association.assoc_model.create_argument_defaults %>
<% end %>
<% signatures.zip(arguments).each do |signature, argument_list| %>
<% if !signature.nil? %>
public <%= association.assoc_model_name %> <%= "create" %><%= association_name %>(<%= signature %>) throws IOException {
<% if !assoc_field.nil? %>
<%= association.assoc_getter_type %> previous = <%= association.assoc_getter %>();
if (previous != null) {
<% if assoc_field.nullable? %>
previous.set<%= assoc_field.name.camelcase %>(null);
previous.save();
<% else %>
throw new RuntimeException("Cannot unset the foreign key on the associated object");
<% end %>
}
<% end %>
<% if !assoc_field_type.nil? %>
<%= assoc_field_type %> <%= association.foreign_key %> = <%= ["Integer", "int"].include?(assoc_field_type) ? "JackUtility.safeLongToInt(getId())" : "getId()" %>;
<% end %>
<%= association.assoc_model_name %> <%= new_model_name %> = databases.<%= association.assoc_model.database_defn.getter %>.<%= association.assoc_model.persistence_getter %>.create(<%= argument_list %>);
<% if !other_model_field.nil? %>
<% safe_cast_other_model_id = ["Integer", "int"].include?(other_model_field.java_type) %>
set<%= other_model_field.name.camelcase %>(<%= (safe_cast_other_model_id ? "JackUtility.safeLongToInt(" : "") + new_model_name + ".getId()" + (safe_cast_other_model_id ? ")" : "") %>);
<% end %>
save();
<%= association.field_name %>.clearCache();
return <%= new_model_name %>;
}
<% end %>
<% end %>
<% end %>
<% end %>
public String toString() {
return "<<%= model_defn.model_name %>"
+ " id: " + this.getId()
<% model_defn.fields.each do |field_defn| %>
+ " <%= field_defn.name %>: " + <%= field_defn.getter %>
<% end %>
+ ">";
}
public void unsetAssociations() {
unsetDatabaseReference();
<% model_defn.associations.each do |assoc| %>
<%= assoc.field_name %> = null;
<% end %>
}
public int compareTo(<%= model_defn.model_name %> that) {
return Long.valueOf(this.getId()).compareTo(that.getId());
}
public static class Attributes extends AttributesWithId {
public static final long serialVersionUID = <%= model_defn.attributes_serial_version_uid %>L;
// Fields
<% model_defn.fields.each do |field_defn| %>
private <%= field_defn.java_type %> __<%= field_defn.name %>;
<% end %>
public Attributes(long id) {
super(id);
}
<% if !create_signature_full.empty? %>
public Attributes(long id, <%= create_signature_full %>) {
super(id);
<% model_defn.fields.each do |field_defn| %>
this.__<%= field_defn.name %> = <%= field_defn.name %>;
<% end %>
}
<% end %>
<% if model_defn.should_make_created_at_methods %>
public Attributes(long id<%= model_defn.create_signature_full("created_at").empty? ? "" : ", " %><%= model_defn.create_signature_full("created_at") %>) {
super(id);
<% model_defn.fields.reject{|f| f.name == "created_at"}.each do |field_defn| %>
this.__<%= field_defn.name %> = <%= field_defn.name %>;
<% end %>
this.__created_at = System.currentTimeMillis();
}
<% end %>
<% if !create_signature_small.nil? && !create_signature_small.empty? %>
public Attributes(long id, <%= create_signature_small %>) {
super(id);
<% model_defn.fields.each do |field_defn| %>
<% if !field_defn.nullable? %>
this.__<%= field_defn.name %> = <%= field_defn.name %>;
<% end %>
<% end %>
}
<% if model_defn.should_make_created_at_methods %>
public Attributes(long id<%= model_defn.create_signature_small("created_at").empty? ? "" : ", " %><%= model_defn.create_signature_small("created_at") %>) {
super(id);
<% model_defn.fields.reject{|f| f.name == "created_at"}.each do |field_defn| %>
<% if !field_defn.nullable? %>
this.__<%= field_defn.name %> = <%= field_defn.name %>;
<% end %>
<% end %>
this.__created_at = System.currentTimeMillis();
}
<% end %>
<% end %>
public static Attributes newDefaultInstance(long id) {
<% if create_argument_defaults.empty? %>
return new Attributes(id);
<% else %>
return new Attributes(id, <%= create_argument_defaults %>);
<% end %>
}
public Attributes(long id, Map fieldsMap) {
super(id);
<% model_defn.fields.each do |field_defn| %>
<% if field_defn.default_value.nil? %>
<%= field_defn.java_type %> <%= field_defn.name %> = (<%= field_defn.java_type(true) %>)fieldsMap.get(<%= model_defn.model_name %>._Fields.<%= field_defn.name %>);
<% else %>
<%= field_defn.java_type(true) %> <%= field_defn.name %>_tmp = (<%= field_defn.java_type(true) %>)fieldsMap.get(<%= model_defn.model_name %>._Fields.<%= field_defn.name %>);
<%= field_defn.java_type %> <%= field_defn.name %> = <%= field_defn.name %>_tmp == null ? <%= field_defn.default_value %> : <%= field_defn.name %>_tmp;
<% end %>
<% end %>
<% model_defn.fields.each do |field_defn| %>
this.__<%= field_defn.name %> = <%= field_defn.name %>;
<% end %>
}
public Attributes(Attributes other) {
super(other.getId());
<% model_defn.fields.each do |field_defn| %>
<% copy = "other.#{field_defn.getter}" %>
<% copy = field_defn.java_type == 'byte[]' ? "copyBinary(#{copy})" : copy %>
this.__<%= field_defn.name %> = <%= copy %>;
<% end %>
}
<% model_defn.fields.each do |field_defn| %>
public <%= field_defn.java_type %> <%= field_defn.getter %> {
return __<%= field_defn.name %>;
}
public Attributes set<%= field_defn.name.camelcase %>(<%= field_defn.java_type %> newval) {
this.__<%= field_defn.name %> = newval;
cachedHashCode = 0;
return this;
}
<% end %>
public void setField(_Fields field, Object value) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
set<%= field_defn.name.camelcase %>((<%= field_defn.java_type(true) %>)value);
break;
<% end %>
default:
throw new IllegalStateException("Invalid field: " + field);
}
}
public void setField(String fieldName, Object value) {
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
set<%= field_defn.name.camelcase %>((<%= field_defn.java_type(true) %>)value);
return;
}
<% end %>
throw new IllegalStateException("Invalid field: " + fieldName);
}
public static Class getFieldType(_Fields field) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
return <%= field_defn.java_type %>.class;
<% end %>
default:
throw new IllegalStateException("Invalid field: " + field);
}
}
public static Class getFieldType(String fieldName) {
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
return <%= field_defn.java_type %>.class;
}
<% end %>
throw new IllegalStateException("Invalid field name: " + fieldName);
}
@Override
public Object getField(String fieldName) {
if (fieldName.equals("id")) {
return getId();
}
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
return <%= field_defn.getter %>;
}
<% end %>
throw new IllegalStateException("Invalid field name: " + fieldName);
}
public Object getField(_Fields field) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
return <%= field_defn.getter %>;
<% end %>
}
throw new IllegalStateException("Invalid field: " + field);
}
public boolean hasField(String fieldName) {
if (fieldName.equals("id")) {
return true;
}
<% model_defn.fields.each do |field_defn| %>
if (fieldName.equals("<%= field_defn.name %>")) {
return true;
}
<% end %>
return false;
}
public static Object getDefaultValue(_Fields field) {
switch (field) {
<% model_defn.fields.each do |field_defn| %>
case <%= field_defn.name %>:
return <%= field_defn.default_value.nil? ? "null" : field_defn.default_value %>;
<% end %>
}
throw new IllegalStateException("Invalid field: " + field);
}
@Override
public Set getFieldSet() {
Set set = EnumSet.allOf(_Fields.class);
return set;
}
public String toString() {
return "<<%= model_defn.model_name %>.Attributes"
<% model_defn.fields.each do |field_defn| %>
+ " <%= field_defn.name %>: " + <%= field_defn.getter %>
<% end %>
+ ">";
}
}
public static class Id implements ModelIdWrapper<<%=model_defn.id_wrapper_name%>> {
public static final long serialVersionUID = 1L;
private final long id;
public Id(Long id) {
this.id = id;
}
@Override
public Long getId() {
return id;
}
@Override
public int compareTo(Id other) {
return this.getId().compareTo(other.getId());
}
@Override
public boolean equals(Object other) {
if (other instanceof Id) {
return this.getId().equals(((Id)other).getId());
}
return false;
}
@Override
public int hashCode() {
return this.getId().hashCode();
}
@Override
public String toString() {
return "<<%= model_defn.model_name %>.Id: " + this.getId() + ">";
}
}
public static Set convertToAttributesSet(Collection<<%= model_defn.model_name%>> models) {
return models.stream()
.map(<%= model_defn.model_name%>::getAttributes)
.collect(Collectors.toSet());
}
public static class AssociationMetadata implements IModelAssociationMetadata {
private List meta = new ArrayList<>();
public AssociationMetadata(){
<% model_defn.associations.each do |assoc| %>
meta.add(new DefaultAssociationMetadata(AssociationType.<%= assoc.type.upcase %>, <%= model_defn.model_name %>.class, <%= assoc.assoc_model.model_name %>.class, "<%= assoc.foreign_key %>"));
<% end %>
}
@Override
public List getAssociationMetadata() {
return meta;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy