Class<? extends Model> getModelClass(String tableName) { MetaModel mm = metaModelsByTableName.get(tableName); return mm == null ? null : mm.getModelClass(); }
static <T extends Model> T instance(Map<String, Object> map, MetaModel metaModel) { return (T) instance(map, metaModel, metaModel.getModelClass()); }
private Object tryParent(String parentTable){ MetaModel parentMM = inferTargetMetaModel(parentTable); if(parentMM == null){ return null; }else return metaModelLocal.hasAssociation(parentMM.getModelClass(), BelongsToAssociation.class) ? parent(parentMM.getModelClass()): null; }
private Object tryPolymorphicChildren(String childTable){ MetaModel childMM = inferTargetMetaModel(childTable); if(childMM == null){ return null; }else return metaModelLocal.hasAssociation(childMM.getModelClass(), OneToManyPolymorphicAssociation.class) ? getAll(childMM.getModelClass()): null; }
private Object tryOther(String otherTable){ MetaModel otherMM = inferTargetMetaModel(otherTable); if(otherMM == null){ return null; }else return metaModelLocal.hasAssociation(otherMM.getModelClass(), Many2ManyAssociation.class) ? getAll(otherMM.getModelClass()): null; }
private Object tryChildren(String childTable){ MetaModel childMM = inferTargetMetaModel(childTable); if(childMM == null){ return null; }else return metaModelLocal.hasAssociation(childMM.getModelClass(), OneToManyAssociation.class) ? getAll(childMM.getModelClass()): null; }
private Object tryPolymorphicParent(String parentTable){ MetaModel parentMM = inferTargetMetaModel(parentTable); if(parentMM == null){ return null; }else return metaModelLocal.hasAssociation(parentMM.getModelClass(), BelongsToPolymorphicAssociation.class) ? parent(parentMM.getModelClass()): null; }
static <T extends Model> T instance(Map<String, Object> map, MetaModel metaModel, Class<T> clazz) { try { T instance = clazz.newInstance(); instance.hydrate(map, true); return instance; } catch(InstantiationException e) { throw new InitException("Failed to create a new instance of: " + metaModel.getModelClass() + ", are you sure this class has a default constructor?"); } catch(DBException e) { throw e; } catch(InitException e) { throw e; } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } }
/** * Checks if this model has a named attribute that has the same name as argument. * * Throws <code>IllegalArgumentException</code> in case it does not find it. * * @param attribute name of attribute or association target. */ protected void checkAttribute(String attribute) { if (!hasAttribute(attribute)) { String sb = "Attribute: '" + attribute + "' is not defined in model: '" + getModelClass() + ". " + "Available attributes: " +getAttributeNames(); throw new IllegalArgumentException(sb); } }
/** * Generates a XML document from content of this list. * * @param pretty pretty format (human readable), or one line text. * @param declaration true to include XML declaration at the top * @param attrs list of attributes to include. No arguments == include all attributes. * @return generated XML. */ public String toXml(boolean pretty, boolean declaration, String... attrs) { String topNode = Inflector.pluralize(Inflector.underscore(metaModel.getModelClass().getSimpleName())); hydrate(); StringBuilder sb = new StringBuilder(); if(declaration) { sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); if (pretty) sb.append('\n'); } sb.append('<').append(topNode).append('>'); if (pretty) { sb.append('\n'); } for (T t : delegate) { t.toXmlP(sb, pretty, pretty ? " " : "", attrs); } sb.append("</").append(topNode).append('>'); if (pretty) { sb.append('\n'); } return sb.toString(); }
protected String toJSON() { List models = new ArrayList(); metaModelsByTableName.values().forEach(metaModel -> { List associations = new ArrayList(); metaModel.getAssociations().forEach(association -> associations.add(association.toMap())); models.add(map( MODEL_CLASS, metaModel.getModelClass().getName(), DB_TYPE, metaModel.getDbType(), DB_NAME, metaModel.getDbName(), COLUMN_METADATA, metaModel.getColumnMetadata(), ASSOCIATIONS, associations )); }); return JsonHelper.toJsonString(models,false); }
if(!metaModel.isAssociatedTo(clazz)) throw new IllegalArgumentException("Model: " + clazz.getName() + " is not associated with: " + metaModel.getModelClass().getName());
addMetaModel(metaModel, metaModel.getModelClass());
private void processPolymorphicChildren(OneToManyPolymorphicAssociation association) { if (delegate.isEmpty()) {//no need to process children if no models selected. return; } MetaModel childMetaModel = metaModelOf(association.getTargetClass()); Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); StringBuilder query = new StringBuilder().append("parent_id IN ("); appendQuestions(query, ids.size()); query.append(") AND parent_type = '").append(association.getTypeLabel()).append('\''); for (Model child : new LazyList<>(query.toString(), childMetaModel, ids.toArray()).orderBy(childMetaModel.getIdName())) { if (childrenByParentId.get(child.get("parent_id")) == null) { childrenByParentId.put(child.get("parent_id"), new SuperLazyList<>()); } childrenByParentId.get(child.get("parent_id")).add(child); } for (T parent : delegate) { List<Model> children = childrenByParentId.get(parent.getId()); if (children != null) { parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
/** * Sets a parent on this instance. Basically this sets a correct value of a foreign key in a * parent/child relationship. This only works for one to many and polymorphic associations. * The act of setting a parent does not result in saving to a database. * * @param parent potential parent of this instance. Its ID value must not be null. */ public void setParent(Model parent) { if (parent == null || parent.getId() == null) { throw new IllegalArgumentException("parent cannot ne null and parent ID cannot be null"); } List<Association> associations = metaModelLocal.getAssociations(); for (Association association : associations) { if (association instanceof BelongsToAssociation && association.getTargetClass().equals(parent.metaModelLocal.getModelClass())) { set(((BelongsToAssociation)association).getFkName(), parent.getId()); return; } if(association instanceof BelongsToPolymorphicAssociation && association.getTargetClass().equals(parent.metaModelLocal.getModelClass())){ set("parent_id", parent.getId()); set("parent_type", ((BelongsToPolymorphicAssociation)association).getTypeLabel()); return; } } StringBuilder sb = new StringBuilder(); sb.append("Class: ").append(parent.getClass()).append(" is not associated with ").append(this.getClass()) .append(", list of existing associations:\n"); join(sb, metaModelLocal.getAssociations(), "\n"); throw new IllegalArgumentException(sb.toString()); }
private void processChildren(OneToManyAssociation association) { if(delegate.isEmpty()){//no need to process children if no models selected. return; } final MetaModel childMetaModel = metaModelOf(association.getTargetClass()); final String fkName = association.getFkName(); final Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); StringBuilder query = new StringBuilder().append(fkName).append(" IN ("); appendQuestions(query, ids.size()); query.append(')'); for (Model child : new LazyList<>(query.toString(), childMetaModel, ids.toArray()).orderBy(childMetaModel.getIdName())) { if(childrenByParentId.get(child.get(fkName)) == null){ childrenByParentId.put(child.get(fkName), new SuperLazyList<>()); } childrenByParentId.get(child.get(fkName)).add(child); } for(T parent : delegate){ List<Model> children = childrenByParentId.get(parent.getId()); if(children != null){ parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
private void processManyToMany(Many2ManyAssociation association) { if(delegate.isEmpty()){//no need to process other if no models selected. return; } final MetaModel childMetaModel = metaModelOf(association.getTargetClass()); final Map<Object, List<Model>> childrenByParentId = new HashMap<>(); List<Object> ids = collect(metaModel.getIdName()); List<Map> childResults = new DB(childMetaModel.getDbName()).findAll(childMetaModel.getDialect().selectManyToManyAssociation( association, "the_parent_record_id", ids.size()), ids.toArray()); for(Map res: childResults){ Model child = ModelDelegate.instance(res, childMetaModel); Object parentId = res.get("the_parent_record_id"); if(childrenByParentId.get(parentId) == null){ childrenByParentId.put(parentId, new SuperLazyList<>()); } childrenByParentId.get(parentId).add(child); } for(T parent : delegate){ List<Model> children = childrenByParentId.get(parent.getId()); if (children != null) { parent.setChildren(childMetaModel.getModelClass(), children); } else { parent.setChildren(childMetaModel.getModelClass(), new SuperLazyList<>()); } } }
Model joinModel = joinMetaModel.getModelClass().newInstance(); joinModel.set(ass.getSourceFkName(), getId()); joinModel.set(ass.getTargetFkName(), child.getId());
subQuery = "parent_id = ? AND " + " parent_type = '" + oneToManyPolymorphicAssociation.getTypeLabel() + "'" + additionalCriteria; } else { throw new NotAssociatedException(metaModelLocal.getModelClass(), targetModelClass);
private Object tryOther(String otherTable){ MetaModel otherMM = inferTargetMetaModel(otherTable); if(otherMM == null){ return null; }else return metaModelLocal.hasAssociation(otherMM.getModelClass(), Many2ManyAssociation.class) ? getAll(otherMM.getModelClass()): null; }