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.Address;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.service.AddressLocalServiceUtil;
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 AddressStagedModelDataHandler
033            extends BaseStagedModelDataHandler<Address> {
034    
035            public static final String[] CLASS_NAMES = {Address.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                    Address address =
045                            AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
046                                    uuid, group.getCompanyId());
047    
048                    if (address != null) {
049                            AddressLocalServiceUtil.deleteAddress(address);
050                    }
051            }
052    
053            @Override
054            public String[] getClassNames() {
055                    return CLASS_NAMES;
056            }
057    
058            @Override
059            protected void doExportStagedModel(
060                            PortletDataContext portletDataContext, Address address)
061                    throws Exception {
062    
063                    Element addressElement = portletDataContext.getExportDataElement(
064                            address);
065    
066                    portletDataContext.addClassedModel(
067                            addressElement, ExportImportPathUtil.getModelPath(address),
068                            address);
069            }
070    
071            @Override
072            protected void doImportStagedModel(
073                            PortletDataContext portletDataContext, Address address)
074                    throws Exception {
075    
076                    long userId = portletDataContext.getUserId(address.getUserUuid());
077    
078                    ServiceContext serviceContext = portletDataContext.createServiceContext(
079                            address);
080    
081                    Address existingAddress =
082                            AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
083                                    address.getUuid(), portletDataContext.getCompanyId());
084    
085                    Address importedAddress = null;
086    
087                    if (existingAddress == null) {
088                            serviceContext.setUuid(address.getUuid());
089    
090                            importedAddress = AddressLocalServiceUtil.addAddress(
091                                    userId, address.getClassName(), address.getClassPK(),
092                                    address.getStreet1(), address.getStreet2(),
093                                    address.getStreet3(), address.getCity(), address.getZip(),
094                                    address.getRegionId(), address.getCountryId(),
095                                    address.getTypeId(), address.getMailing(), address.isPrimary(),
096                                    serviceContext);
097                    }
098                    else {
099                            importedAddress = AddressLocalServiceUtil.updateAddress(
100                                    existingAddress.getAddressId(), address.getStreet1(),
101                                    address.getStreet2(), address.getStreet3(), address.getCity(),
102                                    address.getZip(), address.getRegionId(), address.getCountryId(),
103                                    address.getTypeId(), address.getMailing(), address.isPrimary());
104                    }
105    
106                    portletDataContext.importClassedModel(address, importedAddress);
107            }
108    
109    }