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.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.dao.orm.EntityCache;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCache;
023    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024    import com.liferay.portal.kernel.dao.orm.FinderPath;
025    import com.liferay.portal.kernel.dao.orm.Query;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.NoSuchAccountException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.model.Account;
032    import com.liferay.portal.kernel.model.CacheModel;
033    import com.liferay.portal.kernel.model.MVCCModel;
034    import com.liferay.portal.kernel.service.ServiceContext;
035    import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
036    import com.liferay.portal.kernel.service.persistence.AccountPersistence;
037    import com.liferay.portal.kernel.service.persistence.CompanyProvider;
038    import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
039    import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
040    import com.liferay.portal.kernel.util.OrderByComparator;
041    import com.liferay.portal.kernel.util.SetUtil;
042    import com.liferay.portal.kernel.util.StringBundler;
043    import com.liferay.portal.kernel.util.StringPool;
044    import com.liferay.portal.model.impl.AccountImpl;
045    import com.liferay.portal.model.impl.AccountModelImpl;
046    
047    import java.io.Serializable;
048    
049    import java.util.Collections;
050    import java.util.Date;
051    import java.util.HashMap;
052    import java.util.HashSet;
053    import java.util.Iterator;
054    import java.util.List;
055    import java.util.Map;
056    import java.util.Set;
057    
058    /**
059     * The persistence implementation for the account service.
060     *
061     * <p>
062     * Caching information and settings can be found in <code>portal.properties</code>
063     * </p>
064     *
065     * @author Brian Wing Shun Chan
066     * @see AccountPersistence
067     * @see com.liferay.portal.kernel.service.persistence.AccountUtil
068     * @generated
069     */
070    @ProviderType
071    public class AccountPersistenceImpl extends BasePersistenceImpl<Account>
072            implements AccountPersistence {
073            /*
074             * NOTE FOR DEVELOPERS:
075             *
076             * Never modify or reference this class directly. Always use {@link AccountUtil} to access the account persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
077             */
078            public static final String FINDER_CLASS_NAME_ENTITY = AccountImpl.class.getName();
079            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
080                    ".List1";
081            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
082                    ".List2";
083            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
084                            AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
087                            AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
088                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
089            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
090                            AccountModelImpl.FINDER_CACHE_ENABLED, Long.class,
091                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
092    
093            public AccountPersistenceImpl() {
094                    setModelClass(Account.class);
095            }
096    
097            /**
098             * Caches the account in the entity cache if it is enabled.
099             *
100             * @param account the account
101             */
102            @Override
103            public void cacheResult(Account account) {
104                    entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
105                            AccountImpl.class, account.getPrimaryKey(), account);
106    
107                    account.resetOriginalValues();
108            }
109    
110            /**
111             * Caches the accounts in the entity cache if it is enabled.
112             *
113             * @param accounts the accounts
114             */
115            @Override
116            public void cacheResult(List<Account> accounts) {
117                    for (Account account : accounts) {
118                            if (entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
119                                                    AccountImpl.class, account.getPrimaryKey()) == null) {
120                                    cacheResult(account);
121                            }
122                            else {
123                                    account.resetOriginalValues();
124                            }
125                    }
126            }
127    
128            /**
129             * Clears the cache for all accounts.
130             *
131             * <p>
132             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
133             * </p>
134             */
135            @Override
136            public void clearCache() {
137                    entityCache.clearCache(AccountImpl.class);
138    
139                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
140                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
141                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
142            }
143    
144            /**
145             * Clears the cache for the account.
146             *
147             * <p>
148             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
149             * </p>
150             */
151            @Override
152            public void clearCache(Account account) {
153                    entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
154                            AccountImpl.class, account.getPrimaryKey());
155    
156                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
157                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
158            }
159    
160            @Override
161            public void clearCache(List<Account> accounts) {
162                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
163                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
164    
165                    for (Account account : accounts) {
166                            entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
167                                    AccountImpl.class, account.getPrimaryKey());
168                    }
169            }
170    
171            /**
172             * Creates a new account with the primary key. Does not add the account to the database.
173             *
174             * @param accountId the primary key for the new account
175             * @return the new account
176             */
177            @Override
178            public Account create(long accountId) {
179                    Account account = new AccountImpl();
180    
181                    account.setNew(true);
182                    account.setPrimaryKey(accountId);
183    
184                    account.setCompanyId(companyProvider.getCompanyId());
185    
186                    return account;
187            }
188    
189            /**
190             * Removes the account with the primary key from the database. Also notifies the appropriate model listeners.
191             *
192             * @param accountId the primary key of the account
193             * @return the account that was removed
194             * @throws NoSuchAccountException if a account with the primary key could not be found
195             */
196            @Override
197            public Account remove(long accountId) throws NoSuchAccountException {
198                    return remove((Serializable)accountId);
199            }
200    
201            /**
202             * Removes the account with the primary key from the database. Also notifies the appropriate model listeners.
203             *
204             * @param primaryKey the primary key of the account
205             * @return the account that was removed
206             * @throws NoSuchAccountException if a account with the primary key could not be found
207             */
208            @Override
209            public Account remove(Serializable primaryKey)
210                    throws NoSuchAccountException {
211                    Session session = null;
212    
213                    try {
214                            session = openSession();
215    
216                            Account account = (Account)session.get(AccountImpl.class, primaryKey);
217    
218                            if (account == null) {
219                                    if (_log.isDebugEnabled()) {
220                                            _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
221                                    }
222    
223                                    throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
224                                            primaryKey);
225                            }
226    
227                            return remove(account);
228                    }
229                    catch (NoSuchAccountException nsee) {
230                            throw nsee;
231                    }
232                    catch (Exception e) {
233                            throw processException(e);
234                    }
235                    finally {
236                            closeSession(session);
237                    }
238            }
239    
240            @Override
241            protected Account removeImpl(Account account) {
242                    account = toUnwrappedModel(account);
243    
244                    Session session = null;
245    
246                    try {
247                            session = openSession();
248    
249                            if (!session.contains(account)) {
250                                    account = (Account)session.get(AccountImpl.class,
251                                                    account.getPrimaryKeyObj());
252                            }
253    
254                            if (account != null) {
255                                    session.delete(account);
256                            }
257                    }
258                    catch (Exception e) {
259                            throw processException(e);
260                    }
261                    finally {
262                            closeSession(session);
263                    }
264    
265                    if (account != null) {
266                            clearCache(account);
267                    }
268    
269                    return account;
270            }
271    
272            @Override
273            public Account updateImpl(Account account) {
274                    account = toUnwrappedModel(account);
275    
276                    boolean isNew = account.isNew();
277    
278                    AccountModelImpl accountModelImpl = (AccountModelImpl)account;
279    
280                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
281    
282                    Date now = new Date();
283    
284                    if (isNew && (account.getCreateDate() == null)) {
285                            if (serviceContext == null) {
286                                    account.setCreateDate(now);
287                            }
288                            else {
289                                    account.setCreateDate(serviceContext.getCreateDate(now));
290                            }
291                    }
292    
293                    if (!accountModelImpl.hasSetModifiedDate()) {
294                            if (serviceContext == null) {
295                                    account.setModifiedDate(now);
296                            }
297                            else {
298                                    account.setModifiedDate(serviceContext.getModifiedDate(now));
299                            }
300                    }
301    
302                    Session session = null;
303    
304                    try {
305                            session = openSession();
306    
307                            if (account.isNew()) {
308                                    session.save(account);
309    
310                                    account.setNew(false);
311                            }
312                            else {
313                                    account = (Account)session.merge(account);
314                            }
315                    }
316                    catch (Exception e) {
317                            throw processException(e);
318                    }
319                    finally {
320                            closeSession(session);
321                    }
322    
323                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
324    
325                    if (isNew) {
326                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
327                    }
328    
329                    entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
330                            AccountImpl.class, account.getPrimaryKey(), account, false);
331    
332                    account.resetOriginalValues();
333    
334                    return account;
335            }
336    
337            protected Account toUnwrappedModel(Account account) {
338                    if (account instanceof AccountImpl) {
339                            return account;
340                    }
341    
342                    AccountImpl accountImpl = new AccountImpl();
343    
344                    accountImpl.setNew(account.isNew());
345                    accountImpl.setPrimaryKey(account.getPrimaryKey());
346    
347                    accountImpl.setMvccVersion(account.getMvccVersion());
348                    accountImpl.setAccountId(account.getAccountId());
349                    accountImpl.setCompanyId(account.getCompanyId());
350                    accountImpl.setUserId(account.getUserId());
351                    accountImpl.setUserName(account.getUserName());
352                    accountImpl.setCreateDate(account.getCreateDate());
353                    accountImpl.setModifiedDate(account.getModifiedDate());
354                    accountImpl.setParentAccountId(account.getParentAccountId());
355                    accountImpl.setName(account.getName());
356                    accountImpl.setLegalName(account.getLegalName());
357                    accountImpl.setLegalId(account.getLegalId());
358                    accountImpl.setLegalType(account.getLegalType());
359                    accountImpl.setSicCode(account.getSicCode());
360                    accountImpl.setTickerSymbol(account.getTickerSymbol());
361                    accountImpl.setIndustry(account.getIndustry());
362                    accountImpl.setType(account.getType());
363                    accountImpl.setSize(account.getSize());
364    
365                    return accountImpl;
366            }
367    
368            /**
369             * Returns the account with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
370             *
371             * @param primaryKey the primary key of the account
372             * @return the account
373             * @throws NoSuchAccountException if a account with the primary key could not be found
374             */
375            @Override
376            public Account findByPrimaryKey(Serializable primaryKey)
377                    throws NoSuchAccountException {
378                    Account account = fetchByPrimaryKey(primaryKey);
379    
380                    if (account == null) {
381                            if (_log.isDebugEnabled()) {
382                                    _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
383                            }
384    
385                            throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
386                                    primaryKey);
387                    }
388    
389                    return account;
390            }
391    
392            /**
393             * Returns the account with the primary key or throws a {@link NoSuchAccountException} if it could not be found.
394             *
395             * @param accountId the primary key of the account
396             * @return the account
397             * @throws NoSuchAccountException if a account with the primary key could not be found
398             */
399            @Override
400            public Account findByPrimaryKey(long accountId)
401                    throws NoSuchAccountException {
402                    return findByPrimaryKey((Serializable)accountId);
403            }
404    
405            /**
406             * Returns the account with the primary key or returns <code>null</code> if it could not be found.
407             *
408             * @param primaryKey the primary key of the account
409             * @return the account, or <code>null</code> if a account with the primary key could not be found
410             */
411            @Override
412            public Account fetchByPrimaryKey(Serializable primaryKey) {
413                    Account account = (Account)entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
414                                    AccountImpl.class, primaryKey);
415    
416                    if (account == _nullAccount) {
417                            return null;
418                    }
419    
420                    if (account == null) {
421                            Session session = null;
422    
423                            try {
424                                    session = openSession();
425    
426                                    account = (Account)session.get(AccountImpl.class, primaryKey);
427    
428                                    if (account != null) {
429                                            cacheResult(account);
430                                    }
431                                    else {
432                                            entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
433                                                    AccountImpl.class, primaryKey, _nullAccount);
434                                    }
435                            }
436                            catch (Exception e) {
437                                    entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
438                                            AccountImpl.class, primaryKey);
439    
440                                    throw processException(e);
441                            }
442                            finally {
443                                    closeSession(session);
444                            }
445                    }
446    
447                    return account;
448            }
449    
450            /**
451             * Returns the account with the primary key or returns <code>null</code> if it could not be found.
452             *
453             * @param accountId the primary key of the account
454             * @return the account, or <code>null</code> if a account with the primary key could not be found
455             */
456            @Override
457            public Account fetchByPrimaryKey(long accountId) {
458                    return fetchByPrimaryKey((Serializable)accountId);
459            }
460    
461            @Override
462            public Map<Serializable, Account> fetchByPrimaryKeys(
463                    Set<Serializable> primaryKeys) {
464                    if (primaryKeys.isEmpty()) {
465                            return Collections.emptyMap();
466                    }
467    
468                    Map<Serializable, Account> map = new HashMap<Serializable, Account>();
469    
470                    if (primaryKeys.size() == 1) {
471                            Iterator<Serializable> iterator = primaryKeys.iterator();
472    
473                            Serializable primaryKey = iterator.next();
474    
475                            Account account = fetchByPrimaryKey(primaryKey);
476    
477                            if (account != null) {
478                                    map.put(primaryKey, account);
479                            }
480    
481                            return map;
482                    }
483    
484                    Set<Serializable> uncachedPrimaryKeys = null;
485    
486                    for (Serializable primaryKey : primaryKeys) {
487                            Account account = (Account)entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
488                                            AccountImpl.class, primaryKey);
489    
490                            if (account == null) {
491                                    if (uncachedPrimaryKeys == null) {
492                                            uncachedPrimaryKeys = new HashSet<Serializable>();
493                                    }
494    
495                                    uncachedPrimaryKeys.add(primaryKey);
496                            }
497                            else {
498                                    map.put(primaryKey, account);
499                            }
500                    }
501    
502                    if (uncachedPrimaryKeys == null) {
503                            return map;
504                    }
505    
506                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
507                                    1);
508    
509                    query.append(_SQL_SELECT_ACCOUNT_WHERE_PKS_IN);
510    
511                    for (Serializable primaryKey : uncachedPrimaryKeys) {
512                            query.append(String.valueOf(primaryKey));
513    
514                            query.append(StringPool.COMMA);
515                    }
516    
517                    query.setIndex(query.index() - 1);
518    
519                    query.append(StringPool.CLOSE_PARENTHESIS);
520    
521                    String sql = query.toString();
522    
523                    Session session = null;
524    
525                    try {
526                            session = openSession();
527    
528                            Query q = session.createQuery(sql);
529    
530                            for (Account account : (List<Account>)q.list()) {
531                                    map.put(account.getPrimaryKeyObj(), account);
532    
533                                    cacheResult(account);
534    
535                                    uncachedPrimaryKeys.remove(account.getPrimaryKeyObj());
536                            }
537    
538                            for (Serializable primaryKey : uncachedPrimaryKeys) {
539                                    entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
540                                            AccountImpl.class, primaryKey, _nullAccount);
541                            }
542                    }
543                    catch (Exception e) {
544                            throw processException(e);
545                    }
546                    finally {
547                            closeSession(session);
548                    }
549    
550                    return map;
551            }
552    
553            /**
554             * Returns all the accounts.
555             *
556             * @return the accounts
557             */
558            @Override
559            public List<Account> findAll() {
560                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
561            }
562    
563            /**
564             * Returns a range of all the accounts.
565             *
566             * <p>
567             * 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 AccountModelImpl}. 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.
568             * </p>
569             *
570             * @param start the lower bound of the range of accounts
571             * @param end the upper bound of the range of accounts (not inclusive)
572             * @return the range of accounts
573             */
574            @Override
575            public List<Account> findAll(int start, int end) {
576                    return findAll(start, end, null);
577            }
578    
579            /**
580             * Returns an ordered range of all the accounts.
581             *
582             * <p>
583             * 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 AccountModelImpl}. 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.
584             * </p>
585             *
586             * @param start the lower bound of the range of accounts
587             * @param end the upper bound of the range of accounts (not inclusive)
588             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
589             * @return the ordered range of accounts
590             */
591            @Override
592            public List<Account> findAll(int start, int end,
593                    OrderByComparator<Account> orderByComparator) {
594                    return findAll(start, end, orderByComparator, true);
595            }
596    
597            /**
598             * Returns an ordered range of all the accounts.
599             *
600             * <p>
601             * 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 AccountModelImpl}. 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.
602             * </p>
603             *
604             * @param start the lower bound of the range of accounts
605             * @param end the upper bound of the range of accounts (not inclusive)
606             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
607             * @param retrieveFromCache whether to retrieve from the finder cache
608             * @return the ordered range of accounts
609             */
610            @Override
611            public List<Account> findAll(int start, int end,
612                    OrderByComparator<Account> orderByComparator, boolean retrieveFromCache) {
613                    boolean pagination = true;
614                    FinderPath finderPath = null;
615                    Object[] finderArgs = null;
616    
617                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
618                                    (orderByComparator == null)) {
619                            pagination = false;
620                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
621                            finderArgs = FINDER_ARGS_EMPTY;
622                    }
623                    else {
624                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
625                            finderArgs = new Object[] { start, end, orderByComparator };
626                    }
627    
628                    List<Account> list = null;
629    
630                    if (retrieveFromCache) {
631                            list = (List<Account>)finderCache.getResult(finderPath, finderArgs,
632                                            this);
633                    }
634    
635                    if (list == null) {
636                            StringBundler query = null;
637                            String sql = null;
638    
639                            if (orderByComparator != null) {
640                                    query = new StringBundler(2 +
641                                                    (orderByComparator.getOrderByFields().length * 2));
642    
643                                    query.append(_SQL_SELECT_ACCOUNT);
644    
645                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
646                                            orderByComparator);
647    
648                                    sql = query.toString();
649                            }
650                            else {
651                                    sql = _SQL_SELECT_ACCOUNT;
652    
653                                    if (pagination) {
654                                            sql = sql.concat(AccountModelImpl.ORDER_BY_JPQL);
655                                    }
656                            }
657    
658                            Session session = null;
659    
660                            try {
661                                    session = openSession();
662    
663                                    Query q = session.createQuery(sql);
664    
665                                    if (!pagination) {
666                                            list = (List<Account>)QueryUtil.list(q, getDialect(),
667                                                            start, end, false);
668    
669                                            Collections.sort(list);
670    
671                                            list = Collections.unmodifiableList(list);
672                                    }
673                                    else {
674                                            list = (List<Account>)QueryUtil.list(q, getDialect(),
675                                                            start, end);
676                                    }
677    
678                                    cacheResult(list);
679    
680                                    finderCache.putResult(finderPath, finderArgs, list);
681                            }
682                            catch (Exception e) {
683                                    finderCache.removeResult(finderPath, finderArgs);
684    
685                                    throw processException(e);
686                            }
687                            finally {
688                                    closeSession(session);
689                            }
690                    }
691    
692                    return list;
693            }
694    
695            /**
696             * Removes all the accounts from the database.
697             *
698             */
699            @Override
700            public void removeAll() {
701                    for (Account account : findAll()) {
702                            remove(account);
703                    }
704            }
705    
706            /**
707             * Returns the number of accounts.
708             *
709             * @return the number of accounts
710             */
711            @Override
712            public int countAll() {
713                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
714                                    FINDER_ARGS_EMPTY, this);
715    
716                    if (count == null) {
717                            Session session = null;
718    
719                            try {
720                                    session = openSession();
721    
722                                    Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
723    
724                                    count = (Long)q.uniqueResult();
725    
726                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
727                                            count);
728                            }
729                            catch (Exception e) {
730                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
731                                            FINDER_ARGS_EMPTY);
732    
733                                    throw processException(e);
734                            }
735                            finally {
736                                    closeSession(session);
737                            }
738                    }
739    
740                    return count.intValue();
741            }
742    
743            @Override
744            public Set<String> getBadColumnNames() {
745                    return _badColumnNames;
746            }
747    
748            @Override
749            protected Map<String, Integer> getTableColumnsMap() {
750                    return AccountModelImpl.TABLE_COLUMNS_MAP;
751            }
752    
753            /**
754             * Initializes the account persistence.
755             */
756            public void afterPropertiesSet() {
757            }
758    
759            public void destroy() {
760                    entityCache.removeCache(AccountImpl.class.getName());
761                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
762                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
763                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
764            }
765    
766            @BeanReference(type = CompanyProviderWrapper.class)
767            protected CompanyProvider companyProvider;
768            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
769            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
770            private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
771            private static final String _SQL_SELECT_ACCOUNT_WHERE_PKS_IN = "SELECT account FROM Account account WHERE accountId IN (";
772            private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
773            private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
774            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
775            private static final Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
776            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
777                                    "type", "size"
778                            });
779            private static final Account _nullAccount = new AccountImpl() {
780                            @Override
781                            public Object clone() {
782                                    return this;
783                            }
784    
785                            @Override
786                            public CacheModel<Account> toCacheModel() {
787                                    return _nullAccountCacheModel;
788                            }
789                    };
790    
791            private static final CacheModel<Account> _nullAccountCacheModel = new NullCacheModel();
792    
793            private static class NullCacheModel implements CacheModel<Account>,
794                    MVCCModel {
795                    @Override
796                    public long getMvccVersion() {
797                            return -1;
798                    }
799    
800                    @Override
801                    public void setMvccVersion(long mvccVersion) {
802                    }
803    
804                    @Override
805                    public Account toEntityModel() {
806                            return _nullAccount;
807                    }
808            }
809    }