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 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
1108 plid = PortletKeys.PREFS_PLID_SHARED;
1109 ownerId = portletDataContext.getScopeGroupId();
1110 portletId = PortletConstants.getRootPortletId(portletId);
1111 }
1112
1113 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1114 portletId = PortletConstants.getRootPortletId(portletId);
1115
1116 String userUuid = element.attributeValue(
1117 "archive-user-uuid");
1118 String name = element.attributeValue("archive-name");
1119
1120 long userId = portletDataContext.getUserId(userUuid);
1121
1122 PortletItem portletItem =
1123 PortletItemLocalServiceUtil.updatePortletItem(
1124 userId, groupId, name, portletId,
1125 PortletPreferences.class.getName());
1126
1127 plid = LayoutConstants.DEFAULT_PLID;
1128 ownerId = portletItem.getPortletItemId();
1129 }
1130
1131 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1132 String userUuid = element.attributeValue("user-uuid");
1133
1134 ownerId = portletDataContext.getUserId(userUuid);
1135 }
1136
1137 boolean defaultUser = GetterUtil.getBoolean(
1138 element.attributeValue("default-user"));
1139
1140 if (defaultUser) {
1141 ownerId = UserLocalServiceUtil.getDefaultUserId(companyId);
1142 }
1143
1144 javax.portlet.PortletPreferences jxPortletPreferences =
1145 PortletPreferencesFactoryUtil.fromXML(
1146 companyId, ownerId, ownerType, plid, portletId, xml);
1147
1148 Element importDataRootElement =
1149 portletDataContext.getImportDataRootElement();
1150
1151 try {
1152 Element preferenceDataElement =
1153 portletPreferencesElement.element("preference-data");
1154
1155 if (preferenceDataElement != null) {
1156 portletDataContext.setImportDataRootElement(
1157 preferenceDataElement);
1158 }
1159
1160 Portlet portlet = PortletLocalServiceUtil.getPortletById(
1161 portletDataContext.getCompanyId(), portletId);
1162
1163 PortletDataHandler portletDataHandler =
1164 portlet.getPortletDataHandlerInstance();
1165
1166 jxPortletPreferences =
1167 portletDataHandler.processImportPortletPreferences(
1168 portletDataContext, portletId,
1169 jxPortletPreferences);
1170 }
1171 finally {
1172 portletDataContext.setImportDataRootElement(
1173 importDataRootElement);
1174 }
1175
1176 updatePortletPreferences(
1177 portletDataContext, ownerId, ownerType, plid, portletId,
1178 PortletPreferencesFactoryUtil.toXML(jxPortletPreferences),
1179 importPortletData);
1180 }
1181 }
1182
1183 if (preserveScopeLayoutId && (layout != null)) {
1184 javax.portlet.PortletPreferences jxPortletPreferences =
1185 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
1186 layout, portletId);
1187
1188 try {
1189 jxPortletPreferences.setValue(
1190 "portletSetupTitle_" + languageId, portletSetupTitle);
1191 jxPortletPreferences.setValue("lfrScopeType", scopeType);
1192 jxPortletPreferences.setValue(
1193 "lfrScopeLayoutUuid", scopeLayoutUuid);
1194
1195 jxPortletPreferences.store();
1196 }
1197 finally {
1198 portletDataContext.setScopeType(scopeType);
1199 portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1200 }
1201 }
1202 }
1203
1204 protected void readAssetCategories(PortletDataContext portletDataContext)
1205 throws Exception {
1206
1207 String xml = portletDataContext.getZipEntryAsString(
1208 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1209 "/categories-hierarchy.xml");
1210
1211 if (xml == null) {
1212 return;
1213 }
1214
1215 Document document = SAXReaderUtil.read(xml);
1216
1217 Element rootElement = document.getRootElement();
1218
1219 Element assetVocabulariesElement = rootElement.element("vocabularies");
1220
1221 List<Element> assetVocabularyElements =
1222 assetVocabulariesElement.elements("vocabulary");
1223
1224 Map<Long, Long> assetVocabularyPKs =
1225 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1226 AssetVocabulary.class);
1227
1228 for (Element assetVocabularyElement : assetVocabularyElements) {
1229 String path = assetVocabularyElement.attributeValue("path");
1230
1231 if (!portletDataContext.isPathNotProcessed(path)) {
1232 continue;
1233 }
1234
1235 AssetVocabulary assetVocabulary =
1236 (AssetVocabulary)portletDataContext.getZipEntryAsObject(path);
1237
1238 importAssetVocabulary(
1239 portletDataContext, assetVocabularyPKs, assetVocabularyElement,
1240 assetVocabulary);
1241 }
1242
1243 Element assetCategoriesElement = rootElement.element("categories");
1244
1245 List<Element> assetCategoryElements = assetCategoriesElement.elements(
1246 "category");
1247
1248 Map<Long, Long> assetCategoryPKs =
1249 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1250 AssetCategory.class);
1251
1252 Map<String, String> assetCategoryUuids =
1253 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
1254 AssetCategory.class + ".uuid");
1255
1256 for (Element assetCategoryElement : assetCategoryElements) {
1257 String path = assetCategoryElement.attributeValue("path");
1258
1259 if (!portletDataContext.isPathNotProcessed(path)) {
1260 continue;
1261 }
1262
1263 AssetCategory assetCategory =
1264 (AssetCategory)portletDataContext.getZipEntryAsObject(path);
1265
1266 importAssetCategory(
1267 portletDataContext, assetVocabularyPKs, assetCategoryPKs,
1268 assetCategoryUuids, assetCategoryElement, assetCategory);
1269 }
1270
1271 Element assetsElement = rootElement.element("assets");
1272
1273 List<Element> assetElements = assetsElement.elements("asset");
1274
1275 for (Element assetElement : assetElements) {
1276 String className = GetterUtil.getString(
1277 assetElement.attributeValue("class-name"));
1278 long classPK = GetterUtil.getLong(
1279 assetElement.attributeValue("class-pk"));
1280 String[] assetCategoryUuidArray = StringUtil.split(
1281 GetterUtil.getString(
1282 assetElement.attributeValue("category-uuids")));
1283
1284 long[] assetCategoryIds = new long[0];
1285
1286 for (String assetCategoryUuid : assetCategoryUuidArray) {
1287 assetCategoryUuid = MapUtil.getString(
1288 assetCategoryUuids, assetCategoryUuid, assetCategoryUuid);
1289
1290 AssetCategory assetCategory = AssetCategoryUtil.fetchByUUID_G(
1291 assetCategoryUuid, portletDataContext.getScopeGroupId());
1292
1293 if (assetCategory == null) {
1294 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
1295 portletDataContext.getCompanyId());
1296
1297 assetCategory = AssetCategoryUtil.fetchByUUID_G(
1298 assetCategoryUuid, companyGroup.getGroupId());
1299 }
1300
1301 if (assetCategory != null) {
1302 assetCategoryIds = ArrayUtil.append(
1303 assetCategoryIds, assetCategory.getCategoryId());
1304 }
1305 }
1306
1307 portletDataContext.addAssetCategories(
1308 className, classPK, assetCategoryIds);
1309 }
1310 }
1311
1312 protected void readAssetLinks(PortletDataContext portletDataContext)
1313 throws Exception {
1314
1315 String xml = portletDataContext.getZipEntryAsString(
1316 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1317 "/links.xml");
1318
1319 if (xml == null) {
1320 return;
1321 }
1322
1323 Document document = SAXReaderUtil.read(xml);
1324
1325 Element rootElement = document.getRootElement();
1326
1327 List<Element> assetLinkGroupElements = rootElement.elements(
1328 "asset-link-group");
1329
1330 for (Element assetLinkGroupElement : assetLinkGroupElements) {
1331 String sourceUuid = assetLinkGroupElement.attributeValue(
1332 "source-uuid");
1333
1334 AssetEntry sourceAssetEntry = AssetEntryLocalServiceUtil.fetchEntry(
1335 portletDataContext.getScopeGroupId(), sourceUuid);
1336
1337 if (sourceAssetEntry == null) {
1338 sourceAssetEntry = AssetEntryLocalServiceUtil.fetchEntry(
1339 portletDataContext.getCompanyGroupId(), sourceUuid);
1340 }
1341
1342 if (sourceAssetEntry == null) {
1343 if (_log.isWarnEnabled()) {
1344 _log.warn(
1345 "Unable to find asset entry with uuid " + sourceUuid);
1346 }
1347
1348 continue;
1349 }
1350
1351 List<Element> assetLinksElements = assetLinkGroupElement.elements(
1352 "asset-link");
1353
1354 for (Element assetLinkElement : assetLinksElements) {
1355 String path = assetLinkElement.attributeValue("path");
1356
1357 if (!portletDataContext.isPathNotProcessed(path)) {
1358 continue;
1359 }
1360
1361 String targetUuid = assetLinkElement.attributeValue(
1362 "target-uuid");
1363
1364 AssetEntry targetAssetEntry =
1365 AssetEntryLocalServiceUtil.fetchEntry(
1366 portletDataContext.getScopeGroupId(), targetUuid);
1367
1368 if (targetAssetEntry == null) {
1369 targetAssetEntry = AssetEntryLocalServiceUtil.fetchEntry(
1370 portletDataContext.getCompanyGroupId(), targetUuid);
1371 }
1372
1373 if (targetAssetEntry == null) {
1374 if (_log.isWarnEnabled()) {
1375 _log.warn(
1376 "Unable to find asset entry with uuid " +
1377 targetUuid);
1378 }
1379
1380 continue;
1381 }
1382
1383 AssetLink assetLink =
1384 (AssetLink)portletDataContext.getZipEntryAsObject(path);
1385
1386 long userId = portletDataContext.getUserId(
1387 assetLink.getUserUuid());
1388
1389 AssetLinkLocalServiceUtil.updateLink(
1390 userId, sourceAssetEntry.getEntryId(),
1391 targetAssetEntry.getEntryId(), assetLink.getType(),
1392 assetLink.getWeight());
1393 }
1394 }
1395 }
1396
1397 protected void readAssetTags(PortletDataContext portletDataContext)
1398 throws Exception {
1399
1400 String xml = portletDataContext.getZipEntryAsString(
1401 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1402 "/tags.xml");
1403
1404 if (xml == null) {
1405 return;
1406 }
1407
1408 Document document = SAXReaderUtil.read(xml);
1409
1410 Element rootElement = document.getRootElement();
1411
1412 List<Element> assetTagElements = rootElement.elements("tag");
1413
1414 for (Element assetTagElement : assetTagElements) {
1415 String path = assetTagElement.attributeValue("path");
1416
1417 if (!portletDataContext.isPathNotProcessed(path)) {
1418 continue;
1419 }
1420
1421 AssetTag assetTag =
1422 (AssetTag)portletDataContext.getZipEntryAsObject(path);
1423
1424 Map<Long, Long> assetTagPKs =
1425 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1426 AssetTag.class);
1427
1428 importAssetTag(
1429 portletDataContext, assetTagPKs, assetTagElement, assetTag);
1430 }
1431
1432 List<Element> assetElements = rootElement.elements("asset");
1433
1434 for (Element assetElement : assetElements) {
1435 String className = GetterUtil.getString(
1436 assetElement.attributeValue("class-name"));
1437 long classPK = GetterUtil.getLong(
1438 assetElement.attributeValue("class-pk"));
1439 String assetTagNames = GetterUtil.getString(
1440 assetElement.attributeValue("tags"));
1441
1442 portletDataContext.addAssetTags(
1443 className, classPK, StringUtil.split(assetTagNames));
1444 }
1445 }
1446
1447 protected void readComments(PortletDataContext portletDataContext)
1448 throws Exception {
1449
1450 String xml = portletDataContext.getZipEntryAsString(
1451 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1452 "/comments.xml");
1453
1454 if (xml == null) {
1455 return;
1456 }
1457
1458 Document document = SAXReaderUtil.read(xml);
1459
1460 Element rootElement = document.getRootElement();
1461
1462 List<Element> assetElements = rootElement.elements("asset");
1463
1464 for (Element assetElement : assetElements) {
1465 String path = assetElement.attributeValue("path");
1466 String className = assetElement.attributeValue("class-name");
1467 long classPK = GetterUtil.getLong(
1468 assetElement.attributeValue("class-pk"));
1469
1470 List<String> zipFolderEntries =
1471 portletDataContext.getZipFolderEntries(path);
1472
1473 List<MBMessage> mbMessages = new ArrayList<MBMessage>();
1474
1475 for (String zipFolderEntry : zipFolderEntries) {
1476 MBMessage mbMessage =
1477 (MBMessage)portletDataContext.getZipEntryAsObject(
1478 zipFolderEntry);
1479
1480 if (mbMessage != null) {
1481 mbMessages.add(mbMessage);
1482 }
1483 }
1484
1485 portletDataContext.addComments(className, classPK, mbMessages);
1486 }
1487 }
1488
1489 protected void readExpandoTables(PortletDataContext portletDataContext)
1490 throws Exception {
1491
1492 String xml = portletDataContext.getZipEntryAsString(
1493 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1494 "/expando-tables.xml");
1495
1496 if (xml == null) {
1497 return;
1498 }
1499
1500 Document document = SAXReaderUtil.read(xml);
1501
1502 Element rootElement = document.getRootElement();
1503
1504 List<Element> expandoTableElements = rootElement.elements(
1505 "expando-table");
1506
1507 for (Element expandoTableElement : expandoTableElements) {
1508 String className = expandoTableElement.attributeValue("class-name");
1509
1510 ExpandoTable expandoTable = null;
1511
1512 try {
1513 expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(
1514 portletDataContext.getCompanyId(), className);
1515 }
1516 catch (NoSuchTableException nste) {
1517 expandoTable = ExpandoTableLocalServiceUtil.addDefaultTable(
1518 portletDataContext.getCompanyId(), className);
1519 }
1520
1521 List<Element> expandoColumnElements = expandoTableElement.elements(
1522 "expando-column");
1523
1524 for (Element expandoColumnElement : expandoColumnElements) {
1525 long columnId = GetterUtil.getLong(
1526 expandoColumnElement.attributeValue("column-id"));
1527 String name = expandoColumnElement.attributeValue("name");
1528 int type = GetterUtil.getInteger(
1529 expandoColumnElement.attributeValue("type"));
1530 String defaultData = expandoColumnElement.elementText(
1531 "default-data");
1532 String typeSettings = expandoColumnElement.elementText(
1533 "type-settings");
1534
1535 Serializable defaultDataObject =
1536 ExpandoConverterUtil.getAttributeFromString(
1537 type, defaultData);
1538
1539 ExpandoColumn expandoColumn =
1540 ExpandoColumnLocalServiceUtil.getColumn(
1541 expandoTable.getTableId(), name);
1542
1543 if (expandoColumn != null) {
1544 ExpandoColumnLocalServiceUtil.updateColumn(
1545 expandoColumn.getColumnId(), name, type,
1546 defaultDataObject);
1547 }
1548 else {
1549 expandoColumn = ExpandoColumnLocalServiceUtil.addColumn(
1550 expandoTable.getTableId(), name, type,
1551 defaultDataObject);
1552 }
1553
1554 ExpandoColumnLocalServiceUtil.updateTypeSettings(
1555 expandoColumn.getColumnId(), typeSettings);
1556
1557 portletDataContext.importPermissions(
1558 ExpandoColumn.class, columnId, expandoColumn.getColumnId());
1559 }
1560 }
1561 }
1562
1563 protected void readLocks(PortletDataContext portletDataContext)
1564 throws Exception {
1565
1566 String xml = portletDataContext.getZipEntryAsString(
1567 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1568 "/locks.xml");
1569
1570 if (xml == null) {
1571 return;
1572 }
1573
1574 Document document = SAXReaderUtil.read(xml);
1575
1576 Element rootElement = document.getRootElement();
1577
1578 List<Element> assetElements = rootElement.elements("asset");
1579
1580 for (Element assetElement : assetElements) {
1581 String path = assetElement.attributeValue("path");
1582 String className = assetElement.attributeValue("class-name");
1583 String key = assetElement.attributeValue("key");
1584
1585 Lock lock = (Lock)portletDataContext.getZipEntryAsObject(path);
1586
1587 if (lock != null) {
1588 portletDataContext.addLocks(className, key, lock);
1589 }
1590 }
1591 }
1592
1593 protected void readRatingsEntries(PortletDataContext portletDataContext)
1594 throws Exception {
1595
1596 String xml = portletDataContext.getZipEntryAsString(
1597 ExportImportPathUtil.getSourceRootPath(portletDataContext) +
1598 "/ratings.xml");
1599
1600 if (xml == null) {
1601 return;
1602 }
1603
1604 Document document = SAXReaderUtil.read(xml);
1605
1606 Element rootElement = document.getRootElement();
1607
1608 List<Element> assetElements = rootElement.elements("asset");
1609
1610 for (Element assetElement : assetElements) {
1611 String path = assetElement.attributeValue("path");
1612 String className = assetElement.attributeValue("class-name");
1613 long classPK = GetterUtil.getLong(
1614 assetElement.attributeValue("class-pk"));
1615
1616 List<String> zipFolderEntries =
1617 portletDataContext.getZipFolderEntries(path);
1618
1619 List<RatingsEntry> ratingsEntries = new ArrayList<RatingsEntry>();
1620
1621 for (String zipFolderEntry : zipFolderEntries) {
1622 RatingsEntry ratingsEntry =
1623 (RatingsEntry)portletDataContext.getZipEntryAsObject(
1624 zipFolderEntry);
1625
1626 if (ratingsEntry != null) {
1627 ratingsEntries.add(ratingsEntry);
1628 }
1629 }
1630
1631 portletDataContext.addRatingsEntries(
1632 className, classPK, ratingsEntries);
1633 }
1634 }
1635
1636 protected void readXML(PortletDataContext portletDataContext)
1637 throws Exception {
1638
1639 if ((_rootElement != null) && (_headerElement != null)) {
1640 return;
1641 }
1642
1643 String xml = portletDataContext.getZipEntryAsString("/manifest.xml");
1644
1645 if (xml == null) {
1646 throw new LARFileException("manifest.xml not found in the LAR");
1647 }
1648
1649 try {
1650 Document document = SAXReaderUtil.read(xml);
1651
1652 _rootElement = document.getRootElement();
1653
1654 portletDataContext.setImportDataRootElement(_rootElement);
1655 }
1656 catch (Exception e) {
1657 throw new LARFileException(e);
1658 }
1659
1660 _headerElement = _rootElement.element("header");
1661 }
1662
1663 protected void resetPortletScope(
1664 PortletDataContext portletDataContext, long groupId) {
1665
1666 portletDataContext.setScopeGroupId(groupId);
1667 portletDataContext.setScopeLayoutUuid(StringPool.BLANK);
1668 portletDataContext.setScopeType(StringPool.BLANK);
1669 }
1670
1671 protected void setPortletScope(
1672 PortletDataContext portletDataContext, Element portletElement) {
1673
1674 if (ExportImportThreadLocal.isPortletImportInProcess()) {
1675 return;
1676 }
1677
1678
1679
1680 String scopeLayoutUuid = GetterUtil.getString(
1681 portletElement.attributeValue("scope-layout-uuid"));
1682 String scopeLayoutType = GetterUtil.getString(
1683 portletElement.attributeValue("scope-layout-type"));
1684
1685 portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1686 portletDataContext.setScopeType(scopeLayoutType);
1687
1688
1689
1690 try {
1691 Group scopeGroup = null;
1692
1693 if (scopeLayoutType.equals("company")) {
1694 scopeGroup = GroupLocalServiceUtil.getCompanyGroup(
1695 portletDataContext.getCompanyId());
1696 }
1697 else if (Validator.isNotNull(scopeLayoutUuid)) {
1698 boolean privateLayout = GetterUtil.getBoolean(
1699 portletElement.attributeValue("private-layout"));
1700
1701 Layout scopeLayout =
1702 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1703 scopeLayoutUuid, portletDataContext.getGroupId(),
1704 privateLayout);
1705
1706 if (scopeLayout.hasScopeGroup()) {
1707 scopeGroup = scopeLayout.getScopeGroup();
1708 }
1709 else {
1710 String name = String.valueOf(scopeLayout.getPlid());
1711
1712 scopeGroup = GroupLocalServiceUtil.addGroup(
1713 portletDataContext.getUserId(null),
1714 GroupConstants.DEFAULT_PARENT_GROUP_ID,
1715 Layout.class.getName(), scopeLayout.getPlid(),
1716 GroupConstants.DEFAULT_LIVE_GROUP_ID, name, null, 0,
1717 true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
1718 null, false, true, null);
1719 }
1720
1721 Group group = scopeLayout.getGroup();
1722
1723 if (group.isStaged() && !group.isStagedRemotely()) {
1724 try {
1725 Layout oldLayout =
1726 LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1727 scopeLayoutUuid,
1728 portletDataContext.getSourceGroupId(),
1729 privateLayout);
1730
1731 Group oldScopeGroup = oldLayout.getScopeGroup();
1732
1733 oldScopeGroup.setLiveGroupId(scopeGroup.getGroupId());
1734
1735 GroupLocalServiceUtil.updateGroup(oldScopeGroup);
1736 }
1737 catch (NoSuchLayoutException nsle) {
1738 if (_log.isWarnEnabled()) {
1739 _log.warn(nsle);
1740 }
1741 }
1742 }
1743 }
1744
1745 if (scopeGroup != null) {
1746 portletDataContext.setScopeGroupId(scopeGroup.getGroupId());
1747 }
1748 }
1749 catch (PortalException pe) {
1750 }
1751 catch (Exception e) {
1752 _log.error(e, e);
1753 }
1754 }
1755
1756 protected void updatePortletPreferences(
1757 PortletDataContext portletDataContext, long ownerId, int ownerType,
1758 long plid, String portletId, String xml, boolean importData)
1759 throws Exception {
1760
1761 Portlet portlet = PortletLocalServiceUtil.getPortletById(
1762 portletDataContext.getCompanyId(), portletId);
1763
1764 if (portlet == null) {
1765 if (_log.isDebugEnabled()) {
1766 _log.debug(
1767 "Do not update portlet preferences for " + portletId +
1768 " because the portlet does not exist");
1769 }
1770
1771 return;
1772 }
1773
1774 PortletDataHandler portletDataHandler =
1775 portlet.getPortletDataHandlerInstance();
1776
1777 if (importData ||
1778 (portletDataHandler.isDisplayPortlet() &&
1779 !MergeLayoutPrototypesThreadLocal.isInProgress())) {
1780
1781 PortletPreferencesLocalServiceUtil.updatePreferences(
1782 ownerId, ownerType, plid, portletId, xml);
1783
1784 return;
1785 }
1786
1787
1788
1789 String[] dataPortletPreferences =
1790 portletDataHandler.getDataPortletPreferences();
1791
1792
1793
1794 javax.portlet.PortletPreferences portletPreferences =
1795 PortletPreferencesLocalServiceUtil.getPreferences(
1796 portletDataContext.getCompanyId(), ownerId, ownerType, plid,
1797 portletId);
1798
1799
1800
1801 javax.portlet.PortletPreferences jxPortletPreferences =
1802 PortletPreferencesFactoryUtil.fromXML(
1803 portletDataContext.getCompanyId(), ownerId, ownerType, plid,
1804 portletId, xml);
1805
1806 Enumeration<String> enu = jxPortletPreferences.getNames();
1807
1808 while (enu.hasMoreElements()) {
1809 String name = enu.nextElement();
1810
1811 String scopeLayoutUuid = portletDataContext.getScopeLayoutUuid();
1812 String scopeType = portletDataContext.getScopeType();
1813
1814 if (!ArrayUtil.contains(dataPortletPreferences, name) ||
1815 (Validator.isNull(scopeLayoutUuid) &&
1816 scopeType.equals("company"))) {
1817
1818 String[] values = jxPortletPreferences.getValues(name, null);
1819
1820 portletPreferences.setValues(name, values);
1821 }
1822 }
1823
1824 PortletPreferencesLocalServiceUtil.updatePreferences(
1825 ownerId, ownerType, plid, portletId, portletPreferences);
1826 }
1827
1828 protected void validateFile(
1829 PortletDataContext portletDataContext, String portletId)
1830 throws Exception {
1831
1832
1833
1834 readXML(portletDataContext);
1835
1836 int buildNumber = ReleaseInfo.getBuildNumber();
1837
1838 int importBuildNumber = GetterUtil.getInteger(
1839 _headerElement.attributeValue("build-number"));
1840
1841 if (buildNumber != importBuildNumber) {
1842 throw new LayoutImportException(
1843 "LAR build number " + importBuildNumber + " does not match " +
1844 "portal build number " + buildNumber);
1845 }
1846
1847
1848
1849 String larType = _headerElement.attributeValue("type");
1850
1851 if (!larType.equals("portlet")) {
1852 throw new LARTypeException(larType);
1853 }
1854
1855
1856
1857 String rootPortletId = _headerElement.attributeValue("root-portlet-id");
1858
1859 if (!PortletConstants.getRootPortletId(portletId).equals(
1860 rootPortletId)) {
1861
1862 throw new PortletIdException("Invalid portlet id " + rootPortletId);
1863 }
1864
1865
1866
1867 Portlet portlet = PortletLocalServiceUtil.getPortletById(
1868 portletDataContext.getCompanyId(), portletId);
1869
1870 PortletDataHandler portletDataHandler =
1871 portlet.getPortletDataHandlerInstance();
1872
1873 if (portletDataHandler.isDataLocalized()) {
1874 Locale[] sourceAvailableLocales = LocaleUtil.fromLanguageIds(
1875 StringUtil.split(
1876 _headerElement.attributeValue("available-locales")));
1877
1878 Locale[] targetAvailableLocales = LanguageUtil.getAvailableLocales(
1879 PortalUtil.getSiteGroupId(
1880 portletDataContext.getScopeGroupId()));
1881
1882 for (Locale sourceAvailableLocale : sourceAvailableLocales) {
1883 if (!ArrayUtil.contains(
1884 targetAvailableLocales, sourceAvailableLocale)) {
1885
1886 LocaleException le = new LocaleException(
1887 LocaleException.TYPE_EXPORT_IMPORT,
1888 "Locale " + sourceAvailableLocale + " is not " +
1889 "available in company " +
1890 portletDataContext.getCompanyId());
1891
1892 le.setSourceAvailableLocales(sourceAvailableLocales);
1893 le.setTargetAvailableLocales(targetAvailableLocales);
1894
1895 throw le;
1896 }
1897 }
1898 }
1899 }
1900
1901 private static Log _log = LogFactoryUtil.getLog(PortletImporter.class);
1902
1903 private DeletionSystemEventImporter _deletionSystemEventImporter =
1904 new DeletionSystemEventImporter();
1905 private Element _headerElement;
1906 private PermissionImporter _permissionImporter = new PermissionImporter();
1907 private Element _rootElement;
1908
1909 }