* @param taggableEntity the taggable entity to associate with this taggable
* @return the join entity (you can probably ignore this)
*/
@SuppressWarnings("unchecked")
public static EOEntity registerTaggable(EOEntity entity, String tagsRelationshipName, EOEntity tagEntity, Class<? extends ERTaggableEntity<?>> taggableEntity) {
EORelationship tagsRelationship;
if (tagsRelationshipName == null) {
tagsRelationship = ERTaggableEntity.tagsRelationshipForEntity(entity, tagEntity);
}
else {
tagsRelationship = entity.relationshipNamed(tagsRelationshipName);
}
EOEntity joinEntity = null;
if (tagsRelationship == null) {
joinEntity = new EOEntity();
joinEntity.setName(entity.name() + "Tag");
joinEntity.setExternalName(joinEntity.name());
EORelationship joinToItemRelationship = new EORelationship();
joinToItemRelationship.setName(entity.name());
joinToItemRelationship.setIsMandatory(true);
joinToItemRelationship.setToMany(false);
joinToItemRelationship.setJoinSemantic(EORelationship.InnerJoin);
joinEntity.addRelationship(joinToItemRelationship);
for (EOAttribute itemPrimaryKey : entity.primaryKeyAttributes()) {
EOAttribute itemFKAttribute = new EOAttribute();
itemFKAttribute.setExternalType(itemPrimaryKey.externalType());
itemFKAttribute.setValueType(itemPrimaryKey.valueType());
itemFKAttribute.setName("item_" + itemPrimaryKey.name());
itemFKAttribute.setColumnName("item_" + itemPrimaryKey.columnName());
itemFKAttribute.setClassName(itemPrimaryKey.className());
itemFKAttribute.setWidth(itemPrimaryKey.width());
itemFKAttribute.setPrecision(itemPrimaryKey.precision());
itemFKAttribute.setScale(itemPrimaryKey.scale());
itemFKAttribute.setAllowsNull(false);
joinEntity.addAttribute(itemFKAttribute);
EOJoin join = new EOJoin(itemFKAttribute, itemPrimaryKey);
joinToItemRelationship.addJoin(join);
}
EORelationship joinToTagRelationship = new EORelationship();
joinToTagRelationship.setName(tagEntity.name());
joinToTagRelationship.setIsMandatory(true);
joinToTagRelationship.setToMany(false);
joinToTagRelationship.setJoinSemantic(EORelationship.InnerJoin);
joinEntity.addRelationship(joinToTagRelationship);
for (EOAttribute tagPrimaryKey : tagEntity.primaryKeyAttributes()) {
EOAttribute tagFKAttribute = new EOAttribute();
tagFKAttribute.setExternalType(tagPrimaryKey.externalType());
tagFKAttribute.setValueType(tagPrimaryKey.valueType());
tagFKAttribute.setName("tag_" + tagPrimaryKey.name());
tagFKAttribute.setColumnName("tag_" + tagPrimaryKey.columnName());
tagFKAttribute.setClassName(tagPrimaryKey.className());
tagFKAttribute.setWidth(tagPrimaryKey.width());
tagFKAttribute.setPrecision(tagPrimaryKey.precision());
tagFKAttribute.setScale(tagPrimaryKey.scale());
tagFKAttribute.setAllowsNull(false);
joinEntity.addAttribute(tagFKAttribute);
joinToTagRelationship.addJoin(new EOJoin(tagFKAttribute, tagPrimaryKey));
}
joinEntity.setPrimaryKeyAttributes(joinEntity.attributes());
joinEntity.setAttributesUsedForLocking(joinEntity.attributes());
entity.model().addEntity(joinEntity);
EORelationship itemToJoinRelationship = new EORelationship();
itemToJoinRelationship.setEntity(joinToItemRelationship.destinationEntity());
itemToJoinRelationship.setName("_eofInv_" + joinToItemRelationship.entity().name() + "_" + joinToItemRelationship.name());
NSArray<EOJoin> joinToItemRelationshipJoins = joinToItemRelationship.joins();
for (int joinNum = joinToItemRelationshipJoins.count() - 1; joinNum >= 0; joinNum--) {
EOJoin join = joinToItemRelationshipJoins.objectAtIndex(joinNum);
EOJoin inverseJoin = new EOJoin(join.destinationAttribute(), join.sourceAttribute());
itemToJoinRelationship.addJoin(inverseJoin);
}
itemToJoinRelationship.setDeleteRule(1); // cascade
itemToJoinRelationship.setJoinSemantic(EORelationship.InnerJoin);
itemToJoinRelationship.setToMany(true);
itemToJoinRelationship.setPropagatesPrimaryKey(true);
entity.addRelationship(itemToJoinRelationship);
NSMutableArray properties = entity.classProperties().mutableClone();
properties.remove(itemToJoinRelationship);
entity.setClassProperties(properties);
EORelationship itemToTagsRelationship = new EORelationship();
itemToTagsRelationship.setName(tagsRelationshipName);
entity.addRelationship(itemToTagsRelationship);
itemToTagsRelationship.setDefinition(itemToJoinRelationship.name() + "." + joinToTagRelationship.name());
tagsRelationship = itemToTagsRelationship;
}
else if (!tagsRelationship.isFlattened()) {
throw new IllegalArgumentException("The relationship '" + tagsRelationship.name() + "' on '" + entity.name() + "' must be flattened.");
}
else {
EORelationship itemToJoinRelationship = (EORelationship) tagsRelationship.componentRelationships().objectAtIndex(0);
joinEntity = itemToJoinRelationship.destinationEntity();
}
NSMutableDictionary userInfo = entity.userInfo().mutableClone();
userInfo.setObjectForKey(Boolean.TRUE, ERTaggableEntity.ERTAGGABLE_KEY);
userInfo.setObjectForKey(tagsRelationship.name(), ERTaggableEntity.ERTAGGABLE_TAG_RELATIONSHIP_KEY);