/** * No-op transaction, do nothing. * @param entityClass the type of class to load * @param id - the ID of the object to load. * @param filterExpression - security filters that can be evaluated in the data store. * @param scope - the current request scope. It is optional for the data store to attempt evaluation. * @return a new persistent resource with a new instance of {@code entityClass} */ @Override public Object loadObject(Class<?> entityClass, Serializable id, Optional<FilterExpression> filterExpression, RequestScope scope) { // Loads are supported but empty object (with specified id) is returned. // NOTE: This is primarily useful for enabling objects of solely computed properties to be fetched. Object entity; try { entity = entityClass.newInstance(); } catch (IllegalAccessException | InstantiationException e) { log.error("Could not load object {} through NoopStore", entityClass, e); throw new RuntimeException(e); } String uuid = scope.getUUIDFor(entity); // Side-effecting method of the PersistentResource :( however, it enables us to do this without reinventing // the wheel. Should probably be refactored eventually nonetheless. new PersistentResource<>(entity, null, uuid, scope).setId(id.toString()); return entity; }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }