001
014
015 package com.liferay.portal.lar;
016
017 import com.liferay.portal.LARFileException;
018 import com.liferay.portal.LARTypeException;
019 import com.liferay.portal.LayoutImportException;
020 import com.liferay.portal.LocaleException;
021 import com.liferay.portal.MissingReferenceException;
022 import com.liferay.portal.NoSuchLayoutException;
023 import com.liferay.portal.PortletIdException;
024 import com.liferay.portal.kernel.backgroundtask.BackgroundTaskThreadLocal;
025 import com.liferay.portal.kernel.exception.PortalException;
026 import com.liferay.portal.kernel.exception.SystemException;
027 import com.liferay.portal.kernel.language.LanguageUtil;
028 import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
029 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
030 import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
031 import com.liferay.portal.kernel.lar.ManifestSummary;
032 import com.liferay.portal.kernel.lar.MissingReference;
033 import com.liferay.portal.kernel.lar.MissingReferences;
034 import com.liferay.portal.kernel.lar.PortletDataContext;
035 import com.liferay.portal.kernel.lar.PortletDataContextFactoryUtil;
036 import com.liferay.portal.kernel.lar.PortletDataHandler;
037 import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
038 import com.liferay.portal.kernel.lar.PortletDataHandlerStatusMessageSenderUtil;
039 import com.liferay.portal.kernel.lar.UserIdStrategy;
040 import com.liferay.portal.kernel.log.Log;
041 import com.liferay.portal.kernel.log.LogFactoryUtil;
042 import com.liferay.portal.kernel.search.Indexer;
043 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
044 import com.liferay.portal.kernel.staging.MergeLayoutPrototypesThreadLocal;
045 import com.liferay.portal.kernel.util.ArrayUtil;
046 import com.liferay.portal.kernel.util.GetterUtil;
047 import com.liferay.portal.kernel.util.LocaleUtil;
048 import com.liferay.portal.kernel.util.MapUtil;
049 import com.liferay.portal.kernel.util.ReleaseInfo;
050 import com.liferay.portal.kernel.util.StringBundler;
051 import com.liferay.portal.kernel.util.StringPool;
052 import com.liferay.portal.kernel.util.StringUtil;
053 import com.liferay.portal.kernel.util.Validator;
054 import com.liferay.portal.kernel.xml.Document;
055 import com.liferay.portal.kernel.xml.DocumentException;
056 import com.liferay.portal.kernel.xml.Element;
057 import com.liferay.portal.kernel.xml.Node;
058 import com.liferay.portal.kernel.xml.SAXReaderUtil;
059 import com.liferay.portal.kernel.zip.ZipReader;
060 import com.liferay.portal.kernel.zip.ZipReaderFactoryUtil;
061 import com.liferay.portal.model.Group;
062 import com.liferay.portal.model.GroupConstants;
063 import com.liferay.portal.model.Layout;
064 import com.liferay.portal.model.LayoutConstants;
065 import com.liferay.portal.model.Lock;
066 import com.liferay.portal.model.Portlet;
067 import com.liferay.portal.model.PortletConstants;
068 import com.liferay.portal.model.PortletItem;
069 import com.liferay.portal.model.PortletPreferences;
070 import com.liferay.portal.model.User;
071 import com.liferay.portal.security.permission.PermissionCacheUtil;
072 import com.liferay.portal.service.GroupLocalServiceUtil;
073 import com.liferay.portal.service.LayoutLocalServiceUtil;
074 import com.liferay.portal.service.PortletItemLocalServiceUtil;
075 import com.liferay.portal.service.PortletLocalServiceUtil;
076 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
077 import com.liferay.portal.service.ServiceContext;
078 import com.liferay.portal.service.ServiceContextThreadLocal;
079 import com.liferay.portal.service.UserLocalServiceUtil;
080 import com.liferay.portal.service.persistence.PortletPreferencesUtil;
081 import com.liferay.portal.service.persistence.UserUtil;
082 import com.liferay.portal.servlet.filters.cache.CacheUtil;
083 import com.liferay.portal.util.PortalUtil;
084 import com.liferay.portal.util.PortletKeys;
085 import com.liferay.portlet.PortletPreferencesFactoryUtil;
086 import com.liferay.portlet.PortletPreferencesImpl;
087 import com.liferay.portlet.asset.NoSuchTagException;
088 import com.liferay.portlet.asset.model.AssetCategory;
089 import com.liferay.portlet.asset.model.AssetCategoryConstants;
090 import com.liferay.portlet.asset.model.AssetEntry;
091 import com.liferay.portlet.asset.model.AssetLink;
092 import com.liferay.portlet.asset.model.AssetTag;
093 import com.liferay.portlet.asset.model.AssetVocabulary;
094 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
095 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
096 import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
097 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
098 import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
099 import com.liferay.portlet.asset.service.persistence.AssetCategoryUtil;
100 import com.liferay.portlet.asset.service.persistence.AssetTagUtil;
101 import com.liferay.portlet.asset.service.persistence.AssetVocabularyUtil;
102 import com.liferay.portlet.expando.NoSuchTableException;
103 import com.liferay.portlet.expando.model.ExpandoColumn;
104 import com.liferay.portlet.expando.model.ExpandoTable;
105 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
106 import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
107 import com.liferay.portlet.expando.util.ExpandoConverterUtil;
108 import com.liferay.portlet.journalcontent.util.JournalContentUtil;
109 import com.liferay.portlet.messageboards.model.MBMessage;
110 import com.liferay.portlet.ratings.model.RatingsEntry;
111
112 import java.io.File;
113 import java.io.Serializable;
114
115 import java.util.ArrayList;
116 import java.util.Enumeration;
117 import java.util.HashMap;
118 import java.util.List;
119 import java.util.Locale;
120 import java.util.Map;
121
122 import org.apache.commons.lang.time.StopWatch;
123
124
135 public class PortletImporter {
136
137 public String importPortletData(
138 PortletDataContext portletDataContext, String portletId,
139 PortletPreferences portletPreferences, Element portletDataElement)
140 throws Exception {
141
142 Portlet portlet = PortletLocalServiceUtil.getPortletById(
143 portletDataContext.getCompanyId(), portletId);
144
145 if (portlet == null) {
146 if (_log.isDebugEnabled()) {
147 _log.debug(
148 "Do not import portlet data for " + portletId +
149 " because the portlet does not exist");
150 }
151
152 return null;
153 }
154
155 PortletDataHandler portletDataHandler =
156 portlet.getPortletDataHandlerInstance();
157
158 if (portletDataHandler == null) {
159 if (_log.isDebugEnabled()) {
160 _log.debug(
161 "Do not import portlet data for " + portletId +
162 " because the portlet does not have a " +
163 "PortletDataHandler");
164 }
165
166 return null;
167 }
168
169 if (_log.isDebugEnabled()) {
170 _log.debug("Importing data for " + portletId);
171 }
172
173 PortletPreferencesImpl portletPreferencesImpl = null;
174
175 if (portletPreferences != null) {
176 portletPreferencesImpl =
177 (PortletPreferencesImpl)
178 PortletPreferencesFactoryUtil.fromDefaultXML(
179 portletPreferences.getPreferences());
180 }
181
182 String portletData = portletDataContext.getZipEntryAsString(
183 portletDataElement.attributeValue("path"));
184
185 if (Validator.isNull(portletData)) {
186 return null;
187 }
188
189 portletPreferencesImpl =
190 (PortletPreferencesImpl)portletDataHandler.importData(
191 portletDataContext, portletId, portletPreferencesImpl,
192 portletData);
193
194 if (portletPreferencesImpl == null) {
195 return null;
196 }
197
198 return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
199 }
200
201 public void importPortletInfo(
202 long userId, long plid, long groupId, String portletId,
203 Map<String, String[]> parameterMap, File file)
204 throws Exception {
205
206 try {
207 ExportImportThreadLocal.setPortletImportInProcess(true);
208
209 doImportPortletInfo(
210 userId, plid, groupId, portletId, parameterMap, file);
211 }
212 finally {
213 ExportImportThreadLocal.setPortletImportInProcess(false);
214
215 CacheUtil.clearCache();
216 JournalContentUtil.clearCache();
217 PermissionCacheUtil.clearCache();
218 }
219 }
220
221 public MissingReferences validateFile(
222 long userId, long plid, long groupId, String portletId,
223 Map<String, String[]> parameterMap, File file)
224 throws Exception {
225
226 try {
227 ExportImportThreadLocal.setPortletValidationInProcess(true);
228
229 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
230
231 ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);
232
233 PortletDataContext portletDataContext =
234 PortletDataContextFactoryUtil.createImportPortletDataContext(
235 layout.getCompanyId(), groupId, parameterMap, null,
236 zipReader);
237
238 validateFile(portletDataContext, portletId);
239
240 MissingReferences missingReferences =
241 ExportImportHelperUtil.validateMissingReferences(
242 userId, groupId, parameterMap, file);
243
244 Map<String, MissingReference> dependencyMissingReferences =
245 missingReferences.getDependencyMissingReferences();
246
247 if (!dependencyMissingReferences.isEmpty()) {
248 throw new MissingReferenceException(missingReferences);
249 }
250
251 return missingReferences;
252 }
253 finally {
254 ExportImportThreadLocal.setPortletValidationInProcess(false);
255 }
256 }
257
258 protected void deletePortletData(
259 PortletDataContext portletDataContext, String portletId, long plid)
260 throws Exception {
261
262 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
263 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
264
265 PortletPreferences portletPreferences =
266 PortletPreferencesUtil.fetchByO_O_P_P(
267 ownerId, ownerType, plid, portletId);
268
269 if (portletPreferences == null) {
270 portletPreferences =
271 new com.liferay.portal.model.impl.PortletPreferencesImpl();
272 }
273
274 String xml = deletePortletData(
275 portletDataContext, portletId, portletPreferences);
276
277 if (xml != null) {
278 PortletPreferencesLocalServiceUtil.updatePreferences(
279 ownerId, ownerType, plid, portletId, xml);
280 }
281 }
282
283 protected String deletePortletData(
284 PortletDataContext portletDataContext, String portletId,
285 PortletPreferences portletPreferences)
286 throws Exception {
287
288 Portlet portlet = PortletLocalServiceUtil.getPortletById(
289 portletDataContext.getCompanyId(), portletId);
290
291 if (portlet == null) {
292 if (_log.isDebugEnabled()) {
293 _log.debug(
294 "Do not delete portlet data for " + portletId +
295 " because the portlet does not exist");
296 }
297
298 return null;
299 }
300
301 PortletDataHandler portletDataHandler =
302 portlet.getPortletDataHandlerInstance();
303
304 if (portletDataHandler == null) {
305 if (_log.isDebugEnabled()) {
306 _log.debug(
307 "Do not delete portlet data for " + portletId +
308 " because the portlet does not have a " +
309 "PortletDataHandler");
310 }
311
312 return null;
313 }
314
315 if (_log.isDebugEnabled()) {
316 _log.debug("Deleting data for " + portletId);
317 }
318
319 PortletPreferencesImpl portletPreferencesImpl =
320 (PortletPreferencesImpl)
321 PortletPreferencesFactoryUtil.fromDefaultXML(
322 portletPreferences.getPreferences());
323
324 try {
325 portletPreferencesImpl =
326 (PortletPreferencesImpl)portletDataHandler.deleteData(
327 portletDataContext, portletId, portletPreferencesImpl);
328 }
329 finally {
330 portletDataContext.setGroupId(portletDataContext.getScopeGroupId());
331 }
332
333 if (portletPreferencesImpl == null) {
334 return null;
335 }
336
337 return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
338 }
339
340 protected void doImportPortletInfo(
341 long userId, long plid, long groupId, String portletId,
342 Map<String, String[]> parameterMap, File file)
343 throws Exception {
344
345 boolean deletePortletData = MapUtil.getBoolean(
346 parameterMap, PortletDataHandlerKeys.DELETE_PORTLET_DATA);
347 boolean importPermissions = MapUtil.getBoolean(
348 parameterMap, PortletDataHandlerKeys.PERMISSIONS);
349 boolean importPortletConfiguration = MapUtil.getBoolean(
350 parameterMap, PortletDataHandlerKeys.PORTLET_CONFIGURATION);
351 boolean importPortletConfigurationAll = MapUtil.getBoolean(
352 parameterMap, PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL);
353 boolean importPortletData = MapUtil.getBoolean(
354 parameterMap, PortletDataHandlerKeys.PORTLET_DATA);
355 boolean importPortletDataAll = MapUtil.getBoolean(
356 parameterMap, PortletDataHandlerKeys.PORTLET_DATA_ALL);
357 String userIdStrategyString = MapUtil.getString(
358 parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY);
359
360 String rootPortletId = PortletConstants.getRootPortletId(portletId);
361
362 if (importPortletDataAll) {
363 importPortletData = true;
364 }
365 else if (parameterMap.containsKey(
366 PortletDataHandlerKeys.PORTLET_DATA + StringPool.UNDERLINE +
367 rootPortletId)) {
368
369 importPortletData = MapUtil.getBoolean(
370 parameterMap,
371 PortletDataHandlerKeys.PORTLET_DATA + StringPool.UNDERLINE +
372 rootPortletId);
373 }
374
375 boolean importPortletArchivedSetups = importPortletConfiguration;
376 boolean importPortletSetup = importPortletConfiguration;
377 boolean importPortletUserPreferences = importPortletConfiguration;
378
379 if (importPortletConfigurationAll) {
380 importPortletArchivedSetups =
381 MapUtil.getBoolean(
382 parameterMap,
383 PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS_ALL);
384 importPortletSetup =
385 MapUtil.getBoolean(
386 parameterMap, PortletDataHandlerKeys.PORTLET_SETUP_ALL);
387 importPortletUserPreferences =
388 MapUtil.getBoolean(
389 parameterMap,
390 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES_ALL);
391 }
392 else if (parameterMap.containsKey(
393 PortletDataHandlerKeys.PORTLET_CONFIGURATION + "_" +
394 rootPortletId)) {
395
396 importPortletConfiguration =
397 importPortletConfiguration &&
398 MapUtil.getBoolean(
399 parameterMap,
400 PortletDataHandlerKeys.PORTLET_CONFIGURATION +
401 StringPool.UNDERLINE + rootPortletId);
402
403 importPortletArchivedSetups =
404 importPortletConfiguration &&
405 MapUtil.getBoolean(
406 parameterMap,
407 PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS +
408 StringPool.UNDERLINE + rootPortletId);
409 importPortletSetup =
410 importPortletConfiguration &&
411 MapUtil.getBoolean(
412 parameterMap,
413 PortletDataHandlerKeys.PORTLET_SETUP +
414 StringPool.UNDERLINE + rootPortletId);
415 importPortletUserPreferences =
416 importPortletConfiguration &&
417 MapUtil.getBoolean(
418 parameterMap,
419 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES +
420 StringPool.UNDERLINE + rootPortletId);
421 }
422
423 StopWatch stopWatch = null;
424
425 if (_log.isInfoEnabled()) {
426 stopWatch = new StopWatch();
427
428 stopWatch.start();
429 }
430
431 User user = UserUtil.findByPrimaryKey(userId);
432
433 ServiceContext serviceContext =
434 ServiceContextThreadLocal.getServiceContext();
435
436 if (serviceContext == null) {
437 serviceContext = new ServiceContext();
438
439 serviceContext.setCompanyId(user.getCompanyId());
440 serviceContext.setSignedIn(false);
441 serviceContext.setUserId(userId);
442
443 ServiceContextThreadLocal.pushServiceContext(serviceContext);
444 }
445
446 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
447
448 UserIdStrategy userIdStrategy = getUserIdStrategy(
449 user, userIdStrategyString);
450
451 ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);
452
453 PortletDataContext portletDataContext =
454 PortletDataContextFactoryUtil.createImportPortletDataContext(
455 layout.getCompanyId(), groupId, parameterMap, userIdStrategy,
456 zipReader);
457
458 portletDataContext.setPortetDataContextListener(
459 new PortletDataContextListenerImpl(portletDataContext));
460
461 portletDataContext.setPlid(plid);
462 portletDataContext.setPrivateLayout(layout.isPrivateLayout());
463
464
465
466 validateFile(portletDataContext, portletId);
467
468 if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
469 ManifestSummary manifestSummary =
470 ExportImportHelperUtil.getManifestSummary(
471 userId, groupId, parameterMap, file);
472
473 PortletDataHandlerStatusMessageSenderUtil.sendStatusMessage(
474 "portlet", portletId, manifestSummary);
475 }
476
477
478
479 long sourceCompanyId = GetterUtil.getLong(
480 _headerElement.attributeValue("company-id"));
481
482 portletDataContext.setSourceCompanyId(sourceCompanyId);
483
484
485
486 long sourceCompanyGroupId = GetterUtil.getLong(
487 _headerElement.attributeValue("company-group-id"));
488
489 portletDataContext.setSourceCompanyGroupId(sourceCompanyGroupId);
490
491
492
493 long sourceGroupId = GetterUtil.getLong(
494 _headerElement.attributeValue("group-id"));
495
496 portletDataContext.setSourceGroupId(sourceGroupId);
497
498
499
500 long sourceUserPersonalSiteGroupId = GetterUtil.getLong(
501 _headerElement.attributeValue("user-personal-site-group-id"));
502
503 portletDataContext.setSourceUserPersonalSiteGroupId(
504 sourceUserPersonalSiteGroupId);
505
506
507
508
509
510 if (importPermissions) {
511 _permissionImporter.readPortletDataPermissions(portletDataContext);
512 }
513
514 readAssetCategories(portletDataContext);
515 readAssetTags(portletDataContext);
516 readComments(portletDataContext);
517 readExpandoTables(portletDataContext);
518 readLocks(portletDataContext);
519 readRatingsEntries(portletDataContext);
520
521
522
523 if (_log.isDebugEnabled()) {
524 _log.debug("Deleting portlet data");
525 }
526
527 if (deletePortletData) {
528 deletePortletData(portletDataContext, portletId, plid);
529 }
530
531 Element portletElement = null;
532
533 try {
534 portletElement = _rootElement.element("portlet");
535
536 Document portletDocument = SAXReaderUtil.read(
537 portletDataContext.getZipEntryAsString(
538 portletElement.attributeValue("path")));
539
540 portletElement = portletDocument.getRootElement();
541 }
542 catch (DocumentException de) {
543 throw new SystemException(de);
544 }
545
546 setPortletScope(portletDataContext, portletElement);
547
548 Element portletDataElement = portletElement.element("portlet-data");
549
550 boolean importData = importPortletData && (portletDataElement != null);
551
552 try {
553
554
555
556 importPortletPreferences(
557 portletDataContext, layout.getCompanyId(), groupId, layout,
558 portletId, portletElement, importPortletSetup,
559 importPortletArchivedSetups, importPortletUserPreferences, true,
560 importData);
561
562
563
564 if (importData) {
565 if (_log.isDebugEnabled()) {
566 _log.debug("Importing portlet data");
567 }
568
569 importPortletData(
570 portletDataContext, portletId, plid, portletDataElement);
571 }
572 }
573 finally {
574 resetPortletScope(portletDataContext, groupId);
575 }
576
577
578
579 if (importPermissions) {
580 if (_log.isDebugEnabled()) {
581 _log.debug("Importing portlet permissions");
582 }
583
584 LayoutCache layoutCache = new LayoutCache();
585
586 _permissionImporter.importPortletPermissions(
587 layoutCache, layout.getCompanyId(), groupId, userId, layout,
588 portletElement, portletId);
589
590 if (userId > 0) {
591 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
592 User.class);
593
594 indexer.reindex(userId);
595 }
596 }
597
598
599
600 if (_log.isDebugEnabled()) {
601 _log.debug("Importing asset links");
602 }
603
604 readAssetLinks(portletDataContext);
605
606
607
608 _deletionSystemEventImporter.importDeletionSystemEvents(
609 portletDataContext);
610
611 if (_log.isInfoEnabled()) {
612 _log.info("Importing portlet takes " + stopWatch.getTime() + " ms");
613 }
614
615 zipReader.close();
616 }
617
618 protected String getAssetCategoryName(
619 String uuid, long groupId, long parentCategoryId, String name,
620 long vocabularyId, int count)
621 throws Exception {
622
623 AssetCategory assetCategory = AssetCategoryUtil.fetchByG_P_N_V_First(
624 groupId, parentCategoryId, name, vocabularyId, null);
625
626 if ((assetCategory == null) ||
627 (Validator.isNotNull(uuid) &&
628 uuid.equals(assetCategory.getUuid()))) {
629
630 return name;
631 }
632
633 name = StringUtil.appendParentheticalSuffix(name, count);
634
635 return getAssetCategoryName(
636 uuid, groupId, parentCategoryId, name, vocabularyId, ++count);
637 }
638
639 protected String getAssetCategoryPath(
640 PortletDataContext portletDataContext, long assetCategoryId) {
641
642 StringBundler sb = new StringBundler(6);
643
644 sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
645 sb.append("/categories/");
646 sb.append(assetCategoryId);
647 sb.append(".xml");
648
649 return sb.toString();
650 }
651
652 protected Map<Locale, String> getAssetCategoryTitleMap(
653 long groupId, AssetCategory assetCategory, String name)
654 throws PortalException, SystemException {
655
656 Map<Locale, String> titleMap = assetCategory.getTitleMap();
657
658 if (titleMap == null) {
659 titleMap = new HashMap<Locale, String>();
660 }
661
662 titleMap.put(PortalUtil.getSiteDefaultLocale(groupId), name);
663
664 return titleMap;
665 }
666
667 protected String getAssetVocabularyName(
668 String uuid, long groupId, String name, int count)
669 throws Exception {
670
671 AssetVocabulary assetVocabulary = AssetVocabularyUtil.fetchByG_N(
672 groupId, name);
673
674 if (assetVocabulary == null) {
675 return name;
676 }
677
678 if (Validator.isNotNull(uuid) &&
679 uuid.equals(assetVocabulary.getUuid())) {
680
681 return name;
682 }
683
684 name = StringUtil.appendParentheticalSuffix(name, count);
685
686 return getAssetVocabularyName(uuid, groupId, name, ++count);
687 }
688
689 protected Map<Locale, String> getAssetVocabularyTitleMap(
690 long groupId, AssetVocabulary assetVocabulary, String name)
691 throws PortalException, SystemException {
692
693 Map<Locale, String> titleMap = assetVocabulary.getTitleMap();
694
695 if (titleMap == null) {
696 titleMap = new HashMap<Locale, String>();
697 }
698
699 titleMap.put(PortalUtil.getSiteDefaultLocale(groupId), name);
700
701 return titleMap;
702 }
703
704 protected UserIdStrategy getUserIdStrategy(
705 User user, String userIdStrategy) {
706
707 if (UserIdStrategy.ALWAYS_CURRENT_USER_ID.equals(userIdStrategy)) {
708 return new AlwaysCurrentUserIdStrategy(user);
709 }
710
711 return new CurrentUserIdStrategy(user);
712 }
713
714 protected void importAssetCategory(
715 PortletDataContext portletDataContext,
716 Map<Long, Long> assetVocabularyPKs,
717 Map<Long, Long> assetCategoryPKs,
718 Map<String, String> assetCategoryUuids,
719 Element assetCategoryElement, AssetCategory assetCategory)
720 throws Exception {
721
722 long userId = portletDataContext.getUserId(assetCategory.getUserUuid());
723 long groupId = portletDataContext.getGroupId();
724 long assetVocabularyId = MapUtil.getLong(
725 assetVocabularyPKs, assetCategory.getVocabularyId(),
726 assetCategory.getVocabularyId());
727 long parentAssetCategoryId = MapUtil.getLong(
728 assetCategoryPKs, assetCategory.getParentCategoryId(),
729 assetCategory.getParentCategoryId());
730
731 if ((parentAssetCategoryId !=
732 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
733 (parentAssetCategoryId == assetCategory.getParentCategoryId())) {
734
735 String path = getAssetCategoryPath(
736 portletDataContext, parentAssetCategoryId);
737
738 AssetCategory parentAssetCategory =
739 (AssetCategory)portletDataContext.getZipEntryAsObject(path);
740
741 Node parentCategoryNode =
742 assetCategoryElement.getParent().selectSingleNode(
743 "./category[@path='" + path + "']");
744
745 if (parentCategoryNode != null) {
746 importAssetCategory(
747 portletDataContext, assetVocabularyPKs, assetCategoryPKs,
748 assetCategoryUuids, (Element)parentCategoryNode,
749 parentAssetCategory);
750
751 parentAssetCategoryId = MapUtil.getLong(
752 assetCategoryPKs, assetCategory.getParentCategoryId(),
753 assetCategory.getParentCategoryId());
754 }
755 }
756
757 ServiceContext serviceContext = new ServiceContext();
758
759 serviceContext.setAddGroupPermissions(true);
760 serviceContext.setAddGuestPermissions(true);
761 serviceContext.setCreateDate(assetCategory.getCreateDate());
762 serviceContext.setModifiedDate(assetCategory.getModifiedDate());
763 serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
764
765 AssetCategory importedAssetCategory = null;
766
767 if ((parentAssetCategoryId !=
768 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
769 (AssetCategoryUtil.fetchByPrimaryKey(parentAssetCategoryId) ==
770 null)) {
771
772 _log.error(
773 "Could not find the parent category for category " +
774 assetCategory.getCategoryId());
775
776 return;
777 }
778
779 List<Element> propertyElements = assetCategoryElement.elements(
780 "property");
781
782 String[] properties = new String[propertyElements.size()];
783
784 for (int i = 0; i < propertyElements.size(); i++) {
785 Element propertyElement = propertyElements.get(i);
786
787 String key = propertyElement.attributeValue("key");
788 String value = propertyElement.attributeValue("value");
789
790 properties[i] = key.concat(StringPool.COLON).concat(value);
791 }
792
793 AssetCategory existingAssetCategory = AssetCategoryUtil.fetchByUUID_G(
794 assetCategory.getUuid(), groupId);
795
796 if (existingAssetCategory == null) {
797 existingAssetCategory = AssetCategoryUtil.fetchByUUID_G(
798 assetCategory.getUuid(),
799 portletDataContext.getCompanyGroupId());
800 }
801
802 if (existingAssetCategory == null) {
803 String name = getAssetCategoryName(
804 null, groupId, parentAssetCategoryId, assetCategory.getName(),
805 assetCategory.getVocabularyId(), 2);
806
807 serviceContext.setUuid(assetCategory.getUuid());
808
809 importedAssetCategory =
810 AssetCategoryLocalServiceUtil.addCategory(
811 userId, parentAssetCategoryId,
812 getAssetCategoryTitleMap(groupId, assetCategory, name),
813 assetCategory.getDescriptionMap(), assetVocabularyId,
814 properties, serviceContext);
815 }
816 else if (portletDataContext.isCompanyStagedGroupedModel(
817 existingAssetCategory)) {
818
819 return;
820 }
821 else {
822 String name = getAssetCategoryName(
823 assetCategory.getUuid(), groupId, parentAssetCategoryId,
824 assetCategory.getName(), assetCategory.getVocabularyId(), 2);
825
826 importedAssetCategory =
827 AssetCategoryLocalServiceUtil.updateCategory(
828 userId, existingAssetCategory.getCategoryId(),
829 parentAssetCategoryId,
830 getAssetCategoryTitleMap(groupId, assetCategory, name),
831 assetCategory.getDescriptionMap(), assetVocabularyId,
832 properties, serviceContext);
833 }
834
835 assetCategoryPKs.put(
836 assetCategory.getCategoryId(),
837 importedAssetCategory.getCategoryId());
838
839 assetCategoryUuids.put(
840 assetCategory.getUuid(), importedAssetCategory.getUuid());
841
842 portletDataContext.importPermissions(
843 AssetCategory.class, assetCategory.getCategoryId(),
844 importedAssetCategory.getCategoryId());
845 }
846
847 protected void importAssetTag(
848 PortletDataContext portletDataContext, Map<Long, Long> assetTagPKs,
849 Element assetTagElement, AssetTag assetTag)
850 throws PortalException, SystemException {
851
852 long userId = portletDataContext.getUserId(assetTag.getUserUuid());
853
854 ServiceContext serviceContext = new ServiceContext();
855
856 serviceContext.setAddGroupPermissions(true);
857 serviceContext.setAddGuestPermissions(true);
858 serviceContext.setCreateDate(assetTag.getCreateDate());
859 serviceContext.setModifiedDate(assetTag.getModifiedDate());
860 serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
861
862 AssetTag importedAssetTag = null;
863
864 List<Element> propertyElements = assetTagElement.elements("property");
865
866 String[] properties = new String[propertyElements.size()];
867
868 for (int i = 0; i < propertyElements.size(); i++) {
869 Element propertyElement = propertyElements.get(i);
870
871 String key = propertyElement.attributeValue("key");
872 String value = propertyElement.attributeValue("value");
873
874 properties[i] = key.concat(StringPool.COLON).concat(value);
875 }
876
877 AssetTag existingAssetTag = null;
878
879 try {
880 existingAssetTag = AssetTagUtil.findByG_N(
881 portletDataContext.getScopeGroupId(), assetTag.getName());
882 }
883 catch (NoSuchTagException nste) {
884 if (_log.isDebugEnabled()) {
885 StringBundler sb = new StringBundler(5);
886
887 sb.append("No AssetTag exists with the key {groupId=");
888 sb.append(portletDataContext.getScopeGroupId());
889 sb.append(", name=");
890 sb.append(assetTag.getName());
891 sb.append("}");
892
893 _log.debug(sb.toString());
894 }
895 }
896
897 try {
898 if (existingAssetTag == null) {
899 importedAssetTag = AssetTagLocalServiceUtil.addTag(
900 userId, assetTag.getName(), properties, serviceContext);
901 }
902 else {
903 importedAssetTag = AssetTagLocalServiceUtil.updateTag(
904 userId, existingAssetTag.getTagId(), assetTag.getName(),
905 properties, serviceContext);
906 }
907
908 assetTagPKs.put(assetTag.getTagId(), importedAssetTag.getTagId());
909
910 portletDataContext.importPermissions(
911 AssetTag.class, assetTag.getTagId(),
912 importedAssetTag.getTagId());
913 }
914 catch (NoSuchTagException nste) {
915 _log.error(
916 "Could not find the parent category for category " +
917 assetTag.getTagId());
918 }
919 }
920
921 protected void importAssetVocabulary(
922 PortletDataContext portletDataContext,
923 Map<Long, Long> assetVocabularyPKs, Element assetVocabularyElement,
924 AssetVocabulary assetVocabulary)
925 throws Exception {
926
927 long userId = portletDataContext.getUserId(
928 assetVocabulary.getUserUuid());
929 long groupId = portletDataContext.getScopeGroupId();
930
931 ServiceContext serviceContext = new ServiceContext();
932
933 serviceContext.setAddGroupPermissions(true);
934 serviceContext.setAddGuestPermissions(true);
935 serviceContext.setCreateDate(assetVocabulary.getCreateDate());
936 serviceContext.setModifiedDate(assetVocabulary.getModifiedDate());
937 serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
938
939 AssetVocabulary importedAssetVocabulary = null;
940
941 AssetVocabulary existingAssetVocabulary =
942 AssetVocabularyUtil.fetchByUUID_G(
943 assetVocabulary.getUuid(), groupId);
944
945 if (existingAssetVocabulary == null) {
946 existingAssetVocabulary = AssetVocabularyUtil.fetchByUUID_G(
947 assetVocabulary.getUuid(),
948 portletDataContext.getCompanyGroupId());
949 }
950
951 if (existingAssetVocabulary == null) {
952 String name = getAssetVocabularyName(
953 null, groupId, assetVocabulary.getName(), 2);
954
955 serviceContext.setUuid(assetVocabulary.getUuid());
956
957 importedAssetVocabulary =
958 AssetVocabularyLocalServiceUtil.addVocabulary(
959 userId, StringPool.BLANK,
960 getAssetVocabularyTitleMap(groupId, assetVocabulary, name),
961 assetVocabulary.getDescriptionMap(),
962 assetVocabulary.getSettings(), serviceContext);
963 }
964 else if (portletDataContext.isCompanyStagedGroupedModel(
965 existingAssetVocabulary)) {
966
967 return;
968 }
969 else {
970 String name = getAssetVocabularyName(
971 assetVocabulary.getUuid(), groupId, assetVocabulary.getName(),
972 2);
973
974 importedAssetVocabulary =
975 AssetVocabularyLocalServiceUtil.updateVocabulary(
976 existingAssetVocabulary.getVocabularyId(), StringPool.BLANK,
977 getAssetVocabularyTitleMap(groupId, assetVocabulary, name),
978 assetVocabulary.getDescriptionMap(),
979 assetVocabulary.getSettings(), serviceContext);
980 }
981
982 assetVocabularyPKs.put(
983 assetVocabulary.getVocabularyId(),
984 importedAssetVocabulary.getVocabularyId());
985
986 portletDataContext.importPermissions(
987 AssetVocabulary.class, assetVocabulary.getVocabularyId(),
988 importedAssetVocabulary.getVocabularyId());
989 }
990
991 protected void importPortletData(
992 PortletDataContext portletDataContext, String portletId, long plid,
993 Element portletDataElement)
994 throws Exception {
995
996 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
997 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
998
999 PortletPreferences portletPreferences =
1000 PortletPreferencesUtil.fetchByO_O_P_P(
1001 ownerId, ownerType, plid, portletId);
1002
1003 if (portletPreferences == null) {
1004 portletPreferences =
1005 new com.liferay.portal.model.impl.PortletPreferencesImpl();
1006 }
1007
1008 String xml = importPortletData(
1009 portletDataContext, portletId, portletPreferences,
1010 portletDataElement);
1011
1012 if (Validator.isNotNull(xml)) {
1013 PortletPreferencesLocalServiceUtil.updatePreferences(
1014 ownerId, ownerType, plid, portletId, xml);
1015 }
1016 }
1017
1018 protected void importPortletPreferences(
1019 PortletDataContext portletDataContext, long companyId, long groupId,
1020 Layout layout, String portletId, Element parentElement,
1021 boolean importPortletSetup, boolean importPortletArchivedSetups,
1022 boolean importPortletUserPreferences, boolean preserveScopeLayoutId,
1023 boolean importPortletData)
1024 throws Exception {
1025
1026 if (portletId == null) {
1027 portletId = parentElement.attributeValue("portlet-id");
1028 }
1029
1030 String languageId = LocaleUtil.toLanguageId(
1031 LocaleUtil.getMostRelevantLocale());
1032
1033 long plid = LayoutConstants.DEFAULT_PLID;
1034 String portletSetupTitle = StringPool.BLANK;
1035 String scopeType = StringPool.BLANK;
1036 String scopeLayoutUuid = StringPool.BLANK;
1037
1038 if (layout != null) {
1039 plid = layout.getPlid();
1040
1041 if (preserveScopeLayoutId && (portletId != null)) {
1042 javax.portlet.PortletPreferences jxPortletPreferences =
1043 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
1044 layout, portletId);
1045
1046 portletSetupTitle = GetterUtil.getString(
1047 jxPortletPreferences.getValue(
1048 "portletSetupTitle_" + languageId,
1049 PortalUtil.getPortletTitle(portletId, languageId)));
1050
1051 scopeType = GetterUtil.getString(
1052 jxPortletPreferences.getValue("lfrScopeType", null));
1053 scopeLayoutUuid = GetterUtil.getString(
1054 jxPortletPreferences.getValue("lfrScopeLayoutUuid", null));
1055
1056 portletDataContext.setScopeType(scopeType);
1057 portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1058 }
1059 }
1060
1061 List<Element> portletPreferencesElements = parentElement.elements(
1062 "portlet-preferences");
1063
1064 for (Element portletPreferencesElement : portletPreferencesElements) {
1065 String path = portletPreferencesElement.attributeValue("path");
1066
1067 if (portletDataContext.isPathNotProcessed(path)) {
1068 String xml = null;
1069
1070 Element element = null;
1071
1072 try {
1073 xml = portletDataContext.getZipEntryAsString(path);
1074
1075 Document preferencesDocument = SAXReaderUtil.read(xml);
1076
1077 element = preferencesDocument.getRootElement();
1078 }
1079 catch (DocumentException de) {
1080 throw new SystemException(de);
1081 }
1082
1083 long ownerId = GetterUtil.getLong(
1084 element.attributeValue("owner-id"));
1085 int ownerType = GetterUtil.getInteger(
1086 element.attributeValue("owner-type"));
1087
1088 if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) ||
1089 !importPortletSetup) {
1090
1091 continue;
1092 }
1093
1094 if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) &&
1095 !importPortletArchivedSetups) {
1096
1097 continue;
1098 }
1099
1100 if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) &&
1101 (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT) &&
1102 !importPortletUserPreferences) {
1103
1104 continue;
1105 }
1106
1107 long curPlid = plid;
1108 String curPortletId = portletId;
1109
1110 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
1111 curPlid = PortletKeys.PREFS_PLID_SHARED;
1112 curPortletId = PortletConstants.getRootPortletId(portletId);
1113 ownerId = portletDataContext.getScopeGroupId();
1114 }
1115
1116 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1117 String userUuid = element.attributeValue(
1118 "archive-user-uuid");
1119
1120 long userId = portletDataContext.getUserId(userUuid);
1121
1122 String name = element.attributeValue("archive-name");
1123
1124 curPortletId = PortletConstants.getRootPortletId(portletId);
1125
1126 PortletItem portletItem =
1127 PortletItemLocalServiceUtil.updatePortletItem(
1128 userId, groupId, name, curPortletId,
1129 PortletPreferences.class.getName());
1130
1131 curPlid = LayoutConstants.DEFAULT_PLID;
1132 ownerId = portletItem.getPortletItemId();
1133 }
1134
1135 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1136 String userUuid = element.attributeValue("user-uuid");
1137
1138 ownerId = portletDataContext.getUserId(userUuid);
1139 }
1140
1141 boolean defaultUser = GetterUtil.getBoolean(
1142 element.attributeValue("default-user"));
1143
1144 if (defaultUser) {
1145 ownerId = UserLocalServiceUtil.getDefaultUserId(companyId);
1146 }
1147
1148 javax.portlet.PortletPreferences jxPortletPreferences =
1149 PortletPreferencesFactoryUtil.fromXML(
1150 companyId, ownerId, ownerType, curPlid, curPortletId,
1151 xml);
1152
1153 Element importDataRootElement =
1154 portletDataContext.getImportDataRootElement();
1155
1156 try {
1157 Element preferenceDataElement =
1158 portletPreferencesElement.element("preference-data");
1159
1160 if (preferenceDataElement != null) {
1161 portletDataContext.setImportDataRootElement(
1162 preferenceDataElement);
1163 }
1164
1165 Portlet portlet = PortletLocalServiceUtil.getPortletById(
1166 portletDataContext.getCompanyId(), curPortletId);
1167
1168 PortletDataHandler portletDataHandler =
1169 portlet.getPortletDataHandlerInstance();
1170
1171 jxPortletPreferences =
1172 portletDataHandler.processImportPortletPreferences(
1173 portletDataContext, curPortletId,
1174 jxPortletPreferences);
1175 }
1176 finally {
1177 portletDataContext.setImportDataRootElement(
1178 importDataRootElement);
1179 }
1180
1181 updatePortletPreferences(
1182 portletDataContext, ownerId, ownerType, curPlid,
1183 curPortletId,
1184 PortletPreferencesFactoryUtil.toXML(jxPortletPreferences),
1185 importPortletData);
1186 }
1187 }
1188
1189 if (preserveScopeLayoutId && (layout != null)) {
1190 javax.portlet.PortletPreferences jxPortletPreferences =
1191 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
1192 layout, portletId);
1193
1194 try {
1195 jxPortletPreferences.setValue(
1196 "portletSetupTitle_" + languageId, portletSetupTitle);
1197 jxPortletPreferences.setValue("lfrScopeType", scopeType);
1198 jxPortletPreferences.setValue(
1199 "lfrScopeLayoutUuid", scopeLayoutUuid);
1200
1201 jxPortletPreferences.store();
1202 }
1203 finally {
1204 portletDataContext.setScopeType(scopeType);
1205 portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1206 }
1207 }
1208 }
1209
1210 protected void readAssetCategories(PortletDataContext portletDataContext)
1211 throws Exception {
1212
1213 String xml = portletDataContext.getZipEntryAsString(
1214 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1215 "/categories-hierarchy.xml");
1216
1217 if (xml == null) {
1218 return;
1219 }
1220
1221 Document document = SAXReaderUtil.read(xml);
1222
1223 Element rootElement = document.getRootElement();
1224
1225 Element assetVocabulariesElement = rootElement.element("vocabularies");
1226
1227 List<Element> assetVocabularyElements =
1228 assetVocabulariesElement.elements("vocabulary");
1229
1230 Map<Long, Long> assetVocabularyPKs =
1231 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1232 AssetVocabulary.class);
1233
1234 for (Element assetVocabularyElement : assetVocabularyElements) {
1235 String path = assetVocabularyElement.attributeValue("path");
1236
1237 if (!portletDataContext.isPathNotProcessed(path)) {
1238 continue;
1239 }
1240
1241 AssetVocabulary assetVocabulary =
1242 (AssetVocabulary)portletDataContext.getZipEntryAsObject(path);
1243
1244 importAssetVocabulary(
1245 portletDataContext, assetVocabularyPKs, assetVocabularyElement,
1246 assetVocabulary);
1247 }
1248
1249 Element assetCategoriesElement = rootElement.element("categories");
1250
1251 List<Element> assetCategoryElements = assetCategoriesElement.elements(
1252 "category");
1253
1254 Map<Long, Long> assetCategoryPKs =
1255 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1256 AssetCategory.class);
1257
1258 Map<String, String> assetCategoryUuids =
1259 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
1260 AssetCategory.class + ".uuid");
1261
1262 for (Element assetCategoryElement : assetCategoryElements) {
1263 String path = assetCategoryElement.attributeValue("path");
1264
1265 if (!portletDataContext.isPathNotProcessed(path)) {
1266 continue;
1267 }
1268
1269 AssetCategory assetCategory =
1270 (AssetCategory)portletDataContext.getZipEntryAsObject(path);
1271
1272 importAssetCategory(
1273 portletDataContext, assetVocabularyPKs, assetCategoryPKs,
1274 assetCategoryUuids, assetCategoryElement, assetCategory);
1275 }
1276
1277 Element assetsElement = rootElement.element("assets");
1278
1279 List<Element> assetElements = assetsElement.elements("asset");
1280
1281 for (Element assetElement : assetElements) {
1282 String className = GetterUtil.getString(
1283 assetElement.attributeValue("class-name"));
1284 long classPK = GetterUtil.getLong(
1285 assetElement.attributeValue("class-pk"));
1286 String[] assetCategoryUuidArray = StringUtil.split(
1287 GetterUtil.getString(
1288 assetElement.attributeValue("category-uuids")));
1289
1290 long[] assetCategoryIds = new long[0];
1291
1292 for (String assetCategoryUuid : assetCategoryUuidArray) {
1293 assetCategoryUuid = MapUtil.getString(
1294 assetCategoryUuids, assetCategoryUuid, assetCategoryUuid);
1295
1296 AssetCategory assetCategory = AssetCategoryUtil.fetchByUUID_G(
1297 assetCategoryUuid, portletDataContext.getScopeGroupId());
1298
1299 if (assetCategory == null) {
1300 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
1301 portletDataContext.getCompanyId());
1302
1303 assetCategory = AssetCategoryUtil.fetchByUUID_G(
1304 assetCategoryUuid, companyGroup.getGroupId());
1305 }
1306
1307 if (assetCategory != null) {
1308 assetCategoryIds = ArrayUtil.append(
1309 assetCategoryIds, assetCategory.getCategoryId());
1310 }
1311 }
1312
1313 portletDataContext.addAssetCategories(
1314 className, classPK, assetCategoryIds);
1315 }
1316 }
1317
1318 protected void readAssetLinks(PortletDataContext portletDataContext)
1319 throws Exception {
1320
1321 String xml = portletDataContext.getZipEntryAsString(
1322 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1323 "/links.xml");
1324
1325 if (xml == null) {
1326 return;
1327 }
1328
1329 Document document = SAXReaderUtil.read(xml);
1330
1331 Element rootElement = document.getRootElement();
1332
1333 List<Element> assetLinkGroupElements = rootElement.elements(
1334 "asset-link-group");
1335
1336 for (Element assetLinkGroupElement : assetLinkGroupElements) {
1337 String sourceUuid = assetLinkGroupElement.attributeValue(
1338 "source-uuid");
1339
1340 AssetEntry sourceAssetEntry = AssetEntryLocalServiceUtil.fetchEntry(
1341 portletDataContext.getScopeGroupId(), sourceUuid);
1342
1343 if (sourceAssetEntry == null) {
1344 sourceAssetEntry = AssetEntryLocalServiceUtil.fetchEntry(
1345 portletDataContext.getCompanyGroupId(), sourceUuid);
1346 }
1347
1348 if (sourceAssetEntry == null) {
1349 if (_log.isWarnEnabled()) {
1350 _log.warn(
1351 "Unable to find asset entry with uuid " + sourceUuid);
1352 }
1353
1354 continue;
1355 }
1356
1357 List<Element> assetLinksElements = assetLinkGroupElement.elements(
1358 "asset-link");
1359
1360 for (Element assetLinkElement : assetLinksElements) {
1361 String path = assetLinkElement.attributeValue("path");
1362
1363 if (!portletDataContext.isPathNotProcessed(path)) {
1364 continue;
1365 }
1366
1367 String targetUuid = assetLinkElement.attributeValue(
1368 "target-uuid");
1369
1370 AssetEntry targetAssetEntry =
1371 AssetEntryLocalServiceUtil.fetchEntry(
1372 portletDataContext.getScopeGroupId(), targetUuid);
1373
1374 if (targetAssetEntry == null) {
1375 targetAssetEntry = AssetEntryLocalServiceUtil.fetchEntry(
1376 portletDataContext.getCompanyGroupId(), targetUuid);
1377 }
1378
1379 if (targetAssetEntry == null) {
1380 if (_log.isWarnEnabled()) {
1381 _log.warn(
1382 "Unable to find asset entry with uuid " +
1383 targetUuid);
1384 }
1385
1386 continue;
1387 }
1388
1389 AssetLink assetLink =
1390 (AssetLink)portletDataContext.getZipEntryAsObject(path);
1391
1392 long userId = portletDataContext.getUserId(
1393 assetLink.getUserUuid());
1394
1395 AssetLinkLocalServiceUtil.updateLink(
1396 userId, sourceAssetEntry.getEntryId(),
1397 targetAssetEntry.getEntryId(), assetLink.getType(),
1398 assetLink.getWeight());
1399 }
1400 }
1401 }
1402
1403 protected void readAssetTags(PortletDataContext portletDataContext)
1404 throws Exception {
1405
1406 String xml = portletDataContext.getZipEntryAsString(
1407 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1408 "/tags.xml");
1409
1410 if (xml == null) {
1411 return;
1412 }
1413
1414 Document document = SAXReaderUtil.read(xml);
1415
1416 Element rootElement = document.getRootElement();
1417
1418 List<Element> assetTagElements = rootElement.elements("tag");
1419
1420 for (Element assetTagElement : assetTagElements) {
1421 String path = assetTagElement.attributeValue("path");
1422
1423 if (!portletDataContext.isPathNotProcessed(path)) {
1424 continue;
1425 }
1426
1427 AssetTag assetTag =
1428 (AssetTag)portletDataContext.getZipEntryAsObject(path);
1429
1430 Map<Long, Long> assetTagPKs =
1431 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1432 AssetTag.class);
1433
1434 importAssetTag(
1435 portletDataContext, assetTagPKs, assetTagElement, assetTag);
1436 }
1437
1438 List<Element> assetElements = rootElement.elements("asset");
1439
1440 for (Element assetElement : assetElements) {
1441 String className = GetterUtil.getString(
1442 assetElement.attributeValue("class-name"));
1443 long classPK = GetterUtil.getLong(
1444 assetElement.attributeValue("class-pk"));
1445 String assetTagNames = GetterUtil.getString(
1446 assetElement.attributeValue("tags"));
1447
1448 portletDataContext.addAssetTags(
1449 className, classPK, StringUtil.split(assetTagNames));
1450 }
1451 }
1452
1453 protected void readComments(PortletDataContext portletDataContext)
1454 throws Exception {
1455
1456 String xml = portletDataContext.getZipEntryAsString(
1457 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1458 "/comments.xml");
1459
1460 if (xml == null) {
1461 return;
1462 }
1463
1464 Document document = SAXReaderUtil.read(xml);
1465
1466 Element rootElement = document.getRootElement();
1467
1468 List<Element> assetElements = rootElement.elements("asset");
1469
1470 for (Element assetElement : assetElements) {
1471 String path = assetElement.attributeValue("path");
1472 String className = assetElement.attributeValue("class-name");
1473 long classPK = GetterUtil.getLong(
1474 assetElement.attributeValue("class-pk"));
1475
1476 List<String> zipFolderEntries =
1477 portletDataContext.getZipFolderEntries(path);
1478
1479 List<MBMessage> mbMessages = new ArrayList<MBMessage>();
1480
1481 for (String zipFolderEntry : zipFolderEntries) {
1482 MBMessage mbMessage =
1483 (MBMessage)portletDataContext.getZipEntryAsObject(
1484 zipFolderEntry);
1485
1486 if (mbMessage != null) {
1487 mbMessages.add(mbMessage);
1488 }
1489 }
1490
1491 portletDataContext.addComments(className, classPK, mbMessages);
1492 }
1493 }
1494
1495 protected void readExpandoTables(PortletDataContext portletDataContext)
1496 throws Exception {
1497
1498 String xml = portletDataContext.getZipEntryAsString(
1499 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1500 "/expando-tables.xml");
1501
1502 if (xml == null) {
1503 return;
1504 }
1505
1506 Document document = SAXReaderUtil.read(xml);
1507
1508 Element rootElement = document.getRootElement();
1509
1510 List<Element> expandoTableElements = rootElement.elements(
1511 "expando-table");
1512
1513 for (Element expandoTableElement : expandoTableElements) {
1514 String className = expandoTableElement.attributeValue("class-name");
1515
1516 ExpandoTable expandoTable = null;
1517
1518 try {
1519 expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(
1520 portletDataContext.getCompanyId(), className);
1521 }
1522 catch (NoSuchTableException nste) {
1523 expandoTable = ExpandoTableLocalServiceUtil.addDefaultTable(
1524 portletDataContext.getCompanyId(), className);
1525 }
1526
1527 List<Element> expandoColumnElements = expandoTableElement.elements(
1528 "expando-column");
1529
1530 for (Element expandoColumnElement : expandoColumnElements) {
1531 long columnId = GetterUtil.getLong(
1532 expandoColumnElement.attributeValue("column-id"));
1533 String name = expandoColumnElement.attributeValue("name");
1534 int type = GetterUtil.getInteger(
1535 expandoColumnElement.attributeValue("type"));
1536 String defaultData = expandoColumnElement.elementText(
1537 "default-data");
1538 String typeSettings = expandoColumnElement.elementText(
1539 "type-settings");
1540
1541 Serializable defaultDataObject =
1542 ExpandoConverterUtil.getAttributeFromString(
1543 type, defaultData);
1544
1545 ExpandoColumn expandoColumn =
1546 ExpandoColumnLocalServiceUtil.getColumn(
1547 expandoTable.getTableId(), name);
1548
1549 if (expandoColumn != null) {
1550 ExpandoColumnLocalServiceUtil.updateColumn(
1551 expandoColumn.getColumnId(), name, type,
1552 defaultDataObject);
1553 }
1554 else {
1555 expandoColumn = ExpandoColumnLocalServiceUtil.addColumn(
1556 expandoTable.getTableId(), name, type,
1557 defaultDataObject);
1558 }
1559
1560 ExpandoColumnLocalServiceUtil.updateTypeSettings(
1561 expandoColumn.getColumnId(), typeSettings);
1562
1563 portletDataContext.importPermissions(
1564 ExpandoColumn.class, columnId, expandoColumn.getColumnId());
1565 }
1566 }
1567 }
1568
1569 protected void readLocks(PortletDataContext portletDataContext)
1570 throws Exception {
1571
1572 String xml = portletDataContext.getZipEntryAsString(
1573 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1574 "/locks.xml");
1575
1576 if (xml == null) {
1577 return;
1578 }
1579
1580 Document document = SAXReaderUtil.read(xml);
1581
1582 Element rootElement = document.getRootElement();
1583
1584 List<Element> assetElements = rootElement.elements("asset");
1585
1586 for (Element assetElement : assetElements) {
1587 String path = assetElement.attributeValue("path");
1588 String className = assetElement.attributeValue("class-name");
1589 String key = assetElement.attributeValue("key");
1590
1591 Lock lock = (Lock)portletDataContext.getZipEntryAsObject(path);
1592
1593 if (lock != null) {
1594 portletDataContext.addLocks(className, key, lock);
1595 }
1596 }
1597 }
1598
1599 protected void readRatingsEntries(PortletDataContext portletDataContext)
1600 throws Exception {
1601
1602 String xml = portletDataContext.getZipEntryAsString(
1603 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1604 "/ratings.xml");
1605
1606 if (xml == null) {
1607 return;
1608 }
1609
1610 Document document = SAXReaderUtil.read(xml);
1611
1612 Element rootElement = document.getRootElement();
1613
1614 List<Element> assetElements = rootElement.elements("asset");
1615
1616 for (Element assetElement : assetElements) {
1617 String path = assetElement.attributeValue("path");
1618 String className = assetElement.attributeValue("class-name");
1619 long classPK = GetterUtil.getLong(
1620 assetElement.attributeValue("class-pk"));
1621
1622 List<String> zipFolderEntries =
1623 portletDataContext.getZipFolderEntries(path);
1624
1625 List<RatingsEntry> ratingsEntries = new ArrayList<RatingsEntry>();
1626
1627 for (String zipFolderEntry : zipFolderEntries) {
1628 RatingsEntry ratingsEntry =
1629 (RatingsEntry)portletDataContext.getZipEntryAsObject(
1630 zipFolderEntry);
1631
1632 if (ratingsEntry != null) {
1633 ratingsEntries.add(ratingsEntry);
1634 }
1635 }
1636
1637 portletDataContext.addRatingsEntries(
1638 className, classPK, ratingsEntries);
1639 }
1640 }
1641
1642 protected void readXML(PortletDataContext portletDataContext)
1643 throws Exception {
1644
1645 if ((_rootElement != null) && (_headerElement != null)) {
1646 return;
1647 }
1648
1649 String xml = portletDataContext.getZipEntryAsString("/manifest.xml");
1650
1651 if (xml == null) {
1652 throw new LARFileException("manifest.xml not found in the LAR");
1653 }
1654
1655 try {
1656 Document document = SAXReaderUtil.read(xml);
1657
1658 _rootElement = document.getRootElement();
1659
1660 portletDataContext.setImportDataRootElement(_rootElement);
1661 }
1662 catch (Exception e) {
1663 throw new LARFileException(e);
1664 }
1665
1666 _headerElement = _rootElement.element("header");
1667 }
1668
1669 protected void resetPortletScope(
1670 PortletDataContext portletDataContext, long groupId) {
1671
1672 portletDataContext.setScopeGroupId(groupId);
1673 portletDataContext.setScopeLayoutUuid(StringPool.BLANK);
1674 portletDataContext.setScopeType(StringPool.BLANK);
1675 }
1676
1677 protected void setPortletScope(
1678 PortletDataContext portletDataContext, Element portletElement) {
1679
1680 if (ExportImportThreadLocal.isPortletImportInProcess()) {
1681 return;
1682 }
1683
1684
1685
1686 String scopeLayoutUuid = GetterUtil.getString(
1687 portletElement.attributeValue("scope-layout-uuid"));
1688 String scopeLayoutType = GetterUtil.getString(
1689 portletElement.attributeValue("scope-layout-type"));
1690
1691 portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1692 portletDataContext.setScopeType(scopeLayoutType);
1693
1694
1695
1696 try {
1697 Group scopeGroup = null;
1698
1699 if (scopeLayoutType.equals("company")) {
1700 scopeGroup = GroupLocalServiceUtil.getCompanyGroup(
1701 portletDataContext.getCompanyId());
1702 }
1703 else if (Validator.isNotNull(scopeLayoutUuid)) {
1704 boolean privateLayout = GetterUtil.getBoolean(
1705 portletElement.attributeValue("private-layout"));
1706
1707 Layout scopeLayout =
1708 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1709 scopeLayoutUuid, portletDataContext.getGroupId(),
1710 privateLayout);
1711
1712 if (scopeLayout.hasScopeGroup()) {
1713 scopeGroup = scopeLayout.getScopeGroup();
1714 }
1715 else {
1716 String name = String.valueOf(scopeLayout.getPlid());
1717
1718 scopeGroup = GroupLocalServiceUtil.addGroup(
1719 portletDataContext.getUserId(null),
1720 GroupConstants.DEFAULT_PARENT_GROUP_ID,
1721 Layout.class.getName(), scopeLayout.getPlid(),
1722 GroupConstants.DEFAULT_LIVE_GROUP_ID, name, null, 0,
1723 true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
1724 null, false, true, null);
1725 }
1726
1727 Group group = scopeLayout.getGroup();
1728
1729 if (group.isStaged() && !group.isStagedRemotely()) {
1730 try {
1731 Layout oldLayout =
1732 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1733 scopeLayoutUuid,
1734 portletDataContext.getSourceGroupId(),
1735 privateLayout);
1736
1737 Group oldScopeGroup = oldLayout.getScopeGroup();
1738
1739 oldScopeGroup.setLiveGroupId(scopeGroup.getGroupId());
1740
1741 GroupLocalServiceUtil.updateGroup(oldScopeGroup);
1742 }
1743 catch (NoSuchLayoutException nsle) {
1744 if (_log.isWarnEnabled()) {
1745 _log.warn(nsle);
1746 }
1747 }
1748 }
1749 }
1750
1751 if (scopeGroup != null) {
1752 portletDataContext.setScopeGroupId(scopeGroup.getGroupId());
1753 }
1754 }
1755 catch (PortalException pe) {
1756 }
1757 catch (Exception e) {
1758 _log.error(e, e);
1759 }
1760 }
1761
1762 protected void updatePortletPreferences(
1763 PortletDataContext portletDataContext, long ownerId, int ownerType,
1764 long plid, String portletId, String xml, boolean importData)
1765 throws Exception {
1766
1767 Portlet portlet = PortletLocalServiceUtil.getPortletById(
1768 portletDataContext.getCompanyId(), portletId);
1769
1770 if (portlet == null) {
1771 if (_log.isDebugEnabled()) {
1772 _log.debug(
1773 "Do not update portlet preferences for " + portletId +
1774 " because the portlet does not exist");
1775 }
1776
1777 return;
1778 }
1779
1780 PortletDataHandler portletDataHandler =
1781 portlet.getPortletDataHandlerInstance();
1782
1783 if (importData ||
1784 (portletDataHandler.isDisplayPortlet() &&
1785 !MergeLayoutPrototypesThreadLocal.isInProgress())) {
1786
1787 PortletPreferencesLocalServiceUtil.updatePreferences(
1788 ownerId, ownerType, plid, portletId, xml);
1789
1790 return;
1791 }
1792
1793
1794
1795 String[] dataPortletPreferences =
1796 portletDataHandler.getDataPortletPreferences();
1797
1798
1799
1800 javax.portlet.PortletPreferences portletPreferences =
1801 PortletPreferencesLocalServiceUtil.getPreferences(
1802 portletDataContext.getCompanyId(), ownerId, ownerType, plid,
1803 portletId);
1804
1805
1806
1807 javax.portlet.PortletPreferences jxPortletPreferences =
1808 PortletPreferencesFactoryUtil.fromXML(
1809 portletDataContext.getCompanyId(), ownerId, ownerType, plid,
1810 portletId, xml);
1811
1812 Enumeration<String> enu = jxPortletPreferences.getNames();
1813
1814 while (enu.hasMoreElements()) {
1815 String name = enu.nextElement();
1816
1817 String scopeLayoutUuid = portletDataContext.getScopeLayoutUuid();
1818 String scopeType = portletDataContext.getScopeType();
1819
1820 if (!ArrayUtil.contains(dataPortletPreferences, name) ||
1821 (Validator.isNull(scopeLayoutUuid) &&
1822 scopeType.equals("company"))) {
1823
1824 String[] values = jxPortletPreferences.getValues(name, null);
1825
1826 portletPreferences.setValues(name, values);
1827 }
1828 }
1829
1830 PortletPreferencesLocalServiceUtil.updatePreferences(
1831 ownerId, ownerType, plid, portletId, portletPreferences);
1832 }
1833
1834 protected void validateFile(
1835 PortletDataContext portletDataContext, String portletId)
1836 throws Exception {
1837
1838
1839
1840 readXML(portletDataContext);
1841
1842 int buildNumber = ReleaseInfo.getBuildNumber();
1843
1844 int importBuildNumber = GetterUtil.getInteger(
1845 _headerElement.attributeValue("build-number"));
1846
1847 if (buildNumber != importBuildNumber) {
1848 throw new LayoutImportException(
1849 "LAR build number " + importBuildNumber + " does not match " +
1850 "portal build number " + buildNumber);
1851 }
1852
1853
1854
1855 String larType = _headerElement.attributeValue("type");
1856
1857 if (!larType.equals("portlet")) {
1858 throw new LARTypeException(larType);
1859 }
1860
1861
1862
1863 String rootPortletId = _headerElement.attributeValue("root-portlet-id");
1864
1865 if (!PortletConstants.getRootPortletId(portletId).equals(
1866 rootPortletId)) {
1867
1868 throw new PortletIdException("Invalid portlet id " + rootPortletId);
1869 }
1870
1871
1872
1873 Portlet portlet = PortletLocalServiceUtil.getPortletById(
1874 portletDataContext.getCompanyId(), portletId);
1875
1876 PortletDataHandler portletDataHandler =
1877 portlet.getPortletDataHandlerInstance();
1878
1879 if (portletDataHandler.isDataLocalized()) {
1880 Locale[] sourceAvailableLocales = LocaleUtil.fromLanguageIds(
1881 StringUtil.split(
1882 _headerElement.attributeValue("available-locales")));
1883
1884 Locale[] targetAvailableLocales = LanguageUtil.getAvailableLocales(
1885 PortalUtil.getSiteGroupId(
1886 portletDataContext.getScopeGroupId()));
1887
1888 for (Locale sourceAvailableLocale : sourceAvailableLocales) {
1889 if (!ArrayUtil.contains(
1890 targetAvailableLocales, sourceAvailableLocale)) {
1891
1892 LocaleException le = new LocaleException(
1893 LocaleException.TYPE_EXPORT_IMPORT,
1894 "Locale " + sourceAvailableLocale + " is not " +
1895 "available in company " +
1896 portletDataContext.getCompanyId());
1897
1898 le.setSourceAvailableLocales(sourceAvailableLocales);
1899 le.setTargetAvailableLocales(targetAvailableLocales);
1900
1901 throw le;
1902 }
1903 }
1904 }
1905 }
1906
1907 private static Log _log = LogFactoryUtil.getLog(PortletImporter.class);
1908
1909 private DeletionSystemEventImporter _deletionSystemEventImporter =
1910 new DeletionSystemEventImporter();
1911 private Element _headerElement;
1912 private PermissionImporter _permissionImporter = new PermissionImporter();
1913 private Element _rootElement;
1914
1915 }