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