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