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.impl;
016    
017    import com.liferay.mail.NoSuchCyrusUserException;
018    import com.liferay.mail.model.CyrusUser;
019    import com.liferay.mail.model.CyrusVirtual;
020    import com.liferay.mail.service.CyrusService;
021    import com.liferay.mail.service.persistence.CyrusUserUtil;
022    import com.liferay.mail.service.persistence.CyrusVirtualUtil;
023    import com.liferay.portal.kernel.bean.IdentifiableBean;
024    import com.liferay.portal.kernel.exception.SystemException;
025    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
026    
027    /**
028     * @author Alexander Chow
029     */
030    @DoPrivileged
031    public class CyrusServiceImpl implements CyrusService, IdentifiableBean {
032    
033            @Override
034            public void addUser(long userId, String emailAddress, String password)
035                    throws SystemException {
036    
037                    // User
038    
039                    CyrusUser user = new CyrusUser(userId, password);
040    
041                    CyrusUserUtil.update(user);
042    
043                    // Virtual
044    
045                    CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
046    
047                    CyrusVirtualUtil.update(virtual);
048            }
049    
050            @Override
051            public void deleteEmailAddress(long companyId, long userId)
052                    throws SystemException {
053    
054                    CyrusVirtualUtil.removeByUserId(userId);
055            }
056    
057            @Override
058            public void deleteUser(long userId) throws SystemException {
059    
060                    // User
061    
062                    try {
063                            CyrusUserUtil.remove(userId);
064                    }
065                    catch (NoSuchCyrusUserException nscue) {
066                    }
067    
068                    // Virtual
069    
070                    CyrusVirtualUtil.removeByUserId(userId);
071            }
072    
073            @Override
074            public String getBeanIdentifier() {
075                    return _beanIdentifier;
076            }
077    
078            @Override
079            public void setBeanIdentifier(String beanIdentifier) {
080                    _beanIdentifier = beanIdentifier;
081            }
082    
083            @Override
084            public void updateEmailAddress(
085                            long companyId, long userId, String emailAddress)
086                    throws SystemException {
087    
088                    CyrusVirtualUtil.removeByUserId(userId);
089    
090                    CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
091    
092                    CyrusVirtualUtil.update(virtual);
093            }
094    
095            @Override
096            public void updatePassword(long companyId, long userId, String password)
097                    throws SystemException {
098    
099                    CyrusUser user = null;
100    
101                    try {
102                            user = CyrusUserUtil.findByPrimaryKey(userId);
103                    }
104                    catch (NoSuchCyrusUserException nscue) {
105                            user = new CyrusUser(userId, password);
106                    }
107    
108                    user.setPassword(password);
109    
110                    CyrusUserUtil.update(user);
111            }
112    
113            private String _beanIdentifier;
114    
115    }