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