Search in sources :

Example 16 with EntityStoreException

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() {
        }
    };
}
Also used : DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) BackingStoreException(java.util.prefs.BackingStoreException) StateCommitter(org.qi4j.spi.entitystore.StateCommitter) EntityState(org.qi4j.spi.entity.EntityState) DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Preferences(java.util.prefs.Preferences)

Example 17 with EntityStoreException

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);
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityAlreadyExistsException(org.qi4j.spi.entitystore.EntityAlreadyExistsException) EntityState(org.qi4j.spi.entity.EntityState) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) ConstructionException(org.qi4j.api.common.ConstructionException) EntityCompositeAlreadyExistsException(org.qi4j.api.unitofwork.EntityCompositeAlreadyExistsException)

Example 18 with EntityStoreException

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);
    }
}
Also used : EntityReference(org.qi4j.api.entity.EntityReference) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 19 with EntityStoreException

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);
    }
}
Also used : JSONTokener(org.json.JSONTokener) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) JSONObject(org.json.JSONObject) EntityStatus(org.qi4j.spi.entity.EntityStatus) JSONException(org.json.JSONException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Module(org.qi4j.api.structure.Module)

Example 20 with EntityStoreException

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);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) IOException(java.io.IOException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Aggregations

EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)35 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)13 EntityReference (org.qi4j.api.entity.EntityReference)9 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)9 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 Map (java.util.Map)6 JSONArray (org.json.JSONArray)6 QualifiedName (org.qi4j.api.common.QualifiedName)6 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)5 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)5 StringReader (java.io.StringReader)4 Connection (java.sql.Connection)4 Module (org.qi4j.api.structure.Module)4