use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.
the class MapEntityStoreMixin method readEntityState.
protected EntityState readEntityState(DefaultEntityStoreUnitOfWork unitOfWork, Reader entityState) throws EntityStoreException {
try {
Module module = unitOfWork.module();
JSONObject jsonObject = new JSONObject(new JSONTokener(entityState));
EntityStatus status = EntityStatus.LOADED;
String version = jsonObject.getString(JSONKeys.VERSION);
long modified = jsonObject.getLong(JSONKeys.MODIFIED);
String identity = jsonObject.getString(JSONKeys.IDENTITY);
// Check if version is correct
String currentAppVersion = jsonObject.optString(JSONKeys.APPLICATION_VERSION, "0.0");
if (!currentAppVersion.equals(application.version())) {
if (migration != null) {
migration.migrate(jsonObject, application.version(), this);
} else {
// Do nothing - set version to be correct
jsonObject.put(JSONKeys.APPLICATION_VERSION, application.version());
}
LoggerFactory.getLogger(MapEntityStoreMixin.class).debug("Updated version nr on " + identity + " from " + currentAppVersion + " to " + application.version());
// State changed
status = EntityStatus.UPDATED;
}
String type = jsonObject.getString(JSONKeys.TYPE);
EntityDescriptor entityDescriptor = module.entityDescriptor(type);
if (entityDescriptor == null) {
throw new EntityTypeNotFoundException(type);
}
Map<QualifiedName, Object> properties = new HashMap<>();
JSONObject props = jsonObject.getJSONObject(JSONKeys.PROPERTIES);
for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
Object jsonValue;
try {
jsonValue = props.get(propertyDescriptor.qualifiedName().name());
} catch (JSONException e) {
// Value not found, default it
Object initialValue = propertyDescriptor.initialValue(module);
properties.put(propertyDescriptor.qualifiedName(), initialValue);
status = EntityStatus.UPDATED;
continue;
}
if (JSONObject.NULL.equals(jsonValue)) {
properties.put(propertyDescriptor.qualifiedName(), null);
} else {
Object value = valueSerialization.deserialize(propertyDescriptor.valueType(), jsonValue.toString());
properties.put(propertyDescriptor.qualifiedName(), value);
}
}
Map<QualifiedName, EntityReference> associations = new HashMap<>();
JSONObject assocs = jsonObject.getJSONObject(JSONKeys.ASSOCIATIONS);
for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
try {
Object jsonValue = assocs.get(associationType.qualifiedName().name());
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
associations.put(associationType.qualifiedName(), value);
} catch (JSONException e) {
// Association not found, default it to null
associations.put(associationType.qualifiedName(), null);
status = EntityStatus.UPDATED;
}
}
JSONObject manyAssocs = jsonObject.getJSONObject(JSONKeys.MANY_ASSOCIATIONS);
Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
List<EntityReference> references = new ArrayList<>();
try {
JSONArray jsonValues = manyAssocs.getJSONArray(manyAssociationType.qualifiedName().name());
for (int i = 0; i < jsonValues.length(); i++) {
Object jsonValue = jsonValues.getString(i);
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
references.add(value);
}
manyAssociations.put(manyAssociationType.qualifiedName(), references);
} catch (JSONException e) {
// ManyAssociation not found, default to empty one
manyAssociations.put(manyAssociationType.qualifiedName(), references);
}
}
JSONObject namedAssocs = jsonObject.getJSONObject(JSONKeys.NAMED_ASSOCIATIONS);
Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
for (AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations()) {
Map<String, EntityReference> references = new LinkedHashMap<>();
try {
JSONObject jsonValues = namedAssocs.getJSONObject(namedAssociationType.qualifiedName().name());
JSONArray names = jsonValues.names();
if (names != null) {
for (int idx = 0; idx < names.length(); idx++) {
String name = names.getString(idx);
Object value = jsonValues.get(name);
EntityReference ref = value == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) value);
references.put(name, ref);
}
}
namedAssociations.put(namedAssociationType.qualifiedName(), references);
} catch (JSONException e) {
// NamedAssociation not found, default to empty one
namedAssociations.put(namedAssociationType.qualifiedName(), references);
}
}
return new DefaultEntityState(unitOfWork, version, modified, EntityReference.parseEntityReference(identity), status, entityDescriptor, properties, associations, manyAssociations, namedAssociations);
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.
the class ValueSerializerAdapter method serializeValueComposite.
private void serializeValueComposite(Options options, Object object, OutputType output, boolean rootPass) throws Exception {
CompositeInstance valueInstance = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) object);
ValueDescriptor descriptor = (ValueDescriptor) valueInstance.descriptor();
AssociationStateHolder state = (AssociationStateHolder) valueInstance.state();
onObjectStart(output);
if (options.getBoolean(Options.INCLUDE_TYPE_INFO) && !rootPass) {
onFieldStart(output, "_type");
onValueStart(output);
onValue(output, first(descriptor.valueType().types()).getName());
onValueEnd(output);
onFieldEnd(output);
}
for (PropertyDescriptor persistentProperty : descriptor.valueType().properties()) {
Property<?> property = state.propertyFor(persistentProperty.accessor());
onFieldStart(output, persistentProperty.qualifiedName().name());
onValueStart(output);
doSerialize(options, property.get(), output, false);
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().associations()) {
Association<?> association = state.associationFor(associationDescriptor.accessor());
Object instance = association.get();
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
if (instance == null) {
onValue(output, null);
} else {
onValue(output, ((Identity) instance).identity().get());
}
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().manyAssociations()) {
ManyAssociation<?> manyAssociation = state.manyAssociationFor(associationDescriptor.accessor());
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
onArrayStart(output);
for (Object instance : manyAssociation) {
onValueStart(output);
onValue(output, ((Identity) instance).identity().get());
onValueEnd(output);
}
onArrayEnd(output);
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().namedAssociations()) {
NamedAssociation<?> namedAssociation = state.namedAssociationFor(associationDescriptor.accessor());
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
onObjectStart(output);
for (String name : namedAssociation) {
onFieldStart(output, name);
onValueStart(output);
onValue(output, ((Identity) namedAssociation.get(name)).identity().get());
onValueEnd(output);
onFieldEnd(output);
}
onObjectEnd(output);
onValueEnd(output);
onFieldEnd(output);
}
onObjectEnd(output);
}
use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.
the class AbstractSQLStartup method extractAssociationQNames.
private void extractAssociationQNames(EntityDescriptor entityDesc, Map<QualifiedName, QNameInfo> extractedQNames, Set<QualifiedName> newQNames, Boolean setQNameTableNameToNull) {
for (AssociationDescriptor assoDesc : entityDesc.state().associations()) {
if (SQLSkeletonUtil.isQueryable(assoDesc.accessor())) {
QualifiedName qName = assoDesc.qualifiedName();
if (!extractedQNames.containsKey(qName)) {
//
extractedQNames.put(//
qName, //
QNameInfo.fromAssociation(//
qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + extractedQNames.size()), //
assoDesc));
newQNames.add(qName);
}
}
}
}
use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.
the class AbstractSQLStartup method extractManyAssociationQNames.
private void extractManyAssociationQNames(EntityDescriptor entityDesc, Map<QualifiedName, QNameInfo> extractedQNames, Set<QualifiedName> newQNames, Boolean setQNameTableNameToNull) {
for (AssociationDescriptor mAssoDesc : entityDesc.state().manyAssociations()) {
QualifiedName qName = mAssoDesc.qualifiedName();
if (SQLSkeletonUtil.isQueryable(mAssoDesc.accessor())) {
if (!extractedQNames.containsKey(qName)) {
//
extractedQNames.put(//
qName, //
QNameInfo.fromManyAssociation(//
qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + extractedQNames.size()), //
mAssoDesc));
newQNames.add(qName);
}
}
}
}
use of org.qi4j.api.association.AssociationDescriptor in project qi4j-sdk by Qi4j.
the class AbstractSQLIndexing method insertAssoAndManyAssoQNames.
private void insertAssoAndManyAssoQNames(Map<QualifiedName, PreparedStatement> qNameInsertPSs, PreparedStatement insertToAllQNamesPS, EntityState state, Integer qNamePK, Long entityPK) throws SQLException {
for (AssociationDescriptor aDesc : state.entityDescriptor().state().associations()) {
if (SQLSkeletonUtil.isQueryable(aDesc.accessor())) {
QualifiedName qName = aDesc.qualifiedName();
PreparedStatement ps = qNameInsertPSs.get(qName);
EntityReference ref = state.associationValueOf(qName);
if (ref != null) {
insertToAllQNamesPS.setInt(1, qNamePK);
insertToAllQNamesPS.setLong(2, entityPK);
insertToAllQNamesPS.addBatch();
ps.setInt(1, qNamePK);
ps.setLong(2, entityPK);
ps.setString(3, ref.identity());
ps.addBatch();
++qNamePK;
}
}
}
for (AssociationDescriptor mDesc : state.entityDescriptor().state().manyAssociations()) {
if (SQLSkeletonUtil.isQueryable(mDesc.accessor())) {
QualifiedName qName = mDesc.qualifiedName();
PreparedStatement ps = qNameInsertPSs.get(qName);
Integer index = 0;
for (EntityReference ref : state.manyAssociationValueOf(qName)) {
if (ref != null) {
insertToAllQNamesPS.setInt(1, qNamePK);
insertToAllQNamesPS.setLong(2, entityPK);
insertToAllQNamesPS.addBatch();
ps.setInt(1, qNamePK);
ps.setLong(2, entityPK);
ps.setInt(3, index);
ps.setString(4, ref.identity());
ps.addBatch();
++qNamePK;
}
++index;
}
}
}
}
Aggregations