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