private CounterHolder _obtainIncrement(String counterName, long range, long size) throws SystemException { Session session = null; try { session = openSession(); Counter counter = (Counter) session.get(CounterImpl.class, counterName, LockMode.UPGRADE); long newValue = counter.getCurrentId(); if (size > newValue) { newValue = size; } long rangeMax = newValue + range; counter.setCurrentId(rangeMax); CounterHolder counterHolder = new CounterHolder(newValue, rangeMax); session.saveOrUpdate(counter); session.flush(); return counterHolder; } catch (Exception e) { throw processException(e); } finally { closeSession(session); } }
public void testFetchByPrimaryKeyExisting() throws Exception { Counter newCounter = addCounter(); Counter existingCounter = _persistence.fetchByPrimaryKey(newCounter.getPrimaryKey()); assertEquals(existingCounter, newCounter); }
@Override public Counter updateImpl(com.liferay.counter.model.Counter counter) throws SystemException { counter = toUnwrappedModel(counter); boolean isNew = counter.isNew(); Session session = null; try { session = openSession(); if (counter.isNew()) { session.save(counter); counter.setNew(false); } else { session.merge(counter); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); if (isNew) { FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } EntityCacheUtil.putResult( CounterModelImpl.ENTITY_CACHE_ENABLED, CounterImpl.class, counter.getPrimaryKey(), counter); return counter; }
/** * Creates a new counter with the primary key. Does not add the counter to the database. * * @param name the primary key for the new counter * @return the new counter */ public Counter create(String name) { Counter counter = new CounterImpl(); counter.setNew(true); counter.setPrimaryKey(name); return counter; }
public void testRemove() throws Exception { Counter newCounter = addCounter(); _persistence.remove(newCounter); Counter existingCounter = _persistence.fetchByPrimaryKey(newCounter.getPrimaryKey()); assertNull(existingCounter); }
public void testCreate() throws Exception { String pk = randomString(); Counter counter = _persistence.create(pk); assertNotNull(counter); assertEquals(counter.getPrimaryKey(), pk); }
@Override public void clearCache(List<Counter> counters) { FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); for (Counter counter : counters) { EntityCacheUtil.removeResult( CounterModelImpl.ENTITY_CACHE_ENABLED, CounterImpl.class, counter.getPrimaryKey()); } }
protected Counter addCounter() throws Exception { String pk = randomString(); Counter counter = _persistence.create(pk); counter.setCurrentId(nextLong()); _persistence.update(counter, false); return counter; }
/** * Caches the counters in the entity cache if it is enabled. * * @param counters the counters */ public void cacheResult(List<Counter> counters) { for (Counter counter : counters) { if (EntityCacheUtil.getResult( CounterModelImpl.ENTITY_CACHE_ENABLED, CounterImpl.class, counter.getPrimaryKey()) == null) { cacheResult(counter); } else { counter.resetOriginalValues(); } } }
protected Counter toUnwrappedModel(Counter counter) { if (counter instanceof CounterImpl) { return counter; } CounterImpl counterImpl = new CounterImpl(); counterImpl.setNew(counter.isNew()); counterImpl.setPrimaryKey(counter.getPrimaryKey()); counterImpl.setName(counter.getName()); counterImpl.setCurrentId(counter.getCurrentId()); return counterImpl; }
public void testDynamicQueryByPrimaryKeyExisting() throws Exception { Counter newCounter = addCounter(); DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Counter.class, Counter.class.getClassLoader()); dynamicQuery.add(RestrictionsFactoryUtil.eq("name", newCounter.getName())); List<Counter> result = _persistence.findWithDynamicQuery(dynamicQuery); assertEquals(1, result.size()); Counter existingCounter = result.get(0); assertEquals(existingCounter, newCounter); }
@Override protected Counter removeImpl(Counter counter) throws SystemException { counter = toUnwrappedModel(counter); Session session = null; try { session = openSession(); if (!session.contains(counter)) { counter = (Counter) session.get(CounterImpl.class, counter.getPrimaryKeyObj()); } if (counter != null) { session.delete(counter); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } if (counter != null) { clearCache(counter); } return counter; }
public void testDynamicQueryByProjectionExisting() throws Exception { Counter newCounter = addCounter(); DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Counter.class, Counter.class.getClassLoader()); dynamicQuery.setProjection(ProjectionFactoryUtil.property("name")); Object newName = newCounter.getName(); dynamicQuery.add(RestrictionsFactoryUtil.in("name", new Object[] {newName})); List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery); assertEquals(1, result.size()); Object existingName = result.get(0); assertEquals(existingName, newName); }
public void testUpdateExisting() throws Exception { String pk = randomString(); Counter newCounter = _persistence.create(pk); newCounter.setCurrentId(nextLong()); _persistence.update(newCounter, false); Counter existingCounter = _persistence.findByPrimaryKey(newCounter.getPrimaryKey()); assertEquals(existingCounter.getName(), newCounter.getName()); assertEquals(existingCounter.getCurrentId(), newCounter.getCurrentId()); }
/** * Caches the counter in the entity cache if it is enabled. * * @param counter the counter */ public void cacheResult(Counter counter) { EntityCacheUtil.putResult( CounterModelImpl.ENTITY_CACHE_ENABLED, CounterImpl.class, counter.getPrimaryKey(), counter); counter.resetOriginalValues(); }