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.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    }