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.Group;
023    import com.liferay.portal.model.Phone;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.PhoneLocalServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    
028    /**
029     * @author David Mendez Gonzalez
030     */
031    public class PhoneStagedModelDataHandler
032            extends BaseStagedModelDataHandler<Phone> {
033    
034            public static final String[] CLASS_NAMES = {Phone.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                    Phone phone = fetchStagedModelByUuidAndCompanyId(
044                            uuid, group.getCompanyId());
045    
046                    if (phone != null) {
047                            PhoneLocalServiceUtil.deletePhone(phone);
048                    }
049            }
050    
051            @Override
052            public Phone fetchStagedModelByUuidAndCompanyId(
053                    String uuid, long companyId) {
054    
055                    return PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
056                            uuid, companyId);
057            }
058    
059            @Override
060            public String[] getClassNames() {
061                    return CLASS_NAMES;
062            }
063    
064            @Override
065            protected void doExportStagedModel(
066                            PortletDataContext portletDataContext, Phone phone)
067                    throws Exception {
068    
069                    Element phoneElement = portletDataContext.getExportDataElement(phone);
070    
071                    portletDataContext.addClassedModel(
072                            phoneElement, ExportImportPathUtil.getModelPath(phone), phone);
073            }
074    
075            @Override
076            protected void doImportStagedModel(
077                            PortletDataContext portletDataContext, Phone phone)
078                    throws Exception {
079    
080                    long userId = portletDataContext.getUserId(phone.getUserUuid());
081    
082                    ServiceContext serviceContext = portletDataContext.createServiceContext(
083                            phone);
084    
085                    Phone existingPhone = fetchStagedModelByUuidAndCompanyId(
086                            phone.getUuid(), portletDataContext.getCompanyId());
087    
088                    Phone importedPhone = null;
089    
090                    if (existingPhone == null) {
091                            serviceContext.setUuid(phone.getUuid());
092    
093                            importedPhone = PhoneLocalServiceUtil.addPhone(
094                                    userId, phone.getClassName(), phone.getClassPK(),
095                                    phone.getNumber(), phone.getExtension(), phone.getTypeId(),
096                                    phone.isPrimary(), serviceContext);
097                    }
098                    else {
099                            importedPhone = PhoneLocalServiceUtil.updatePhone(
100                                    existingPhone.getPhoneId(), phone.getNumber(),
101                                    phone.getExtension(), phone.getTypeId(), phone.isPrimary());
102                    }
103    
104                    portletDataContext.importClassedModel(phone, importedPhone);
105            }
106    
107    }