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