001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void delete(Session session, BaseModel<?> model)
030                    throws ORMException {
031    
032                    if (model.isCachedModel() || isEnabled()) {
033                            Object staleObject = session.get(
034                                    model.getClass(), model.getPrimaryKeyObj());
035    
036                            if (staleObject != null) {
037                                    session.evict(staleObject);
038                            }
039                    }
040    
041                    session.delete(model);
042    
043                    if (!isEnabled()) {
044                            session.flush();
045    
046                            return;
047                    }
048    
049                    if ((PropsValues.HIBERNATE_JDBC_BATCH_SIZE == 0) ||
050                            ((_counter.get() % PropsValues.HIBERNATE_JDBC_BATCH_SIZE) == 0)) {
051    
052                            session.flush();
053                    }
054    
055                    _counter.set(_counter.get() + 1);
056            }
057    
058            public boolean isEnabled() {
059                    return _enabled.get();
060            }
061    
062            public void setEnabled(boolean enabled) {
063                    _enabled.set(enabled);
064            }
065    
066            public void update(Session session, BaseModel<?> model, boolean merge)
067                    throws ORMException {
068    
069                    if (merge || model.isCachedModel()) {
070                            session.merge(model);
071                    }
072                    else {
073                            if (model.isNew()) {
074                                    session.save(model);
075                            }
076                            else {
077                                    boolean contains = false;
078    
079                                    if (isEnabled()) {
080                                            Object obj = session.get(
081                                                    model.getClass(), model.getPrimaryKeyObj());
082    
083                                            if ((obj != null) && obj.equals(model)) {
084                                                    contains = true;
085                                            }
086                                    }
087    
088                                    if (!contains && !session.contains(model)) {
089                                            session.saveOrUpdate(model);
090                                    }
091                            }
092                    }
093    
094                    if (!isEnabled()) {
095                            session.flush();
096    
097                            return;
098                    }
099    
100                    if ((PropsValues.HIBERNATE_JDBC_BATCH_SIZE == 0) ||
101                            ((_counter.get() % PropsValues.HIBERNATE_JDBC_BATCH_SIZE) == 0)) {
102    
103                            session.flush();
104                    }
105    
106                    _counter.set(_counter.get() + 1);
107            }
108    
109            private static final long _INITIAL_COUNTER = 1;
110    
111            private static ThreadLocal<Long> _counter = new InitialThreadLocal<Long>(
112                    BatchSessionImpl.class + "._counter", _INITIAL_COUNTER);
113            private static ThreadLocal<Boolean> _enabled =
114                    new InitialThreadLocal<Boolean>(
115                            BatchSessionImpl.class + "._enabled", false);
116    
117    }