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.portlet.usersadmin.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.EmailAddress;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.service.EmailAddressLocalServiceUtil;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    
029    /**
030     * @author David Mendez Gonzalez
031     */
032    public class EmailAddressStagedModelDataHandler
033            extends BaseStagedModelDataHandler<EmailAddress> {
034    
035            public static final String[] CLASS_NAMES = {EmailAddress.class.getName()};
036    
037            @Override
038            public void deleteStagedModel(
039                            String uuid, long groupId, String className, String extraData)
040                    throws PortalException, SystemException {
041    
042                    Group group = GroupLocalServiceUtil.getGroup(groupId);
043    
044                    EmailAddress emailAddress =
045                            EmailAddressLocalServiceUtil.fetchEmailAddressByUuidAndCompanyId(
046                                    uuid, group.getCompanyId());
047    
048                    EmailAddressLocalServiceUtil.deleteEmailAddress(emailAddress);
049            }
050    
051            @Override
052            public String[] getClassNames() {
053                    return CLASS_NAMES;
054            }
055    
056            @Override
057            protected void doExportStagedModel(
058                            PortletDataContext portletDataContext, EmailAddress emailAddress)
059                    throws Exception {
060    
061                    Element emailAddressElement = portletDataContext.getExportDataElement(
062                            emailAddress);
063    
064                    portletDataContext.addClassedModel(
065                            emailAddressElement,
066                            ExportImportPathUtil.getModelPath(emailAddress), emailAddress);
067            }
068    
069            @Override
070            protected void doImportStagedModel(
071                            PortletDataContext portletDataContext, EmailAddress emailAddress)
072                    throws Exception {
073    
074                    long userId = portletDataContext.getUserId(emailAddress.getUserUuid());
075    
076                    ServiceContext serviceContext = portletDataContext.createServiceContext(
077                            emailAddress);
078    
079                    EmailAddress existingEmailAddress =
080                            EmailAddressLocalServiceUtil.fetchEmailAddressByUuidAndCompanyId(
081                                    emailAddress.getUuid(), portletDataContext.getCompanyId());
082    
083                    EmailAddress importedEmailAddress = null;
084    
085                    if (existingEmailAddress == null) {
086                            serviceContext.setUuid(emailAddress.getUuid());
087    
088                            importedEmailAddress = EmailAddressLocalServiceUtil.addEmailAddress(
089                                    userId, emailAddress.getClassName(), emailAddress.getClassPK(),
090                                    emailAddress.getAddress(), emailAddress.getTypeId(),
091                                    emailAddress.isPrimary(), serviceContext);
092                    }
093                    else {
094                            importedEmailAddress =
095                                    EmailAddressLocalServiceUtil.updateEmailAddress(
096                                            existingEmailAddress.getEmailAddressId(),
097                                            emailAddress.getAddress(), emailAddress.getTypeId(),
098                                            emailAddress.isPrimary());
099                    }
100    
101                    portletDataContext.importClassedModel(
102                            emailAddress, importedEmailAddress);
103            }
104    
105    }