001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }