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.Group;
020    import com.liferay.portal.model.Phone;
021    import com.liferay.portal.service.GroupLocalServiceUtil;
022    import com.liferay.portal.service.PhoneLocalServiceUtil;
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 PhoneStagedModelDataHandler
035            extends BaseStagedModelDataHandler<Phone> {
036    
037            public static final String[] CLASS_NAMES = {Phone.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(Phone phone) {
041                    PhoneLocalServiceUtil.deletePhone(phone);
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                    Phone phone = PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
052                            uuid, group.getCompanyId());
053    
054                    if (phone != null) {
055                            deleteStagedModel(phone);
056                    }
057            }
058    
059            @Override
060            public List<Phone> fetchStagedModelsByUuidAndCompanyId(
061                    String uuid, long companyId) {
062    
063                    List<Phone> phones = new ArrayList<>();
064    
065                    phones.add(
066                            PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
067                                    uuid, companyId));
068    
069                    return phones;
070            }
071    
072            @Override
073            public String[] getClassNames() {
074                    return CLASS_NAMES;
075            }
076    
077            @Override
078            protected void doExportStagedModel(
079                            PortletDataContext portletDataContext, Phone phone)
080                    throws Exception {
081    
082                    Element phoneElement = portletDataContext.getExportDataElement(phone);
083    
084                    portletDataContext.addClassedModel(
085                            phoneElement, ExportImportPathUtil.getModelPath(phone), phone);
086            }
087    
088            @Override
089            protected void doImportStagedModel(
090                            PortletDataContext portletDataContext, Phone phone)
091                    throws Exception {
092    
093                    long userId = portletDataContext.getUserId(phone.getUserUuid());
094    
095                    ServiceContext serviceContext = portletDataContext.createServiceContext(
096                            phone);
097    
098                    Phone existingPhone =
099                            PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
100                                    phone.getUuid(), portletDataContext.getCompanyId());
101    
102                    Phone importedPhone = null;
103    
104                    if (existingPhone == null) {
105                            serviceContext.setUuid(phone.getUuid());
106    
107                            importedPhone = PhoneLocalServiceUtil.addPhone(
108                                    userId, phone.getClassName(), phone.getClassPK(),
109                                    phone.getNumber(), phone.getExtension(), phone.getTypeId(),
110                                    phone.isPrimary(), serviceContext);
111                    }
112                    else {
113                            importedPhone = PhoneLocalServiceUtil.updatePhone(
114                                    existingPhone.getPhoneId(), phone.getNumber(),
115                                    phone.getExtension(), phone.getTypeId(), phone.isPrimary());
116                    }
117    
118                    portletDataContext.importClassedModel(phone, importedPhone);
119            }
120    
121    }