001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.NoSuchWorkflowInstanceLinkException;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.model.CacheModel;
033    import com.liferay.portal.model.MVCCModel;
034    import com.liferay.portal.model.WorkflowInstanceLink;
035    import com.liferay.portal.model.impl.WorkflowInstanceLinkImpl;
036    import com.liferay.portal.model.impl.WorkflowInstanceLinkModelImpl;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portal.service.ServiceContextThreadLocal;
039    import com.liferay.portal.service.persistence.WorkflowInstanceLinkPersistence;
040    
041    import java.io.Serializable;
042    
043    import java.util.Collections;
044    import java.util.Date;
045    import java.util.HashMap;
046    import java.util.HashSet;
047    import java.util.Iterator;
048    import java.util.List;
049    import java.util.Map;
050    import java.util.Set;
051    
052    /**
053     * The persistence implementation for the workflow instance link service.
054     *
055     * <p>
056     * Caching information and settings can be found in <code>portal.properties</code>
057     * </p>
058     *
059     * @author Brian Wing Shun Chan
060     * @see WorkflowInstanceLinkPersistence
061     * @see com.liferay.portal.service.persistence.WorkflowInstanceLinkUtil
062     * @generated
063     */
064    @ProviderType
065    public class WorkflowInstanceLinkPersistenceImpl extends BasePersistenceImpl<WorkflowInstanceLink>
066            implements WorkflowInstanceLinkPersistence {
067            /*
068             * NOTE FOR DEVELOPERS:
069             *
070             * Never modify or reference this class directly. Always use {@link WorkflowInstanceLinkUtil} to access the workflow instance link persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
071             */
072            public static final String FINDER_CLASS_NAME_ENTITY = WorkflowInstanceLinkImpl.class.getName();
073            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074                    ".List1";
075            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
076                    ".List2";
077            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
078                            WorkflowInstanceLinkModelImpl.FINDER_CACHE_ENABLED,
079                            WorkflowInstanceLinkImpl.class,
080                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
081            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
082                            WorkflowInstanceLinkModelImpl.FINDER_CACHE_ENABLED,
083                            WorkflowInstanceLinkImpl.class,
084                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
086                            WorkflowInstanceLinkModelImpl.FINDER_CACHE_ENABLED, Long.class,
087                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
088            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C_C_C = new FinderPath(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
089                            WorkflowInstanceLinkModelImpl.FINDER_CACHE_ENABLED,
090                            WorkflowInstanceLinkImpl.class,
091                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByG_C_C_C",
092                            new String[] {
093                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
094                                    Long.class.getName(),
095                                    
096                            Integer.class.getName(), Integer.class.getName(),
097                                    OrderByComparator.class.getName()
098                            });
099            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C_C =
100                    new FinderPath(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
101                            WorkflowInstanceLinkModelImpl.FINDER_CACHE_ENABLED,
102                            WorkflowInstanceLinkImpl.class,
103                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByG_C_C_C",
104                            new String[] {
105                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
106                                    Long.class.getName()
107                            },
108                            WorkflowInstanceLinkModelImpl.GROUPID_COLUMN_BITMASK |
109                            WorkflowInstanceLinkModelImpl.COMPANYID_COLUMN_BITMASK |
110                            WorkflowInstanceLinkModelImpl.CLASSNAMEID_COLUMN_BITMASK |
111                            WorkflowInstanceLinkModelImpl.CLASSPK_COLUMN_BITMASK |
112                            WorkflowInstanceLinkModelImpl.CREATEDATE_COLUMN_BITMASK);
113            public static final FinderPath FINDER_PATH_COUNT_BY_G_C_C_C = new FinderPath(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
114                            WorkflowInstanceLinkModelImpl.FINDER_CACHE_ENABLED, Long.class,
115                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_C_C_C",
116                            new String[] {
117                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
118                                    Long.class.getName()
119                            });
120    
121            /**
122             * Returns all the workflow instance links where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
123             *
124             * @param groupId the group ID
125             * @param companyId the company ID
126             * @param classNameId the class name ID
127             * @param classPK the class p k
128             * @return the matching workflow instance links
129             */
130            @Override
131            public List<WorkflowInstanceLink> findByG_C_C_C(long groupId,
132                    long companyId, long classNameId, long classPK) {
133                    return findByG_C_C_C(groupId, companyId, classNameId, classPK,
134                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
135            }
136    
137            /**
138             * Returns a range of all the workflow instance links where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
139             *
140             * <p>
141             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link WorkflowInstanceLinkModelImpl}. 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.
142             * </p>
143             *
144             * @param groupId the group ID
145             * @param companyId the company ID
146             * @param classNameId the class name ID
147             * @param classPK the class p k
148             * @param start the lower bound of the range of workflow instance links
149             * @param end the upper bound of the range of workflow instance links (not inclusive)
150             * @return the range of matching workflow instance links
151             */
152            @Override
153            public List<WorkflowInstanceLink> findByG_C_C_C(long groupId,
154                    long companyId, long classNameId, long classPK, int start, int end) {
155                    return findByG_C_C_C(groupId, companyId, classNameId, classPK, start,
156                            end, null);
157            }
158    
159            /**
160             * Returns an ordered range of all the workflow instance links where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
161             *
162             * <p>
163             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link WorkflowInstanceLinkModelImpl}. 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.
164             * </p>
165             *
166             * @param groupId the group ID
167             * @param companyId the company ID
168             * @param classNameId the class name ID
169             * @param classPK the class p k
170             * @param start the lower bound of the range of workflow instance links
171             * @param end the upper bound of the range of workflow instance links (not inclusive)
172             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
173             * @return the ordered range of matching workflow instance links
174             */
175            @Override
176            public List<WorkflowInstanceLink> findByG_C_C_C(long groupId,
177                    long companyId, long classNameId, long classPK, int start, int end,
178                    OrderByComparator<WorkflowInstanceLink> orderByComparator) {
179                    boolean pagination = true;
180                    FinderPath finderPath = null;
181                    Object[] finderArgs = null;
182    
183                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
184                                    (orderByComparator == null)) {
185                            pagination = false;
186                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C_C;
187                            finderArgs = new Object[] { groupId, companyId, classNameId, classPK };
188                    }
189                    else {
190                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C_C_C;
191                            finderArgs = new Object[] {
192                                            groupId, companyId, classNameId, classPK,
193                                            
194                                            start, end, orderByComparator
195                                    };
196                    }
197    
198                    List<WorkflowInstanceLink> list = (List<WorkflowInstanceLink>)FinderCacheUtil.getResult(finderPath,
199                                    finderArgs, this);
200    
201                    if ((list != null) && !list.isEmpty()) {
202                            for (WorkflowInstanceLink workflowInstanceLink : list) {
203                                    if ((groupId != workflowInstanceLink.getGroupId()) ||
204                                                    (companyId != workflowInstanceLink.getCompanyId()) ||
205                                                    (classNameId != workflowInstanceLink.getClassNameId()) ||
206                                                    (classPK != workflowInstanceLink.getClassPK())) {
207                                            list = null;
208    
209                                            break;
210                                    }
211                            }
212                    }
213    
214                    if (list == null) {
215                            StringBundler query = null;
216    
217                            if (orderByComparator != null) {
218                                    query = new StringBundler(6 +
219                                                    (orderByComparator.getOrderByFields().length * 3));
220                            }
221                            else {
222                                    query = new StringBundler(6);
223                            }
224    
225                            query.append(_SQL_SELECT_WORKFLOWINSTANCELINK_WHERE);
226    
227                            query.append(_FINDER_COLUMN_G_C_C_C_GROUPID_2);
228    
229                            query.append(_FINDER_COLUMN_G_C_C_C_COMPANYID_2);
230    
231                            query.append(_FINDER_COLUMN_G_C_C_C_CLASSNAMEID_2);
232    
233                            query.append(_FINDER_COLUMN_G_C_C_C_CLASSPK_2);
234    
235                            if (orderByComparator != null) {
236                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
237                                            orderByComparator);
238                            }
239                            else
240                             if (pagination) {
241                                    query.append(WorkflowInstanceLinkModelImpl.ORDER_BY_JPQL);
242                            }
243    
244                            String sql = query.toString();
245    
246                            Session session = null;
247    
248                            try {
249                                    session = openSession();
250    
251                                    Query q = session.createQuery(sql);
252    
253                                    QueryPos qPos = QueryPos.getInstance(q);
254    
255                                    qPos.add(groupId);
256    
257                                    qPos.add(companyId);
258    
259                                    qPos.add(classNameId);
260    
261                                    qPos.add(classPK);
262    
263                                    if (!pagination) {
264                                            list = (List<WorkflowInstanceLink>)QueryUtil.list(q,
265                                                            getDialect(), start, end, false);
266    
267                                            Collections.sort(list);
268    
269                                            list = Collections.unmodifiableList(list);
270                                    }
271                                    else {
272                                            list = (List<WorkflowInstanceLink>)QueryUtil.list(q,
273                                                            getDialect(), start, end);
274                                    }
275    
276                                    cacheResult(list);
277    
278                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
279                            }
280                            catch (Exception e) {
281                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
282    
283                                    throw processException(e);
284                            }
285                            finally {
286                                    closeSession(session);
287                            }
288                    }
289    
290                    return list;
291            }
292    
293            /**
294             * Returns the first workflow instance link in the ordered set where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
295             *
296             * @param groupId the group ID
297             * @param companyId the company ID
298             * @param classNameId the class name ID
299             * @param classPK the class p k
300             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
301             * @return the first matching workflow instance link
302             * @throws NoSuchWorkflowInstanceLinkException if a matching workflow instance link could not be found
303             */
304            @Override
305            public WorkflowInstanceLink findByG_C_C_C_First(long groupId,
306                    long companyId, long classNameId, long classPK,
307                    OrderByComparator<WorkflowInstanceLink> orderByComparator)
308                    throws NoSuchWorkflowInstanceLinkException {
309                    WorkflowInstanceLink workflowInstanceLink = fetchByG_C_C_C_First(groupId,
310                                    companyId, classNameId, classPK, orderByComparator);
311    
312                    if (workflowInstanceLink != null) {
313                            return workflowInstanceLink;
314                    }
315    
316                    StringBundler msg = new StringBundler(10);
317    
318                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
319    
320                    msg.append("groupId=");
321                    msg.append(groupId);
322    
323                    msg.append(", companyId=");
324                    msg.append(companyId);
325    
326                    msg.append(", classNameId=");
327                    msg.append(classNameId);
328    
329                    msg.append(", classPK=");
330                    msg.append(classPK);
331    
332                    msg.append(StringPool.CLOSE_CURLY_BRACE);
333    
334                    throw new NoSuchWorkflowInstanceLinkException(msg.toString());
335            }
336    
337            /**
338             * Returns the first workflow instance link in the ordered set where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
339             *
340             * @param groupId the group ID
341             * @param companyId the company ID
342             * @param classNameId the class name ID
343             * @param classPK the class p k
344             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
345             * @return the first matching workflow instance link, or <code>null</code> if a matching workflow instance link could not be found
346             */
347            @Override
348            public WorkflowInstanceLink fetchByG_C_C_C_First(long groupId,
349                    long companyId, long classNameId, long classPK,
350                    OrderByComparator<WorkflowInstanceLink> orderByComparator) {
351                    List<WorkflowInstanceLink> list = findByG_C_C_C(groupId, companyId,
352                                    classNameId, classPK, 0, 1, orderByComparator);
353    
354                    if (!list.isEmpty()) {
355                            return list.get(0);
356                    }
357    
358                    return null;
359            }
360    
361            /**
362             * Returns the last workflow instance link in the ordered set where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
363             *
364             * @param groupId the group ID
365             * @param companyId the company ID
366             * @param classNameId the class name ID
367             * @param classPK the class p k
368             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
369             * @return the last matching workflow instance link
370             * @throws NoSuchWorkflowInstanceLinkException if a matching workflow instance link could not be found
371             */
372            @Override
373            public WorkflowInstanceLink findByG_C_C_C_Last(long groupId,
374                    long companyId, long classNameId, long classPK,
375                    OrderByComparator<WorkflowInstanceLink> orderByComparator)
376                    throws NoSuchWorkflowInstanceLinkException {
377                    WorkflowInstanceLink workflowInstanceLink = fetchByG_C_C_C_Last(groupId,
378                                    companyId, classNameId, classPK, orderByComparator);
379    
380                    if (workflowInstanceLink != null) {
381                            return workflowInstanceLink;
382                    }
383    
384                    StringBundler msg = new StringBundler(10);
385    
386                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
387    
388                    msg.append("groupId=");
389                    msg.append(groupId);
390    
391                    msg.append(", companyId=");
392                    msg.append(companyId);
393    
394                    msg.append(", classNameId=");
395                    msg.append(classNameId);
396    
397                    msg.append(", classPK=");
398                    msg.append(classPK);
399    
400                    msg.append(StringPool.CLOSE_CURLY_BRACE);
401    
402                    throw new NoSuchWorkflowInstanceLinkException(msg.toString());
403            }
404    
405            /**
406             * Returns the last workflow instance link in the ordered set where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
407             *
408             * @param groupId the group ID
409             * @param companyId the company ID
410             * @param classNameId the class name ID
411             * @param classPK the class p k
412             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
413             * @return the last matching workflow instance link, or <code>null</code> if a matching workflow instance link could not be found
414             */
415            @Override
416            public WorkflowInstanceLink fetchByG_C_C_C_Last(long groupId,
417                    long companyId, long classNameId, long classPK,
418                    OrderByComparator<WorkflowInstanceLink> orderByComparator) {
419                    int count = countByG_C_C_C(groupId, companyId, classNameId, classPK);
420    
421                    if (count == 0) {
422                            return null;
423                    }
424    
425                    List<WorkflowInstanceLink> list = findByG_C_C_C(groupId, companyId,
426                                    classNameId, classPK, count - 1, count, orderByComparator);
427    
428                    if (!list.isEmpty()) {
429                            return list.get(0);
430                    }
431    
432                    return null;
433            }
434    
435            /**
436             * Returns the workflow instance links before and after the current workflow instance link in the ordered set where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
437             *
438             * @param workflowInstanceLinkId the primary key of the current workflow instance link
439             * @param groupId the group ID
440             * @param companyId the company ID
441             * @param classNameId the class name ID
442             * @param classPK the class p k
443             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
444             * @return the previous, current, and next workflow instance link
445             * @throws NoSuchWorkflowInstanceLinkException if a workflow instance link with the primary key could not be found
446             */
447            @Override
448            public WorkflowInstanceLink[] findByG_C_C_C_PrevAndNext(
449                    long workflowInstanceLinkId, long groupId, long companyId,
450                    long classNameId, long classPK,
451                    OrderByComparator<WorkflowInstanceLink> orderByComparator)
452                    throws NoSuchWorkflowInstanceLinkException {
453                    WorkflowInstanceLink workflowInstanceLink = findByPrimaryKey(workflowInstanceLinkId);
454    
455                    Session session = null;
456    
457                    try {
458                            session = openSession();
459    
460                            WorkflowInstanceLink[] array = new WorkflowInstanceLinkImpl[3];
461    
462                            array[0] = getByG_C_C_C_PrevAndNext(session, workflowInstanceLink,
463                                            groupId, companyId, classNameId, classPK,
464                                            orderByComparator, true);
465    
466                            array[1] = workflowInstanceLink;
467    
468                            array[2] = getByG_C_C_C_PrevAndNext(session, workflowInstanceLink,
469                                            groupId, companyId, classNameId, classPK,
470                                            orderByComparator, false);
471    
472                            return array;
473                    }
474                    catch (Exception e) {
475                            throw processException(e);
476                    }
477                    finally {
478                            closeSession(session);
479                    }
480            }
481    
482            protected WorkflowInstanceLink getByG_C_C_C_PrevAndNext(Session session,
483                    WorkflowInstanceLink workflowInstanceLink, long groupId,
484                    long companyId, long classNameId, long classPK,
485                    OrderByComparator<WorkflowInstanceLink> orderByComparator,
486                    boolean previous) {
487                    StringBundler query = null;
488    
489                    if (orderByComparator != null) {
490                            query = new StringBundler(6 +
491                                            (orderByComparator.getOrderByFields().length * 6));
492                    }
493                    else {
494                            query = new StringBundler(3);
495                    }
496    
497                    query.append(_SQL_SELECT_WORKFLOWINSTANCELINK_WHERE);
498    
499                    query.append(_FINDER_COLUMN_G_C_C_C_GROUPID_2);
500    
501                    query.append(_FINDER_COLUMN_G_C_C_C_COMPANYID_2);
502    
503                    query.append(_FINDER_COLUMN_G_C_C_C_CLASSNAMEID_2);
504    
505                    query.append(_FINDER_COLUMN_G_C_C_C_CLASSPK_2);
506    
507                    if (orderByComparator != null) {
508                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
509    
510                            if (orderByConditionFields.length > 0) {
511                                    query.append(WHERE_AND);
512                            }
513    
514                            for (int i = 0; i < orderByConditionFields.length; i++) {
515                                    query.append(_ORDER_BY_ENTITY_ALIAS);
516                                    query.append(orderByConditionFields[i]);
517    
518                                    if ((i + 1) < orderByConditionFields.length) {
519                                            if (orderByComparator.isAscending() ^ previous) {
520                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
521                                            }
522                                            else {
523                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
524                                            }
525                                    }
526                                    else {
527                                            if (orderByComparator.isAscending() ^ previous) {
528                                                    query.append(WHERE_GREATER_THAN);
529                                            }
530                                            else {
531                                                    query.append(WHERE_LESSER_THAN);
532                                            }
533                                    }
534                            }
535    
536                            query.append(ORDER_BY_CLAUSE);
537    
538                            String[] orderByFields = orderByComparator.getOrderByFields();
539    
540                            for (int i = 0; i < orderByFields.length; i++) {
541                                    query.append(_ORDER_BY_ENTITY_ALIAS);
542                                    query.append(orderByFields[i]);
543    
544                                    if ((i + 1) < orderByFields.length) {
545                                            if (orderByComparator.isAscending() ^ previous) {
546                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
547                                            }
548                                            else {
549                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
550                                            }
551                                    }
552                                    else {
553                                            if (orderByComparator.isAscending() ^ previous) {
554                                                    query.append(ORDER_BY_ASC);
555                                            }
556                                            else {
557                                                    query.append(ORDER_BY_DESC);
558                                            }
559                                    }
560                            }
561                    }
562                    else {
563                            query.append(WorkflowInstanceLinkModelImpl.ORDER_BY_JPQL);
564                    }
565    
566                    String sql = query.toString();
567    
568                    Query q = session.createQuery(sql);
569    
570                    q.setFirstResult(0);
571                    q.setMaxResults(2);
572    
573                    QueryPos qPos = QueryPos.getInstance(q);
574    
575                    qPos.add(groupId);
576    
577                    qPos.add(companyId);
578    
579                    qPos.add(classNameId);
580    
581                    qPos.add(classPK);
582    
583                    if (orderByComparator != null) {
584                            Object[] values = orderByComparator.getOrderByConditionValues(workflowInstanceLink);
585    
586                            for (Object value : values) {
587                                    qPos.add(value);
588                            }
589                    }
590    
591                    List<WorkflowInstanceLink> list = q.list();
592    
593                    if (list.size() == 2) {
594                            return list.get(1);
595                    }
596                    else {
597                            return null;
598                    }
599            }
600    
601            /**
602             * Removes all the workflow instance links where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63; from the database.
603             *
604             * @param groupId the group ID
605             * @param companyId the company ID
606             * @param classNameId the class name ID
607             * @param classPK the class p k
608             */
609            @Override
610            public void removeByG_C_C_C(long groupId, long companyId, long classNameId,
611                    long classPK) {
612                    for (WorkflowInstanceLink workflowInstanceLink : findByG_C_C_C(
613                                    groupId, companyId, classNameId, classPK, QueryUtil.ALL_POS,
614                                    QueryUtil.ALL_POS, null)) {
615                            remove(workflowInstanceLink);
616                    }
617            }
618    
619            /**
620             * Returns the number of workflow instance links where groupId = &#63; and companyId = &#63; and classNameId = &#63; and classPK = &#63;.
621             *
622             * @param groupId the group ID
623             * @param companyId the company ID
624             * @param classNameId the class name ID
625             * @param classPK the class p k
626             * @return the number of matching workflow instance links
627             */
628            @Override
629            public int countByG_C_C_C(long groupId, long companyId, long classNameId,
630                    long classPK) {
631                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_C_C_C;
632    
633                    Object[] finderArgs = new Object[] {
634                                    groupId, companyId, classNameId, classPK
635                            };
636    
637                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
638                                    this);
639    
640                    if (count == null) {
641                            StringBundler query = new StringBundler(5);
642    
643                            query.append(_SQL_COUNT_WORKFLOWINSTANCELINK_WHERE);
644    
645                            query.append(_FINDER_COLUMN_G_C_C_C_GROUPID_2);
646    
647                            query.append(_FINDER_COLUMN_G_C_C_C_COMPANYID_2);
648    
649                            query.append(_FINDER_COLUMN_G_C_C_C_CLASSNAMEID_2);
650    
651                            query.append(_FINDER_COLUMN_G_C_C_C_CLASSPK_2);
652    
653                            String sql = query.toString();
654    
655                            Session session = null;
656    
657                            try {
658                                    session = openSession();
659    
660                                    Query q = session.createQuery(sql);
661    
662                                    QueryPos qPos = QueryPos.getInstance(q);
663    
664                                    qPos.add(groupId);
665    
666                                    qPos.add(companyId);
667    
668                                    qPos.add(classNameId);
669    
670                                    qPos.add(classPK);
671    
672                                    count = (Long)q.uniqueResult();
673    
674                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
675                            }
676                            catch (Exception e) {
677                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
678    
679                                    throw processException(e);
680                            }
681                            finally {
682                                    closeSession(session);
683                            }
684                    }
685    
686                    return count.intValue();
687            }
688    
689            private static final String _FINDER_COLUMN_G_C_C_C_GROUPID_2 = "workflowInstanceLink.groupId = ? AND ";
690            private static final String _FINDER_COLUMN_G_C_C_C_COMPANYID_2 = "workflowInstanceLink.companyId = ? AND ";
691            private static final String _FINDER_COLUMN_G_C_C_C_CLASSNAMEID_2 = "workflowInstanceLink.classNameId = ? AND ";
692            private static final String _FINDER_COLUMN_G_C_C_C_CLASSPK_2 = "workflowInstanceLink.classPK = ?";
693    
694            public WorkflowInstanceLinkPersistenceImpl() {
695                    setModelClass(WorkflowInstanceLink.class);
696            }
697    
698            /**
699             * Caches the workflow instance link in the entity cache if it is enabled.
700             *
701             * @param workflowInstanceLink the workflow instance link
702             */
703            @Override
704            public void cacheResult(WorkflowInstanceLink workflowInstanceLink) {
705                    EntityCacheUtil.putResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
706                            WorkflowInstanceLinkImpl.class,
707                            workflowInstanceLink.getPrimaryKey(), workflowInstanceLink);
708    
709                    workflowInstanceLink.resetOriginalValues();
710            }
711    
712            /**
713             * Caches the workflow instance links in the entity cache if it is enabled.
714             *
715             * @param workflowInstanceLinks the workflow instance links
716             */
717            @Override
718            public void cacheResult(List<WorkflowInstanceLink> workflowInstanceLinks) {
719                    for (WorkflowInstanceLink workflowInstanceLink : workflowInstanceLinks) {
720                            if (EntityCacheUtil.getResult(
721                                                    WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
722                                                    WorkflowInstanceLinkImpl.class,
723                                                    workflowInstanceLink.getPrimaryKey()) == null) {
724                                    cacheResult(workflowInstanceLink);
725                            }
726                            else {
727                                    workflowInstanceLink.resetOriginalValues();
728                            }
729                    }
730            }
731    
732            /**
733             * Clears the cache for all workflow instance links.
734             *
735             * <p>
736             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
737             * </p>
738             */
739            @Override
740            public void clearCache() {
741                    EntityCacheUtil.clearCache(WorkflowInstanceLinkImpl.class);
742    
743                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
744                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
745                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
746            }
747    
748            /**
749             * Clears the cache for the workflow instance link.
750             *
751             * <p>
752             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
753             * </p>
754             */
755            @Override
756            public void clearCache(WorkflowInstanceLink workflowInstanceLink) {
757                    EntityCacheUtil.removeResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
758                            WorkflowInstanceLinkImpl.class, workflowInstanceLink.getPrimaryKey());
759    
760                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
761                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
762            }
763    
764            @Override
765            public void clearCache(List<WorkflowInstanceLink> workflowInstanceLinks) {
766                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
767                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
768    
769                    for (WorkflowInstanceLink workflowInstanceLink : workflowInstanceLinks) {
770                            EntityCacheUtil.removeResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
771                                    WorkflowInstanceLinkImpl.class,
772                                    workflowInstanceLink.getPrimaryKey());
773                    }
774            }
775    
776            /**
777             * Creates a new workflow instance link with the primary key. Does not add the workflow instance link to the database.
778             *
779             * @param workflowInstanceLinkId the primary key for the new workflow instance link
780             * @return the new workflow instance link
781             */
782            @Override
783            public WorkflowInstanceLink create(long workflowInstanceLinkId) {
784                    WorkflowInstanceLink workflowInstanceLink = new WorkflowInstanceLinkImpl();
785    
786                    workflowInstanceLink.setNew(true);
787                    workflowInstanceLink.setPrimaryKey(workflowInstanceLinkId);
788    
789                    return workflowInstanceLink;
790            }
791    
792            /**
793             * Removes the workflow instance link with the primary key from the database. Also notifies the appropriate model listeners.
794             *
795             * @param workflowInstanceLinkId the primary key of the workflow instance link
796             * @return the workflow instance link that was removed
797             * @throws NoSuchWorkflowInstanceLinkException if a workflow instance link with the primary key could not be found
798             */
799            @Override
800            public WorkflowInstanceLink remove(long workflowInstanceLinkId)
801                    throws NoSuchWorkflowInstanceLinkException {
802                    return remove((Serializable)workflowInstanceLinkId);
803            }
804    
805            /**
806             * Removes the workflow instance link with the primary key from the database. Also notifies the appropriate model listeners.
807             *
808             * @param primaryKey the primary key of the workflow instance link
809             * @return the workflow instance link that was removed
810             * @throws NoSuchWorkflowInstanceLinkException if a workflow instance link with the primary key could not be found
811             */
812            @Override
813            public WorkflowInstanceLink remove(Serializable primaryKey)
814                    throws NoSuchWorkflowInstanceLinkException {
815                    Session session = null;
816    
817                    try {
818                            session = openSession();
819    
820                            WorkflowInstanceLink workflowInstanceLink = (WorkflowInstanceLink)session.get(WorkflowInstanceLinkImpl.class,
821                                            primaryKey);
822    
823                            if (workflowInstanceLink == null) {
824                                    if (_log.isWarnEnabled()) {
825                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
826                                    }
827    
828                                    throw new NoSuchWorkflowInstanceLinkException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
829                                            primaryKey);
830                            }
831    
832                            return remove(workflowInstanceLink);
833                    }
834                    catch (NoSuchWorkflowInstanceLinkException nsee) {
835                            throw nsee;
836                    }
837                    catch (Exception e) {
838                            throw processException(e);
839                    }
840                    finally {
841                            closeSession(session);
842                    }
843            }
844    
845            @Override
846            protected WorkflowInstanceLink removeImpl(
847                    WorkflowInstanceLink workflowInstanceLink) {
848                    workflowInstanceLink = toUnwrappedModel(workflowInstanceLink);
849    
850                    Session session = null;
851    
852                    try {
853                            session = openSession();
854    
855                            if (!session.contains(workflowInstanceLink)) {
856                                    workflowInstanceLink = (WorkflowInstanceLink)session.get(WorkflowInstanceLinkImpl.class,
857                                                    workflowInstanceLink.getPrimaryKeyObj());
858                            }
859    
860                            if (workflowInstanceLink != null) {
861                                    session.delete(workflowInstanceLink);
862                            }
863                    }
864                    catch (Exception e) {
865                            throw processException(e);
866                    }
867                    finally {
868                            closeSession(session);
869                    }
870    
871                    if (workflowInstanceLink != null) {
872                            clearCache(workflowInstanceLink);
873                    }
874    
875                    return workflowInstanceLink;
876            }
877    
878            @Override
879            public WorkflowInstanceLink updateImpl(
880                    WorkflowInstanceLink workflowInstanceLink) {
881                    workflowInstanceLink = toUnwrappedModel(workflowInstanceLink);
882    
883                    boolean isNew = workflowInstanceLink.isNew();
884    
885                    WorkflowInstanceLinkModelImpl workflowInstanceLinkModelImpl = (WorkflowInstanceLinkModelImpl)workflowInstanceLink;
886    
887                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
888    
889                    Date now = new Date();
890    
891                    if (isNew && (workflowInstanceLink.getCreateDate() == null)) {
892                            if (serviceContext == null) {
893                                    workflowInstanceLink.setCreateDate(now);
894                            }
895                            else {
896                                    workflowInstanceLink.setCreateDate(serviceContext.getCreateDate(
897                                                    now));
898                            }
899                    }
900    
901                    if (!workflowInstanceLinkModelImpl.hasSetModifiedDate()) {
902                            if (serviceContext == null) {
903                                    workflowInstanceLink.setModifiedDate(now);
904                            }
905                            else {
906                                    workflowInstanceLink.setModifiedDate(serviceContext.getModifiedDate(
907                                                    now));
908                            }
909                    }
910    
911                    Session session = null;
912    
913                    try {
914                            session = openSession();
915    
916                            if (workflowInstanceLink.isNew()) {
917                                    session.save(workflowInstanceLink);
918    
919                                    workflowInstanceLink.setNew(false);
920                            }
921                            else {
922                                    session.merge(workflowInstanceLink);
923                            }
924                    }
925                    catch (Exception e) {
926                            throw processException(e);
927                    }
928                    finally {
929                            closeSession(session);
930                    }
931    
932                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
933    
934                    if (isNew || !WorkflowInstanceLinkModelImpl.COLUMN_BITMASK_ENABLED) {
935                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
936                    }
937    
938                    else {
939                            if ((workflowInstanceLinkModelImpl.getColumnBitmask() &
940                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C_C.getColumnBitmask()) != 0) {
941                                    Object[] args = new Object[] {
942                                                    workflowInstanceLinkModelImpl.getOriginalGroupId(),
943                                                    workflowInstanceLinkModelImpl.getOriginalCompanyId(),
944                                                    workflowInstanceLinkModelImpl.getOriginalClassNameId(),
945                                                    workflowInstanceLinkModelImpl.getOriginalClassPK()
946                                            };
947    
948                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_C_C_C, args);
949                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C_C,
950                                            args);
951    
952                                    args = new Object[] {
953                                                    workflowInstanceLinkModelImpl.getGroupId(),
954                                                    workflowInstanceLinkModelImpl.getCompanyId(),
955                                                    workflowInstanceLinkModelImpl.getClassNameId(),
956                                                    workflowInstanceLinkModelImpl.getClassPK()
957                                            };
958    
959                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_C_C_C, args);
960                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C_C,
961                                            args);
962                            }
963                    }
964    
965                    EntityCacheUtil.putResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
966                            WorkflowInstanceLinkImpl.class,
967                            workflowInstanceLink.getPrimaryKey(), workflowInstanceLink, false);
968    
969                    workflowInstanceLink.resetOriginalValues();
970    
971                    return workflowInstanceLink;
972            }
973    
974            protected WorkflowInstanceLink toUnwrappedModel(
975                    WorkflowInstanceLink workflowInstanceLink) {
976                    if (workflowInstanceLink instanceof WorkflowInstanceLinkImpl) {
977                            return workflowInstanceLink;
978                    }
979    
980                    WorkflowInstanceLinkImpl workflowInstanceLinkImpl = new WorkflowInstanceLinkImpl();
981    
982                    workflowInstanceLinkImpl.setNew(workflowInstanceLink.isNew());
983                    workflowInstanceLinkImpl.setPrimaryKey(workflowInstanceLink.getPrimaryKey());
984    
985                    workflowInstanceLinkImpl.setMvccVersion(workflowInstanceLink.getMvccVersion());
986                    workflowInstanceLinkImpl.setWorkflowInstanceLinkId(workflowInstanceLink.getWorkflowInstanceLinkId());
987                    workflowInstanceLinkImpl.setGroupId(workflowInstanceLink.getGroupId());
988                    workflowInstanceLinkImpl.setCompanyId(workflowInstanceLink.getCompanyId());
989                    workflowInstanceLinkImpl.setUserId(workflowInstanceLink.getUserId());
990                    workflowInstanceLinkImpl.setUserName(workflowInstanceLink.getUserName());
991                    workflowInstanceLinkImpl.setCreateDate(workflowInstanceLink.getCreateDate());
992                    workflowInstanceLinkImpl.setModifiedDate(workflowInstanceLink.getModifiedDate());
993                    workflowInstanceLinkImpl.setClassNameId(workflowInstanceLink.getClassNameId());
994                    workflowInstanceLinkImpl.setClassPK(workflowInstanceLink.getClassPK());
995                    workflowInstanceLinkImpl.setWorkflowInstanceId(workflowInstanceLink.getWorkflowInstanceId());
996    
997                    return workflowInstanceLinkImpl;
998            }
999    
1000            /**
1001             * Returns the workflow instance link with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1002             *
1003             * @param primaryKey the primary key of the workflow instance link
1004             * @return the workflow instance link
1005             * @throws NoSuchWorkflowInstanceLinkException if a workflow instance link with the primary key could not be found
1006             */
1007            @Override
1008            public WorkflowInstanceLink findByPrimaryKey(Serializable primaryKey)
1009                    throws NoSuchWorkflowInstanceLinkException {
1010                    WorkflowInstanceLink workflowInstanceLink = fetchByPrimaryKey(primaryKey);
1011    
1012                    if (workflowInstanceLink == null) {
1013                            if (_log.isWarnEnabled()) {
1014                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1015                            }
1016    
1017                            throw new NoSuchWorkflowInstanceLinkException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1018                                    primaryKey);
1019                    }
1020    
1021                    return workflowInstanceLink;
1022            }
1023    
1024            /**
1025             * Returns the workflow instance link with the primary key or throws a {@link NoSuchWorkflowInstanceLinkException} if it could not be found.
1026             *
1027             * @param workflowInstanceLinkId the primary key of the workflow instance link
1028             * @return the workflow instance link
1029             * @throws NoSuchWorkflowInstanceLinkException if a workflow instance link with the primary key could not be found
1030             */
1031            @Override
1032            public WorkflowInstanceLink findByPrimaryKey(long workflowInstanceLinkId)
1033                    throws NoSuchWorkflowInstanceLinkException {
1034                    return findByPrimaryKey((Serializable)workflowInstanceLinkId);
1035            }
1036    
1037            /**
1038             * Returns the workflow instance link with the primary key or returns <code>null</code> if it could not be found.
1039             *
1040             * @param primaryKey the primary key of the workflow instance link
1041             * @return the workflow instance link, or <code>null</code> if a workflow instance link with the primary key could not be found
1042             */
1043            @Override
1044            public WorkflowInstanceLink fetchByPrimaryKey(Serializable primaryKey) {
1045                    WorkflowInstanceLink workflowInstanceLink = (WorkflowInstanceLink)EntityCacheUtil.getResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
1046                                    WorkflowInstanceLinkImpl.class, primaryKey);
1047    
1048                    if (workflowInstanceLink == _nullWorkflowInstanceLink) {
1049                            return null;
1050                    }
1051    
1052                    if (workflowInstanceLink == null) {
1053                            Session session = null;
1054    
1055                            try {
1056                                    session = openSession();
1057    
1058                                    workflowInstanceLink = (WorkflowInstanceLink)session.get(WorkflowInstanceLinkImpl.class,
1059                                                    primaryKey);
1060    
1061                                    if (workflowInstanceLink != null) {
1062                                            cacheResult(workflowInstanceLink);
1063                                    }
1064                                    else {
1065                                            EntityCacheUtil.putResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
1066                                                    WorkflowInstanceLinkImpl.class, primaryKey,
1067                                                    _nullWorkflowInstanceLink);
1068                                    }
1069                            }
1070                            catch (Exception e) {
1071                                    EntityCacheUtil.removeResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
1072                                            WorkflowInstanceLinkImpl.class, primaryKey);
1073    
1074                                    throw processException(e);
1075                            }
1076                            finally {
1077                                    closeSession(session);
1078                            }
1079                    }
1080    
1081                    return workflowInstanceLink;
1082            }
1083    
1084            /**
1085             * Returns the workflow instance link with the primary key or returns <code>null</code> if it could not be found.
1086             *
1087             * @param workflowInstanceLinkId the primary key of the workflow instance link
1088             * @return the workflow instance link, or <code>null</code> if a workflow instance link with the primary key could not be found
1089             */
1090            @Override
1091            public WorkflowInstanceLink fetchByPrimaryKey(long workflowInstanceLinkId) {
1092                    return fetchByPrimaryKey((Serializable)workflowInstanceLinkId);
1093            }
1094    
1095            @Override
1096            public Map<Serializable, WorkflowInstanceLink> fetchByPrimaryKeys(
1097                    Set<Serializable> primaryKeys) {
1098                    if (primaryKeys.isEmpty()) {
1099                            return Collections.emptyMap();
1100                    }
1101    
1102                    Map<Serializable, WorkflowInstanceLink> map = new HashMap<Serializable, WorkflowInstanceLink>();
1103    
1104                    if (primaryKeys.size() == 1) {
1105                            Iterator<Serializable> iterator = primaryKeys.iterator();
1106    
1107                            Serializable primaryKey = iterator.next();
1108    
1109                            WorkflowInstanceLink workflowInstanceLink = fetchByPrimaryKey(primaryKey);
1110    
1111                            if (workflowInstanceLink != null) {
1112                                    map.put(primaryKey, workflowInstanceLink);
1113                            }
1114    
1115                            return map;
1116                    }
1117    
1118                    Set<Serializable> uncachedPrimaryKeys = null;
1119    
1120                    for (Serializable primaryKey : primaryKeys) {
1121                            WorkflowInstanceLink workflowInstanceLink = (WorkflowInstanceLink)EntityCacheUtil.getResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
1122                                            WorkflowInstanceLinkImpl.class, primaryKey);
1123    
1124                            if (workflowInstanceLink == null) {
1125                                    if (uncachedPrimaryKeys == null) {
1126                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1127                                    }
1128    
1129                                    uncachedPrimaryKeys.add(primaryKey);
1130                            }
1131                            else {
1132                                    map.put(primaryKey, workflowInstanceLink);
1133                            }
1134                    }
1135    
1136                    if (uncachedPrimaryKeys == null) {
1137                            return map;
1138                    }
1139    
1140                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1141                                    1);
1142    
1143                    query.append(_SQL_SELECT_WORKFLOWINSTANCELINK_WHERE_PKS_IN);
1144    
1145                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1146                            query.append(String.valueOf(primaryKey));
1147    
1148                            query.append(StringPool.COMMA);
1149                    }
1150    
1151                    query.setIndex(query.index() - 1);
1152    
1153                    query.append(StringPool.CLOSE_PARENTHESIS);
1154    
1155                    String sql = query.toString();
1156    
1157                    Session session = null;
1158    
1159                    try {
1160                            session = openSession();
1161    
1162                            Query q = session.createQuery(sql);
1163    
1164                            for (WorkflowInstanceLink workflowInstanceLink : (List<WorkflowInstanceLink>)q.list()) {
1165                                    map.put(workflowInstanceLink.getPrimaryKeyObj(),
1166                                            workflowInstanceLink);
1167    
1168                                    cacheResult(workflowInstanceLink);
1169    
1170                                    uncachedPrimaryKeys.remove(workflowInstanceLink.getPrimaryKeyObj());
1171                            }
1172    
1173                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1174                                    EntityCacheUtil.putResult(WorkflowInstanceLinkModelImpl.ENTITY_CACHE_ENABLED,
1175                                            WorkflowInstanceLinkImpl.class, primaryKey,
1176                                            _nullWorkflowInstanceLink);
1177                            }
1178                    }
1179                    catch (Exception e) {
1180                            throw processException(e);
1181                    }
1182                    finally {
1183                            closeSession(session);
1184                    }
1185    
1186                    return map;
1187            }
1188    
1189            /**
1190             * Returns all the workflow instance links.
1191             *
1192             * @return the workflow instance links
1193             */
1194            @Override
1195            public List<WorkflowInstanceLink> findAll() {
1196                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1197            }
1198    
1199            /**
1200             * Returns a range of all the workflow instance links.
1201             *
1202             * <p>
1203             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link WorkflowInstanceLinkModelImpl}. 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.
1204             * </p>
1205             *
1206             * @param start the lower bound of the range of workflow instance links
1207             * @param end the upper bound of the range of workflow instance links (not inclusive)
1208             * @return the range of workflow instance links
1209             */
1210            @Override
1211            public List<WorkflowInstanceLink> findAll(int start, int end) {
1212                    return findAll(start, end, null);
1213            }
1214    
1215            /**
1216             * Returns an ordered range of all the workflow instance links.
1217             *
1218             * <p>
1219             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link WorkflowInstanceLinkModelImpl}. 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.
1220             * </p>
1221             *
1222             * @param start the lower bound of the range of workflow instance links
1223             * @param end the upper bound of the range of workflow instance links (not inclusive)
1224             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1225             * @return the ordered range of workflow instance links
1226             */
1227            @Override
1228            public List<WorkflowInstanceLink> findAll(int start, int end,
1229                    OrderByComparator<WorkflowInstanceLink> orderByComparator) {
1230                    boolean pagination = true;
1231                    FinderPath finderPath = null;
1232                    Object[] finderArgs = null;
1233    
1234                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1235                                    (orderByComparator == null)) {
1236                            pagination = false;
1237                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1238                            finderArgs = FINDER_ARGS_EMPTY;
1239                    }
1240                    else {
1241                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1242                            finderArgs = new Object[] { start, end, orderByComparator };
1243                    }
1244    
1245                    List<WorkflowInstanceLink> list = (List<WorkflowInstanceLink>)FinderCacheUtil.getResult(finderPath,
1246                                    finderArgs, this);
1247    
1248                    if (list == null) {
1249                            StringBundler query = null;
1250                            String sql = null;
1251    
1252                            if (orderByComparator != null) {
1253                                    query = new StringBundler(2 +
1254                                                    (orderByComparator.getOrderByFields().length * 3));
1255    
1256                                    query.append(_SQL_SELECT_WORKFLOWINSTANCELINK);
1257    
1258                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1259                                            orderByComparator);
1260    
1261                                    sql = query.toString();
1262                            }
1263                            else {
1264                                    sql = _SQL_SELECT_WORKFLOWINSTANCELINK;
1265    
1266                                    if (pagination) {
1267                                            sql = sql.concat(WorkflowInstanceLinkModelImpl.ORDER_BY_JPQL);
1268                                    }
1269                            }
1270    
1271                            Session session = null;
1272    
1273                            try {
1274                                    session = openSession();
1275    
1276                                    Query q = session.createQuery(sql);
1277    
1278                                    if (!pagination) {
1279                                            list = (List<WorkflowInstanceLink>)QueryUtil.list(q,
1280                                                            getDialect(), start, end, false);
1281    
1282                                            Collections.sort(list);
1283    
1284                                            list = Collections.unmodifiableList(list);
1285                                    }
1286                                    else {
1287                                            list = (List<WorkflowInstanceLink>)QueryUtil.list(q,
1288                                                            getDialect(), start, end);
1289                                    }
1290    
1291                                    cacheResult(list);
1292    
1293                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1294                            }
1295                            catch (Exception e) {
1296                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1297    
1298                                    throw processException(e);
1299                            }
1300                            finally {
1301                                    closeSession(session);
1302                            }
1303                    }
1304    
1305                    return list;
1306            }
1307    
1308            /**
1309             * Removes all the workflow instance links from the database.
1310             *
1311             */
1312            @Override
1313            public void removeAll() {
1314                    for (WorkflowInstanceLink workflowInstanceLink : findAll()) {
1315                            remove(workflowInstanceLink);
1316                    }
1317            }
1318    
1319            /**
1320             * Returns the number of workflow instance links.
1321             *
1322             * @return the number of workflow instance links
1323             */
1324            @Override
1325            public int countAll() {
1326                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1327                                    FINDER_ARGS_EMPTY, this);
1328    
1329                    if (count == null) {
1330                            Session session = null;
1331    
1332                            try {
1333                                    session = openSession();
1334    
1335                                    Query q = session.createQuery(_SQL_COUNT_WORKFLOWINSTANCELINK);
1336    
1337                                    count = (Long)q.uniqueResult();
1338    
1339                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1340                                            FINDER_ARGS_EMPTY, count);
1341                            }
1342                            catch (Exception e) {
1343                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1344                                            FINDER_ARGS_EMPTY);
1345    
1346                                    throw processException(e);
1347                            }
1348                            finally {
1349                                    closeSession(session);
1350                            }
1351                    }
1352    
1353                    return count.intValue();
1354            }
1355    
1356            /**
1357             * Initializes the workflow instance link persistence.
1358             */
1359            public void afterPropertiesSet() {
1360            }
1361    
1362            public void destroy() {
1363                    EntityCacheUtil.removeCache(WorkflowInstanceLinkImpl.class.getName());
1364                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1365                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1366                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1367            }
1368    
1369            private static final String _SQL_SELECT_WORKFLOWINSTANCELINK = "SELECT workflowInstanceLink FROM WorkflowInstanceLink workflowInstanceLink";
1370            private static final String _SQL_SELECT_WORKFLOWINSTANCELINK_WHERE_PKS_IN = "SELECT workflowInstanceLink FROM WorkflowInstanceLink workflowInstanceLink WHERE workflowInstanceLinkId IN (";
1371            private static final String _SQL_SELECT_WORKFLOWINSTANCELINK_WHERE = "SELECT workflowInstanceLink FROM WorkflowInstanceLink workflowInstanceLink WHERE ";
1372            private static final String _SQL_COUNT_WORKFLOWINSTANCELINK = "SELECT COUNT(workflowInstanceLink) FROM WorkflowInstanceLink workflowInstanceLink";
1373            private static final String _SQL_COUNT_WORKFLOWINSTANCELINK_WHERE = "SELECT COUNT(workflowInstanceLink) FROM WorkflowInstanceLink workflowInstanceLink WHERE ";
1374            private static final String _ORDER_BY_ENTITY_ALIAS = "workflowInstanceLink.";
1375            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No WorkflowInstanceLink exists with the primary key ";
1376            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No WorkflowInstanceLink exists with the key {";
1377            private static final Log _log = LogFactoryUtil.getLog(WorkflowInstanceLinkPersistenceImpl.class);
1378            private static final WorkflowInstanceLink _nullWorkflowInstanceLink = new WorkflowInstanceLinkImpl() {
1379                            @Override
1380                            public Object clone() {
1381                                    return this;
1382                            }
1383    
1384                            @Override
1385                            public CacheModel<WorkflowInstanceLink> toCacheModel() {
1386                                    return _nullWorkflowInstanceLinkCacheModel;
1387                            }
1388                    };
1389    
1390            private static final CacheModel<WorkflowInstanceLink> _nullWorkflowInstanceLinkCacheModel =
1391                    new NullCacheModel();
1392    
1393            private static class NullCacheModel implements CacheModel<WorkflowInstanceLink>,
1394                    MVCCModel {
1395                    @Override
1396                    public long getMvccVersion() {
1397                            return -1;
1398                    }
1399    
1400                    @Override
1401                    public void setMvccVersion(long mvccVersion) {
1402                    }
1403    
1404                    @Override
1405                    public WorkflowInstanceLink toEntityModel() {
1406                            return _nullWorkflowInstanceLink;
1407                    }
1408            }
1409    }