001    /**
002     * Copyright (c) 2000-2013 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.mail.service.persistence;
016    
017    import com.liferay.mail.NoSuchCyrusUserException;
018    import com.liferay.mail.model.CyrusUser;
019    import com.liferay.portal.kernel.dao.orm.ObjectNotFoundException;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.model.Dummy;
023    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class CyrusUserPersistenceImpl
029            extends BasePersistenceImpl<Dummy> implements CyrusUserPersistence {
030    
031            @Override
032            public CyrusUser findByPrimaryKey(long userId)
033                    throws NoSuchCyrusUserException, SystemException {
034    
035                    Session session = null;
036    
037                    try {
038                            session = openSession();
039    
040                            return (CyrusUser)session.load(
041                                    CyrusUser.class, String.valueOf(userId));
042                    }
043                    catch (ObjectNotFoundException onfe) {
044                            throw new NoSuchCyrusUserException();
045                    }
046                    catch (Exception e) {
047                            throw processException(e);
048                    }
049                    finally {
050                            closeSession(session);
051                    }
052            }
053    
054            @Override
055            public void remove(long userId)
056                    throws NoSuchCyrusUserException, SystemException {
057    
058                    Session session = null;
059    
060                    try {
061                            session = openSession();
062    
063                            CyrusUser user = (CyrusUser)session.load(
064                                    CyrusUser.class, String.valueOf(userId));
065    
066                            session.delete(user);
067    
068                            session.flush();
069                    }
070                    catch (ObjectNotFoundException onfe) {
071                            throw new NoSuchCyrusUserException();
072                    }
073                    catch (Exception e) {
074                            throw processException(e);
075                    }
076                    finally {
077                            closeSession(session);
078                    }
079            }
080    
081            @Override
082            public void update(CyrusUser user) throws SystemException {
083                    Session session = null;
084    
085                    try {
086                            session = openSession();
087    
088                            try {
089                                    CyrusUser userModel = (CyrusUser)session.load(
090                                            CyrusUser.class, String.valueOf(user.getUserId()));
091    
092                                    userModel.setPassword(user.getPassword());
093    
094                                    session.flush();
095                            }
096                            catch (ObjectNotFoundException onfe) {
097                                    CyrusUser userModel = new CyrusUser(
098                                            user.getUserId(), user.getPassword());
099    
100                                    session.save(userModel);
101    
102                                    session.flush();
103                            }
104                    }
105                    catch (Exception e) {
106                            throw processException(e);
107                    }
108                    finally {
109                            closeSession(session);
110                    }
111            }
112    
113    }