001    /**
002     * Copyright (c) 2000-2013 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.kernel.dao.orm.ORMException;
018    import com.liferay.portal.kernel.dao.orm.Session;
019    import com.liferay.portal.kernel.util.InitialThreadLocal;
020    import com.liferay.portal.model.BaseModel;
021    import com.liferay.portal.util.PropsValues;
022    
023    /**
024     * @author Raymond Aug??
025     * @author Brian Wing Shun Chan
026     */
027    public class BatchSessionImpl implements BatchSession {
028    
029            @Override
030            public void delete(Session session, BaseModel<?> model)
031                    throws ORMException {
032    
033                    if (!session.contains(model)) {
034                            model = (BaseModel<?>)session.get(
035                                    model.getClass(), model.getPrimaryKeyObj());
036                    }
037    
038                    if (model != null) {
039                            session.delete(model);
040                    }
041    
042                    if (!isEnabled()) {
043                            session.flush();
044    
045                            return;
046                    }
047    
048                    if ((PropsValues.HIBERNATE_JDBC_BATCH_SIZE == 0) ||
049                            ((_counter.get() % PropsValues.HIBERNATE_JDBC_BATCH_SIZE) == 0)) {
050    
051                            session.flush();
052                    }
053    
054                    _counter.set(_counter.get() + 1);
055            }
056    
057            @Override
058            public boolean isEnabled() {
059                    return _enabled.get();
060            }
061    
062            @Override
063            public void setEnabled(boolean enabled) {
064                    _enabled.set(enabled);
065            }
066    
067            @Override
068            public void update(Session session, BaseModel<?> model, boolean merge)
069                    throws ORMException {
070    
071                    if (merge || model.isCachedModel()) {
072                            session.merge(model);
073                    }
074                    else {
075                            if (model.isNew()) {
076                                    session.save(model);
077    
078                                    model.setNew(false);
079                            }
080                            else {
081                                    session.merge(model);
082                            }
083                    }
084    
085                    if (!isEnabled()) {
086                            session.flush();
087    
088                            return;
089                    }
090    
091                    if ((PropsValues.HIBERNATE_JDBC_BATCH_SIZE == 0) ||
092                            ((_counter.get() % PropsValues.HIBERNATE_JDBC_BATCH_SIZE) == 0)) {
093    
094                            session.flush();
095                    }
096    
097                    _counter.set(_counter.get() + 1);
098            }
099    
100            private static final long _INITIAL_COUNTER = 1;
101    
102            private static ThreadLocal<Long> _counter = new InitialThreadLocal<Long>(
103                    BatchSessionImpl.class + "._counter", _INITIAL_COUNTER);
104            private static ThreadLocal<Boolean> _enabled =
105                    new InitialThreadLocal<Boolean>(
106                            BatchSessionImpl.class + "._enabled", false);
107    
108    }