Example usage for com.liferay.portal.kernel.util StringPool CLOSE_PARENTHESIS

List of usage examples for com.liferay.portal.kernel.util StringPool CLOSE_PARENTHESIS

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util StringPool CLOSE_PARENTHESIS.

Prototype

String CLOSE_PARENTHESIS

To view the source code for com.liferay.portal.kernel.util StringPool CLOSE_PARENTHESIS.

Click Source Link

Usage

From source file:com.liferay.calendar.service.persistence.impl.CalendarBookingPersistenceImpl.java

License:Open Source License

/**
 * Returns the number of calendar bookings where calendarId = ? and status = any ?.
 *
 * @param calendarId the calendar ID//from   w ww.  ja v  a  2  s .c om
 * @param statuses the statuses
 * @return the number of matching calendar bookings
 */
@Override
public int countByC_S(long calendarId, int[] statuses) {
    if (statuses == null) {
        statuses = new int[0];
    } else {
        statuses = ArrayUtil.unique(statuses);
    }

    Object[] finderArgs = new Object[] { calendarId, StringUtil.merge(statuses) };

    Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_S, finderArgs, this);

    if (count == null) {
        StringBundler query = new StringBundler();

        query.append(_SQL_COUNT_CALENDARBOOKING_WHERE);

        query.append(_FINDER_COLUMN_C_S_CALENDARID_2);

        if (statuses.length > 0) {
            query.append(StringPool.OPEN_PARENTHESIS);

            query.append(_FINDER_COLUMN_C_S_STATUS_7);

            query.append(StringUtil.merge(statuses));

            query.append(StringPool.CLOSE_PARENTHESIS);

            query.append(StringPool.CLOSE_PARENTHESIS);
        }

        query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

        String sql = query.toString();

        Session session = null;

        try {
            session = openSession();

            Query q = session.createQuery(sql);

            QueryPos qPos = QueryPos.getInstance(q);

            qPos.add(calendarId);

            count = (Long) q.uniqueResult();

            FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_S, finderArgs, count);
        } catch (Exception e) {
            FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_S, finderArgs);

            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    return count.intValue();
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarBookingPersistenceImpl.java

License:Open Source License

@Override
public Map<Serializable, CalendarBooking> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
        return Collections.emptyMap();
    }//  w w  w  . j a  v  a2  s.c  om

    Map<Serializable, CalendarBooking> map = new HashMap<Serializable, CalendarBooking>();

    if (primaryKeys.size() == 1) {
        Iterator<Serializable> iterator = primaryKeys.iterator();

        Serializable primaryKey = iterator.next();

        CalendarBooking calendarBooking = fetchByPrimaryKey(primaryKey);

        if (calendarBooking != null) {
            map.put(primaryKey, calendarBooking);
        }

        return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
        CalendarBooking calendarBooking = (CalendarBooking) EntityCacheUtil.getResult(
                CalendarBookingModelImpl.ENTITY_CACHE_ENABLED, CalendarBookingImpl.class, primaryKey);

        if (calendarBooking == null) {
            if (uncachedPrimaryKeys == null) {
                uncachedPrimaryKeys = new HashSet<Serializable>();
            }

            uncachedPrimaryKeys.add(primaryKey);
        } else {
            map.put(primaryKey, calendarBooking);
        }
    }

    if (uncachedPrimaryKeys == null) {
        return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_CALENDARBOOKING_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
        query.append(String.valueOf(primaryKey));

        query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        for (CalendarBooking calendarBooking : (List<CalendarBooking>) q.list()) {
            map.put(calendarBooking.getPrimaryKeyObj(), calendarBooking);

            cacheResult(calendarBooking);

            uncachedPrimaryKeys.remove(calendarBooking.getPrimaryKeyObj());
        }

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            EntityCacheUtil.putResult(CalendarBookingModelImpl.ENTITY_CACHE_ENABLED, CalendarBookingImpl.class,
                    primaryKey, _nullCalendarBooking);
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }

    return map;
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarNotificationTemplatePersistenceImpl.java

License:Open Source License

@Override
public Map<Serializable, CalendarNotificationTemplate> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
        return Collections.emptyMap();
    }//from w w w .  j  a va  2 s  .  c o m

    Map<Serializable, CalendarNotificationTemplate> map = new HashMap<Serializable, CalendarNotificationTemplate>();

    if (primaryKeys.size() == 1) {
        Iterator<Serializable> iterator = primaryKeys.iterator();

        Serializable primaryKey = iterator.next();

        CalendarNotificationTemplate calendarNotificationTemplate = fetchByPrimaryKey(primaryKey);

        if (calendarNotificationTemplate != null) {
            map.put(primaryKey, calendarNotificationTemplate);
        }

        return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
        CalendarNotificationTemplate calendarNotificationTemplate = (CalendarNotificationTemplate) EntityCacheUtil
                .getResult(CalendarNotificationTemplateModelImpl.ENTITY_CACHE_ENABLED,
                        CalendarNotificationTemplateImpl.class, primaryKey);

        if (calendarNotificationTemplate == null) {
            if (uncachedPrimaryKeys == null) {
                uncachedPrimaryKeys = new HashSet<Serializable>();
            }

            uncachedPrimaryKeys.add(primaryKey);
        } else {
            map.put(primaryKey, calendarNotificationTemplate);
        }
    }

    if (uncachedPrimaryKeys == null) {
        return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_CALENDARNOTIFICATIONTEMPLATE_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
        query.append(String.valueOf(primaryKey));

        query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        for (CalendarNotificationTemplate calendarNotificationTemplate : (List<CalendarNotificationTemplate>) q
                .list()) {
            map.put(calendarNotificationTemplate.getPrimaryKeyObj(), calendarNotificationTemplate);

            cacheResult(calendarNotificationTemplate);

            uncachedPrimaryKeys.remove(calendarNotificationTemplate.getPrimaryKeyObj());
        }

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            EntityCacheUtil.putResult(CalendarNotificationTemplateModelImpl.ENTITY_CACHE_ENABLED,
                    CalendarNotificationTemplateImpl.class, primaryKey, _nullCalendarNotificationTemplate);
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }

    return map;
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarPersistenceImpl.java

License:Open Source License

@Override
public Map<Serializable, Calendar> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
        return Collections.emptyMap();
    }/*from www  . j a v a 2s .c  om*/

    Map<Serializable, Calendar> map = new HashMap<Serializable, Calendar>();

    if (primaryKeys.size() == 1) {
        Iterator<Serializable> iterator = primaryKeys.iterator();

        Serializable primaryKey = iterator.next();

        Calendar calendar = fetchByPrimaryKey(primaryKey);

        if (calendar != null) {
            map.put(primaryKey, calendar);
        }

        return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
        Calendar calendar = (Calendar) EntityCacheUtil.getResult(CalendarModelImpl.ENTITY_CACHE_ENABLED,
                CalendarImpl.class, primaryKey);

        if (calendar == null) {
            if (uncachedPrimaryKeys == null) {
                uncachedPrimaryKeys = new HashSet<Serializable>();
            }

            uncachedPrimaryKeys.add(primaryKey);
        } else {
            map.put(primaryKey, calendar);
        }
    }

    if (uncachedPrimaryKeys == null) {
        return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_CALENDAR_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
        query.append(String.valueOf(primaryKey));

        query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        for (Calendar calendar : (List<Calendar>) q.list()) {
            map.put(calendar.getPrimaryKeyObj(), calendar);

            cacheResult(calendar);

            uncachedPrimaryKeys.remove(calendar.getPrimaryKeyObj());
        }

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            EntityCacheUtil.putResult(CalendarModelImpl.ENTITY_CACHE_ENABLED, CalendarImpl.class, primaryKey,
                    _nullCalendar);
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }

    return map;
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarResourcePersistenceImpl.java

License:Open Source License

/**
 * Returns an ordered range of all the calendar resources that the user has permission to view where groupId = any &#63; and code = &#63;.
 *
 * <p>/*from w  w  w .  j  av a2 s  .c  o m*/
 * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.calendar.model.impl.CalendarResourceModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
 * </p>
 *
 * @param groupIds the group IDs
 * @param code the code
 * @param start the lower bound of the range of calendar resources
 * @param end the upper bound of the range of calendar resources (not inclusive)
 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
 * @return the ordered range of matching calendar resources that the user has permission to view
 */
@Override
public List<CalendarResource> filterFindByG_C(long[] groupIds, String code, int start, int end,
        OrderByComparator<CalendarResource> orderByComparator) {
    if (!InlineSQLHelperUtil.isEnabled(groupIds)) {
        return findByG_C(groupIds, code, start, end, orderByComparator);
    }

    if (groupIds == null) {
        groupIds = new long[0];
    } else {
        groupIds = ArrayUtil.unique(groupIds);
    }

    StringBundler query = new StringBundler();

    query.append(_SQL_SELECT_CALENDARRESOURCE_WHERE);

    if (groupIds.length > 0) {
        query.append(StringPool.OPEN_PARENTHESIS);

        query.append(_FINDER_COLUMN_G_C_GROUPID_7);

        query.append(StringUtil.merge(groupIds));

        query.append(StringPool.CLOSE_PARENTHESIS);

        query.append(StringPool.CLOSE_PARENTHESIS);

        query.append(WHERE_AND);
    }

    boolean bindCode = false;

    if (code == null) {
        query.append(_FINDER_COLUMN_G_C_CODE_1);
    } else if (code.equals(StringPool.BLANK)) {
        query.append(_FINDER_COLUMN_G_C_CODE_3);
    } else {
        bindCode = true;

        query.append(_FINDER_COLUMN_G_C_CODE_2);
    }

    query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

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

    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), CalendarResource.class.getName(),
            _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, _FILTER_ENTITY_TABLE_FILTER_USERID_COLUMN, groupIds);

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindCode) {
            qPos.add(code);
        }

        return (List<CalendarResource>) QueryUtil.list(q, getDialect(), start, end);
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarResourcePersistenceImpl.java

License:Open Source License

/**
 * Returns an ordered range of all the calendar resources where groupId = any &#63; and code = &#63;.
 *
 * <p>//w w  w. j  a  va2 s. co m
 * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.calendar.model.impl.CalendarResourceModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
 * </p>
 *
 * @param groupIds the group IDs
 * @param code the code
 * @param start the lower bound of the range of calendar resources
 * @param end the upper bound of the range of calendar resources (not inclusive)
 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
 * @return the ordered range of matching calendar resources
 */
@Override
public List<CalendarResource> findByG_C(long[] groupIds, String code, int start, int end,
        OrderByComparator<CalendarResource> orderByComparator) {
    if (groupIds == null) {
        groupIds = new long[0];
    } else {
        groupIds = ArrayUtil.unique(groupIds);
    }

    if (groupIds.length == 1) {
        return findByG_C(groupIds[0], code, start, end, orderByComparator);
    }

    boolean pagination = true;
    Object[] finderArgs = null;

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
        pagination = false;
        finderArgs = new Object[] { StringUtil.merge(groupIds), code };
    } else {
        finderArgs = new Object[] { StringUtil.merge(groupIds), code,

                start, end, orderByComparator };
    }

    List<CalendarResource> list = (List<CalendarResource>) FinderCacheUtil
            .getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C, finderArgs, this);

    if ((list != null) && !list.isEmpty()) {
        for (CalendarResource calendarResource : list) {
            if (!ArrayUtil.contains(groupIds, calendarResource.getGroupId())
                    || !Validator.equals(code, calendarResource.getCode())) {
                list = null;

                break;
            }
        }
    }

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

        query.append(_SQL_SELECT_CALENDARRESOURCE_WHERE);

        if (groupIds.length > 0) {
            query.append(StringPool.OPEN_PARENTHESIS);

            query.append(_FINDER_COLUMN_G_C_GROUPID_7);

            query.append(StringUtil.merge(groupIds));

            query.append(StringPool.CLOSE_PARENTHESIS);

            query.append(StringPool.CLOSE_PARENTHESIS);

            query.append(WHERE_AND);
        }

        boolean bindCode = false;

        if (code == null) {
            query.append(_FINDER_COLUMN_G_C_CODE_1);
        } else if (code.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_G_C_CODE_3);
        } else {
            bindCode = true;

            query.append(_FINDER_COLUMN_G_C_CODE_2);
        }

        query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

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

        String sql = query.toString();

        Session session = null;

        try {
            session = openSession();

            Query q = session.createQuery(sql);

            QueryPos qPos = QueryPos.getInstance(q);

            if (bindCode) {
                qPos.add(code);
            }

            if (!pagination) {
                list = (List<CalendarResource>) QueryUtil.list(q, getDialect(), start, end, false);

                Collections.sort(list);

                list = Collections.unmodifiableList(list);
            } else {
                list = (List<CalendarResource>) QueryUtil.list(q, getDialect(), start, end);
            }

            cacheResult(list);

            FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C, finderArgs, list);
        } catch (Exception e) {
            FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C, finderArgs);

            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    return list;
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarResourcePersistenceImpl.java

License:Open Source License

/**
 * Returns the number of calendar resources where groupId = any &#63; and code = &#63;.
 *
 * @param groupIds the group IDs// w w  w. java2s  . c  o  m
 * @param code the code
 * @return the number of matching calendar resources
 */
@Override
public int countByG_C(long[] groupIds, String code) {
    if (groupIds == null) {
        groupIds = new long[0];
    } else {
        groupIds = ArrayUtil.unique(groupIds);
    }

    Object[] finderArgs = new Object[] { StringUtil.merge(groupIds), code };

    Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_G_C, finderArgs, this);

    if (count == null) {
        StringBundler query = new StringBundler();

        query.append(_SQL_COUNT_CALENDARRESOURCE_WHERE);

        if (groupIds.length > 0) {
            query.append(StringPool.OPEN_PARENTHESIS);

            query.append(_FINDER_COLUMN_G_C_GROUPID_7);

            query.append(StringUtil.merge(groupIds));

            query.append(StringPool.CLOSE_PARENTHESIS);

            query.append(StringPool.CLOSE_PARENTHESIS);

            query.append(WHERE_AND);
        }

        boolean bindCode = false;

        if (code == null) {
            query.append(_FINDER_COLUMN_G_C_CODE_1);
        } else if (code.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_G_C_CODE_3);
        } else {
            bindCode = true;

            query.append(_FINDER_COLUMN_G_C_CODE_2);
        }

        query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

        String sql = query.toString();

        Session session = null;

        try {
            session = openSession();

            Query q = session.createQuery(sql);

            QueryPos qPos = QueryPos.getInstance(q);

            if (bindCode) {
                qPos.add(code);
            }

            count = (Long) q.uniqueResult();

            FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_G_C, finderArgs, count);
        } catch (Exception e) {
            FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_G_C, finderArgs);

            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    return count.intValue();
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarResourcePersistenceImpl.java

License:Open Source License

/**
 * Returns the number of calendar resources that the user has permission to view where groupId = any &#63; and code = &#63;.
 *
 * @param groupIds the group IDs/*from   w  w w  .  j av a 2 s.c  o m*/
 * @param code the code
 * @return the number of matching calendar resources that the user has permission to view
 */
@Override
public int filterCountByG_C(long[] groupIds, String code) {
    if (!InlineSQLHelperUtil.isEnabled(groupIds)) {
        return countByG_C(groupIds, code);
    }

    if (groupIds == null) {
        groupIds = new long[0];
    } else {
        groupIds = ArrayUtil.unique(groupIds);
    }

    StringBundler query = new StringBundler();

    query.append(_SQL_COUNT_CALENDARRESOURCE_WHERE);

    if (groupIds.length > 0) {
        query.append(StringPool.OPEN_PARENTHESIS);

        query.append(_FINDER_COLUMN_G_C_GROUPID_7);

        query.append(StringUtil.merge(groupIds));

        query.append(StringPool.CLOSE_PARENTHESIS);

        query.append(StringPool.CLOSE_PARENTHESIS);

        query.append(WHERE_AND);
    }

    boolean bindCode = false;

    if (code == null) {
        query.append(_FINDER_COLUMN_G_C_CODE_1);
    } else if (code.equals(StringPool.BLANK)) {
        query.append(_FINDER_COLUMN_G_C_CODE_3);
    } else {
        bindCode = true;

        query.append(_FINDER_COLUMN_G_C_CODE_2);
    }

    query.setStringAt(removeConjunction(query.stringAt(query.index() - 1)), query.index() - 1);

    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(), CalendarResource.class.getName(),
            _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, _FILTER_ENTITY_TABLE_FILTER_USERID_COLUMN, groupIds);

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindCode) {
            qPos.add(code);
        }

        Long count = (Long) q.uniqueResult();

        return count.intValue();
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.liferay.calendar.service.persistence.impl.CalendarResourcePersistenceImpl.java

License:Open Source License

@Override
public Map<Serializable, CalendarResource> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
        return Collections.emptyMap();
    }/*from   www  .  j a v  a 2 s.c  om*/

    Map<Serializable, CalendarResource> map = new HashMap<Serializable, CalendarResource>();

    if (primaryKeys.size() == 1) {
        Iterator<Serializable> iterator = primaryKeys.iterator();

        Serializable primaryKey = iterator.next();

        CalendarResource calendarResource = fetchByPrimaryKey(primaryKey);

        if (calendarResource != null) {
            map.put(primaryKey, calendarResource);
        }

        return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
        CalendarResource calendarResource = (CalendarResource) EntityCacheUtil.getResult(
                CalendarResourceModelImpl.ENTITY_CACHE_ENABLED, CalendarResourceImpl.class, primaryKey);

        if (calendarResource == null) {
            if (uncachedPrimaryKeys == null) {
                uncachedPrimaryKeys = new HashSet<Serializable>();
            }

            uncachedPrimaryKeys.add(primaryKey);
        } else {
            map.put(primaryKey, calendarResource);
        }
    }

    if (uncachedPrimaryKeys == null) {
        return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_CALENDARRESOURCE_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
        query.append(String.valueOf(primaryKey));

        query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        for (CalendarResource calendarResource : (List<CalendarResource>) q.list()) {
            map.put(calendarResource.getPrimaryKeyObj(), calendarResource);

            cacheResult(calendarResource);

            uncachedPrimaryKeys.remove(calendarResource.getPrimaryKeyObj());
        }

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            EntityCacheUtil.putResult(CalendarResourceModelImpl.ENTITY_CACHE_ENABLED,
                    CalendarResourceImpl.class, primaryKey, _nullCalendarResource);
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }

    return map;
}

From source file:com.liferay.chat.service.persistence.impl.EntryPersistenceImpl.java

License:Open Source License

@Override
public Map<Serializable, Entry> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
        return Collections.emptyMap();
    }//from  w  w  w  .j ava2  s .  c  o  m

    Map<Serializable, Entry> map = new HashMap<Serializable, Entry>();

    if (primaryKeys.size() == 1) {
        Iterator<Serializable> iterator = primaryKeys.iterator();

        Serializable primaryKey = iterator.next();

        Entry entry = fetchByPrimaryKey(primaryKey);

        if (entry != null) {
            map.put(primaryKey, entry);
        }

        return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
        Entry entry = (Entry) EntityCacheUtil.getResult(EntryModelImpl.ENTITY_CACHE_ENABLED, EntryImpl.class,
                primaryKey);

        if (entry == null) {
            if (uncachedPrimaryKeys == null) {
                uncachedPrimaryKeys = new HashSet<Serializable>();
            }

            uncachedPrimaryKeys.add(primaryKey);
        } else {
            map.put(primaryKey, entry);
        }
    }

    if (uncachedPrimaryKeys == null) {
        return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_ENTRY_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
        query.append(String.valueOf(primaryKey));

        query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        for (Entry entry : (List<Entry>) q.list()) {
            map.put(entry.getPrimaryKeyObj(), entry);

            cacheResult(entry);

            uncachedPrimaryKeys.remove(entry.getPrimaryKeyObj());
        }

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            EntityCacheUtil.putResult(EntryModelImpl.ENTITY_CACHE_ENABLED, EntryImpl.class, primaryKey,
                    _nullEntry);
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }

    return map;
}