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