/** * {@inheritDoc} * * @return the entity lifecycle handler */ protected IEntityLifecycleHandler getEntityLifecycleHandler() { return getBackendController(); }
/** * Search Mongo using query. The result is then merged into session unless the method is called into a * pre-existing transaction, in which case, the merge mode is ignored and the merge is not performed. * * @param <T> * the entity type to return * @param query * the Mongo query. * @param mergeMode * the merge mode to use when merging back retrieved entities or null * if no merge is requested. * @param clazz * the type of the entity. * @return the first found entity or null */ public <T extends IEntity> List<T> findByQuery(final Query query, EMergeMode mergeMode, Class<? extends T> clazz) { return findByQuery(query, -1, -1, mergeMode, clazz); }
EMergeMode mergeMode, Class<? extends T> clazz) { List<T> res; if (isUnitOfWorkActive()) { res = find(query, firstResult, maxResults, null, clazz); } else { res = find(query, firstResult, maxResults, mergeMode, clazz);
final Class<? extends T> clazz) { T res; if (isUnitOfWorkActive()) { res = cloneInUnitOfWork(getMongoTemplate().findById(id, clazz)); } else { res = getTransactionTemplate().execute(new TransactionCallback<T>() {
@SuppressWarnings("unchecked") @Override public T doInTransaction(TransactionStatus status) { return merge(getMongoTemplate().findById(id, clazz), mergeMode); } });
private <T extends IEntity> List<T> find(final Query query, final int firstResult, final int maxResults, final EMergeMode mergeMode, final Class<? extends T> clazz) { return getTransactionTemplate().execute(new TransactionCallback<List<T>>() { @SuppressWarnings("unchecked") @Override public List<T> doInTransaction(TransactionStatus status) { if (firstResult >= 0) { query.skip(firstResult); } if (maxResults > 0) { query.limit(maxResults); } List<? extends T> entities = getMongoTemplate().find(query, clazz); if (mergeMode != null) { entities = merge(entities, mergeMode); } return (List<T>) entities; } }); }
@Override protected void doInTransactionWithoutResult(TransactionStatus status) { List<Company> companies = mongoBackendController.findByQuery(new Query(), null, Company.class); for (Company company : companies) { try { mongoBackendController.cleanRelationshipsOnDeletion(company, false); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(System.err); } } List<City> cities = mongoBackendController.findByQuery(new Query(), null, City.class); for (City city : cities) { try { mongoBackendController.cleanRelationshipsOnDeletion(city, false); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(System.err); } } } });
/** * Cleans flushed entities. * * @param status * the status */ @Override public void afterCompletion(int status) { if (status == STATUS_ROLLED_BACK) { // In case of rollback, we must still merge back flushed entities, since MongoDB is not transactional. if (flushedEntities != null) { for (IEntity updatedEntity : flushedEntities) { recordAsSynchronized(updatedEntity); } mergeBackFlushedEntities(flushedEntities); } } flushedEntities = null; super.afterCompletion(status); }
/** * Gets the hibernateSession. * * @return the hibernateSession. */ protected MongoTemplate getMongoTemplate() { return getBackendController().getMongoTemplate(); } }
@Override protected void doInTransactionWithoutResult(TransactionStatus status) { getMongoTemplate().save(mongoBackendController.cloneInUnitOfWork(entity)); } });
private Set<IEntity> buildReachableDirtyEntitySet(IEntity entity) { Set<IEntity> reachableDirtyEntities = new HashSet<>(); completeReachableDirtyEntities(entity, reachableDirtyEntities, new HashSet<IEntity>()); return reachableDirtyEntities; }
/** * Creates and persist the test data. */ public final void persistTestData() { MongoBackendController hbc = (MongoBackendController) BackendControllerHolder.getThreadBackendController(); boolean wasNull = false; if (hbc == null) { wasNull = true; hbc = (MongoBackendController) beanFactory.getBean("applicationBackController"); BackendControllerHolder.setThreadBackendController(hbc); } try { createAndPersistTestData(); } finally { hbc.cleanupRequestResources(); if (wasNull) { BackendControllerHolder.setThreadBackendController(null); } } }
@Override protected void doInTransactionWithoutResult(TransactionStatus status) { merge(getMongoTemplate().findById(entity.getId(), getComponentContract(entity)), EMergeMode.MERGE_CLEAN_EAGER); } });
/** * Persists or update an entity. * * @param entity * the entity to persist or update. */ protected void saveOrUpdate(final IEntity entity) { final MongoBackendController mongoBackendController = getBackendController(); mongoBackendController.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { getMongoTemplate().save(mongoBackendController.cloneInUnitOfWork(entity)); } }); }
/** * Search Mongo using query. The result is then merged into session unless the method is called into a * pre-existing transaction, in which case, the merge mode is ignored and the merge is not performed. * * @param <T> * the entity type to return * @param query * the Mongo query. * @param mergeMode * the merge mode to use when merging back retrieved entities or null * if no merge is requested. * @param clazz * the type of the entity. * @return the first found entity or null */ public <T extends IEntity> T findFirstByQuery(Query query, EMergeMode mergeMode, Class<? extends T> clazz) { List<T> ret = findByQuery(query, 0, 1, mergeMode, clazz); if (ret != null && !ret.isEmpty()) { return ret.get(0); } return null; }
/** * Gets the principal of the application session. * <p/> * {@inheritDoc} * * @return the principal */ protected UserPrincipal getPrincipal() { return getBackendController().getApplicationSession().getPrincipal(); }
@SuppressWarnings("unchecked") @Override public List<T> doInTransaction(TransactionStatus status) { if (firstResult >= 0) { query.skip(firstResult); } if (maxResults > 0) { query.limit(maxResults); } List<? extends T> entities = getMongoTemplate().find(query, clazz); if (mergeMode != null) { entities = merge(entities, mergeMode); } return (List<T>) entities; } });
private void clearPresiousData() { final MongoBackendController mongoBackendController = getBackendController(); mongoBackendController.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { List<Company> companies = mongoBackendController.findByQuery(new Query(), null, Company.class); for (Company company : companies) { try { mongoBackendController.cleanRelationshipsOnDeletion(company, false); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(System.err); } } List<City> cities = mongoBackendController.findByQuery(new Query(), null, City.class); for (City city : cities) { try { mongoBackendController.cleanRelationshipsOnDeletion(city, false); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(System.err); } } } }); }
/** * On after save. * * @param event * the event */ @Override public void onAfterSave(AfterSaveEvent<IEntity> event) { super.onAfterSave(event); IEntity entity = event.getSource(); getBackendController().recordAsSynchronized(entity); }
/** * On after convert. * * @param event * the event */ @Override public void onAfterConvert(AfterConvertEvent<IEntity> event) { super.onAfterConvert(event); IEntity entity = event.getSource(); getBackendController().registerEntity(entity); ((ILifecycleCapable) entity).onLoad(); }