001
014
015 package com.liferay.portal.lar;
016
017 import com.liferay.portal.LayoutImportException;
018 import com.liferay.portal.NoSuchPortletPreferencesException;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.language.LanguageUtil;
022 import com.liferay.portal.kernel.lar.ImportExportThreadLocal;
023 import com.liferay.portal.kernel.lar.PortletDataContext;
024 import com.liferay.portal.kernel.lar.PortletDataHandler;
025 import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
026 import com.liferay.portal.kernel.log.Log;
027 import com.liferay.portal.kernel.log.LogFactoryUtil;
028 import com.liferay.portal.kernel.util.CharPool;
029 import com.liferay.portal.kernel.util.FileUtil;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.MapUtil;
032 import com.liferay.portal.kernel.util.ReleaseInfo;
033 import com.liferay.portal.kernel.util.StringBundler;
034 import com.liferay.portal.kernel.util.StringPool;
035 import com.liferay.portal.kernel.util.StringUtil;
036 import com.liferay.portal.kernel.util.Time;
037 import com.liferay.portal.kernel.util.UnicodeProperties;
038 import com.liferay.portal.kernel.util.Validator;
039 import com.liferay.portal.kernel.xml.Document;
040 import com.liferay.portal.kernel.xml.Element;
041 import com.liferay.portal.kernel.xml.Node;
042 import com.liferay.portal.kernel.xml.SAXReaderUtil;
043 import com.liferay.portal.kernel.zip.ZipWriter;
044 import com.liferay.portal.kernel.zip.ZipWriterFactoryUtil;
045 import com.liferay.portal.model.Company;
046 import com.liferay.portal.model.Group;
047 import com.liferay.portal.model.Layout;
048 import com.liferay.portal.model.LayoutConstants;
049 import com.liferay.portal.model.LayoutTypePortlet;
050 import com.liferay.portal.model.Lock;
051 import com.liferay.portal.model.Portlet;
052 import com.liferay.portal.model.PortletConstants;
053 import com.liferay.portal.model.PortletItem;
054 import com.liferay.portal.model.PortletPreferences;
055 import com.liferay.portal.model.User;
056 import com.liferay.portal.service.CompanyLocalServiceUtil;
057 import com.liferay.portal.service.GroupLocalServiceUtil;
058 import com.liferay.portal.service.LayoutLocalServiceUtil;
059 import com.liferay.portal.service.PortletItemLocalServiceUtil;
060 import com.liferay.portal.service.PortletLocalServiceUtil;
061 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
062 import com.liferay.portal.service.UserLocalServiceUtil;
063 import com.liferay.portal.util.PortalUtil;
064 import com.liferay.portal.util.PortletKeys;
065 import com.liferay.portlet.PortletPreferencesFactoryUtil;
066 import com.liferay.portlet.asset.model.AssetCategory;
067 import com.liferay.portlet.asset.model.AssetCategoryConstants;
068 import com.liferay.portlet.asset.model.AssetCategoryProperty;
069 import com.liferay.portlet.asset.model.AssetTag;
070 import com.liferay.portlet.asset.model.AssetTagProperty;
071 import com.liferay.portlet.asset.model.AssetVocabulary;
072 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
073 import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
074 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
075 import com.liferay.portlet.asset.service.AssetTagPropertyLocalServiceUtil;
076 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
077 import com.liferay.portlet.asset.service.persistence.AssetCategoryUtil;
078 import com.liferay.portlet.asset.service.persistence.AssetVocabularyUtil;
079 import com.liferay.portlet.expando.model.ExpandoColumn;
080 import com.liferay.portlet.journal.model.JournalStructure;
081 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
082 import com.liferay.portlet.messageboards.model.MBMessage;
083 import com.liferay.portlet.ratings.model.RatingsEntry;
084 import com.liferay.util.xml.DocUtil;
085
086 import java.io.File;
087 import java.io.IOException;
088
089 import java.util.Date;
090 import java.util.Enumeration;
091 import java.util.HashSet;
092 import java.util.List;
093 import java.util.Map;
094
095 import org.apache.commons.lang.time.StopWatch;
096
097
108 public class PortletExporter {
109
110 @Deprecated
111 public byte[] exportPortletInfo(
112 long plid, long groupId, String portletId,
113 Map<String, String[]> parameterMap, Date startDate, Date endDate)
114 throws Exception {
115
116 File file = exportPortletInfoAsFile(
117 plid, groupId, portletId, parameterMap, startDate, endDate);
118
119 try {
120 return FileUtil.getBytes(file);
121 }
122 catch (IOException ioe) {
123 throw new SystemException(ioe);
124 }
125 finally {
126 file.delete();
127 }
128 }
129
130 public File exportPortletInfoAsFile(
131 long plid, long groupId, String portletId,
132 Map<String, String[]> parameterMap, Date startDate, Date endDate)
133 throws Exception {
134
135 try {
136 ImportExportThreadLocal.setPortletExportInProcess(true);
137
138 return doExportPortletInfoAsFile(
139 plid, groupId, portletId, parameterMap, startDate, endDate);
140 }
141 finally {
142 ImportExportThreadLocal.setPortletExportInProcess(false);
143 }
144 }
145
146 protected File doExportPortletInfoAsFile(
147 long plid, long groupId, String portletId,
148 Map<String, String[]> parameterMap, Date startDate, Date endDate)
149 throws Exception {
150
151 boolean exportCategories = MapUtil.getBoolean(
152 parameterMap, PortletDataHandlerKeys.CATEGORIES);
153 boolean exportPermissions = MapUtil.getBoolean(
154 parameterMap, PortletDataHandlerKeys.PERMISSIONS);
155 boolean exportPortletArchivedSetups = MapUtil.getBoolean(
156 parameterMap, PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS);
157
158 boolean exportPortletData = false;
159
160 if (parameterMap.containsKey(
161 PortletDataHandlerKeys.PORTLET_DATA + "_" +
162 PortletConstants.getRootPortletId(portletId))) {
163
164 exportPortletData = MapUtil.getBoolean(
165 parameterMap,
166 PortletDataHandlerKeys.PORTLET_DATA + "_" +
167 PortletConstants.getRootPortletId(portletId));
168 }
169 else {
170 exportPortletData = MapUtil.getBoolean(
171 parameterMap, PortletDataHandlerKeys.PORTLET_DATA);
172 }
173
174 boolean exportPortletDataAll = MapUtil.getBoolean(
175 parameterMap, PortletDataHandlerKeys.PORTLET_DATA_ALL);
176 boolean exportPortletSetup = MapUtil.getBoolean(
177 parameterMap, PortletDataHandlerKeys.PORTLET_SETUP);
178 boolean exportPortletUserPreferences = MapUtil.getBoolean(
179 parameterMap, PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
180
181 if (_log.isDebugEnabled()) {
182 _log.debug("Export categories " + exportCategories);
183 _log.debug("Export permissions " + exportPermissions);
184 _log.debug(
185 "Export portlet archived setups " +
186 exportPortletArchivedSetups);
187 _log.debug("Export portlet data " + exportPortletData);
188 _log.debug("Export all portlet data " + exportPortletDataAll);
189 _log.debug("Export portlet setup " + exportPortletSetup);
190 _log.debug(
191 "Export portlet user preferences " +
192 exportPortletUserPreferences);
193 }
194
195 if (exportPortletDataAll) {
196 exportPortletData = true;
197 }
198
199 StopWatch stopWatch = null;
200
201 if (_log.isInfoEnabled()) {
202 stopWatch = new StopWatch();
203
204 stopWatch.start();
205 }
206
207 LayoutCache layoutCache = new LayoutCache();
208
209 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
210
211 if (!layout.isTypeControlPanel() && !layout.isTypePanel() &&
212 !layout.isTypePortlet()) {
213
214 throw new LayoutImportException(
215 "Layout type " + layout.getType() + " is not valid");
216 }
217
218 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
219 layout.getCompanyId());
220
221 ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();
222
223 long scopeGroupId = groupId;
224
225 javax.portlet.PortletPreferences jxPreferences =
226 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
227 layout, portletId);
228
229 String scopeType = GetterUtil.getString(
230 jxPreferences.getValue("lfrScopeType", null));
231 String scopeLayoutUuid = GetterUtil.getString(
232 jxPreferences.getValue("lfrScopeLayoutUuid", null));
233
234 if (Validator.isNotNull(scopeType)) {
235 Group scopeGroup = null;
236
237 if (scopeType.equals("company")) {
238 scopeGroup = GroupLocalServiceUtil.getCompanyGroup(
239 layout.getCompanyId());
240 }
241 else if (Validator.isNotNull(scopeLayoutUuid)) {
242 scopeGroup = layout.getScopeGroup();
243 }
244
245 if (scopeGroup != null) {
246 scopeGroupId = scopeGroup.getGroupId();
247 }
248 }
249
250 PortletDataContext portletDataContext = new PortletDataContextImpl(
251 layout.getCompanyId(), scopeGroupId, parameterMap,
252 new HashSet<String>(), startDate, endDate, zipWriter);
253
254 portletDataContext.setPortetDataContextListener(
255 new PortletDataContextListenerImpl(portletDataContext));
256
257 portletDataContext.setPlid(plid);
258 portletDataContext.setOldPlid(plid);
259 portletDataContext.setScopeType(scopeType);
260 portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
261
262 Document document = SAXReaderUtil.createDocument();
263
264 Element rootElement = document.addElement("root");
265
266 Element headerElement = rootElement.addElement("header");
267
268 headerElement.addAttribute(
269 "available-locales",
270 StringUtil.merge(LanguageUtil.getAvailableLocales()));
271 headerElement.addAttribute(
272 "build-number", String.valueOf(ReleaseInfo.getBuildNumber()));
273 headerElement.addAttribute("export-date", Time.getRFC822());
274
275 if (portletDataContext.hasDateRange()) {
276 headerElement.addAttribute(
277 "start-date",
278 String.valueOf(portletDataContext.getStartDate()));
279 headerElement.addAttribute(
280 "end-date", String.valueOf(portletDataContext.getEndDate()));
281 }
282
283 headerElement.addAttribute("type", "portlet");
284 headerElement.addAttribute("group-id", String.valueOf(scopeGroupId));
285 headerElement.addAttribute(
286 "private-layout", String.valueOf(layout.isPrivateLayout()));
287 headerElement.addAttribute(
288 "root-portlet-id", PortletConstants.getRootPortletId(portletId));
289
290 exportPortlet(
291 portletDataContext, layoutCache, portletId, layout, rootElement,
292 defaultUserId, exportPermissions, exportPortletArchivedSetups,
293 exportPortletData, exportPortletSetup,
294 exportPortletUserPreferences);
295
296 if (exportCategories) {
297 exportAssetCategories(portletDataContext);
298 }
299
300 exportAssetLinks(portletDataContext);
301 exportAssetTags(portletDataContext);
302 exportComments(portletDataContext);
303 exportExpandoTables(portletDataContext);
304 exportLocks(portletDataContext);
305
306 if (exportPermissions) {
307 _permissionExporter.exportPortletDataPermissions(
308 portletDataContext);
309 }
310
311 exportRatingsEntries(portletDataContext, rootElement);
312
313 if (_log.isInfoEnabled()) {
314 _log.info("Exporting portlet took " + stopWatch.getTime() + " ms");
315 }
316
317 try {
318 portletDataContext.addZipEntry(
319 "/manifest.xml", document.formattedString());
320 }
321 catch (IOException ioe) {
322 throw new SystemException(ioe);
323 }
324
325 return zipWriter.getFile();
326 }
327
328 protected void exportAssetCategories(PortletDataContext portletDataContext)
329 throws Exception {
330
331 Document document = SAXReaderUtil.createDocument();
332
333 Element rootElement = document.addElement("categories-hierarchy");
334
335 exportAssetCategories(portletDataContext, rootElement);
336
337 portletDataContext.addZipEntry(
338 portletDataContext.getRootPath() + "/categories-hierarchy.xml",
339 document.formattedString());
340 }
341
342 protected void exportAssetCategories(
343 PortletDataContext portletDataContext, Element rootElement)
344 throws Exception {
345
346 Element assetsElement = rootElement.element("assets");
347
348 if (assetsElement == null) {
349 assetsElement = rootElement.addElement("assets");
350 }
351
352 Element assetCategoriesElement = rootElement.element("categories");
353
354 if (assetCategoriesElement == null) {
355 assetCategoriesElement = rootElement.addElement("categories");
356 }
357
358 Element assetVocabulariesElement = rootElement.element("vocabularies");
359
360 if (assetVocabulariesElement == null) {
361 assetVocabulariesElement = rootElement.addElement("vocabularies");
362 }
363
364 Map<String, String[]> assetCategoryUuidsMap =
365 portletDataContext.getAssetCategoryUuidsMap();
366
367 for (Map.Entry<String, String[]> entry :
368 assetCategoryUuidsMap.entrySet()) {
369
370 String[] assetCategoryEntryParts = StringUtil.split(
371 entry.getKey(), CharPool.POUND);
372
373 String className = assetCategoryEntryParts[0];
374 String classPK = assetCategoryEntryParts[1];
375
376 Element assetElement = assetsElement.addElement("asset");
377
378 assetElement.addAttribute("class-name", className);
379 assetElement.addAttribute("class-pk", classPK);
380 assetElement.addAttribute(
381 "category-uuids", StringUtil.merge(entry.getValue()));
382
383 List<AssetCategory> assetCategories =
384 AssetCategoryLocalServiceUtil.getCategories(
385 className, GetterUtil.getLong(classPK));
386
387 for (AssetCategory assestCategory : assetCategories) {
388 exportAssetCategory(
389 portletDataContext, assetVocabulariesElement,
390 assetCategoriesElement, assestCategory);
391 }
392 }
393 }
394
395 protected void exportAssetCategory(
396 PortletDataContext portletDataContext,
397 Element assetVocabulariesElement, Element assetCategoriesElement,
398 AssetCategory assetCategory)
399 throws Exception {
400
401 exportAssetVocabulary(
402 portletDataContext, assetVocabulariesElement,
403 assetCategory.getVocabularyId());
404
405 if (assetCategory.getParentCategoryId() !=
406 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
407
408 exportAssetCategory(
409 portletDataContext, assetVocabulariesElement,
410 assetCategoriesElement, assetCategory.getParentCategoryId());
411 }
412
413 String path = getAssetCategoryPath(
414 portletDataContext, assetCategory.getCategoryId());
415
416 if (!portletDataContext.isPathNotProcessed(path)) {
417 return;
418 }
419
420 Element assetCategoryElement = assetCategoriesElement.addElement(
421 "category");
422
423 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
424 assetCategory.getCompanyId());
425
426 if (assetCategory.getGroupId() == companyGroup.getGroupId()) {
427 assetCategoryElement.addAttribute("global", "true");
428 }
429
430 assetCategoryElement.addAttribute("path", path);
431
432 assetCategory.setUserUuid(assetCategory.getUserUuid());
433
434 portletDataContext.addZipEntry(path, assetCategory);
435
436 List<AssetCategoryProperty> assetCategoryProperties =
437 AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
438 assetCategory.getCategoryId());
439
440 for (AssetCategoryProperty assetCategoryProperty :
441 assetCategoryProperties) {
442
443 Element propertyElement = assetCategoryElement.addElement(
444 "property");
445
446 propertyElement.addAttribute(
447 "userUuid", assetCategoryProperty.getUserUuid());
448 propertyElement.addAttribute("key", assetCategoryProperty.getKey());
449 propertyElement.addAttribute(
450 "value", assetCategoryProperty.getValue());
451 }
452
453 portletDataContext.addPermissions(
454 AssetCategory.class, assetCategory.getCategoryId());
455 }
456
457 protected void exportAssetCategory(
458 PortletDataContext portletDataContext,
459 Element assetVocabulariesElement, Element assetCategoriesElement,
460 long assetCategoryId)
461 throws Exception {
462
463 AssetCategory assetCategory = AssetCategoryUtil.fetchByPrimaryKey(
464 assetCategoryId);
465
466 if (assetCategory != null) {
467 exportAssetCategory(
468 portletDataContext, assetVocabulariesElement,
469 assetCategoriesElement, assetCategory);
470 }
471 }
472
473 protected void exportAssetLinks(PortletDataContext portletDataContext)
474 throws Exception {
475
476 Document document = SAXReaderUtil.createDocument();
477
478 Element rootElement = document.addElement("links");
479
480 Map<String, String[]> assetLinkUuidsMap =
481 portletDataContext.getAssetLinkUuidsMap();
482
483 for (Map.Entry<String, String[]> entry : assetLinkUuidsMap.entrySet()) {
484 String[] assetLinkNameParts = StringUtil.split(
485 entry.getKey(), CharPool.POUND);
486 String[] targetAssetEntryUuids = entry.getValue();
487
488 String sourceAssetEntryUuid = assetLinkNameParts[0];
489 String assetLinkType = assetLinkNameParts[1];
490
491 Element assetElement = rootElement.addElement("asset-link");
492
493 assetElement.addAttribute("source-uuid", sourceAssetEntryUuid);
494 assetElement.addAttribute(
495 "target-uuids", StringUtil.merge(targetAssetEntryUuids));
496 assetElement.addAttribute("type", assetLinkType);
497 }
498
499 portletDataContext.addZipEntry(
500 portletDataContext.getRootPath() + "/links.xml",
501 document.formattedString());
502 }
503
504 protected void exportAssetTag(
505 PortletDataContext portletDataContext, AssetTag assetTag,
506 Element assetTagsElement)
507 throws PortalException, SystemException {
508
509 String path = getAssetTagPath(portletDataContext, assetTag.getTagId());
510
511 if (!portletDataContext.isPathNotProcessed(path)) {
512 return;
513 }
514
515 Element assetTagElement = assetTagsElement.addElement("tag");
516
517 assetTagElement.addAttribute("path", path);
518
519 assetTag.setUserUuid(assetTag.getUserUuid());
520
521 portletDataContext.addZipEntry(path, assetTag);
522
523 List<AssetTagProperty> assetTagProperties =
524 AssetTagPropertyLocalServiceUtil.getTagProperties(
525 assetTag.getTagId());
526
527 for (AssetTagProperty assetTagProperty : assetTagProperties) {
528 Element propertyElement = assetTagElement.addElement("property");
529
530 propertyElement.addAttribute("key", assetTagProperty.getKey());
531 propertyElement.addAttribute("value", assetTagProperty.getValue());
532 }
533
534 portletDataContext.addPermissions(AssetTag.class, assetTag.getTagId());
535 }
536
537 protected void exportAssetTags(PortletDataContext portletDataContext)
538 throws Exception {
539
540 Document document = SAXReaderUtil.createDocument();
541
542 Element rootElement = document.addElement("tags");
543
544 Map<String, String[]> assetTagNamesMap =
545 portletDataContext.getAssetTagNamesMap();
546
547 if (assetTagNamesMap.isEmpty()) {
548 return;
549 }
550
551 for (Map.Entry<String, String[]> entry : assetTagNamesMap.entrySet()) {
552 String[] assetTagNameParts = StringUtil.split(
553 entry.getKey(), CharPool.POUND);
554
555 String className = assetTagNameParts[0];
556 String classPK = assetTagNameParts[1];
557
558 Element assetElement = rootElement.addElement("asset");
559
560 assetElement.addAttribute("class-name", className);
561 assetElement.addAttribute("class-pk", classPK);
562 assetElement.addAttribute(
563 "tags", StringUtil.merge(entry.getValue()));
564
565 List<AssetTag> assetTags = AssetTagLocalServiceUtil.getTags(
566 className, GetterUtil.getLong(classPK));
567
568 for (AssetTag assetTag : assetTags) {
569 exportAssetTag(portletDataContext, assetTag, rootElement);
570 }
571 }
572
573 portletDataContext.addZipEntry(
574 portletDataContext.getRootPath() + "/tags.xml",
575 document.formattedString());
576 }
577
578 protected void exportAssetVocabulary(
579 PortletDataContext portletDataContext,
580 Element assetVocabulariesElement, AssetVocabulary assetVocabulary)
581 throws Exception {
582
583 String path = getAssetVocabulariesPath(
584 portletDataContext, assetVocabulary.getVocabularyId());
585
586 if (!portletDataContext.isPathNotProcessed(path)) {
587 return;
588 }
589
590 Element assetVocabularyElement = assetVocabulariesElement.addElement(
591 "vocabulary");
592
593 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
594 assetVocabulary.getCompanyId());
595
596 if (assetVocabulary.getGroupId() == companyGroup.getGroupId()) {
597 assetVocabularyElement.addAttribute("global", "true");
598 }
599
600 assetVocabularyElement.addAttribute("path", path);
601
602 assetVocabulary.setUserUuid(assetVocabulary.getUserUuid());
603
604 portletDataContext.addZipEntry(path, assetVocabulary);
605
606 portletDataContext.addPermissions(
607 AssetVocabulary.class, assetVocabulary.getVocabularyId());
608 }
609
610 protected void exportAssetVocabulary(
611 PortletDataContext portletDataContext,
612 Element assetVocabulariesElement, long assetVocabularyId)
613 throws Exception {
614
615 AssetVocabulary assetVocabulary = AssetVocabularyUtil.findByPrimaryKey(
616 assetVocabularyId);
617
618 exportAssetVocabulary(
619 portletDataContext, assetVocabulariesElement, assetVocabulary);
620 }
621
622 protected void exportComments(PortletDataContext portletDataContext)
623 throws Exception {
624
625 Document document = SAXReaderUtil.createDocument();
626
627 Element rootElement = document.addElement("comments");
628
629 Map<String, List<MBMessage>> commentsMap =
630 portletDataContext.getComments();
631
632 for (Map.Entry<String, List<MBMessage>> entry :
633 commentsMap.entrySet()) {
634
635 String[] commentParts = StringUtil.split(
636 entry.getKey(), CharPool.POUND);
637
638 String className = commentParts[0];
639 String classPK = commentParts[1];
640
641 String commentsPath = getCommentsPath(
642 portletDataContext, className, classPK);
643
644 Element assetElement = rootElement.addElement("asset");
645
646 assetElement.addAttribute("path", commentsPath);
647 assetElement.addAttribute("class-name", className);
648 assetElement.addAttribute("class-pk", classPK);
649
650 List<MBMessage> mbMessages = entry.getValue();
651
652 for (MBMessage mbMessage : mbMessages) {
653 String commentPath = getCommentPath(
654 portletDataContext, className, classPK, mbMessage);
655
656 if (portletDataContext.isPathNotProcessed(commentPath)) {
657 portletDataContext.addZipEntry(commentPath, mbMessage);
658 }
659 }
660 }
661
662 portletDataContext.addZipEntry(
663 portletDataContext.getRootPath() + "/comments.xml",
664 document.formattedString());
665 }
666
667 protected void exportExpandoTables(PortletDataContext portletDataContext)
668 throws Exception {
669
670 Document document = SAXReaderUtil.createDocument();
671
672 Element rootElement = document.addElement("expando-tables");
673
674 Map<String, List<ExpandoColumn>> expandoColumnsMap =
675 portletDataContext.getExpandoColumns();
676
677 for (Map.Entry<String, List<ExpandoColumn>> entry :
678 expandoColumnsMap.entrySet()) {
679
680 String className = entry.getKey();
681
682 Element expandoTableElement = rootElement.addElement(
683 "expando-table");
684
685 expandoTableElement.addAttribute("class-name", className);
686
687 List<ExpandoColumn> expandoColumns = entry.getValue();
688
689 for (ExpandoColumn expandoColumn : expandoColumns) {
690 Element expandoColumnElement = expandoTableElement.addElement(
691 "expando-column");
692
693 expandoColumnElement.addAttribute(
694 "column-id", String.valueOf(expandoColumn.getColumnId()));
695 expandoColumnElement.addAttribute(
696 "name", expandoColumn.getName());
697 expandoColumnElement.addAttribute(
698 "type", String.valueOf(expandoColumn.getType()));
699
700 DocUtil.add(
701 expandoColumnElement, "default-data",
702 expandoColumn.getDefaultData());
703
704 Element typeSettingsElement = expandoColumnElement.addElement(
705 "type-settings");
706
707 UnicodeProperties typeSettingsProperties =
708 expandoColumn.getTypeSettingsProperties();
709
710 typeSettingsElement.addCDATA(typeSettingsProperties.toString());
711 }
712 }
713
714 portletDataContext.addZipEntry(
715 portletDataContext.getRootPath() + "/expando-tables.xml",
716 document.formattedString());
717 }
718
719 protected void exportLocks(PortletDataContext portletDataContext)
720 throws Exception {
721
722 Document document = SAXReaderUtil.createDocument();
723
724 Element rootElement = document.addElement("locks");
725
726 Map<String, Lock> locksMap = portletDataContext.getLocks();
727
728 for (Map.Entry<String, Lock> entry : locksMap.entrySet()) {
729 Lock lock = entry.getValue();
730
731 String entryKey = entry.getKey();
732
733 int pos = entryKey.indexOf(CharPool.POUND);
734
735 String className = entryKey.substring(0, pos);
736 String key = entryKey.substring(pos + 1);
737
738 String path = getLockPath(portletDataContext, className, key, lock);
739
740 Element assetElement = rootElement.addElement("asset");
741
742 assetElement.addAttribute("path", path);
743 assetElement.addAttribute("class-name", className);
744 assetElement.addAttribute("key", key);
745
746 if (portletDataContext.isPathNotProcessed(path)) {
747 portletDataContext.addZipEntry(path, lock);
748 }
749 }
750
751 portletDataContext.addZipEntry(
752 portletDataContext.getRootPath() + "/locks.xml",
753 document.formattedString());
754 }
755
756 protected void exportPortlet(
757 PortletDataContext portletDataContext, LayoutCache layoutCache,
758 String portletId, Layout layout, Element parentElement,
759 long defaultUserId, boolean exportPermissions,
760 boolean exportPortletArchivedSetups, boolean exportPortletData,
761 boolean exportPortletSetup, boolean exportPortletUserPreferences)
762 throws Exception {
763
764 long plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
765 long layoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
766
767 if (layout != null) {
768 plid = layout.getPlid();
769 layoutId = layout.getLayoutId();
770 }
771
772 Portlet portlet = PortletLocalServiceUtil.getPortletById(
773 portletDataContext.getCompanyId(), portletId);
774
775 if (portlet == null) {
776 if (_log.isDebugEnabled()) {
777 _log.debug(
778 "Do not export portlet " + portletId +
779 " because the portlet does not exist");
780 }
781
782 return;
783 }
784
785 if (!portlet.isInstanceable() &&
786 !portlet.isPreferencesUniquePerLayout() &&
787 portletDataContext.hasNotUniquePerLayout(portletId)) {
788
789 return;
790 }
791
792 Document document = SAXReaderUtil.createDocument();
793
794 Element portletElement = document.addElement("portlet");
795
796 portletElement.addAttribute("portlet-id", portletId);
797 portletElement.addAttribute(
798 "root-portlet-id", PortletConstants.getRootPortletId(portletId));
799 portletElement.addAttribute("old-plid", String.valueOf(plid));
800 portletElement.addAttribute(
801 "scope-layout-type", portletDataContext.getScopeType());
802 portletElement.addAttribute(
803 "scope-layout-uuid", portletDataContext.getScopeLayoutUuid());
804
805
806
807 if (exportPortletData) {
808 javax.portlet.PortletPreferences jxPreferences =
809 PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
810 layout, portletId);
811
812 if (!portlet.isPreferencesUniquePerLayout()) {
813 StringBundler sb = new StringBundler(5);
814
815 sb.append(portletId);
816 sb.append(StringPool.AT);
817 sb.append(portletDataContext.getScopeType());
818 sb.append(StringPool.AT);
819 sb.append(portletDataContext.getScopeLayoutUuid());
820
821 String dataKey = sb.toString();
822
823 if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
824 portletDataContext.putNotUniquePerLayout(dataKey);
825
826 exportPortletData(
827 portletDataContext, portlet, layout, jxPreferences,
828 portletElement);
829 }
830 }
831 else {
832 exportPortletData(
833 portletDataContext, portlet, layout, jxPreferences,
834 portletElement);
835 }
836 }
837
838
839
840 if (exportPortletSetup) {
841 exportPortletPreferences(
842 portletDataContext, PortletKeys.PREFS_OWNER_ID_DEFAULT,
843 PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false, layout, plid,
844 portletId, portletElement);
845
846 exportPortletPreferences(
847 portletDataContext, portletDataContext.getScopeGroupId(),
848 PortletKeys.PREFS_OWNER_TYPE_GROUP, false, layout, plid,
849 portletId, portletElement);
850
851 exportPortletPreferences(
852 portletDataContext, portletDataContext.getCompanyId(),
853 PortletKeys.PREFS_OWNER_TYPE_COMPANY, false, layout, plid,
854 portletId, portletElement);
855 }
856
857
858
859 if (exportPortletUserPreferences) {
860 List<PortletPreferences> portletPreferencesList =
861 PortletPreferencesLocalServiceUtil.getPortletPreferences(
862 PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId);
863
864 for (PortletPreferences portletPreferences :
865 portletPreferencesList) {
866
867 boolean defaultUser = false;
868
869 if (portletPreferences.getOwnerId() ==
870 PortletKeys.PREFS_OWNER_ID_DEFAULT) {
871
872 defaultUser = true;
873 }
874
875 exportPortletPreferences(
876 portletDataContext, portletPreferences.getOwnerId(),
877 PortletKeys.PREFS_OWNER_TYPE_USER, defaultUser, layout,
878 plid, portletId, portletElement);
879 }
880
881 try {
882 PortletPreferences groupPortletPreferences =
883 PortletPreferencesLocalServiceUtil.getPortletPreferences(
884 portletDataContext.getScopeGroupId(),
885 PortletKeys.PREFS_OWNER_TYPE_GROUP,
886 PortletKeys.PREFS_PLID_SHARED, portletId);
887
888 exportPortletPreference(
889 portletDataContext, portletDataContext.getScopeGroupId(),
890 PortletKeys.PREFS_OWNER_TYPE_GROUP, false,
891 groupPortletPreferences, portletId,
892 PortletKeys.PREFS_PLID_SHARED, portletElement);
893 }
894 catch (NoSuchPortletPreferencesException nsppe) {
895 }
896 }
897
898
899
900 if (exportPortletArchivedSetups) {
901 String rootPortletId = PortletConstants.getRootPortletId(portletId);
902
903 List<PortletItem> portletItems =
904 PortletItemLocalServiceUtil.getPortletItems(
905 portletDataContext.getGroupId(), rootPortletId,
906 PortletPreferences.class.getName());
907
908 for (PortletItem portletItem : portletItems) {
909 exportPortletPreferences(
910 portletDataContext, portletItem.getPortletItemId(),
911 PortletKeys.PREFS_OWNER_TYPE_ARCHIVED, false, null, plid,
912 portletItem.getPortletId(), portletElement);
913 }
914 }
915
916
917
918 if (exportPermissions) {
919 _permissionExporter.exportPortletPermissions(
920 portletDataContext, layoutCache, portletId, layout,
921 portletElement);
922 }
923
924
925
926 StringBundler sb = new StringBundler(4);
927
928 sb.append(portletDataContext.getPortletPath(portletId));
929 sb.append(StringPool.SLASH);
930 sb.append(plid);
931 sb.append("/portlet.xml");
932
933 String path = sb.toString();
934
935 Element element = parentElement.addElement("portlet");
936
937 element.addAttribute("portlet-id", portletId);
938 element.addAttribute("layout-id", String.valueOf(layoutId));
939 element.addAttribute("path", path);
940
941 if (portletDataContext.isPathNotProcessed(path)) {
942 try {
943 portletDataContext.addZipEntry(
944 path, document.formattedString());
945 }
946 catch (IOException ioe) {
947 if (_log.isWarnEnabled()) {
948 _log.warn(ioe.getMessage());
949 }
950 }
951
952 portletDataContext.addPrimaryKey(String.class, path);
953 }
954 }
955
956 protected void exportPortletData(
957 PortletDataContext portletDataContext, Portlet portlet,
958 Layout layout, javax.portlet.PortletPreferences jxPreferences,
959 Element parentElement)
960 throws Exception {
961
962 if (portlet == null) {
963 return;
964 }
965
966 PortletDataHandler portletDataHandler =
967 portlet.getPortletDataHandlerInstance();
968
969 if (portletDataHandler == null) {
970 return;
971 }
972
973 String portletId = portlet.getPortletId();
974
975 Group liveGroup = layout.getGroup();
976
977 if (liveGroup.isStagingGroup()) {
978 liveGroup = liveGroup.getLiveGroup();
979 }
980
981 boolean staged = liveGroup.isStagedPortlet(portlet.getRootPortletId());
982
983 if (!staged && ImportExportThreadLocal.isLayoutExportInProcess()) {
984 if (_log.isDebugEnabled()) {
985 _log.debug(
986 "Not exporting data for " + portletId +
987 " because it is configured not to be staged");
988 }
989
990 return;
991 }
992
993 if (_log.isDebugEnabled()) {
994 _log.debug("Exporting data for " + portletId);
995 }
996
997 StringBundler sb = new StringBundler(4);
998
999 sb.append(portletDataContext.getPortletPath(portletId));
1000 sb.append(StringPool.SLASH);
1001
1002 if (portlet.isPreferencesUniquePerLayout()) {
1003 sb.append(layout.getPlid());
1004 }
1005 else {
1006 sb.append(portletDataContext.getScopeGroupId());
1007 }
1008
1009 sb.append("/portlet-data.xml");
1010
1011 String path = sb.toString();
1012
1013 if (!portletDataContext.isPathNotProcessed(path)) {
1014 return;
1015 }
1016
1017 long lastPublishDate = GetterUtil.getLong(
1018 jxPreferences.getValue("last-publish-date", StringPool.BLANK));
1019
1020 Date startDate = portletDataContext.getStartDate();
1021
1022 if ((lastPublishDate > 0) && (startDate != null) &&
1023 (lastPublishDate < startDate.getTime())) {
1024
1025 portletDataContext.setStartDate(new Date(lastPublishDate));
1026 }
1027
1028 String data = null;
1029
1030 long groupId = portletDataContext.getGroupId();
1031
1032 portletDataContext.setGroupId(portletDataContext.getScopeGroupId());
1033
1034 try {
1035 data = portletDataHandler.exportData(
1036 portletDataContext, portletId, jxPreferences);
1037 }
1038 catch (Exception e) {
1039 throw new SystemException(e);
1040 }
1041 finally {
1042 portletDataContext.setGroupId(groupId);
1043 portletDataContext.setStartDate(startDate);
1044 }
1045
1046 if (Validator.isNull(data)) {
1047 if (_log.isDebugEnabled()) {
1048 _log.debug(
1049 "Not exporting data for " + portletId +
1050 " because null data was returned");
1051 }
1052
1053 return;
1054 }
1055
1056 Element portletDataElement = parentElement.addElement("portlet-data");
1057
1058 portletDataElement.addAttribute("path", path);
1059
1060 portletDataContext.addZipEntry(path, data);
1061
1062 Date endDate = portletDataContext.getEndDate();
1063
1064 if (endDate != null) {
1065 try {
1066 jxPreferences.setValue(
1067 "last-publish-date", String.valueOf(endDate.getTime()));
1068
1069 jxPreferences.store();
1070 }
1071 catch (Exception e) {
1072 _log.error(e, e);
1073 }
1074 }
1075 }
1076
1077 protected void exportPortletPreference(
1078 PortletDataContext portletDataContext, long ownerId, int ownerType,
1079 boolean defaultUser, PortletPreferences portletPreferences,
1080 String portletId, long plid, Element parentElement)
1081 throws Exception {
1082
1083 String preferencesXML = portletPreferences.getPreferences();
1084
1085 if (Validator.isNull(preferencesXML)) {
1086 preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
1087 }
1088
1089 String rootPotletId = PortletConstants.getRootPortletId(portletId);
1090
1091 if (rootPotletId.equals(PortletKeys.ASSET_PUBLISHER)) {
1092 preferencesXML = updateAssetPublisherPortletPreferences(
1093 preferencesXML, plid);
1094 }
1095 else if (rootPotletId.equals(PortletKeys.TAGS_CATEGORIES_NAVIGATION)) {
1096 preferencesXML = updateAssetCategoriesNavigationPortletPreferences(
1097 preferencesXML, plid);
1098 }
1099
1100 Document document = SAXReaderUtil.read(preferencesXML);
1101
1102 Element rootElement = document.getRootElement();
1103
1104 rootElement.addAttribute("owner-id", String.valueOf(ownerId));
1105 rootElement.addAttribute("owner-type", String.valueOf(ownerType));
1106 rootElement.addAttribute("default-user", String.valueOf(defaultUser));
1107 rootElement.addAttribute("plid", String.valueOf(plid));
1108 rootElement.addAttribute("portlet-id", portletId);
1109
1110 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1111 PortletItem portletItem =
1112 PortletItemLocalServiceUtil.getPortletItem(ownerId);
1113
1114 rootElement.addAttribute(
1115 "archive-user-uuid", portletItem.getUserUuid());
1116 rootElement.addAttribute("archive-name", portletItem.getName());
1117 }
1118 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1119 User user = UserLocalServiceUtil.fetchUserById(ownerId);
1120
1121 if (user == null) {
1122 return;
1123 }
1124
1125 rootElement.addAttribute("user-uuid", user.getUserUuid());
1126 }
1127
1128 List<Node> nodes = document.selectNodes(
1129 "/portlet-preferences/preference[name/text() = " +
1130 "'last-publish-date']");
1131
1132 for (Node node : nodes) {
1133 document.remove(node);
1134 }
1135
1136 String path = getPortletPreferencesPath(
1137 portletDataContext, portletId, ownerId, ownerType, plid);
1138
1139 Element portletPreferencesElement = parentElement.addElement(
1140 "portlet-preferences");
1141
1142 portletPreferencesElement.addAttribute("path", path);
1143
1144 if (portletDataContext.isPathNotProcessed(path)) {
1145 portletDataContext.addZipEntry(
1146 path, document.formattedString(StringPool.TAB, false, false));
1147 }
1148 }
1149
1150 protected void exportPortletPreferences(
1151 PortletDataContext portletDataContext, long ownerId, int ownerType,
1152 boolean defaultUser, Layout layout, long plid, String portletId,
1153 Element parentElement)
1154 throws Exception {
1155
1156 PortletPreferences portletPreferences = null;
1157
1158 if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) ||
1159 (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) ||
1160 (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED)) {
1161
1162 plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
1163 }
1164
1165 try {
1166 portletPreferences =
1167 PortletPreferencesLocalServiceUtil.getPortletPreferences(
1168 ownerId, ownerType, plid, portletId);
1169 }
1170 catch (NoSuchPortletPreferencesException nsppe) {
1171 return;
1172 }
1173
1174 LayoutTypePortlet layoutTypePortlet = null;
1175
1176 if (layout != null) {
1177 layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
1178 }
1179
1180 if ((layoutTypePortlet == null) ||
1181 layoutTypePortlet.hasPortletId(portletId)) {
1182
1183 exportPortletPreference(
1184 portletDataContext, ownerId, ownerType, defaultUser,
1185 portletPreferences, portletId, plid, parentElement);
1186 }
1187 }
1188
1189 protected void exportRatingsEntries(
1190 PortletDataContext portletDataContext, Element parentElement)
1191 throws Exception {
1192
1193 Document document = SAXReaderUtil.createDocument();
1194
1195 Element rootElement = document.addElement("ratings");
1196
1197 Map<String, List<RatingsEntry>> ratingsEntriesMap =
1198 portletDataContext.getRatingsEntries();
1199
1200 for (Map.Entry<String, List<RatingsEntry>> entry :
1201 ratingsEntriesMap.entrySet()) {
1202
1203 String[] ratingsEntryParts = StringUtil.split(
1204 entry.getKey(), CharPool.POUND);
1205
1206 String className = ratingsEntryParts[0];
1207 String classPK = ratingsEntryParts[1];
1208
1209 String ratingsEntriesPath = getRatingsEntriesPath(
1210 portletDataContext, className, classPK);
1211
1212 Element assetElement = rootElement.addElement("asset");
1213
1214 assetElement.addAttribute("path", ratingsEntriesPath);
1215 assetElement.addAttribute("class-name", className);
1216 assetElement.addAttribute("class-pk", classPK);
1217
1218 List<RatingsEntry> ratingsEntries = entry.getValue();
1219
1220 for (RatingsEntry ratingsEntry : ratingsEntries) {
1221 String ratingsEntryPath = getRatingsEntryPath(
1222 portletDataContext, className, classPK, ratingsEntry);
1223
1224 portletDataContext.addZipEntry(ratingsEntryPath, ratingsEntry);
1225 }
1226 }
1227
1228 portletDataContext.addZipEntry(
1229 portletDataContext.getRootPath() + "/ratings.xml",
1230 document.formattedString());
1231 }
1232
1233 protected String getAssetCategoryPath(
1234 PortletDataContext portletDataContext, long assetCategoryId) {
1235
1236 StringBundler sb = new StringBundler(6);
1237
1238 sb.append(portletDataContext.getRootPath());
1239 sb.append("/categories/");
1240 sb.append(assetCategoryId);
1241 sb.append(".xml");
1242
1243 return sb.toString();
1244 }
1245
1246 protected String getAssetTagPath(
1247 PortletDataContext portletDataContext, long assetCategoryId) {
1248
1249 StringBundler sb = new StringBundler(4);
1250
1251 sb.append(portletDataContext.getRootPath());
1252 sb.append("/tags/");
1253 sb.append(assetCategoryId);
1254 sb.append(".xml");
1255
1256 return sb.toString();
1257 }
1258
1259 protected String getAssetVocabulariesPath(
1260 PortletDataContext portletDataContext, long assetVocabularyId) {
1261
1262 StringBundler sb = new StringBundler(8);
1263
1264 sb.append(portletDataContext.getRootPath());
1265 sb.append("/vocabularies/");
1266 sb.append(assetVocabularyId);
1267 sb.append(".xml");
1268
1269 return sb.toString();
1270 }
1271
1272 protected String getCommentPath(
1273 PortletDataContext portletDataContext, String className, String classPK,
1274 MBMessage mbMessage) {
1275
1276 StringBundler sb = new StringBundler(8);
1277
1278 sb.append(portletDataContext.getRootPath());
1279 sb.append("/comments/");
1280 sb.append(PortalUtil.getClassNameId(className));
1281 sb.append(CharPool.FORWARD_SLASH);
1282 sb.append(classPK);
1283 sb.append(CharPool.FORWARD_SLASH);
1284 sb.append(mbMessage.getMessageId());
1285 sb.append(".xml");
1286
1287 return sb.toString();
1288 }
1289
1290 protected String getCommentsPath(
1291 PortletDataContext portletDataContext, String className,
1292 String classPK) {
1293
1294 StringBundler sb = new StringBundler(6);
1295
1296 sb.append(portletDataContext.getRootPath());
1297 sb.append("/comments/");
1298 sb.append(PortalUtil.getClassNameId(className));
1299 sb.append(CharPool.FORWARD_SLASH);
1300 sb.append(classPK);
1301 sb.append(CharPool.FORWARD_SLASH);
1302
1303 return sb.toString();
1304 }
1305
1306 protected String getLockPath(
1307 PortletDataContext portletDataContext, String className, String key,
1308 Lock lock) {
1309
1310 StringBundler sb = new StringBundler(8);
1311
1312 sb.append(portletDataContext.getRootPath());
1313 sb.append("/locks/");
1314 sb.append(PortalUtil.getClassNameId(className));
1315 sb.append(CharPool.FORWARD_SLASH);
1316 sb.append(key);
1317 sb.append(CharPool.FORWARD_SLASH);
1318 sb.append(lock.getLockId());
1319 sb.append(".xml");
1320
1321 return sb.toString();
1322 }
1323
1324 protected String getPortletDataPath(
1325 PortletDataContext portletDataContext, String portletId) {
1326
1327 return portletDataContext.getPortletPath(portletId) +
1328 "/portlet-data.xml";
1329 }
1330
1331 protected String getPortletPreferencesPath(
1332 PortletDataContext portletDataContext, String portletId, long ownerId,
1333 int ownerType, long plid) {
1334
1335 StringBundler sb = new StringBundler(8);
1336
1337 sb.append(portletDataContext.getPortletPath(portletId));
1338 sb.append("/preferences/");
1339
1340 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
1341 sb.append("company/");
1342 }
1343 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
1344 sb.append("group/");
1345 }
1346 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT) {
1347 sb.append("layout/");
1348 }
1349 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1350 sb.append("user/");
1351 }
1352 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1353 sb.append("archived/");
1354 }
1355
1356 sb.append(ownerId);
1357 sb.append(CharPool.FORWARD_SLASH);
1358 sb.append(plid);
1359 sb.append(CharPool.FORWARD_SLASH);
1360 sb.append("portlet-preferences.xml");
1361
1362 return sb.toString();
1363 }
1364
1365 protected String getRatingsEntriesPath(
1366 PortletDataContext portletDataContext, String className,
1367 String classPK) {
1368
1369 StringBundler sb = new StringBundler(6);
1370
1371 sb.append(portletDataContext.getRootPath());
1372 sb.append("/ratings/");
1373 sb.append(PortalUtil.getClassNameId(className));
1374 sb.append(CharPool.FORWARD_SLASH);
1375 sb.append(classPK);
1376 sb.append(CharPool.FORWARD_SLASH);
1377
1378 return sb.toString();
1379 }
1380
1381 protected String getRatingsEntryPath(
1382 PortletDataContext portletDataContext, String className, String classPK,
1383 RatingsEntry ratingsEntry) {
1384
1385 StringBundler sb = new StringBundler(8);
1386
1387 sb.append(portletDataContext.getRootPath());
1388 sb.append("/ratings/");
1389 sb.append(PortalUtil.getClassNameId(className));
1390 sb.append(CharPool.FORWARD_SLASH);
1391 sb.append(classPK);
1392 sb.append(CharPool.FORWARD_SLASH);
1393 sb.append(ratingsEntry.getEntryId());
1394 sb.append(".xml");
1395
1396 return sb.toString();
1397 }
1398
1399 protected String updateAssetCategoriesNavigationPortletPreferences(
1400 String xml, long plid)
1401 throws Exception {
1402
1403 javax.portlet.PortletPreferences jxPreferences =
1404 PortletPreferencesFactoryUtil.fromDefaultXML(xml);
1405
1406 Enumeration<String> enu = jxPreferences.getNames();
1407
1408 while (enu.hasMoreElements()) {
1409 String name = enu.nextElement();
1410
1411 if (name.equals("assetVocabularyIds")) {
1412 updatePreferencesClassPKs(
1413 jxPreferences, name, AssetVocabulary.class.getName());
1414 }
1415 }
1416
1417 return PortletPreferencesFactoryUtil.toXML(jxPreferences);
1418 }
1419
1420 protected void updateAssetPublisherClassNameIds(
1421 javax.portlet.PortletPreferences jxPreferences, String key)
1422 throws Exception {
1423
1424 String[] oldValues = jxPreferences.getValues(key, null);
1425
1426 if (oldValues == null) {
1427 return;
1428 }
1429
1430 String[] newValues = new String[oldValues.length];
1431
1432 int i = 0;
1433
1434 for (String oldValue : oldValues) {
1435 if (key.equals("anyAssetType") &&
1436 (oldValue.equals("false") || oldValue.equals("true"))) {
1437
1438 newValues[i++] = oldValue;
1439
1440 continue;
1441 }
1442
1443 try {
1444 long classNameId = Long.parseLong(oldValue);
1445
1446 String className = PortalUtil.getClassName(classNameId);
1447
1448 newValues[i++] = className;
1449 }
1450 catch (Exception e) {
1451 if (_log.isWarnEnabled()) {
1452 _log.warn(
1453 "Unable to find class name ID for class name " +
1454 oldValue);
1455 }
1456 }
1457 }
1458
1459 jxPreferences.setValues(key, newValues);
1460 }
1461
1462 protected void updateAssetPublisherGlobalScopeId(
1463 javax.portlet.PortletPreferences jxPreferences, String key,
1464 long plid)
1465 throws Exception {
1466
1467 String[] oldValues = jxPreferences.getValues(key, null);
1468
1469 if (oldValues == null) {
1470 return;
1471 }
1472
1473 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1474
1475 Company company = CompanyLocalServiceUtil.getCompany(
1476 layout.getCompanyId());
1477
1478 Group companyGroup = company.getGroup();
1479
1480 String[] newValues = new String[oldValues.length];
1481
1482 for (int i = 0; i < oldValues.length; i++) {
1483 String oldValue = oldValues[i];
1484
1485 newValues[i] = StringUtil.replace(
1486 oldValue, "Group_" + companyGroup.getGroupId(),
1487 "Group_Company");
1488 }
1489
1490 jxPreferences.setValues(key, newValues);
1491 }
1492
1493 protected String updateAssetPublisherPortletPreferences(
1494 String xml, long plid)
1495 throws Exception {
1496
1497 javax.portlet.PortletPreferences jxPreferences =
1498 PortletPreferencesFactoryUtil.fromDefaultXML(xml);
1499
1500 Enumeration<String> enu = jxPreferences.getNames();
1501
1502 while (enu.hasMoreElements()) {
1503 String name = enu.nextElement();
1504
1505 String value = GetterUtil.getString(
1506 jxPreferences.getValue(name, null));
1507
1508 if (name.equals(
1509 "anyClassTypeJournalArticleAssetRendererFactory") ||
1510 name.equals(
1511 "classTypeIdsJournalArticleAssetRendererFactory") ||
1512 name.equals("classTypeIds")) {
1513
1514 updatePreferencesClassPKs(
1515 jxPreferences, name, JournalStructure.class.getName());
1516 }
1517 else if (name.equals("anyAssetType") ||
1518 name.equals("classNameIds")) {
1519
1520 updateAssetPublisherClassNameIds(jxPreferences, name);
1521 }
1522 else if (name.equals("defaultScope") || name.equals("scopeIds")) {
1523 updateAssetPublisherGlobalScopeId(jxPreferences, name, plid);
1524 }
1525 else if (name.startsWith("queryName") &&
1526 value.equalsIgnoreCase("assetCategories")) {
1527
1528 String index = name.substring(9);
1529
1530 updatePreferencesClassPKs(
1531 jxPreferences, "queryValues" + index,
1532 AssetCategory.class.getName());
1533 }
1534 }
1535
1536 return PortletPreferencesFactoryUtil.toXML(jxPreferences);
1537 }
1538
1539 protected void updatePreferencesClassPKs(
1540 javax.portlet.PortletPreferences jxPreferences, String key,
1541 String className)
1542 throws Exception {
1543
1544 String[] oldValues = jxPreferences.getValues(key, null);
1545
1546 if (oldValues == null) {
1547 return;
1548 }
1549
1550 String[] newValues = new String[oldValues.length];
1551
1552 for (int i = 0; i < oldValues.length; i++) {
1553 String oldValue = oldValues[i];
1554
1555 String newValue = oldValue;
1556
1557 String[] primaryKeys = StringUtil.split(oldValue);
1558
1559 for (String primaryKey : primaryKeys) {
1560 if (!Validator.isNumber(primaryKey)) {
1561 break;
1562 }
1563
1564 long primaryKeyLong = GetterUtil.getLong(primaryKey);
1565
1566 String uuid = null;
1567
1568 if (className.equals(AssetCategory.class.getName())) {
1569 AssetCategory assetCategory =
1570 AssetCategoryLocalServiceUtil.fetchCategory(
1571 primaryKeyLong);
1572
1573 if (assetCategory != null) {
1574 uuid = assetCategory.getUuid();
1575 }
1576 }
1577 else if (className.equals(AssetVocabulary.class.getName())) {
1578 AssetVocabulary assetVocabulary =
1579 AssetVocabularyLocalServiceUtil.fetchAssetVocabulary(
1580 primaryKeyLong);
1581
1582 if (assetVocabulary != null) {
1583 uuid = assetVocabulary.getUuid();
1584 }
1585 }
1586 else if (className.equals(JournalStructure.class.getName())) {
1587 JournalStructure journalStructure =
1588 JournalStructureLocalServiceUtil.fetchJournalStructure(
1589 primaryKeyLong);
1590
1591 if (journalStructure != null) {
1592 uuid = journalStructure.getUuid();
1593 }
1594 }
1595
1596 if (Validator.isNull(uuid)) {
1597 if (_log.isWarnEnabled()) {
1598 _log.warn(
1599 "Unable to get UUID for class " + className +
1600 " with primary key " + primaryKeyLong);
1601 }
1602
1603 continue;
1604 }
1605
1606 newValue = StringUtil.replace(newValue, primaryKey, uuid);
1607 }
1608
1609 newValues[i] = newValue;
1610 }
1611
1612 jxPreferences.setValues(key, newValues);
1613 }
1614
1615 private static Log _log = LogFactoryUtil.getLog(PortletExporter.class);
1616
1617 private PermissionExporter _permissionExporter = new PermissionExporter();
1618
1619 }