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