@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;
  }
  public void testFetchByPrimaryKeyExisting() throws Exception {
    Counter newCounter = addCounter();

    Counter existingCounter = _persistence.fetchByPrimaryKey(newCounter.getPrimaryKey());

    assertEquals(existingCounter, newCounter);
  }
  /**
   * Clears the cache for the counter.
   *
   * <p>The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link
   * com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
   */
  @Override
  public void clearCache(Counter counter) {
    EntityCacheUtil.removeResult(
        CounterModelImpl.ENTITY_CACHE_ENABLED, CounterImpl.class, counter.getPrimaryKey());

    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
  }
  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);
  }
 /**
  * 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();
     }
   }
 }
  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());
  }
  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;
  }
  /**
   * 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();
  }