use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.
the class AbstractSQLStartup method writeAppMetadataToDB.
private void writeAppMetadataToDB(Connection connection, ApplicationInfo appInfo, Map<String, Long> tablePKs) throws SQLException {
String schemaName = this._state.schemaName().get();
SQLVendor vendor = this._vendor;
ModificationFactory m = vendor.getModificationFactory();
TableReferenceFactory t = vendor.getTableReferenceFactory();
LiteralFactory l = vendor.getLiteralFactory();
// @formatter:off
PreparedStatement ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
try {
Set<String> insertedTypeNames = new HashSet<>();
for (EntityDescriptor descriptor : appInfo.entityDescriptors.values()) {
for (Class<?> entityType : descriptor.types()) {
String entityTypeName = entityType.getName();
if (!insertedTypeNames.contains(entityTypeName)) {
long pk = tablePKs.get(ENTITY_TYPES_TABLE_NAME);
ps.setInt(1, (int) pk);
ps.setString(2, entityTypeName);
ps.executeUpdate();
this._state.entityTypePKs().get().put(entityTypeName, (int) pk);
// this._state.entityTypeInfos().get().put( entityTypeName, new EntityTypeInfo( descriptor, (int) pk ) );
tablePKs.put(ENTITY_TYPES_TABLE_NAME, pk + 1);
}
}
}
} finally {
SQLUtil.closeQuietly(ps);
}
ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, USED_CLASSES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
try {
for (CompositeDescriptorInfo descInfo : appInfo.usedValueComposites) {
String vDescStr = compositeDescriptorToString(descInfo.layer, descInfo.module, descInfo.composite);
long pk = tablePKs.get(USED_CLASSES_TABLE_NAME);
ps.setInt(1, (int) pk);
ps.setString(2, vDescStr);
ps.executeUpdate();
this._state.usedClassesPKs().get().put(descInfo.composite, (int) pk);
tablePKs.put(USED_CLASSES_TABLE_NAME, pk + 1);
}
} finally {
SQLUtil.closeQuietly(ps);
}
ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, ENUM_LOOKUP_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
try {
for (String enumValue : appInfo.enumValues) {
long pk = tablePKs.get(ENUM_LOOKUP_TABLE_NAME);
ps.setInt(1, (int) pk);
ps.setString(2, enumValue);
ps.executeUpdate();
this._state.enumPKs().get().put(enumValue, (int) pk);
tablePKs.put(ENUM_LOOKUP_TABLE_NAME, pk + 1);
}
} finally {
SQLUtil.closeQuietly(ps);
}
Statement stmt = connection.createStatement();
ps = connection.prepareStatement(this.createInsertStatementForQNameInfo(connection, schemaName, vendor).toString());
try {
DefinitionFactory d = vendor.getDefinitionFactory();
for (QNameInfo qNameInfo : this._state.qNameInfos().get().values()) {
QNameType type = qNameInfo.getQNameType();
TableElementListBuilder builder = d.createTableElementListBuilder();
builder.addTableElement(d.createColumnDefinition(ALL_QNAMES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false));
if (type.equals(QNameType.PROPERTY)) {
builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, this._primitiveTypes.get(Integer.class), true));
if (qNameInfo.getCollectionDepth() > 0) {
builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME, this.getCollectionPathDataType(), false));
}
this.appendColumnDefinitionsForProperty(builder, qNameInfo);
builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).addTargetColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
} else {
if (type.equals(QNameType.ASSOCIATION)) {
builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression()));
} else if (type.equals(QNameType.MANY_ASSOCIATION)) {
builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_ASSOCIATION_INDEX_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression()));
} else {
throw new IllegalArgumentException("Did not how to create table for qName type: " + type + ".");
}
builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_VALUE_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ENTITY_TABLE_NAME)).addTargetColumns(ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
tablePKs.put(qNameInfo.getTableName(), 0L);
}
builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).addTargetColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
stmt.execute(this._vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, qNameInfo.getTableName())).setTableContentsSource(builder.createExpression()).createExpression()));
// stmt.execute( "COMMENT ON TABLE " + schemaName + "." + qNameInfo.getTableName() + " IS '"
// + qNameInfo.getQName() + "'" );
ps.setString(1, qNameInfo.getQName().toString());
ps.setString(2, qNameInfo.getTableName());
ps.execute();
}
} finally {
SQLUtil.closeQuietly(stmt);
SQLUtil.closeQuietly(ps);
}
// @formatter:off
}
use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.
the class AbstractSQLStartup method initConnection.
@Override
public void initConnection() throws SQLException {
this._configuration.refresh();
this.initTypes();
this.modifyPrimitiveTypes(this._primitiveTypes, this._state.javaTypes2SQLTypes().get());
String schemaName = this._configuration.get().schemaName().get();
if (schemaName == null) {
schemaName = DEFAULT_SCHEMA_NAME;
} else {
this.checkSchemaName(schemaName);
}
LOGGER.debug("Will use '{}' as schema name", schemaName);
this._state.schemaName().set(schemaName);
this._state.entityTypePKs().set(new HashMap<String, Integer>());
this._state.usedClassesPKs().set(new HashMap<CompositeDescriptor, Integer>());
this._state.entityUsedQNames().set(new HashMap<EntityDescriptor, Set<QualifiedName>>());
this._state.qNameInfos().set(new HashMap<QualifiedName, QNameInfo>());
this._state.enumPKs().set(new HashMap<String, Integer>());
Connection connection = this._dataSource.getConnection();
try {
connection.setAutoCommit(true);
connection.setReadOnly(false);
this.syncDB(connection);
} finally {
SQLUtil.closeQuietly(connection);
}
if (LOGGER.isDebugEnabled()) {
String newline = "\n";
String tab = "\t";
String colonspace = ": ";
StringBuilder report = new StringBuilder();
report.append("schemaName: ").append(_state.schemaName().get()).append(newline);
report.append("qNameInfos: ").append(newline);
for (Map.Entry<QualifiedName, QNameInfo> entry : _state.qNameInfos().get().entrySet()) {
report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
}
report.append("entityUsedQNames:").append(newline);
for (Map.Entry<EntityDescriptor, Set<QualifiedName>> entry : _state.entityUsedQNames().get().entrySet()) {
report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
}
report.append("usedClassesPKs:").append(newline);
for (Map.Entry<CompositeDescriptor, Integer> entry : _state.usedClassesPKs().get().entrySet()) {
report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
}
report.append("javaTypes2SQLTypes:").append(newline);
for (Map.Entry<Class<?>, Integer> entry : _state.javaTypes2SQLTypes().get().entrySet()) {
report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
}
report.append("entityTypePKs:").append(newline);
for (Map.Entry<String, Integer> entry : _state.entityTypePKs().get().entrySet()) {
report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
}
report.append("enumPKs:").append(newline);
for (Map.Entry<String, Integer> entry : _state.enumPKs().get().entrySet()) {
report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
}
LOGGER.debug("SQLDBState after initConnection:\n{}", report.toString());
}
}
use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.
the class ValueToEntityMixin method update.
@Override
public void update(Object entity, Object value) throws NoSuchEntityException {
EntityComposite eComposite = (EntityComposite) entity;
ValueComposite vComposite = (ValueComposite) value;
EntityDescriptor eDesc = spi.entityDescriptorFor(eComposite);
AssociationStateHolder eState = spi.stateOf(eComposite);
AssociationStateDescriptor eStateDesc = eDesc.state();
ValueDescriptor vDesc = spi.valueDescriptorFor(vComposite);
AssociationStateHolder vState = spi.stateOf(vComposite);
AssociationStateDescriptor vStateDesc = vDesc.state();
Unqualified unqualified = vDesc.metaInfo(Unqualified.class);
if (unqualified == null || !unqualified.value()) {
doQualifiedUpdate(eState, eStateDesc, vState, vStateDesc);
} else {
doUnQualifiedUpdate(eState, eStateDesc, vState, vStateDesc);
}
}
use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.
the class ValueToEntityMixin method doConversion.
private <T> EntityBuilder<?> doConversion(Class<T> entityType, String identity, Object value) {
EntityDescriptor eDesc = module.entityDescriptor(entityType.getName());
if (eDesc == null) {
throw new EntityTypeNotFoundException(entityType.getName());
}
ValueComposite vComposite = (ValueComposite) value;
ValueDescriptor vDesc = spi.valueDescriptorFor(vComposite);
AssociationStateHolder vState = spi.stateOf(vComposite);
AssociationStateDescriptor vStateDesc = vDesc.state();
Unqualified unqualified = vDesc.metaInfo(Unqualified.class);
if (unqualified == null || !unqualified.value()) {
return doQualifiedConversion(entityType, identity, vState, vStateDesc);
}
return doUnqualifiedConversion(entityType, identity, vState, vStateDesc);
}
use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.
the class ApplicationVisitor method visitEnter.
@Override
public boolean visitEnter(Object visited) throws RuntimeException {
if (visited instanceof ApplicationDescriptor) {
ApplicationDescriptor applicationDescriptor = (ApplicationDescriptor) visited;
appUri = context.createApplicationUri(applicationDescriptor.name());
context.setNameAndType(appUri, applicationDescriptor.name(), Qi4jRdf.TYPE_APPLICATION);
}
if (visited instanceof LayerDescriptor) {
LayerDescriptor layerDescriptor = (LayerDescriptor) visited;
layerUri = context.createLayerUri(appUri, layerDescriptor.name());
context.setNameAndType(layerUri, layerDescriptor.name(), Qi4jRdf.TYPE_LAYER);
context.addRelationship(appUri, Qi4jRdf.RELATIONSHIP_LAYER, layerUri);
}
if (visited instanceof ModuleDescriptor) {
ModuleDescriptor moduleDescriptor = (ModuleDescriptor) visited;
moduleUri = context.createModuleUri(layerUri, moduleDescriptor.name());
context.setNameAndType(layerUri, moduleDescriptor.name(), Qi4jRdf.TYPE_MODULE);
context.addRelationship(layerUri, Qi4jRdf.RELATIONSHIP_MODULE, moduleUri);
}
if (visited instanceof TransientDescriptor) {
TransientDescriptor transientDescriptor = (TransientDescriptor) visited;
compositeUri = context.createCompositeUri(moduleUri, first(transientDescriptor.types()));
context.addType(compositeUri, Qi4jRdf.TYPE_COMPOSITE);
context.addRelationship(moduleUri, Qi4jRdf.RELATIONSHIP_COMPOSITE, compositeUri);
}
if (visited instanceof EntityDescriptor) {
EntityDescriptor entityDescriptor = (EntityDescriptor) visited;
compositeUri = context.createCompositeUri(moduleUri, first(entityDescriptor.types()));
context.addType(compositeUri, Qi4jRdf.TYPE_ENTITY);
context.addRelationship(moduleUri, Qi4jRdf.RELATIONSHIP_ENTITY, compositeUri);
}
if (visited instanceof ObjectDescriptor) {
ObjectDescriptor objectDescriptor = (ObjectDescriptor) visited;
compositeUri = context.createCompositeUri(moduleUri, first(objectDescriptor.types()));
context.addType(compositeUri, Qi4jRdf.TYPE_OBJECT);
context.addRelationship(moduleUri, Qi4jRdf.RELATIONSHIP_OBJECT, compositeUri);
}
if (visited instanceof MethodDescriptor) {
MethodDescriptor compositeMethodDescriptor = (MethodDescriptor) visited;
String compositeMethodUri = context.createCompositeMethodUri(compositeUri, compositeMethodDescriptor.method());
context.addType(compositeMethodUri, Qi4jRdf.TYPE_METHOD);
context.addRelationship(compositeUri, Qi4jRdf.RELATIONSHIP_METHOD, compositeMethodUri);
}
return true;
}
Aggregations