Search in sources :

Example 11 with StringBundler

use of com.liferay.petra.string.StringBundler in project liferay-blade-samples by liferay.

the class FooCacheModel method toString.

@Override
public String toString() {
    StringBundler sb = new StringBundler(27);
    sb.append("{uuid=");
    sb.append(uuid);
    sb.append(", fooId=");
    sb.append(fooId);
    sb.append(", groupId=");
    sb.append(groupId);
    sb.append(", companyId=");
    sb.append(companyId);
    sb.append(", userId=");
    sb.append(userId);
    sb.append(", userName=");
    sb.append(userName);
    sb.append(", createDate=");
    sb.append(createDate);
    sb.append(", modifiedDate=");
    sb.append(modifiedDate);
    sb.append(", field1=");
    sb.append(field1);
    sb.append(", field2=");
    sb.append(field2);
    sb.append(", field3=");
    sb.append(field3);
    sb.append(", field4=");
    sb.append(field4);
    sb.append(", field5=");
    sb.append(field5);
    sb.append("}");
    return sb.toString();
}
Also used : StringBundler(com.liferay.petra.string.StringBundler)

Example 12 with StringBundler

use of com.liferay.petra.string.StringBundler in project liferay-blade-samples by liferay.

the class FooPersistenceImpl method getByUuid_PrevAndNext.

protected Foo getByUuid_PrevAndNext(Session session, Foo foo, String uuid, OrderByComparator<Foo> orderByComparator, boolean previous) {
    StringBundler query = null;
    if (orderByComparator != null) {
        query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3) + (orderByComparator.getOrderByFields().length * 3));
    } else {
        query = new StringBundler(3);
    }
    query.append(_SQL_SELECT_FOO_WHERE);
    boolean bindUuid = false;
    if (uuid.isEmpty()) {
        query.append(_FINDER_COLUMN_UUID_UUID_3);
    } else {
        bindUuid = true;
        query.append(_FINDER_COLUMN_UUID_UUID_2);
    }
    if (orderByComparator != null) {
        String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
        if (orderByConditionFields.length > 0) {
            query.append(WHERE_AND);
        }
        for (int i = 0; i < orderByConditionFields.length; i++) {
            query.append(_ORDER_BY_ENTITY_ALIAS);
            query.append(orderByConditionFields[i]);
            if ((i + 1) < orderByConditionFields.length) {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
                } else {
                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
                }
            } else {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(WHERE_GREATER_THAN);
                } else {
                    query.append(WHERE_LESSER_THAN);
                }
            }
        }
        query.append(ORDER_BY_CLAUSE);
        String[] orderByFields = orderByComparator.getOrderByFields();
        for (int i = 0; i < orderByFields.length; i++) {
            query.append(_ORDER_BY_ENTITY_ALIAS);
            query.append(orderByFields[i]);
            if ((i + 1) < orderByFields.length) {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(ORDER_BY_ASC_HAS_NEXT);
                } else {
                    query.append(ORDER_BY_DESC_HAS_NEXT);
                }
            } else {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(ORDER_BY_ASC);
                } else {
                    query.append(ORDER_BY_DESC);
                }
            }
        }
    } else {
        query.append(FooModelImpl.ORDER_BY_JPQL);
    }
    String sql = query.toString();
    Query q = session.createQuery(sql);
    q.setFirstResult(0);
    q.setMaxResults(2);
    QueryPos qPos = QueryPos.getInstance(q);
    if (bindUuid) {
        qPos.add(uuid);
    }
    if (orderByComparator != null) {
        for (Object orderByConditionValue : orderByComparator.getOrderByConditionValues(foo)) {
            qPos.add(orderByConditionValue);
        }
    }
    List<Foo> list = q.list();
    if (list.size() == 2) {
        return list.get(1);
    } else {
        return null;
    }
}
Also used : Query(com.liferay.portal.kernel.dao.orm.Query) Foo(com.liferay.blade.basic.model.Foo) QueryPos(com.liferay.portal.kernel.dao.orm.QueryPos) StringBundler(com.liferay.petra.string.StringBundler)

Example 13 with StringBundler

use of com.liferay.petra.string.StringBundler in project liferay-blade-samples by liferay.

the class FooPersistenceImpl method fetchByUUID_G.

/**
 * Returns the foo where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
 *
 * @param uuid the uuid
 * @param groupId the group ID
 * @param useFinderCache whether to use the finder cache
 * @return the matching foo, or <code>null</code> if a matching foo could not be found
 */
@Override
public Foo fetchByUUID_G(String uuid, long groupId, boolean useFinderCache) {
    uuid = Objects.toString(uuid, "");
    Object[] finderArgs = null;
    if (useFinderCache) {
        finderArgs = new Object[] { uuid, groupId };
    }
    Object result = null;
    if (useFinderCache) {
        result = finderCache.getResult(_finderPathFetchByUUID_G, finderArgs, this);
    }
    if (result instanceof Foo) {
        Foo foo = (Foo) result;
        if (!Objects.equals(uuid, foo.getUuid()) || (groupId != foo.getGroupId())) {
            result = null;
        }
    }
    if (result == null) {
        StringBundler query = new StringBundler(4);
        query.append(_SQL_SELECT_FOO_WHERE);
        boolean bindUuid = false;
        if (uuid.isEmpty()) {
            query.append(_FINDER_COLUMN_UUID_G_UUID_3);
        } else {
            bindUuid = true;
            query.append(_FINDER_COLUMN_UUID_G_UUID_2);
        }
        query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
        String sql = query.toString();
        Session session = null;
        try {
            session = openSession();
            Query q = session.createQuery(sql);
            QueryPos qPos = QueryPos.getInstance(q);
            if (bindUuid) {
                qPos.add(uuid);
            }
            qPos.add(groupId);
            List<Foo> list = q.list();
            if (list.isEmpty()) {
                if (useFinderCache) {
                    finderCache.putResult(_finderPathFetchByUUID_G, finderArgs, list);
                }
            } else {
                Foo foo = list.get(0);
                result = foo;
                cacheResult(foo);
            }
        } catch (Exception e) {
            if (useFinderCache) {
                finderCache.removeResult(_finderPathFetchByUUID_G, finderArgs);
            }
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }
    if (result instanceof List<?>) {
        return null;
    } else {
        return (Foo) result;
    }
}
Also used : Query(com.liferay.portal.kernel.dao.orm.Query) Foo(com.liferay.blade.basic.model.Foo) List(java.util.List) QueryPos(com.liferay.portal.kernel.dao.orm.QueryPos) StringBundler(com.liferay.petra.string.StringBundler) NoSuchFooException(com.liferay.blade.basic.exception.NoSuchFooException) Session(com.liferay.portal.kernel.dao.orm.Session)

Example 14 with StringBundler

use of com.liferay.petra.string.StringBundler in project liferay-blade-samples by liferay.

the class FooPersistenceImpl method findByField2_Last.

/**
 * Returns the last foo in the ordered set where field2 = &#63;.
 *
 * @param field2 the field2
 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
 * @return the last matching foo
 * @throws NoSuchFooException if a matching foo could not be found
 */
@Override
public Foo findByField2_Last(boolean field2, OrderByComparator<Foo> orderByComparator) throws NoSuchFooException {
    Foo foo = fetchByField2_Last(field2, orderByComparator);
    if (foo != null) {
        return foo;
    }
    StringBundler msg = new StringBundler(4);
    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
    msg.append("field2=");
    msg.append(field2);
    msg.append("}");
    throw new NoSuchFooException(msg.toString());
}
Also used : NoSuchFooException(com.liferay.blade.basic.exception.NoSuchFooException) Foo(com.liferay.blade.basic.model.Foo) StringBundler(com.liferay.petra.string.StringBundler)

Example 15 with StringBundler

use of com.liferay.petra.string.StringBundler in project liferay-blade-samples by liferay.

the class FooPersistenceImpl method findAll.

/**
 * Returns an ordered range of all the foos.
 *
 * <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 <code>QueryUtil#ALL_POS</code> 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, then the query will include the default ORDER BY logic from <code>FooModelImpl</code>.
 * </p>
 *
 * @param start the lower bound of the range of foos
 * @param end the upper bound of the range of foos (not inclusive)
 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
 * @param useFinderCache whether to use the finder cache
 * @return the ordered range of foos
 */
@Override
public List<Foo> findAll(int start, int end, OrderByComparator<Foo> orderByComparator, boolean useFinderCache) {
    FinderPath finderPath = null;
    Object[] finderArgs = null;
    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
        if (useFinderCache) {
            finderPath = _finderPathWithoutPaginationFindAll;
            finderArgs = FINDER_ARGS_EMPTY;
        }
    } else if (useFinderCache) {
        finderPath = _finderPathWithPaginationFindAll;
        finderArgs = new Object[] { start, end, orderByComparator };
    }
    List<Foo> list = null;
    if (useFinderCache) {
        list = (List<Foo>) finderCache.getResult(finderPath, finderArgs, this);
    }
    if (list == null) {
        StringBundler query = null;
        String sql = null;
        if (orderByComparator != null) {
            query = new StringBundler(2 + (orderByComparator.getOrderByFields().length * 2));
            query.append(_SQL_SELECT_FOO);
            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            sql = query.toString();
        } else {
            sql = _SQL_SELECT_FOO;
            sql = sql.concat(FooModelImpl.ORDER_BY_JPQL);
        }
        Session session = null;
        try {
            session = openSession();
            Query q = session.createQuery(sql);
            list = (List<Foo>) QueryUtil.list(q, getDialect(), start, end);
            cacheResult(list);
            if (useFinderCache) {
                finderCache.putResult(finderPath, finderArgs, list);
            }
        } catch (Exception e) {
            if (useFinderCache) {
                finderCache.removeResult(finderPath, finderArgs);
            }
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }
    return list;
}
Also used : Query(com.liferay.portal.kernel.dao.orm.Query) FinderPath(com.liferay.portal.kernel.dao.orm.FinderPath) Foo(com.liferay.blade.basic.model.Foo) StringBundler(com.liferay.petra.string.StringBundler) NoSuchFooException(com.liferay.blade.basic.exception.NoSuchFooException) Session(com.liferay.portal.kernel.dao.orm.Session)

Aggregations

StringBundler (com.liferay.petra.string.StringBundler)90 Query (com.liferay.portal.kernel.dao.orm.Query)45 QueryPos (com.liferay.portal.kernel.dao.orm.QueryPos)38 Session (com.liferay.portal.kernel.dao.orm.Session)35 FinderPath (com.liferay.portal.kernel.dao.orm.FinderPath)31 Foo (com.liferay.blade.basic.model.Foo)17 Bar (com.liferay.blade.samples.servicebuilder.adq.model.Bar)17 NoSuchFooException (com.liferay.blade.basic.exception.NoSuchFooException)16 NoSuchBarException (com.liferay.blade.samples.servicebuilder.adq.exception.NoSuchBarException)16 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)14 Map (java.util.Map)14 Function (java.util.function.Function)14 Qux (com.liferay.blade.workflow.asset.model.Qux)13 Baz (com.liferay.blade.workflow.basic.model.Baz)13 NoSuchQuxException (com.liferay.blade.workflow.asset.exception.NoSuchQuxException)12 NoSuchBazException (com.liferay.blade.workflow.basic.exception.NoSuchBazException)12 List (java.util.List)4 Country (com.liferay.blade.samples.dspservicebuilder.model.Country)3 Country (com.liferay.blade.samples.jdbcservicebuilder.model.Country)3