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