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