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.PortletDataException;
024 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
025 import com.liferay.portal.kernel.util.FileUtil;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.LocaleUtil;
028 import com.liferay.portal.kernel.util.LocalizationUtil;
029 import com.liferay.portal.kernel.util.MapUtil;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.xml.Element;
033 import com.liferay.portal.model.Image;
034 import com.liferay.portal.service.ImageLocalServiceUtil;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portal.service.UserLocalServiceUtil;
037 import com.liferay.portal.util.PortalUtil;
038 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
039 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
040 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
041 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
042
043 import java.io.File;
044
045 import java.util.HashMap;
046 import java.util.Locale;
047 import java.util.Map;
048
049
053 public class DDMTemplateStagedModelDataHandler
054 extends BaseStagedModelDataHandler<DDMTemplate> {
055
056 public static final String[] CLASS_NAMES = {DDMTemplate.class.getName()};
057
058 @Override
059 public void deleteStagedModel(
060 String uuid, long groupId, String className, String extraData)
061 throws PortalException, SystemException {
062
063 DDMTemplate ddmTemplate =
064 DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
065 uuid, groupId);
066
067 if (ddmTemplate != null) {
068 DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
069 }
070 }
071
072 @Override
073 public String[] getClassNames() {
074 return CLASS_NAMES;
075 }
076
077 @Override
078 public String getDisplayName(DDMTemplate template) {
079 return template.getNameCurrentValue();
080 }
081
082 @Override
083 public Map<String, String> getReferenceAttributes(
084 PortletDataContext portletDataContext, DDMTemplate template) {
085
086 Map<String, String> referenceAttributes = new HashMap<String, String>();
087
088 referenceAttributes.put(
089 "referenced-class-name", template.getClassName());
090 referenceAttributes.put("template-key", template.getTemplateKey());
091
092 long defaultUserId = 0;
093
094 try {
095 defaultUserId = UserLocalServiceUtil.getDefaultUserId(
096 template.getCompanyId());
097 }
098 catch (Exception e) {
099 return referenceAttributes;
100 }
101
102 boolean preloaded = false;
103
104 if (defaultUserId == template.getUserId()) {
105 preloaded = true;
106 }
107
108 referenceAttributes.put("preloaded", String.valueOf(preloaded));
109
110 return referenceAttributes;
111 }
112
113 @Override
114 public void importCompanyStagedModel(
115 PortletDataContext portletDataContext, Element element)
116 throws PortletDataException {
117
118 String uuid = element.attributeValue("uuid");
119 long classNameId = PortalUtil.getClassNameId(
120 element.attributeValue("referenced-class-name"));
121 String templateKey = element.attributeValue("template-key");
122 boolean preloaded = GetterUtil.getBoolean(
123 element.attributeValue("preloaded"));
124
125 DDMTemplate existingTemplate = null;
126
127 try {
128 existingTemplate = getExistingTemplate(
129 uuid, portletDataContext.getCompanyGroupId(), classNameId,
130 templateKey, preloaded);
131 }
132 catch (Exception e) {
133 throw new PortletDataException(e);
134 }
135
136 Map<Long, Long> templateIds =
137 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
138 DDMTemplate.class);
139
140 long templateId = GetterUtil.getLong(
141 element.attributeValue("class-pk"));
142
143 templateIds.put(templateId, existingTemplate.getTemplateId());
144
145 Map<String, String> templateKeys =
146 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
147 DDMTemplate.class + ".ddmTemplateKey");
148
149 templateKeys.put(templateKey, existingTemplate.getTemplateKey());
150 }
151
152 @Override
153 public boolean validateReference(
154 PortletDataContext portletDataContext, Element referenceElement) {
155
156 String uuid = referenceElement.attributeValue("uuid");
157 long classNameId = PortalUtil.getClassNameId(
158 referenceElement.attributeValue("referenced-class-name"));
159 String templateKey = referenceElement.attributeValue("template-key");
160 boolean preloaded = GetterUtil.getBoolean(
161 referenceElement.attributeValue("preloaded"));
162
163 try {
164 DDMTemplate existingTemplate = getExistingTemplate(
165 uuid, portletDataContext.getScopeGroupId(), classNameId,
166 templateKey, preloaded);
167
168 if (existingTemplate == null) {
169 existingTemplate = getExistingTemplate(
170 uuid, portletDataContext.getCompanyGroupId(), classNameId,
171 templateKey, preloaded);
172 }
173
174 if (existingTemplate == null) {
175 return false;
176 }
177
178 return true;
179 }
180 catch (Exception e) {
181 return false;
182 }
183 }
184
185 @Override
186 protected void doExportStagedModel(
187 PortletDataContext portletDataContext, DDMTemplate template)
188 throws Exception {
189
190 Element templateElement = portletDataContext.getExportDataElement(
191 template);
192
193 DDMStructure structure = DDMStructureLocalServiceUtil.fetchStructure(
194 template.getClassPK());
195
196 if (structure != null) {
197 StagedModelDataHandlerUtil.exportReferenceStagedModel(
198 portletDataContext, template, structure,
199 PortletDataContext.REFERENCE_TYPE_STRONG);
200 }
201
202 if (template.isSmallImage()) {
203 Image smallImage = ImageLocalServiceUtil.fetchImage(
204 template.getSmallImageId());
205
206 if (Validator.isNotNull(template.getSmallImageURL())) {
207 String smallImageURL =
208 ExportImportHelperUtil.replaceExportContentReferences(
209 portletDataContext, template, templateElement,
210 template.getSmallImageURL().concat(StringPool.SPACE),
211 true);
212
213 template.setSmallImageURL(smallImageURL);
214 }
215 else if (smallImage != null) {
216 String smallImagePath = ExportImportPathUtil.getModelPath(
217 template,
218 smallImage.getImageId() + StringPool.PERIOD +
219 template.getSmallImageType());
220
221 templateElement.addAttribute(
222 "small-image-path", smallImagePath);
223
224 template.setSmallImageType(smallImage.getType());
225
226 portletDataContext.addZipEntry(
227 smallImagePath, smallImage.getTextObj());
228 }
229 }
230
231 String script = ExportImportHelperUtil.replaceExportContentReferences(
232 portletDataContext, template, templateElement, template.getScript(),
233 portletDataContext.getBooleanParameter(
234 DDMPortletDataHandler.NAMESPACE, "referenced-content"));
235
236 template.setScript(script);
237
238 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
239 template.getCompanyId());
240
241 if (defaultUserId == template.getUserId()) {
242 templateElement.addAttribute("preloaded", "true");
243 }
244
245 portletDataContext.addClassedModel(
246 templateElement, ExportImportPathUtil.getModelPath(template),
247 template);
248 }
249
250 @Override
251 protected void doImportStagedModel(
252 PortletDataContext portletDataContext, DDMTemplate template)
253 throws Exception {
254
255 prepareLanguagesForImport(template);
256
257 long userId = portletDataContext.getUserId(template.getUserUuid());
258
259 long classPK = template.getClassPK();
260
261 if (classPK > 0) {
262 StagedModelDataHandlerUtil.importReferenceStagedModel(
263 portletDataContext, template, DDMStructure.class, classPK);
264
265 Map<Long, Long> structureIds =
266 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
267 DDMStructure.class);
268
269 classPK = MapUtil.getLong(structureIds, classPK, classPK);
270 }
271
272 File smallFile = null;
273
274 try {
275 Element templateElement =
276 portletDataContext.getImportDataStagedModelElement(template);
277
278 if (template.isSmallImage()) {
279 String smallImagePath = templateElement.attributeValue(
280 "small-image-path");
281
282 if (Validator.isNotNull(template.getSmallImageURL())) {
283 String smallImageURL =
284 ExportImportHelperUtil.replaceImportContentReferences(
285 portletDataContext, templateElement,
286 template.getSmallImageURL(), true);
287
288 template.setSmallImageURL(smallImageURL);
289 }
290 else if (Validator.isNotNull(smallImagePath)) {
291 byte[] bytes = portletDataContext.getZipEntryAsByteArray(
292 smallImagePath);
293
294 if (bytes != null) {
295 smallFile = FileUtil.createTempFile(
296 template.getSmallImageType());
297
298 FileUtil.write(smallFile, bytes);
299 }
300 }
301 }
302
303 String script =
304 ExportImportHelperUtil.replaceImportContentReferences(
305 portletDataContext, templateElement, template.getScript(),
306 true);
307
308 template.setScript(script);
309
310 ServiceContext serviceContext =
311 portletDataContext.createServiceContext(template);
312
313 DDMTemplate importedTemplate = null;
314
315 if (portletDataContext.isDataStrategyMirror()) {
316 boolean preloaded = GetterUtil.getBoolean(
317 templateElement.attributeValue("preloaded"));
318
319 DDMTemplate existingTemplate = getExistingTemplate(
320 template.getUuid(), portletDataContext.getScopeGroupId(),
321 template.getClassNameId(), template.getTemplateKey(),
322 preloaded);
323
324 if (existingTemplate == null) {
325 serviceContext.setUuid(template.getUuid());
326
327 importedTemplate = DDMTemplateLocalServiceUtil.addTemplate(
328 userId, portletDataContext.getScopeGroupId(),
329 template.getClassNameId(), classPK,
330 template.getTemplateKey(), template.getNameMap(),
331 template.getDescriptionMap(), template.getType(),
332 template.getMode(), template.getLanguage(),
333 template.getScript(), template.isCacheable(),
334 template.isSmallImage(), template.getSmallImageURL(),
335 smallFile, serviceContext);
336 }
337 else {
338 importedTemplate =
339 DDMTemplateLocalServiceUtil.updateTemplate(
340 existingTemplate.getTemplateId(),
341 template.getClassPK(), template.getNameMap(),
342 template.getDescriptionMap(), template.getType(),
343 template.getMode(), template.getLanguage(),
344 template.getScript(), template.isCacheable(),
345 template.isSmallImage(),
346 template.getSmallImageURL(), smallFile,
347 serviceContext);
348 }
349 }
350 else {
351 importedTemplate = DDMTemplateLocalServiceUtil.addTemplate(
352 userId, portletDataContext.getScopeGroupId(),
353 template.getClassNameId(), classPK, null,
354 template.getNameMap(), template.getDescriptionMap(),
355 template.getType(), template.getMode(),
356 template.getLanguage(), template.getScript(),
357 template.isCacheable(), template.isSmallImage(),
358 template.getSmallImageURL(), smallFile, serviceContext);
359 }
360
361 portletDataContext.importClassedModel(template, importedTemplate);
362
363 Map<String, String> ddmTemplateKeys =
364 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
365 DDMTemplate.class + ".ddmTemplateKey");
366
367 ddmTemplateKeys.put(
368 template.getTemplateKey(), importedTemplate.getTemplateKey());
369 }
370 finally {
371 if (smallFile != null) {
372 smallFile.delete();
373 }
374 }
375 }
376
377 protected DDMTemplate getExistingTemplate(
378 String uuid, long groupId, long classNameId, String templateKey,
379 boolean preloaded)
380 throws Exception {
381
382 DDMTemplate existingTemplate = null;
383
384 if (!preloaded) {
385 existingTemplate =
386 DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
387 uuid, groupId);
388 }
389 else {
390 existingTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
391 groupId, classNameId, templateKey);
392 }
393
394 return existingTemplate;
395 }
396
397 protected void prepareLanguagesForImport(DDMTemplate template)
398 throws PortalException {
399
400 Locale defaultLocale = LocaleUtil.fromLanguageId(
401 template.getDefaultLanguageId());
402
403 Locale[] availableLocales = LocaleUtil.fromLanguageIds(
404 template.getAvailableLanguageIds());
405
406 Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
407 DDMTemplate.class.getName(), template.getPrimaryKey(),
408 defaultLocale, availableLocales);
409
410 template.prepareLocalizedFieldsForImport(defaultImportLocale);
411 }
412
413 }