001    /**
002     * Copyright (c) 2000-2010 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.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            public CyrusUser findByPrimaryKey(long userId)
032                    throws NoSuchCyrusUserException, SystemException {
033    
034                    Session session = null;
035    
036                    try {
037                            session = openSession();
038    
039                            return (CyrusUser)session.load(
040                                    CyrusUser.class, String.valueOf(userId));
041                    }
042                    catch (ObjectNotFoundException onfe) {
043                            throw new NoSuchCyrusUserException();
044                    }
045                    catch (Exception e) {
046                            throw processException(e);
047                    }
048                    finally {
049                            closeSession(session);
050                    }
051            }
052    
053            public void remove(long userId)
054                    throws NoSuchCyrusUserException, SystemException {
055    
056                    Session session = null;
057    
058                    try {
059                            session = openSession();
060    
061                            CyrusUser user = (CyrusUser)session.load(
062                                    CyrusUser.class, String.valueOf(userId));
063    
064                            session.delete(user);
065    
066                            session.flush();
067                    }
068                    catch (ObjectNotFoundException onfe) {
069                            throw new NoSuchCyrusUserException();
070                    }
071                    catch (Exception e) {
072                            throw processException(e);
073                    }
074                    finally {
075                            closeSession(session);
076                    }
077            }
078    
079            public void update(CyrusUser user) throws SystemException {
080                    Session session = null;
081    
082                    try {
083                            session = openSession();
084    
085                            try {
086                                    CyrusUser userModel = (CyrusUser)session.load(
087                                            CyrusUser.class, String.valueOf(user.getUserId()));
088    
089                                    userModel.setPassword(user.getPassword());
090    
091                                    session.flush();
092                            }
093                            catch (ObjectNotFoundException onfe) {
094                                    CyrusUser userModel = new CyrusUser(
095                                            user.getUserId(), user.getPassword());
096    
097                                    session.save(userModel);
098    
099                                    session.flush();
100                            }
101                    }
102                    catch (Exception e) {
103                            throw processException(e);
104                    }
105                    finally {
106                            closeSession(session);
107                    }
108            }
109    
110    }