/** * Hook method to close the topia transaction of the request at the end of * the request when all filter has been consumed. * * @param transaction the transaction to close (can be null if transaction * was not required while the current request) * @since 1.9.1 */ protected void onCloseTransaction(PersistenceContext transaction) { if (transaction == null) { if (log.isTraceEnabled()) { log.trace("no transaction to close"); } } else if (transaction.isClosed()) { if (log.isTraceEnabled()) { log.trace("transaction " + transaction + " is already closed"); } } else { if (log.isDebugEnabled()) { log.debug("closing transaction " + transaction); } // The close will trigger a rollback if necessary transaction.close(); } }
public static TopiaEntity[] getEntities(TopiaPersistenceContext srcCtxt, String... entityList) throws TopiaException { TopiaEntity[] srcList = new TopiaEntity[entityList.length]; int index = 0; for (String id : entityList) { TopiaEntity e2 = srcCtxt.findByTopiaId(id); srcList[index++] = e2; } return srcList; }
public void init(TopiaApplicationContext ctxt, boolean doCreate, boolean doUpdate) throws TopiaException { if (sequences.isEmpty()) { // no sequence registed return; } String firstSequenceKey = sequences.keySet().iterator().next(); TopiaPersistenceContext newContext = ctxt.newPersistenceContext(); TopiaSqlSupport sqlSupport = ((AbstractTopiaPersistenceContext)newContext).getSqlSupport(); boolean exists = existSequence(firstSequenceKey, sqlSupport); if (!exists) { if (!doCreate) { // not exists and do not create return; } createSequences(sqlSupport); } else { if (doUpdate) { updateSequences(sqlSupport); } } newContext.commit(); newContext.close(); }
@Override public boolean onPreInsert(PreInsertEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPreCreate(context, entity, event.getState()); // when using composition, hibernate will persist entities by him self // entity must have an id in this case. if (StringUtils.isBlank(entity.getTopiaId())) { if (log.isDebugEnabled()) { log.debug("Adding topia id on entity " + entity.getClass().getSimpleName()); } } } return false; }
@Override public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException { try { // this event is called when hibernate try to persist entities using cascade (save) TopiaPersistenceContext context = getContext(event); // warning, event.getEntity() return null here :( if (context != null && event.getObject() instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) event.getObject(); if (StringUtils.isBlank(entity.getTopiaId())) { if (log.isDebugEnabled()) { log.debug("Adding topiaId into entity " + entity.getClass()); } // TopiaIdFactory#newTopiaId only requires interface. First get the entity class name. String entityClassName = event.getEntityName(); // event.getEntityName() may be null in case .update(...) method is called on a new (not yet persisted) entity (see http://nuiton.org/issues/3153) if (Strings.isNullOrEmpty(entityClassName)) { entityClassName = entity.getClass().getName(); } // Then extract interface name // FIXME echatellier 20130713 : hack to find interface for current entity class Class entityInterface = Class.forName(entityClassName.replace("Impl", "")); // Generate topiaId String topiaId = context.getTopiaIdFactory().newTopiaId(entityInterface, entity); entity.setTopiaId(topiaId); } } } catch (ClassNotFoundException ex) { throw new HibernateException("Can't set id", ex); } }
@Override public void onPostLoad(PostLoadEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { attachContext(event.getEntity(), context, context.getTopiaFiresSupport()); TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPostLoad(context, entity, new Object[]{}); } }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { try { filterChain.doFilter(servletRequest, servletResponse); } finally { TopiaPersistenceContext transaction = (TopiaPersistenceContext) servletRequest.getAttribute( OpenTopiaTransactionInterceptor.TOPIA_TRANSACTION_REQUEST_ATTRIBUTE); if (transaction == null) { if (log.isTraceEnabled()) { log.trace("no transaction to close"); } } else if (transaction.isClosed()) { if (log.isTraceEnabled()) { log.trace("transaction " + transaction + " is already closed"); } } else { if (log.isDebugEnabled()) { log.debug("close transaction " + transaction); } transaction.close(); } } }
@Override public void onPostInsert(PostInsertEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPostCreate(context, entity, event.getState()); } }
public static List<? extends TopiaEntity> getEntitiesList( TopiaPersistenceContext srcCtxt, String... entityList) throws TopiaException { List<TopiaEntity> srcList = new ArrayList<TopiaEntity>(entityList.length); for (String id : entityList) { TopiaEntity e2 = srcCtxt.findByTopiaId(id); srcList.add(e2); } return srcList; }
@Override public void onPostUpdate(PostUpdateEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPostUpdate(context, entity, event.getState()); } }
@SuppressWarnings({"unchecked"}) public static <E extends TopiaEntity> List<E> getEntities( TopiaPersistenceContext srcCtxt, List<E> entityList, boolean canBeNull) throws TopiaException { List<E> srcList = new ArrayList<E>(entityList.size()); for (E e : entityList) { E e2 = (E) srcCtxt.findByTopiaId(e.getTopiaId()); if (e2 == null && !canBeNull) { continue; } srcList.add(e2); } return srcList; }
@Override public void onPostDelete(PostDeleteEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPostDelete(context, entity, event.getDeletedState()); } }
@Override public void onPreLoad(PreLoadEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { // try { TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPreLoad(context, entity, event.getState()); } }
@Override public boolean onPreUpdate(PreUpdateEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPreUpdate(context, entity, event.getOldState()); } return false; }
@Override public boolean onPreDelete(PreDeleteEvent event) { TopiaPersistenceContext context = getContext(event); if (context != null && event.getEntity() instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) event.getEntity(); context.getTopiaFiresSupport().fireOnPreDelete(context, entity, event.getDeletedState()); } return false; }