use of org.qi4j.api.property.PropertyDescriptor 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.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class GaeEntityState method initializeValueTypes.
private HashMap<QualifiedName, ValueType> initializeValueTypes(EntityDescriptor descriptor) {
HashMap<QualifiedName, ValueType> result = new HashMap<QualifiedName, ValueType>();
for (PropertyDescriptor persistent : descriptor.state().properties()) {
if (persistent.valueType() instanceof ValueCompositeType) {
QualifiedName name = persistent.qualifiedName();
result.put(name, persistent.valueType());
}
}
return result;
}
use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class SQLEntityStoreMixin method writeEntityState.
protected void writeEntityState(DefaultEntityState state, Writer writer, String version) throws EntityStoreException {
try {
JSONWriter json = new JSONWriter(writer);
JSONWriter properties = json.object().key(JSONKeys.IDENTITY).value(state.identity().identity()).key(JSONKeys.APPLICATION_VERSION).value(application.version()).key(JSONKeys.TYPE).value(first(state.entityDescriptor().types()).getName()).key(JSONKeys.VERSION).value(version).key(JSONKeys.MODIFIED).value(state.lastModified()).key(JSONKeys.PROPERTIES).object();
for (PropertyDescriptor persistentProperty : state.entityDescriptor().state().properties()) {
Object value = state.properties().get(persistentProperty.qualifiedName());
json.key(persistentProperty.qualifiedName().name());
if (value == null || ValueType.isPrimitiveValue(value)) {
json.value(value);
} else {
String serialized = valueSerialization.serialize(value);
if (serialized.startsWith("{")) {
json.value(new JSONObject(serialized));
} else if (serialized.startsWith("[")) {
json.value(new JSONArray(serialized));
} else {
json.value(serialized);
}
}
}
JSONWriter associations = properties.endObject().key(JSONKeys.ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry : state.associations().entrySet()) {
EntityReference value = stateNameEntityReferenceEntry.getValue();
associations.key(stateNameEntityReferenceEntry.getKey().name()).value(value != null ? value.identity() : null);
}
JSONWriter manyAssociations = associations.endObject().key(JSONKeys.MANY_ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry : state.manyAssociations().entrySet()) {
JSONWriter assocs = manyAssociations.key(stateNameListEntry.getKey().name()).array();
for (EntityReference entityReference : stateNameListEntry.getValue()) {
assocs.value(entityReference.identity());
}
assocs.endArray();
}
JSONWriter namedAssociations = manyAssociations.endObject().key(JSONKeys.NAMED_ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameMapEntry : state.namedAssociations().entrySet()) {
JSONWriter assocs = namedAssociations.key(stateNameMapEntry.getKey().name()).object();
for (Map.Entry<String, EntityReference> entry : stateNameMapEntry.getValue().entrySet()) {
assocs.key(entry.getKey()).value(entry.getValue().identity());
}
assocs.endObject();
}
namedAssociations.endObject().endObject();
} catch (JSONException e) {
throw new EntityStoreException("Could not store EntityState", e);
}
}
use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class AbstractSQLIndexing method insertPropertyQNames.
private Integer insertPropertyQNames(Connection connection, Map<QualifiedName, PreparedStatement> qNameInsertPSs, PreparedStatement insertAllQNamesPS, EntityState state, Long entityPK) throws SQLException {
Set<QualifiedName> qNames = this._state.entityUsedQNames().get().get(state.entityDescriptor());
this.syncQNamesInsertPSs(connection, qNameInsertPSs, qNames);
Integer propertyPK = 0;
for (PropertyDescriptor pDesc : state.entityDescriptor().state().properties()) {
if (SQLSkeletonUtil.isQueryable(pDesc.accessor())) {
propertyPK = this.insertProperty(qNameInsertPSs, insertAllQNamesPS, propertyPK, entityPK, pDesc.qualifiedName(), state.propertyValueOf(pDesc.qualifiedName()), //
null);
}
}
return propertyPK;
}
use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class AbstractSQLStartup method processPropertyTypeForQNames.
private void processPropertyTypeForQNames(PropertyDescriptor pType, Map<QualifiedName, QNameInfo> qNameInfos, Set<QualifiedName> newQNames, List<CompositeDescriptorInfo> vDescriptors, Set<String> enumValues, Boolean setQNameTableNameToNull) {
QualifiedName qName = pType.qualifiedName();
if (!newQNames.contains(qName) && !qName.name().equals(Identity.class.getName())) {
newQNames.add(qName);
// System.out.println("QName: " + qName + ", hc: " + qName.hashCode());
QNameInfo info = qNameInfos.get(qName);
if (info == null) {
info = QNameInfo.fromProperty(//
qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + qNameInfos.size()), //
pType);
qNameInfos.put(qName, info);
}
Type vType = info.getFinalType();
while (vType instanceof ParameterizedType) {
vType = ((ParameterizedType) vType).getRawType();
}
if (//
vType instanceof Class<?>) {
final Class<?> vTypeClass = (Class<?>) vType;
if (((Class<?>) vType).isInterface()) {
for (CompositeDescriptorInfo descInfo : vDescriptors) {
CompositeDescriptor desc = descInfo.composite;
if (desc instanceof ValueDescriptor) {
ValueDescriptor vDesc = (ValueDescriptor) desc;
// other Serializable
if (Iterables.matchesAny(new Specification<Class<?>>() {
@Override
public boolean satisfiedBy(Class<?> item) {
return vTypeClass.isAssignableFrom(item);
}
}, vDesc.types())) {
for (PropertyDescriptor subPDesc : vDesc.state().properties()) {
//
this.processPropertyTypeForQNames(//
subPDesc, //
qNameInfos, //
newQNames, //
vDescriptors, //
enumValues, //
setQNameTableNameToNull);
}
}
}
}
} else if (Enum.class.isAssignableFrom((Class<?>) vType)) {
for (Object value : ((Class<?>) vType).getEnumConstants()) {
enumValues.add(QualifiedName.fromClass((Class<?>) vType, value.toString()).toString());
}
}
}
}
}
Aggregations