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