001    /**
002     * Copyright (c) 2000-2010 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;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.NoSuchPortletException;
019    import com.liferay.portal.kernel.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.model.Portlet;
040    import com.liferay.portal.model.impl.PortletImpl;
041    import com.liferay.portal.model.impl.PortletModelImpl;
042    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043    
044    import java.io.Serializable;
045    
046    import java.util.ArrayList;
047    import java.util.Collections;
048    import java.util.List;
049    
050    /**
051     * @author    Brian Wing Shun Chan
052     * @see       PortletPersistence
053     * @see       PortletUtil
054     * @generated
055     */
056    public class PortletPersistenceImpl extends BasePersistenceImpl<Portlet>
057            implements PortletPersistence {
058            public static final String FINDER_CLASS_NAME_ENTITY = PortletImpl.class.getName();
059            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
060                    ".List";
061            public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
062                            PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
063                            "findByCompanyId",
064                            new String[] {
065                                    Long.class.getName(),
066                                    
067                            "java.lang.Integer", "java.lang.Integer",
068                                    "com.liferay.portal.kernel.util.OrderByComparator"
069                            });
070            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
071                            PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
072                            "countByCompanyId", new String[] { Long.class.getName() });
073            public static final FinderPath FINDER_PATH_FETCH_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
074                            PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
075                            "fetchByC_P",
076                            new String[] { Long.class.getName(), String.class.getName() });
077            public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
078                            PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
079                            "countByC_P",
080                            new String[] { Long.class.getName(), String.class.getName() });
081            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
082                            PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
083                            "findAll", new String[0]);
084            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
085                            PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
086                            "countAll", new String[0]);
087    
088            public void cacheResult(Portlet portlet) {
089                    EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
090                            PortletImpl.class, portlet.getPrimaryKey(), portlet);
091    
092                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
093                            new Object[] {
094                                    new Long(portlet.getCompanyId()),
095                                    
096                            portlet.getPortletId()
097                            }, portlet);
098            }
099    
100            public void cacheResult(List<Portlet> portlets) {
101                    for (Portlet portlet : portlets) {
102                            if (EntityCacheUtil.getResult(
103                                                    PortletModelImpl.ENTITY_CACHE_ENABLED,
104                                                    PortletImpl.class, portlet.getPrimaryKey(), this) == null) {
105                                    cacheResult(portlet);
106                            }
107                    }
108            }
109    
110            public void clearCache() {
111                    CacheRegistryUtil.clear(PortletImpl.class.getName());
112                    EntityCacheUtil.clearCache(PortletImpl.class.getName());
113                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
114                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
115            }
116    
117            public void clearCache(Portlet portlet) {
118                    EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
119                            PortletImpl.class, portlet.getPrimaryKey());
120    
121                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
122                            new Object[] {
123                                    new Long(portlet.getCompanyId()),
124                                    
125                            portlet.getPortletId()
126                            });
127            }
128    
129            public Portlet create(long id) {
130                    Portlet portlet = new PortletImpl();
131    
132                    portlet.setNew(true);
133                    portlet.setPrimaryKey(id);
134    
135                    return portlet;
136            }
137    
138            public Portlet remove(Serializable primaryKey)
139                    throws NoSuchModelException, SystemException {
140                    return remove(((Long)primaryKey).longValue());
141            }
142    
143            public Portlet remove(long id)
144                    throws NoSuchPortletException, SystemException {
145                    Session session = null;
146    
147                    try {
148                            session = openSession();
149    
150                            Portlet portlet = (Portlet)session.get(PortletImpl.class,
151                                            new Long(id));
152    
153                            if (portlet == null) {
154                                    if (_log.isWarnEnabled()) {
155                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
156                                    }
157    
158                                    throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
159                                            id);
160                            }
161    
162                            return remove(portlet);
163                    }
164                    catch (NoSuchPortletException nsee) {
165                            throw nsee;
166                    }
167                    catch (Exception e) {
168                            throw processException(e);
169                    }
170                    finally {
171                            closeSession(session);
172                    }
173            }
174    
175            protected Portlet removeImpl(Portlet portlet) throws SystemException {
176                    portlet = toUnwrappedModel(portlet);
177    
178                    Session session = null;
179    
180                    try {
181                            session = openSession();
182    
183                            if (portlet.isCachedModel() || BatchSessionUtil.isEnabled()) {
184                                    Object staleObject = session.get(PortletImpl.class,
185                                                    portlet.getPrimaryKeyObj());
186    
187                                    if (staleObject != null) {
188                                            session.evict(staleObject);
189                                    }
190                            }
191    
192                            session.delete(portlet);
193    
194                            session.flush();
195                    }
196                    catch (Exception e) {
197                            throw processException(e);
198                    }
199                    finally {
200                            closeSession(session);
201                    }
202    
203                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
204    
205                    PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
206    
207                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
208                            new Object[] {
209                                    new Long(portletModelImpl.getOriginalCompanyId()),
210                                    
211                            portletModelImpl.getOriginalPortletId()
212                            });
213    
214                    EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
215                            PortletImpl.class, portlet.getPrimaryKey());
216    
217                    return portlet;
218            }
219    
220            public Portlet updateImpl(com.liferay.portal.model.Portlet portlet,
221                    boolean merge) throws SystemException {
222                    portlet = toUnwrappedModel(portlet);
223    
224                    boolean isNew = portlet.isNew();
225    
226                    PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
227    
228                    Session session = null;
229    
230                    try {
231                            session = openSession();
232    
233                            BatchSessionUtil.update(session, portlet, merge);
234    
235                            portlet.setNew(false);
236                    }
237                    catch (Exception e) {
238                            throw processException(e);
239                    }
240                    finally {
241                            closeSession(session);
242                    }
243    
244                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
245    
246                    EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
247                            PortletImpl.class, portlet.getPrimaryKey(), portlet);
248    
249                    if (!isNew &&
250                                    ((portlet.getCompanyId() != portletModelImpl.getOriginalCompanyId()) ||
251                                    !Validator.equals(portlet.getPortletId(),
252                                            portletModelImpl.getOriginalPortletId()))) {
253                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
254                                    new Object[] {
255                                            new Long(portletModelImpl.getOriginalCompanyId()),
256                                            
257                                    portletModelImpl.getOriginalPortletId()
258                                    });
259                    }
260    
261                    if (isNew ||
262                                    ((portlet.getCompanyId() != portletModelImpl.getOriginalCompanyId()) ||
263                                    !Validator.equals(portlet.getPortletId(),
264                                            portletModelImpl.getOriginalPortletId()))) {
265                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
266                                    new Object[] {
267                                            new Long(portlet.getCompanyId()),
268                                            
269                                    portlet.getPortletId()
270                                    }, portlet);
271                    }
272    
273                    return portlet;
274            }
275    
276            protected Portlet toUnwrappedModel(Portlet portlet) {
277                    if (portlet instanceof PortletImpl) {
278                            return portlet;
279                    }
280    
281                    PortletImpl portletImpl = new PortletImpl();
282    
283                    portletImpl.setNew(portlet.isNew());
284                    portletImpl.setPrimaryKey(portlet.getPrimaryKey());
285    
286                    portletImpl.setId(portlet.getId());
287                    portletImpl.setCompanyId(portlet.getCompanyId());
288                    portletImpl.setPortletId(portlet.getPortletId());
289                    portletImpl.setRoles(portlet.getRoles());
290                    portletImpl.setActive(portlet.isActive());
291    
292                    return portletImpl;
293            }
294    
295            public Portlet findByPrimaryKey(Serializable primaryKey)
296                    throws NoSuchModelException, SystemException {
297                    return findByPrimaryKey(((Long)primaryKey).longValue());
298            }
299    
300            public Portlet findByPrimaryKey(long id)
301                    throws NoSuchPortletException, SystemException {
302                    Portlet portlet = fetchByPrimaryKey(id);
303    
304                    if (portlet == null) {
305                            if (_log.isWarnEnabled()) {
306                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
307                            }
308    
309                            throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
310                                    id);
311                    }
312    
313                    return portlet;
314            }
315    
316            public Portlet fetchByPrimaryKey(Serializable primaryKey)
317                    throws SystemException {
318                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
319            }
320    
321            public Portlet fetchByPrimaryKey(long id) throws SystemException {
322                    Portlet portlet = (Portlet)EntityCacheUtil.getResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
323                                    PortletImpl.class, id, this);
324    
325                    if (portlet == null) {
326                            Session session = null;
327    
328                            try {
329                                    session = openSession();
330    
331                                    portlet = (Portlet)session.get(PortletImpl.class, new Long(id));
332                            }
333                            catch (Exception e) {
334                                    throw processException(e);
335                            }
336                            finally {
337                                    if (portlet != null) {
338                                            cacheResult(portlet);
339                                    }
340    
341                                    closeSession(session);
342                            }
343                    }
344    
345                    return portlet;
346            }
347    
348            public List<Portlet> findByCompanyId(long companyId)
349                    throws SystemException {
350                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
351                            null);
352            }
353    
354            public List<Portlet> findByCompanyId(long companyId, int start, int end)
355                    throws SystemException {
356                    return findByCompanyId(companyId, start, end, null);
357            }
358    
359            public List<Portlet> findByCompanyId(long companyId, int start, int end,
360                    OrderByComparator orderByComparator) throws SystemException {
361                    Object[] finderArgs = new Object[] {
362                                    companyId,
363                                    
364                                    String.valueOf(start), String.valueOf(end),
365                                    String.valueOf(orderByComparator)
366                            };
367    
368                    List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
369                                    finderArgs, this);
370    
371                    if (list == null) {
372                            Session session = null;
373    
374                            try {
375                                    session = openSession();
376    
377                                    StringBundler query = null;
378    
379                                    if (orderByComparator != null) {
380                                            query = new StringBundler(3 +
381                                                            (orderByComparator.getOrderByFields().length * 3));
382                                    }
383                                    else {
384                                            query = new StringBundler(2);
385                                    }
386    
387                                    query.append(_SQL_SELECT_PORTLET_WHERE);
388    
389                                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
390    
391                                    if (orderByComparator != null) {
392                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
393                                                    orderByComparator);
394                                    }
395    
396                                    String sql = query.toString();
397    
398                                    Query q = session.createQuery(sql);
399    
400                                    QueryPos qPos = QueryPos.getInstance(q);
401    
402                                    qPos.add(companyId);
403    
404                                    list = (List<Portlet>)QueryUtil.list(q, getDialect(), start, end);
405                            }
406                            catch (Exception e) {
407                                    throw processException(e);
408                            }
409                            finally {
410                                    if (list == null) {
411                                            list = new ArrayList<Portlet>();
412                                    }
413    
414                                    cacheResult(list);
415    
416                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
417                                            finderArgs, list);
418    
419                                    closeSession(session);
420                            }
421                    }
422    
423                    return list;
424            }
425    
426            public Portlet findByCompanyId_First(long companyId,
427                    OrderByComparator orderByComparator)
428                    throws NoSuchPortletException, SystemException {
429                    List<Portlet> list = findByCompanyId(companyId, 0, 1, orderByComparator);
430    
431                    if (list.isEmpty()) {
432                            StringBundler msg = new StringBundler(4);
433    
434                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
435    
436                            msg.append("companyId=");
437                            msg.append(companyId);
438    
439                            msg.append(StringPool.CLOSE_CURLY_BRACE);
440    
441                            throw new NoSuchPortletException(msg.toString());
442                    }
443                    else {
444                            return list.get(0);
445                    }
446            }
447    
448            public Portlet findByCompanyId_Last(long companyId,
449                    OrderByComparator orderByComparator)
450                    throws NoSuchPortletException, SystemException {
451                    int count = countByCompanyId(companyId);
452    
453                    List<Portlet> list = findByCompanyId(companyId, count - 1, count,
454                                    orderByComparator);
455    
456                    if (list.isEmpty()) {
457                            StringBundler msg = new StringBundler(4);
458    
459                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
460    
461                            msg.append("companyId=");
462                            msg.append(companyId);
463    
464                            msg.append(StringPool.CLOSE_CURLY_BRACE);
465    
466                            throw new NoSuchPortletException(msg.toString());
467                    }
468                    else {
469                            return list.get(0);
470                    }
471            }
472    
473            public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
474                    OrderByComparator orderByComparator)
475                    throws NoSuchPortletException, SystemException {
476                    Portlet portlet = findByPrimaryKey(id);
477    
478                    Session session = null;
479    
480                    try {
481                            session = openSession();
482    
483                            Portlet[] array = new PortletImpl[3];
484    
485                            array[0] = getByCompanyId_PrevAndNext(session, portlet, companyId,
486                                            orderByComparator, true);
487    
488                            array[1] = portlet;
489    
490                            array[2] = getByCompanyId_PrevAndNext(session, portlet, companyId,
491                                            orderByComparator, false);
492    
493                            return array;
494                    }
495                    catch (Exception e) {
496                            throw processException(e);
497                    }
498                    finally {
499                            closeSession(session);
500                    }
501            }
502    
503            protected Portlet getByCompanyId_PrevAndNext(Session session,
504                    Portlet portlet, long companyId, OrderByComparator orderByComparator,
505                    boolean previous) {
506                    StringBundler query = null;
507    
508                    if (orderByComparator != null) {
509                            query = new StringBundler(6 +
510                                            (orderByComparator.getOrderByFields().length * 6));
511                    }
512                    else {
513                            query = new StringBundler(3);
514                    }
515    
516                    query.append(_SQL_SELECT_PORTLET_WHERE);
517    
518                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
519    
520                    if (orderByComparator != null) {
521                            String[] orderByFields = orderByComparator.getOrderByFields();
522    
523                            if (orderByFields.length > 0) {
524                                    query.append(WHERE_AND);
525                            }
526    
527                            for (int i = 0; i < orderByFields.length; i++) {
528                                    query.append(_ORDER_BY_ENTITY_ALIAS);
529                                    query.append(orderByFields[i]);
530    
531                                    if ((i + 1) < orderByFields.length) {
532                                            if (orderByComparator.isAscending() ^ previous) {
533                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
534                                            }
535                                            else {
536                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
537                                            }
538                                    }
539                                    else {
540                                            if (orderByComparator.isAscending() ^ previous) {
541                                                    query.append(WHERE_GREATER_THAN);
542                                            }
543                                            else {
544                                                    query.append(WHERE_LESSER_THAN);
545                                            }
546                                    }
547                            }
548    
549                            query.append(ORDER_BY_CLAUSE);
550    
551                            for (int i = 0; i < orderByFields.length; i++) {
552                                    query.append(_ORDER_BY_ENTITY_ALIAS);
553                                    query.append(orderByFields[i]);
554    
555                                    if ((i + 1) < orderByFields.length) {
556                                            if (orderByComparator.isAscending() ^ previous) {
557                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
558                                            }
559                                            else {
560                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
561                                            }
562                                    }
563                                    else {
564                                            if (orderByComparator.isAscending() ^ previous) {
565                                                    query.append(ORDER_BY_ASC);
566                                            }
567                                            else {
568                                                    query.append(ORDER_BY_DESC);
569                                            }
570                                    }
571                            }
572                    }
573    
574                    String sql = query.toString();
575    
576                    Query q = session.createQuery(sql);
577    
578                    q.setFirstResult(0);
579                    q.setMaxResults(2);
580    
581                    QueryPos qPos = QueryPos.getInstance(q);
582    
583                    qPos.add(companyId);
584    
585                    if (orderByComparator != null) {
586                            Object[] values = orderByComparator.getOrderByValues(portlet);
587    
588                            for (Object value : values) {
589                                    qPos.add(value);
590                            }
591                    }
592    
593                    List<Portlet> list = q.list();
594    
595                    if (list.size() == 2) {
596                            return list.get(1);
597                    }
598                    else {
599                            return null;
600                    }
601            }
602    
603            public Portlet findByC_P(long companyId, String portletId)
604                    throws NoSuchPortletException, SystemException {
605                    Portlet portlet = fetchByC_P(companyId, portletId);
606    
607                    if (portlet == null) {
608                            StringBundler msg = new StringBundler(6);
609    
610                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
611    
612                            msg.append("companyId=");
613                            msg.append(companyId);
614    
615                            msg.append(", portletId=");
616                            msg.append(portletId);
617    
618                            msg.append(StringPool.CLOSE_CURLY_BRACE);
619    
620                            if (_log.isWarnEnabled()) {
621                                    _log.warn(msg.toString());
622                            }
623    
624                            throw new NoSuchPortletException(msg.toString());
625                    }
626    
627                    return portlet;
628            }
629    
630            public Portlet fetchByC_P(long companyId, String portletId)
631                    throws SystemException {
632                    return fetchByC_P(companyId, portletId, true);
633            }
634    
635            public Portlet fetchByC_P(long companyId, String portletId,
636                    boolean retrieveFromCache) throws SystemException {
637                    Object[] finderArgs = new Object[] { companyId, portletId };
638    
639                    Object result = null;
640    
641                    if (retrieveFromCache) {
642                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
643                                            finderArgs, this);
644                    }
645    
646                    if (result == null) {
647                            Session session = null;
648    
649                            try {
650                                    session = openSession();
651    
652                                    StringBundler query = new StringBundler(3);
653    
654                                    query.append(_SQL_SELECT_PORTLET_WHERE);
655    
656                                    query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
657    
658                                    if (portletId == null) {
659                                            query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
660                                    }
661                                    else {
662                                            if (portletId.equals(StringPool.BLANK)) {
663                                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
664                                            }
665                                            else {
666                                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
667                                            }
668                                    }
669    
670                                    String sql = query.toString();
671    
672                                    Query q = session.createQuery(sql);
673    
674                                    QueryPos qPos = QueryPos.getInstance(q);
675    
676                                    qPos.add(companyId);
677    
678                                    if (portletId != null) {
679                                            qPos.add(portletId);
680                                    }
681    
682                                    List<Portlet> list = q.list();
683    
684                                    result = list;
685    
686                                    Portlet portlet = null;
687    
688                                    if (list.isEmpty()) {
689                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
690                                                    finderArgs, list);
691                                    }
692                                    else {
693                                            portlet = list.get(0);
694    
695                                            cacheResult(portlet);
696    
697                                            if ((portlet.getCompanyId() != companyId) ||
698                                                            (portlet.getPortletId() == null) ||
699                                                            !portlet.getPortletId().equals(portletId)) {
700                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
701                                                            finderArgs, portlet);
702                                            }
703                                    }
704    
705                                    return portlet;
706                            }
707                            catch (Exception e) {
708                                    throw processException(e);
709                            }
710                            finally {
711                                    if (result == null) {
712                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
713                                                    finderArgs, new ArrayList<Portlet>());
714                                    }
715    
716                                    closeSession(session);
717                            }
718                    }
719                    else {
720                            if (result instanceof List<?>) {
721                                    return null;
722                            }
723                            else {
724                                    return (Portlet)result;
725                            }
726                    }
727            }
728    
729            public List<Portlet> findAll() throws SystemException {
730                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
731            }
732    
733            public List<Portlet> findAll(int start, int end) throws SystemException {
734                    return findAll(start, end, null);
735            }
736    
737            public List<Portlet> findAll(int start, int end,
738                    OrderByComparator orderByComparator) throws SystemException {
739                    Object[] finderArgs = new Object[] {
740                                    String.valueOf(start), String.valueOf(end),
741                                    String.valueOf(orderByComparator)
742                            };
743    
744                    List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
745                                    finderArgs, this);
746    
747                    if (list == null) {
748                            Session session = null;
749    
750                            try {
751                                    session = openSession();
752    
753                                    StringBundler query = null;
754                                    String sql = null;
755    
756                                    if (orderByComparator != null) {
757                                            query = new StringBundler(2 +
758                                                            (orderByComparator.getOrderByFields().length * 3));
759    
760                                            query.append(_SQL_SELECT_PORTLET);
761    
762                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
763                                                    orderByComparator);
764    
765                                            sql = query.toString();
766                                    }
767                                    else {
768                                            sql = _SQL_SELECT_PORTLET;
769                                    }
770    
771                                    Query q = session.createQuery(sql);
772    
773                                    if (orderByComparator == null) {
774                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
775                                                            start, end, false);
776    
777                                            Collections.sort(list);
778                                    }
779                                    else {
780                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
781                                                            start, end);
782                                    }
783                            }
784                            catch (Exception e) {
785                                    throw processException(e);
786                            }
787                            finally {
788                                    if (list == null) {
789                                            list = new ArrayList<Portlet>();
790                                    }
791    
792                                    cacheResult(list);
793    
794                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
795    
796                                    closeSession(session);
797                            }
798                    }
799    
800                    return list;
801            }
802    
803            public void removeByCompanyId(long companyId) throws SystemException {
804                    for (Portlet portlet : findByCompanyId(companyId)) {
805                            remove(portlet);
806                    }
807            }
808    
809            public void removeByC_P(long companyId, String portletId)
810                    throws NoSuchPortletException, SystemException {
811                    Portlet portlet = findByC_P(companyId, portletId);
812    
813                    remove(portlet);
814            }
815    
816            public void removeAll() throws SystemException {
817                    for (Portlet portlet : findAll()) {
818                            remove(portlet);
819                    }
820            }
821    
822            public int countByCompanyId(long companyId) throws SystemException {
823                    Object[] finderArgs = new Object[] { companyId };
824    
825                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
826                                    finderArgs, this);
827    
828                    if (count == null) {
829                            Session session = null;
830    
831                            try {
832                                    session = openSession();
833    
834                                    StringBundler query = new StringBundler(2);
835    
836                                    query.append(_SQL_COUNT_PORTLET_WHERE);
837    
838                                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
839    
840                                    String sql = query.toString();
841    
842                                    Query q = session.createQuery(sql);
843    
844                                    QueryPos qPos = QueryPos.getInstance(q);
845    
846                                    qPos.add(companyId);
847    
848                                    count = (Long)q.uniqueResult();
849                            }
850                            catch (Exception e) {
851                                    throw processException(e);
852                            }
853                            finally {
854                                    if (count == null) {
855                                            count = Long.valueOf(0);
856                                    }
857    
858                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
859                                            finderArgs, count);
860    
861                                    closeSession(session);
862                            }
863                    }
864    
865                    return count.intValue();
866            }
867    
868            public int countByC_P(long companyId, String portletId)
869                    throws SystemException {
870                    Object[] finderArgs = new Object[] { companyId, portletId };
871    
872                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
873                                    finderArgs, this);
874    
875                    if (count == null) {
876                            Session session = null;
877    
878                            try {
879                                    session = openSession();
880    
881                                    StringBundler query = new StringBundler(3);
882    
883                                    query.append(_SQL_COUNT_PORTLET_WHERE);
884    
885                                    query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
886    
887                                    if (portletId == null) {
888                                            query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
889                                    }
890                                    else {
891                                            if (portletId.equals(StringPool.BLANK)) {
892                                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
893                                            }
894                                            else {
895                                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
896                                            }
897                                    }
898    
899                                    String sql = query.toString();
900    
901                                    Query q = session.createQuery(sql);
902    
903                                    QueryPos qPos = QueryPos.getInstance(q);
904    
905                                    qPos.add(companyId);
906    
907                                    if (portletId != null) {
908                                            qPos.add(portletId);
909                                    }
910    
911                                    count = (Long)q.uniqueResult();
912                            }
913                            catch (Exception e) {
914                                    throw processException(e);
915                            }
916                            finally {
917                                    if (count == null) {
918                                            count = Long.valueOf(0);
919                                    }
920    
921                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
922                                            count);
923    
924                                    closeSession(session);
925                            }
926                    }
927    
928                    return count.intValue();
929            }
930    
931            public int countAll() throws SystemException {
932                    Object[] finderArgs = new Object[0];
933    
934                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
935                                    finderArgs, this);
936    
937                    if (count == null) {
938                            Session session = null;
939    
940                            try {
941                                    session = openSession();
942    
943                                    Query q = session.createQuery(_SQL_COUNT_PORTLET);
944    
945                                    count = (Long)q.uniqueResult();
946                            }
947                            catch (Exception e) {
948                                    throw processException(e);
949                            }
950                            finally {
951                                    if (count == null) {
952                                            count = Long.valueOf(0);
953                                    }
954    
955                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
956                                            count);
957    
958                                    closeSession(session);
959                            }
960                    }
961    
962                    return count.intValue();
963            }
964    
965            public void afterPropertiesSet() {
966                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
967                                            com.liferay.portal.util.PropsUtil.get(
968                                                    "value.object.listener.com.liferay.portal.model.Portlet")));
969    
970                    if (listenerClassNames.length > 0) {
971                            try {
972                                    List<ModelListener<Portlet>> listenersList = new ArrayList<ModelListener<Portlet>>();
973    
974                                    for (String listenerClassName : listenerClassNames) {
975                                            listenersList.add((ModelListener<Portlet>)InstanceFactory.newInstance(
976                                                            listenerClassName));
977                                    }
978    
979                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
980                            }
981                            catch (Exception e) {
982                                    _log.error(e);
983                            }
984                    }
985            }
986    
987            @BeanReference(type = AccountPersistence.class)
988            protected AccountPersistence accountPersistence;
989            @BeanReference(type = AddressPersistence.class)
990            protected AddressPersistence addressPersistence;
991            @BeanReference(type = BrowserTrackerPersistence.class)
992            protected BrowserTrackerPersistence browserTrackerPersistence;
993            @BeanReference(type = ClassNamePersistence.class)
994            protected ClassNamePersistence classNamePersistence;
995            @BeanReference(type = CompanyPersistence.class)
996            protected CompanyPersistence companyPersistence;
997            @BeanReference(type = ContactPersistence.class)
998            protected ContactPersistence contactPersistence;
999            @BeanReference(type = CountryPersistence.class)
1000            protected CountryPersistence countryPersistence;
1001            @BeanReference(type = EmailAddressPersistence.class)
1002            protected EmailAddressPersistence emailAddressPersistence;
1003            @BeanReference(type = GroupPersistence.class)
1004            protected GroupPersistence groupPersistence;
1005            @BeanReference(type = ImagePersistence.class)
1006            protected ImagePersistence imagePersistence;
1007            @BeanReference(type = LayoutPersistence.class)
1008            protected LayoutPersistence layoutPersistence;
1009            @BeanReference(type = LayoutPrototypePersistence.class)
1010            protected LayoutPrototypePersistence layoutPrototypePersistence;
1011            @BeanReference(type = LayoutSetPersistence.class)
1012            protected LayoutSetPersistence layoutSetPersistence;
1013            @BeanReference(type = LayoutSetPrototypePersistence.class)
1014            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1015            @BeanReference(type = ListTypePersistence.class)
1016            protected ListTypePersistence listTypePersistence;
1017            @BeanReference(type = LockPersistence.class)
1018            protected LockPersistence lockPersistence;
1019            @BeanReference(type = MembershipRequestPersistence.class)
1020            protected MembershipRequestPersistence membershipRequestPersistence;
1021            @BeanReference(type = OrganizationPersistence.class)
1022            protected OrganizationPersistence organizationPersistence;
1023            @BeanReference(type = OrgGroupPermissionPersistence.class)
1024            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1025            @BeanReference(type = OrgGroupRolePersistence.class)
1026            protected OrgGroupRolePersistence orgGroupRolePersistence;
1027            @BeanReference(type = OrgLaborPersistence.class)
1028            protected OrgLaborPersistence orgLaborPersistence;
1029            @BeanReference(type = PasswordPolicyPersistence.class)
1030            protected PasswordPolicyPersistence passwordPolicyPersistence;
1031            @BeanReference(type = PasswordPolicyRelPersistence.class)
1032            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1033            @BeanReference(type = PasswordTrackerPersistence.class)
1034            protected PasswordTrackerPersistence passwordTrackerPersistence;
1035            @BeanReference(type = PermissionPersistence.class)
1036            protected PermissionPersistence permissionPersistence;
1037            @BeanReference(type = PhonePersistence.class)
1038            protected PhonePersistence phonePersistence;
1039            @BeanReference(type = PluginSettingPersistence.class)
1040            protected PluginSettingPersistence pluginSettingPersistence;
1041            @BeanReference(type = PortletPersistence.class)
1042            protected PortletPersistence portletPersistence;
1043            @BeanReference(type = PortletItemPersistence.class)
1044            protected PortletItemPersistence portletItemPersistence;
1045            @BeanReference(type = PortletPreferencesPersistence.class)
1046            protected PortletPreferencesPersistence portletPreferencesPersistence;
1047            @BeanReference(type = RegionPersistence.class)
1048            protected RegionPersistence regionPersistence;
1049            @BeanReference(type = ReleasePersistence.class)
1050            protected ReleasePersistence releasePersistence;
1051            @BeanReference(type = ResourcePersistence.class)
1052            protected ResourcePersistence resourcePersistence;
1053            @BeanReference(type = ResourceActionPersistence.class)
1054            protected ResourceActionPersistence resourceActionPersistence;
1055            @BeanReference(type = ResourceCodePersistence.class)
1056            protected ResourceCodePersistence resourceCodePersistence;
1057            @BeanReference(type = ResourcePermissionPersistence.class)
1058            protected ResourcePermissionPersistence resourcePermissionPersistence;
1059            @BeanReference(type = RolePersistence.class)
1060            protected RolePersistence rolePersistence;
1061            @BeanReference(type = ServiceComponentPersistence.class)
1062            protected ServiceComponentPersistence serviceComponentPersistence;
1063            @BeanReference(type = ShardPersistence.class)
1064            protected ShardPersistence shardPersistence;
1065            @BeanReference(type = SubscriptionPersistence.class)
1066            protected SubscriptionPersistence subscriptionPersistence;
1067            @BeanReference(type = TicketPersistence.class)
1068            protected TicketPersistence ticketPersistence;
1069            @BeanReference(type = TeamPersistence.class)
1070            protected TeamPersistence teamPersistence;
1071            @BeanReference(type = UserPersistence.class)
1072            protected UserPersistence userPersistence;
1073            @BeanReference(type = UserGroupPersistence.class)
1074            protected UserGroupPersistence userGroupPersistence;
1075            @BeanReference(type = UserGroupGroupRolePersistence.class)
1076            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1077            @BeanReference(type = UserGroupRolePersistence.class)
1078            protected UserGroupRolePersistence userGroupRolePersistence;
1079            @BeanReference(type = UserIdMapperPersistence.class)
1080            protected UserIdMapperPersistence userIdMapperPersistence;
1081            @BeanReference(type = UserTrackerPersistence.class)
1082            protected UserTrackerPersistence userTrackerPersistence;
1083            @BeanReference(type = UserTrackerPathPersistence.class)
1084            protected UserTrackerPathPersistence userTrackerPathPersistence;
1085            @BeanReference(type = WebDAVPropsPersistence.class)
1086            protected WebDAVPropsPersistence webDAVPropsPersistence;
1087            @BeanReference(type = WebsitePersistence.class)
1088            protected WebsitePersistence websitePersistence;
1089            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1090            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1091            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1092            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1093            private static final String _SQL_SELECT_PORTLET = "SELECT portlet FROM Portlet portlet";
1094            private static final String _SQL_SELECT_PORTLET_WHERE = "SELECT portlet FROM Portlet portlet WHERE ";
1095            private static final String _SQL_COUNT_PORTLET = "SELECT COUNT(portlet) FROM Portlet portlet";
1096            private static final String _SQL_COUNT_PORTLET_WHERE = "SELECT COUNT(portlet) FROM Portlet portlet WHERE ";
1097            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "portlet.companyId = ?";
1098            private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "portlet.companyId = ? AND ";
1099            private static final String _FINDER_COLUMN_C_P_PORTLETID_1 = "portlet.portletId IS NULL";
1100            private static final String _FINDER_COLUMN_C_P_PORTLETID_2 = "portlet.portletId = ?";
1101            private static final String _FINDER_COLUMN_C_P_PORTLETID_3 = "(portlet.portletId IS NULL OR portlet.portletId = ?)";
1102            private static final String _ORDER_BY_ENTITY_ALIAS = "portlet.";
1103            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Portlet exists with the primary key ";
1104            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Portlet exists with the key {";
1105            private static Log _log = LogFactoryUtil.getLog(PortletPersistenceImpl.class);
1106    }