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.Group;
020 import com.liferay.portal.model.Phone;
021 import com.liferay.portal.service.GroupLocalServiceUtil;
022 import com.liferay.portal.service.PhoneLocalServiceUtil;
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 PhoneStagedModelDataHandler
035 extends BaseStagedModelDataHandler<Phone> {
036
037 public static final String[] CLASS_NAMES = {Phone.class.getName()};
038
039 @Override
040 public void deleteStagedModel(Phone phone) {
041 PhoneLocalServiceUtil.deletePhone(phone);
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 Phone phone = PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
052 uuid, group.getCompanyId());
053
054 if (phone != null) {
055 deleteStagedModel(phone);
056 }
057 }
058
059 @Override
060 public List<Phone> fetchStagedModelsByUuidAndCompanyId(
061 String uuid, long companyId) {
062
063 List<Phone> phones = new ArrayList<>();
064
065 phones.add(
066 PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
067 uuid, companyId));
068
069 return phones;
070 }
071
072 @Override
073 public String[] getClassNames() {
074 return CLASS_NAMES;
075 }
076
077 @Override
078 protected void doExportStagedModel(
079 PortletDataContext portletDataContext, Phone phone)
080 throws Exception {
081
082 Element phoneElement = portletDataContext.getExportDataElement(phone);
083
084 portletDataContext.addClassedModel(
085 phoneElement, ExportImportPathUtil.getModelPath(phone), phone);
086 }
087
088 @Override
089 protected void doImportStagedModel(
090 PortletDataContext portletDataContext, Phone phone)
091 throws Exception {
092
093 long userId = portletDataContext.getUserId(phone.getUserUuid());
094
095 ServiceContext serviceContext = portletDataContext.createServiceContext(
096 phone);
097
098 Phone existingPhone =
099 PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
100 phone.getUuid(), portletDataContext.getCompanyId());
101
102 Phone importedPhone = null;
103
104 if (existingPhone == null) {
105 serviceContext.setUuid(phone.getUuid());
106
107 importedPhone = PhoneLocalServiceUtil.addPhone(
108 userId, phone.getClassName(), phone.getClassPK(),
109 phone.getNumber(), phone.getExtension(), phone.getTypeId(),
110 phone.isPrimary(), serviceContext);
111 }
112 else {
113 importedPhone = PhoneLocalServiceUtil.updatePhone(
114 existingPhone.getPhoneId(), phone.getNumber(),
115 phone.getExtension(), phone.getTypeId(), phone.isPrimary());
116 }
117
118 portletDataContext.importClassedModel(phone, importedPhone);
119 }
120
121 }