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.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.dao.orm.ORMException;
018    import com.liferay.portal.kernel.dao.orm.ObjectNotFoundException;
019    import com.liferay.portal.model.BaseModel;
020    
021    import org.hibernate.Session;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class ExceptionTranslator {
027    
028            public static ORMException translate(Exception e) {
029                    if (e instanceof org.hibernate.ObjectNotFoundException) {
030                            return new ObjectNotFoundException(e);
031                    }
032                    else {
033                            return new ORMException(e);
034                    }
035            }
036    
037            public static ORMException translate(
038                    Exception e, Session session, Object object) {
039    
040                    if (e instanceof org.hibernate.StaleObjectStateException) {
041                            BaseModel<?> baseModel = (BaseModel<?>)object;
042    
043                            Object currentObject = session.get(
044                                    object.getClass(), baseModel.getPrimaryKeyObj());
045    
046                            return new ORMException(
047                                    object + " is stale in comparison to " + currentObject, e);
048                    }
049                    else {
050                            return new ORMException(e);
051                    }
052            }
053    
054    }