001    /**
002     * Copyright (c) 2000-2013 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            @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("{userId=" + userId + "}");
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("{userId=" + userId + "}");
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    }