001
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
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 }