/**
   * Creates a new announcements flag with the primary key. Does not add the announcements flag to
   * the database.
   *
   * @param flagId the primary key for the new announcements flag
   * @return the new announcements flag
   */
  public AnnouncementsFlag create(long flagId) {
    AnnouncementsFlag announcementsFlag = new AnnouncementsFlagImpl();

    announcementsFlag.setNew(true);
    announcementsFlag.setPrimaryKey(flagId);

    return announcementsFlag;
  }
 protected void clearUniqueFindersCache(AnnouncementsFlag announcementsFlag) {
   FinderCacheUtil.removeResult(
       FINDER_PATH_FETCH_BY_U_E_V,
       new Object[] {
         Long.valueOf(announcementsFlag.getUserId()),
         Long.valueOf(announcementsFlag.getEntryId()),
         Integer.valueOf(announcementsFlag.getValue())
       });
 }
 /**
  * Caches the announcements flags in the entity cache if it is enabled.
  *
  * @param announcementsFlags the announcements flags
  */
 public void cacheResult(List<AnnouncementsFlag> announcementsFlags) {
   for (AnnouncementsFlag announcementsFlag : announcementsFlags) {
     if (EntityCacheUtil.getResult(
             AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
             AnnouncementsFlagImpl.class,
             announcementsFlag.getPrimaryKey())
         == null) {
       cacheResult(announcementsFlag);
     } else {
       announcementsFlag.resetOriginalValues();
     }
   }
 }
  @Override
  public void clearCache(List<AnnouncementsFlag> announcementsFlags) {
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

    for (AnnouncementsFlag announcementsFlag : announcementsFlags) {
      EntityCacheUtil.removeResult(
          AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
          AnnouncementsFlagImpl.class,
          announcementsFlag.getPrimaryKey());

      clearUniqueFindersCache(announcementsFlag);
    }
  }
  /**
   * Caches the announcements flag in the entity cache if it is enabled.
   *
   * @param announcementsFlag the announcements flag
   */
  public void cacheResult(AnnouncementsFlag announcementsFlag) {
    EntityCacheUtil.putResult(
        AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
        AnnouncementsFlagImpl.class,
        announcementsFlag.getPrimaryKey(),
        announcementsFlag);

    FinderCacheUtil.putResult(
        FINDER_PATH_FETCH_BY_U_E_V,
        new Object[] {
          Long.valueOf(announcementsFlag.getUserId()),
          Long.valueOf(announcementsFlag.getEntryId()),
          Integer.valueOf(announcementsFlag.getValue())
        },
        announcementsFlag);

    announcementsFlag.resetOriginalValues();
  }
  protected AnnouncementsFlag toUnwrappedModel(AnnouncementsFlag announcementsFlag) {
    if (announcementsFlag instanceof AnnouncementsFlagImpl) {
      return announcementsFlag;
    }

    AnnouncementsFlagImpl announcementsFlagImpl = new AnnouncementsFlagImpl();

    announcementsFlagImpl.setNew(announcementsFlag.isNew());
    announcementsFlagImpl.setPrimaryKey(announcementsFlag.getPrimaryKey());

    announcementsFlagImpl.setFlagId(announcementsFlag.getFlagId());
    announcementsFlagImpl.setUserId(announcementsFlag.getUserId());
    announcementsFlagImpl.setCreateDate(announcementsFlag.getCreateDate());
    announcementsFlagImpl.setEntryId(announcementsFlag.getEntryId());
    announcementsFlagImpl.setValue(announcementsFlag.getValue());

    return announcementsFlagImpl;
  }
  /**
   * Returns the announcements flag where userId = &#63; and entryId = &#63; and value = &#63; or
   * returns <code>null</code> if it could not be found, optionally using the finder cache.
   *
   * @param userId the user ID
   * @param entryId the entry ID
   * @param value the value
   * @param retrieveFromCache whether to use the finder cache
   * @return the matching announcements flag, or <code>null</code> if a matching announcements flag
   *     could not be found
   * @throws SystemException if a system exception occurred
   */
  public AnnouncementsFlag fetchByU_E_V(
      long userId, long entryId, int value, boolean retrieveFromCache) throws SystemException {
    Object[] finderArgs = new Object[] {userId, entryId, value};

    Object result = null;

    if (retrieveFromCache) {
      result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_E_V, finderArgs, this);
    }

    if (result instanceof AnnouncementsFlag) {
      AnnouncementsFlag announcementsFlag = (AnnouncementsFlag) result;

      if ((userId != announcementsFlag.getUserId())
          || (entryId != announcementsFlag.getEntryId())
          || (value != announcementsFlag.getValue())) {
        result = null;
      }
    }

    if (result == null) {
      StringBundler query = new StringBundler(5);

      query.append(_SQL_SELECT_ANNOUNCEMENTSFLAG_WHERE);

      query.append(_FINDER_COLUMN_U_E_V_USERID_2);

      query.append(_FINDER_COLUMN_U_E_V_ENTRYID_2);

      query.append(_FINDER_COLUMN_U_E_V_VALUE_2);

      query.append(AnnouncementsFlagModelImpl.ORDER_BY_JPQL);

      String sql = query.toString();

      Session session = null;

      try {
        session = openSession();

        Query q = session.createQuery(sql);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(userId);

        qPos.add(entryId);

        qPos.add(value);

        List<AnnouncementsFlag> list = q.list();

        result = list;

        AnnouncementsFlag announcementsFlag = null;

        if (list.isEmpty()) {
          FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_E_V, finderArgs, list);
        } else {
          announcementsFlag = list.get(0);

          cacheResult(announcementsFlag);

          if ((announcementsFlag.getUserId() != userId)
              || (announcementsFlag.getEntryId() != entryId)
              || (announcementsFlag.getValue() != value)) {
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_E_V, finderArgs, announcementsFlag);
          }
        }

        return announcementsFlag;
      } catch (Exception e) {
        throw processException(e);
      } finally {
        if (result == null) {
          FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_E_V, finderArgs);
        }

        closeSession(session);
      }
    } else {
      if (result instanceof List<?>) {
        return null;
      } else {
        return (AnnouncementsFlag) result;
      }
    }
  }
  /**
   * Returns an ordered range of all the announcements flags where entryId = &#63;.
   *
   * <p>Useful when paginating results. Returns a maximum of <code>end - start</code> instances.
   * <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result
   * set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start
   * </code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}
   * will return the full result set.
   *
   * @param entryId the entry ID
   * @param start the lower bound of the range of announcements flags
   * @param end the upper bound of the range of announcements flags (not inclusive)
   * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
   * @return the ordered range of matching announcements flags
   * @throws SystemException if a system exception occurred
   */
  public List<AnnouncementsFlag> findByEntryId(
      long entryId, int start, int end, OrderByComparator orderByComparator)
      throws SystemException {
    FinderPath finderPath = null;
    Object[] finderArgs = null;

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
      finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID;
      finderArgs = new Object[] {entryId};
    } else {
      finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ENTRYID;
      finderArgs = new Object[] {entryId, start, end, orderByComparator};
    }

    List<AnnouncementsFlag> list =
        (List<AnnouncementsFlag>) FinderCacheUtil.getResult(finderPath, finderArgs, this);

    if ((list != null) && !list.isEmpty()) {
      for (AnnouncementsFlag announcementsFlag : list) {
        if ((entryId != announcementsFlag.getEntryId())) {
          list = null;

          break;
        }
      }
    }

    if (list == null) {
      StringBundler query = null;

      if (orderByComparator != null) {
        query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 3));
      } else {
        query = new StringBundler(3);
      }

      query.append(_SQL_SELECT_ANNOUNCEMENTSFLAG_WHERE);

      query.append(_FINDER_COLUMN_ENTRYID_ENTRYID_2);

      if (orderByComparator != null) {
        appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
      } else {
        query.append(AnnouncementsFlagModelImpl.ORDER_BY_JPQL);
      }

      String sql = query.toString();

      Session session = null;

      try {
        session = openSession();

        Query q = session.createQuery(sql);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(entryId);

        list = (List<AnnouncementsFlag>) QueryUtil.list(q, getDialect(), start, end);
      } catch (Exception e) {
        throw processException(e);
      } finally {
        if (list == null) {
          FinderCacheUtil.removeResult(finderPath, finderArgs);
        } else {
          cacheResult(list);

          FinderCacheUtil.putResult(finderPath, finderArgs, list);
        }

        closeSession(session);
      }
    }

    return list;
  }
  @Override
  public AnnouncementsFlag updateImpl(
      com.liferay.portlet.announcements.model.AnnouncementsFlag announcementsFlag, boolean merge)
      throws SystemException {
    announcementsFlag = toUnwrappedModel(announcementsFlag);

    boolean isNew = announcementsFlag.isNew();

    AnnouncementsFlagModelImpl announcementsFlagModelImpl =
        (AnnouncementsFlagModelImpl) announcementsFlag;

    Session session = null;

    try {
      session = openSession();

      BatchSessionUtil.update(session, announcementsFlag, merge);

      announcementsFlag.setNew(false);
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

    if (isNew || !AnnouncementsFlagModelImpl.COLUMN_BITMASK_ENABLED) {
      FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    } else {
      if ((announcementsFlagModelImpl.getColumnBitmask()
              & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID.getColumnBitmask())
          != 0) {
        Object[] args =
            new Object[] {Long.valueOf(announcementsFlagModelImpl.getOriginalEntryId())};

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ENTRYID, args);
        FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID, args);

        args = new Object[] {Long.valueOf(announcementsFlagModelImpl.getEntryId())};

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ENTRYID, args);
        FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID, args);
      }
    }

    EntityCacheUtil.putResult(
        AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
        AnnouncementsFlagImpl.class,
        announcementsFlag.getPrimaryKey(),
        announcementsFlag);

    if (isNew) {
      FinderCacheUtil.putResult(
          FINDER_PATH_FETCH_BY_U_E_V,
          new Object[] {
            Long.valueOf(announcementsFlag.getUserId()),
            Long.valueOf(announcementsFlag.getEntryId()),
            Integer.valueOf(announcementsFlag.getValue())
          },
          announcementsFlag);
    } else {
      if ((announcementsFlagModelImpl.getColumnBitmask()
              & FINDER_PATH_FETCH_BY_U_E_V.getColumnBitmask())
          != 0) {
        Object[] args =
            new Object[] {
              Long.valueOf(announcementsFlagModelImpl.getOriginalUserId()),
              Long.valueOf(announcementsFlagModelImpl.getOriginalEntryId()),
              Integer.valueOf(announcementsFlagModelImpl.getOriginalValue())
            };

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_U_E_V, args);

        FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_E_V, args);

        FinderCacheUtil.putResult(
            FINDER_PATH_FETCH_BY_U_E_V,
            new Object[] {
              Long.valueOf(announcementsFlag.getUserId()),
              Long.valueOf(announcementsFlag.getEntryId()),
              Integer.valueOf(announcementsFlag.getValue())
            },
            announcementsFlag);
      }
    }

    return announcementsFlag;
  }