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