use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class PreferencesEntityStoreMixin method applyChanges.
@Override
public StateCommitter applyChanges(final EntityStoreUnitOfWork unitofwork, final Iterable<EntityState> state) {
return new StateCommitter() {
@Override
public void commit() {
try {
synchronized (root) {
for (EntityState entityState : state) {
DefaultEntityState state = (DefaultEntityState) entityState;
if (state.status().equals(EntityStatus.NEW)) {
Preferences entityPrefs = root.node(state.identity().identity());
writeEntityState(state, entityPrefs, unitofwork.identity(), unitofwork.currentTime());
} else if (state.status().equals(EntityStatus.UPDATED)) {
Preferences entityPrefs = root.node(state.identity().identity());
writeEntityState(state, entityPrefs, unitofwork.identity(), unitofwork.currentTime());
} else if (state.status().equals(EntityStatus.REMOVED)) {
root.node(state.identity().identity()).removeNode();
}
}
root.flush();
}
} catch (BackingStoreException e) {
throw new EntityStoreException(e);
}
}
@Override
public void cancel() {
}
};
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class EntityModel method newEntityState.
public EntityState newEntityState(EntityStoreUnitOfWork store, EntityReference identity) throws ConstraintViolationException, EntityStoreException {
try {
// New EntityState
EntityState entityState = store.newEntityState(identity, this);
// Set identity property
PropertyDescriptor persistentPropertyDescriptor = state().propertyModelFor(IDENTITY_METHOD);
entityState.setPropertyValue(persistentPropertyDescriptor.qualifiedName(), identity.identity());
return entityState;
} catch (EntityAlreadyExistsException e) {
throw new EntityCompositeAlreadyExistsException(identity);
} catch (EntityStoreException e) {
throw new ConstructionException("Could not create new entity in store", e);
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JSONEntityState method associationValueOf.
@Override
public EntityReference associationValueOf(QualifiedName stateName) {
try {
Object jsonValue = state.getJSONObject(JSONKeys.ASSOCIATIONS).opt(stateName.name());
if (jsonValue == null) {
return null;
}
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
return value;
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JSONMapEntityStoreMixin method readEntityState.
protected JSONEntityState 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 NamedAssociation is supported
if (!jsonObject.has(JSONKeys.NAMED_ASSOCIATIONS)) {
jsonObject.put(JSONKeys.NAMED_ASSOCIATIONS, new JSONObject());
}
// 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(getClass()).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);
}
return new JSONEntityState(unitOfWork, valueSerialization, version, modified, EntityReference.parseEntityReference(identity), status, entityDescriptor, jsonObject);
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JSONMapEntityStoreMixin method writeEntityState.
protected void writeEntityState(JSONEntityState state, Writer writer, String identity, long lastModified) throws EntityStoreException {
try {
JSONObject jsonState = state.state();
jsonState.put(JSONKeys.VERSION, identity);
jsonState.put(JSONKeys.MODIFIED, lastModified);
writer.append(jsonState.toString());
} catch (JSONException | IOException e) {
throw new EntityStoreException("Could not store EntityState", e);
}
}
Aggregations