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.Address;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.service.AddressLocalServiceUtil;
022    import com.liferay.portal.service.GroupLocalServiceUtil;
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 AddressStagedModelDataHandler
035            extends BaseStagedModelDataHandler<Address> {
036    
037            public static final String[] CLASS_NAMES = {Address.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(Address address) {
041                    AddressLocalServiceUtil.deleteAddress(address);
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                    Address address =
052                            AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
053                                    uuid, group.getCompanyId());
054    
055                    if (address != null) {
056                            deleteStagedModel(address);
057                    }
058            }
059    
060            @Override
061            public void doExportStagedModel(
062                            PortletDataContext portletDataContext, Address address)
063                    throws Exception {
064    
065                    Element addressElement = portletDataContext.getExportDataElement(
066                            address);
067    
068                    portletDataContext.addClassedModel(
069                            addressElement, ExportImportPathUtil.getModelPath(address),
070                            address);
071            }
072    
073            @Override
074            public List<Address> fetchStagedModelsByUuidAndCompanyId(
075                    String uuid, long companyId) {
076    
077                    List<Address> addresses = new ArrayList<>();
078    
079                    addresses.add(
080                            AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
081                                    uuid, companyId));
082    
083                    return addresses;
084            }
085    
086            @Override
087            public String[] getClassNames() {
088                    return CLASS_NAMES;
089            }
090    
091            @Override
092            protected void doImportStagedModel(
093                            PortletDataContext portletDataContext, Address address)
094                    throws Exception {
095    
096                    long userId = portletDataContext.getUserId(address.getUserUuid());
097    
098                    ServiceContext serviceContext = portletDataContext.createServiceContext(
099                            address);
100    
101                    Address existingAddress =
102                            AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
103                                    address.getUuid(), portletDataContext.getCompanyId());
104    
105                    Address importedAddress = null;
106    
107                    if (existingAddress == null) {
108                            serviceContext.setUuid(address.getUuid());
109    
110                            importedAddress = AddressLocalServiceUtil.addAddress(
111                                    userId, address.getClassName(), address.getClassPK(),
112                                    address.getStreet1(), address.getStreet2(),
113                                    address.getStreet3(), address.getCity(), address.getZip(),
114                                    address.getRegionId(), address.getCountryId(),
115                                    address.getTypeId(), address.getMailing(), address.isPrimary(),
116                                    serviceContext);
117                    }
118                    else {
119                            importedAddress = AddressLocalServiceUtil.updateAddress(
120                                    existingAddress.getAddressId(), address.getStreet1(),
121                                    address.getStreet2(), address.getStreet3(), address.getCity(),
122                                    address.getZip(), address.getRegionId(), address.getCountryId(),
123                                    address.getTypeId(), address.getMailing(), address.isPrimary());
124                    }
125    
126                    portletDataContext.importClassedModel(address, importedAddress);
127            }
128    
129    }