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