001
014
015 package com.liferay.portlet.dynamicdatamapping.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.ExportImportHelperUtil;
021 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
022 import com.liferay.portal.kernel.lar.PortletDataContext;
023 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.FileUtil;
027 import com.liferay.portal.kernel.util.MapUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.kernel.xml.Element;
032 import com.liferay.portal.model.Image;
033 import com.liferay.portal.service.ImageLocalServiceUtil;
034 import com.liferay.portal.service.ServiceContext;
035 import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException;
036 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
038 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
040
041 import java.io.File;
042
043 import java.util.Map;
044
045
049 public class DDMTemplateStagedModelDataHandler
050 extends BaseStagedModelDataHandler<DDMTemplate> {
051
052 public static final String[] CLASS_NAMES = {DDMTemplate.class.getName()};
053
054 @Override
055 public void deleteStagedModel(
056 String uuid, long groupId, String className, String extraData)
057 throws PortalException, SystemException {
058
059 DDMTemplate ddmTemplate =
060 DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
061 uuid, groupId);
062
063 if (ddmTemplate != null) {
064 DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
065 }
066 }
067
068 @Override
069 public String[] getClassNames() {
070 return CLASS_NAMES;
071 }
072
073 @Override
074 public String getDisplayName(DDMTemplate template) {
075 return template.getNameCurrentValue();
076 }
077
078 protected DDMTemplate addTemplate(
079 long userId, long groupId, DDMTemplate template, long classPK,
080 File smallFile, ServiceContext serviceContext)
081 throws Exception {
082
083 DDMTemplate newTemplate = null;
084
085 try {
086 return DDMTemplateLocalServiceUtil.addTemplate(
087 userId, groupId, template.getClassNameId(), classPK,
088 template.getTemplateKey(), template.getNameMap(),
089 template.getDescriptionMap(), template.getType(),
090 template.getMode(), template.getLanguage(),
091 template.getScript(), template.isCacheable(),
092 template.isSmallImage(), template.getSmallImageURL(), smallFile,
093 serviceContext);
094 }
095 catch (TemplateDuplicateTemplateKeyException tdtke) {
096 newTemplate = DDMTemplateLocalServiceUtil.addTemplate(
097 userId, groupId, template.getClassNameId(), classPK, null,
098 template.getNameMap(), template.getDescriptionMap(),
099 template.getType(), template.getMode(), template.getLanguage(),
100 template.getScript(), template.isCacheable(),
101 template.isSmallImage(), template.getSmallImageURL(), smallFile,
102 serviceContext);
103
104 if (_log.isWarnEnabled()) {
105 StringBundler sb = new StringBundler(4);
106
107 sb.append("A template with the key ");
108 sb.append(template.getTemplateKey());
109 sb.append(" already exists. The new generated key is ");
110 sb.append(newTemplate.getTemplateKey());
111
112 _log.warn(sb.toString());
113 }
114 }
115
116 return newTemplate;
117 }
118
119 @Override
120 protected void doExportStagedModel(
121 PortletDataContext portletDataContext, DDMTemplate template)
122 throws Exception {
123
124 Element templateElement = portletDataContext.getExportDataElement(
125 template);
126
127 DDMStructure structure = DDMStructureLocalServiceUtil.fetchStructure(
128 template.getClassPK());
129
130 if (structure != null) {
131 StagedModelDataHandlerUtil.exportReferenceStagedModel(
132 portletDataContext, template, structure,
133 PortletDataContext.REFERENCE_TYPE_STRONG);
134 }
135
136 if (template.isSmallImage()) {
137 Image smallImage = ImageLocalServiceUtil.fetchImage(
138 template.getSmallImageId());
139
140 if (Validator.isNotNull(template.getSmallImageURL())) {
141 String smallImageURL =
142 ExportImportHelperUtil.replaceExportContentReferences(
143 portletDataContext, template, templateElement,
144 template.getSmallImageURL().concat(StringPool.SPACE),
145 true);
146
147 template.setSmallImageURL(smallImageURL);
148 }
149 else if (smallImage != null) {
150 String smallImagePath = ExportImportPathUtil.getModelPath(
151 template,
152 smallImage.getImageId() + StringPool.PERIOD +
153 template.getSmallImageType());
154
155 templateElement.addAttribute(
156 "small-image-path", smallImagePath);
157
158 template.setSmallImageType(smallImage.getType());
159
160 portletDataContext.addZipEntry(
161 smallImagePath, smallImage.getTextObj());
162 }
163 }
164
165 if (portletDataContext.getBooleanParameter(
166 DDMPortletDataHandler.NAMESPACE, "referenced-content")) {
167
168 String content =
169 ExportImportHelperUtil.replaceExportContentReferences(
170 portletDataContext, template, templateElement,
171 template.getScript(), true);
172
173 template.setScript(content);
174 }
175
176 portletDataContext.addClassedModel(
177 templateElement, ExportImportPathUtil.getModelPath(template),
178 template);
179 }
180
181 @Override
182 protected void doImportCompanyStagedModel(
183 PortletDataContext portletDataContext, DDMTemplate template)
184 throws Exception {
185
186 DDMTemplate existingTemplate =
187 DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
188 template.getUuid(), portletDataContext.getCompanyGroupId());
189
190 Map<Long, Long> templateIds =
191 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
192 DDMTemplate.class);
193
194 templateIds.put(
195 template.getTemplateId(), existingTemplate.getTemplateId());
196
197 Map<String, String> templateKeys =
198 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
199 DDMTemplate.class + ".ddmTemplateKey");
200
201 templateKeys.put(
202 template.getTemplateKey(), existingTemplate.getTemplateKey());
203 }
204
205 @Override
206 protected void doImportStagedModel(
207 PortletDataContext portletDataContext, DDMTemplate template)
208 throws Exception {
209
210 long userId = portletDataContext.getUserId(template.getUserUuid());
211
212 long classPK = template.getClassPK();
213
214 Element structureElement = portletDataContext.getReferenceDataElement(
215 template, DDMStructure.class, classPK);
216
217 if (structureElement != null) {
218 StagedModelDataHandlerUtil.importStagedModel(
219 portletDataContext, structureElement);
220
221 Map<Long, Long> structureIds =
222 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
223 DDMStructure.class);
224
225 classPK = MapUtil.getLong(structureIds, classPK, classPK);
226 }
227
228 File smallFile = null;
229
230 try {
231 if (template.isSmallImage()) {
232 Element element =
233 portletDataContext.getImportDataStagedModelElement(
234 template);
235
236 String smallImagePath = element.attributeValue(
237 "small-image-path");
238
239 if (Validator.isNotNull(template.getSmallImageURL())) {
240 String smallImageURL =
241 ExportImportHelperUtil.replaceImportContentReferences(
242 portletDataContext, element,
243 template.getSmallImageURL(), true);
244
245 template.setSmallImageURL(smallImageURL);
246 }
247 else if (Validator.isNotNull(smallImagePath)) {
248 byte[] bytes = portletDataContext.getZipEntryAsByteArray(
249 smallImagePath);
250
251 if (bytes != null) {
252 smallFile = FileUtil.createTempFile(
253 template.getSmallImageType());
254
255 FileUtil.write(smallFile, bytes);
256 }
257 }
258 }
259
260 ServiceContext serviceContext =
261 portletDataContext.createServiceContext(template);
262
263 DDMTemplate importedTemplate = null;
264
265 if (portletDataContext.isDataStrategyMirror()) {
266 DDMTemplate existingTemplate =
267 DDMTemplateLocalServiceUtil.
268 fetchDDMTemplateByUuidAndGroupId(
269 template.getUuid(),
270 portletDataContext.getScopeGroupId());
271
272 if (existingTemplate == null) {
273 serviceContext.setUuid(template.getUuid());
274
275 importedTemplate = addTemplate(
276 userId, portletDataContext.getScopeGroupId(), template,
277 classPK, smallFile, serviceContext);
278 }
279 else {
280 importedTemplate =
281 DDMTemplateLocalServiceUtil.updateTemplate(
282 existingTemplate.getTemplateId(),
283 template.getClassPK(), template.getNameMap(),
284 template.getDescriptionMap(), template.getType(),
285 template.getMode(), template.getLanguage(),
286 template.getScript(), template.isCacheable(),
287 template.isSmallImage(),
288 template.getSmallImageURL(), smallFile,
289 serviceContext);
290 }
291 }
292 else {
293 importedTemplate = addTemplate(
294 userId, portletDataContext.getScopeGroupId(), template,
295 classPK, smallFile, serviceContext);
296 }
297
298 portletDataContext.importClassedModel(template, importedTemplate);
299
300 Map<String, String> ddmTemplateKeys =
301 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
302 DDMTemplate.class + ".ddmTemplateKey");
303
304 ddmTemplateKeys.put(
305 template.getTemplateKey(), importedTemplate.getTemplateKey());
306 }
307 finally {
308 if (smallFile != null) {
309 smallFile.delete();
310 }
311 }
312 }
313
314 @Override
315 protected boolean validateMissingReference(
316 String uuid, long companyId, long groupId)
317 throws Exception {
318
319 DDMTemplate template =
320 DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
321 uuid, groupId);
322
323 if (template == null) {
324 return false;
325 }
326
327 return true;
328 }
329
330 private static Log _log = LogFactoryUtil.getLog(
331 DDMTemplateStagedModelDataHandler.class);
332
333 }