001
014
015 package com.liferay.portlet.dynamicdatalists.lar;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
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.lar.PortletDataException;
023 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024 import com.liferay.portal.kernel.lar.StagedModelModifiedDateComparator;
025 import com.liferay.portal.kernel.util.ArrayUtil;
026 import com.liferay.portal.kernel.util.ListUtil;
027 import com.liferay.portal.kernel.util.MapUtil;
028 import com.liferay.portal.kernel.workflow.WorkflowConstants;
029 import com.liferay.portal.kernel.xml.Element;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
032 import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
033 import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
034 import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
035 import com.liferay.portlet.dynamicdatamapping.storage.Fields;
036 import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
037
038 import java.util.List;
039 import java.util.Map;
040
041
044 public class DDLRecordStagedModelDataHandler
045 extends BaseStagedModelDataHandler<DDLRecord> {
046
047 public static final String[] CLASS_NAMES = {DDLRecord.class.getName()};
048
049 @Override
050 public void deleteStagedModel(
051 String uuid, long groupId, String className, String extraData)
052 throws PortalException {
053
054 DDLRecord record = fetchStagedModelByUuidAndGroupId(uuid, groupId);
055
056 if (record != null) {
057 DDLRecordLocalServiceUtil.deleteRecord(record);
058 }
059 }
060
061 @Override
062 public DDLRecord fetchStagedModelByUuidAndCompanyId(
063 String uuid, long companyId) {
064
065 List<DDLRecord> records =
066 DDLRecordLocalServiceUtil.getDDLRecordsByUuidAndCompanyId(
067 uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
068 new StagedModelModifiedDateComparator<DDLRecord>());
069
070 if (ListUtil.isEmpty(records)) {
071 return null;
072 }
073
074 return records.get(0);
075 }
076
077 @Override
078 public DDLRecord fetchStagedModelByUuidAndGroupId(
079 String uuid, long groupId) {
080
081 return DDLRecordLocalServiceUtil.fetchDDLRecordByUuidAndGroupId(
082 uuid, groupId);
083 }
084
085 @Override
086 public String[] getClassNames() {
087 return CLASS_NAMES;
088 }
089
090 @Override
091 public String getDisplayName(DDLRecord record) {
092 return record.getUuid();
093 }
094
095 @Override
096 protected void doExportStagedModel(
097 PortletDataContext portletDataContext, DDLRecord record)
098 throws Exception {
099
100 StagedModelDataHandlerUtil.exportReferenceStagedModel(
101 portletDataContext, record, record.getRecordSet(),
102 PortletDataContext.REFERENCE_TYPE_STRONG);
103
104 DDLRecordVersion recordVersion = record.getRecordVersion();
105
106 Fields fields = StorageEngineUtil.getFields(
107 recordVersion.getDDMStorageId());
108
109 String fieldsPath = ExportImportPathUtil.getModelPath(
110 record, "fields.xml");
111
112 portletDataContext.addZipEntry(fieldsPath, fields);
113
114 Element recordElement = portletDataContext.getExportDataElement(record);
115
116 recordElement.addAttribute("fields-path", fieldsPath);
117
118 portletDataContext.addClassedModel(
119 recordElement, ExportImportPathUtil.getModelPath(record), record);
120 }
121
122 @Override
123 protected void doImportStagedModel(
124 PortletDataContext portletDataContext, DDLRecord record)
125 throws Exception {
126
127 long userId = portletDataContext.getUserId(record.getUserUuid());
128
129 Map<Long, Long> recordSetIds =
130 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
131 DDLRecordSet.class);
132
133 long recordSetId = MapUtil.getLong(
134 recordSetIds, record.getRecordSetId(), record.getRecordSetId());
135
136 ServiceContext serviceContext = portletDataContext.createServiceContext(
137 record);
138
139 Element recordElement = portletDataContext.getImportDataElement(record);
140
141 Fields fields = (Fields)portletDataContext.getZipEntryAsObject(
142 recordElement.attributeValue("fields-path"));
143
144 DDLRecord importedRecord = null;
145
146 if (portletDataContext.isDataStrategyMirror()) {
147 DDLRecord existingRecord = fetchStagedModelByUuidAndGroupId(
148 record.getUuid(), portletDataContext.getScopeGroupId());
149
150 if (existingRecord == null) {
151 serviceContext.setUuid(record.getUuid());
152
153 importedRecord = DDLRecordLocalServiceUtil.addRecord(
154 userId, portletDataContext.getScopeGroupId(), recordSetId,
155 record.getDisplayIndex(), fields, serviceContext);
156 }
157 else {
158 importedRecord = DDLRecordLocalServiceUtil.updateRecord(
159 userId, existingRecord.getRecordId(), false,
160 record.getDisplayIndex(), fields, true, serviceContext);
161 }
162 }
163 else {
164 importedRecord = DDLRecordLocalServiceUtil.addRecord(
165 userId, portletDataContext.getScopeGroupId(), recordSetId,
166 record.getDisplayIndex(), fields, serviceContext);
167 }
168
169 portletDataContext.importClassedModel(record, importedRecord);
170 }
171
172 @Override
173 protected void validateExport(
174 PortletDataContext portletDataContext, DDLRecord record)
175 throws PortletDataException {
176
177 int status = WorkflowConstants.STATUS_ANY;
178
179 try {
180 status = record.getStatus();
181 }
182 catch (Exception e) {
183 throw new PortletDataException(e);
184 }
185
186 if (!ArrayUtil.contains(getExportableStatuses(), status)) {
187 PortletDataException pde = new PortletDataException(
188 PortletDataException.STATUS_UNAVAILABLE);
189
190 pde.setStagedModel(record);
191
192 throw pde;
193 }
194 }
195
196 }