use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class DefaultValueTransformerFactory method getJavaType.
// TODO: calculating Java type of ObjAttribute may become unneeded per
// CAY-1752, as DbAttribute will have it.
protected String getJavaType(DbAttribute a) {
DbEntity dbEntity = a.getEntity();
DataMap dataMap = dbEntity.getDataMap();
Collection<String> javaTypes = new HashSet<>();
for (ObjEntity objEntity : dataMap.getMappedEntities(dbEntity)) {
for (ObjAttribute oa : objEntity.getAttributes()) {
// TODO: this won't pick up flattened attributes
if (a.getName().equals(oa.getDbAttributePath())) {
javaTypes.add(oa.getType());
}
}
}
if (javaTypes.size() != 1) {
String javaType = TypesMapping.getJavaBySqlType(a.getType());
String attributeName = dbEntity.getName() + "." + a.getName();
String msg = javaTypes.size() > 1 ? "ObjAttributes with different java types" : "No ObjAttributes";
// Warn user about this problem as there is nothing else we can do
logger.warn(msg + " bound to DbAttribute '" + attributeName + "', " + javaType + " type will be used.");
return javaType;
}
return javaTypes.iterator().next();
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class EntityMergeSupport method createObjRelationship.
private boolean createObjRelationship(ObjEntity entity, DbRelationship dr, String targetEntityName) {
ObjRelationship or = new ObjRelationship();
or.setName(NameBuilder.builder(or, entity).baseName(nameGenerator.relationshipName(dr)).name());
or.addDbRelationship(dr);
Map<String, ObjEntity> objEntities = entity.getDataMap().getSubclassesForObjEntity(entity);
boolean hasFlattingAttributes = false;
boolean needGeneratedEntity = true;
if (objEntities.containsKey(targetEntityName)) {
needGeneratedEntity = false;
}
for (ObjEntity subObjEntity : objEntities.values()) {
for (ObjAttribute objAttribute : subObjEntity.getAttributes()) {
String path = objAttribute.getDbAttributePath();
if (path != null) {
if (path.startsWith(or.getDbRelationshipPath())) {
hasFlattingAttributes = true;
break;
}
}
}
}
if (!hasFlattingAttributes) {
if (needGeneratedEntity) {
or.setTargetEntityName(targetEntityName);
or.setSourceEntity(entity);
}
entity.addRelationship(or);
fireRelationshipAdded(or);
}
return needGeneratedEntity;
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class DropColumnToModel method execute.
@Override
public void execute(MergerContext mergerContext) {
// remove relationships mapped to column. duplicate List to prevent
// ConcurrentModificationException
List<DbRelationship> dbRelationships = new ArrayList<>(getEntity().getRelationships());
for (DbRelationship dbRelationship : dbRelationships) {
for (DbJoin join : dbRelationship.getJoins()) {
if (join.getSource() == getColumn() || join.getTarget() == getColumn()) {
remove(mergerContext.getDelegate(), dbRelationship, true);
}
}
}
// remove ObjAttribute mapped to same column
for (ObjEntity objEntity : getEntity().mappedObjEntities()) {
ObjAttribute objAttribute = objEntity.getAttributeForDbAttribute(getColumn());
if (objAttribute != null) {
objEntity.removeAttribute(objAttribute.getName());
mergerContext.getDelegate().objAttributeRemoved(objAttribute);
}
}
// remove DbAttribute
getEntity().removeAttribute(getColumn().getName());
mergerContext.getDelegate().dbAttributeRemoved(getColumn());
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class MergerFactoryIT method testAddTableToDb.
@Test
public void testAddTableToDb() throws Exception {
dropTableIfPresent("NEW_TABLE");
assertTokensAndExecute(0, 0);
DbEntity dbEntity = new DbEntity("NEW_TABLE");
DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
column1.setMandatory(true);
column1.setPrimaryKey(true);
dbEntity.addAttribute(column1);
DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
column2.setMaxLength(10);
column2.setMandatory(false);
dbEntity.addAttribute(column2);
map.addDbEntity(dbEntity);
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
ObjEntity objEntity = new ObjEntity("NewTable");
objEntity.setDbEntity(dbEntity);
ObjAttribute oatr1 = new ObjAttribute("name");
oatr1.setDbAttributePath(column2.getName());
oatr1.setType("java.lang.String");
objEntity.addAttribute(oatr1);
map.addObjEntity(objEntity);
for (int i = 0; i < 5; i++) {
CayenneDataObject dao = (CayenneDataObject) context.newObject(objEntity.getName());
dao.writeProperty(oatr1.getName(), "test " + i);
}
context.commitChanges();
// clear up
map.removeObjEntity(objEntity.getName(), true);
map.removeDbEntity(dbEntity.getName(), true);
resolver.refreshMappingCache();
assertNull(map.getObjEntity(objEntity.getName()));
assertNull(map.getDbEntity(dbEntity.getName()));
assertFalse(map.getDbEntities().contains(dbEntity));
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class ObjAttributeTableModel method setObjAttributeType.
private void setObjAttributeType(ObjAttributeWrapper attribute, Object value) {
String oldType = attribute.getType();
String newType = value != null ? value.toString() : null;
attribute.setType(newType);
if (oldType == null || newType == null) {
return;
}
String[] registeredTypes = ModelerUtil.getRegisteredTypeNames();
Collection<String> registeredTypesList = Arrays.asList(registeredTypes);
if (registeredTypesList.contains(oldType) == registeredTypesList.contains(newType)) {
return;
}
ObjEntity entity = attribute.getEntity();
ObjAttribute attributeNew;
if (registeredTypesList.contains(newType) || !mediator.getEmbeddableNamesInCurrentDataDomain().contains(newType)) {
attributeNew = new ObjAttribute();
attributeNew.setDbAttributePath(attribute.getDbAttributePath());
} else {
attributeNew = new EmbeddedAttribute();
attributeNew.setDbAttributePath(null);
}
attributeNew.setName(attribute.getName());
attributeNew.setEntity(entity);
attributeNew.setParent(attribute.getParent());
attributeNew.setType(attribute.getType());
attributeNew.setUsedForLocking(attribute.isUsedForLocking());
entity.updateAttribute(attributeNew);
mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent(this, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
mediator.fireObjAttributeEvent(new AttributeEvent(this, attributeNew, entity, MapEvent.CHANGE));
mediator.fireObjAttributeDisplayEvent(new AttributeDisplayEvent(this, attributeNew, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
}
Aggregations