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