001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
123     * @author Brian Wing Shun Chan
124     * @author Joel Kozikowski
125     * @author Charles May
126     * @author Raymond Augé
127     * @author Jorge Ferrer
128     * @author Bruno Farache
129     * @author Zsigmond Rab
130     * @author Douglas Wong
131     * @author Mate Thurzo
132     */
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                    // Manifest
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                    // Build compatibility
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                    // Type compatibility
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                    // Portlet compatibility
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                    // Available locales
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                    // Import group id
368    
369                    long sourceGroupId = GetterUtil.getLong(
370                            headerElement.attributeValue("group-id"));
371    
372                    portletDataContext.setSourceGroupId(sourceGroupId);
373    
374                    // Read asset categories, asset tags, comments, locks, and ratings
375                    // entries to make them available to the data handlers through the
376                    // context
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                    // Delete portlet data
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                            // Portlet preferences
426    
427                            importPortletPreferences(
428                                    portletDataContext, layout.getCompanyId(), groupId, layout,
429                                    portletId, portletElement, importPortletSetup,
430                                    importPortletArchivedSetups, importPortletUserPreferences, true,
431                                    importData);
432    
433                            // Portlet data
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                    // Portlet permissions
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                    // Asset links
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            /**
485             * @see com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl#getFileEntryTypeName(
486             *      String, long, String, int)
487             * @see com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl#getFolderName(
488             *      String, long, long, String, int)
489             */
490            protected String getAssetCategoryName(
491                            String uuid, long groupId, long parentCategoryId, String name,
492                            int count)
493                    throws Exception {
494    
495                    AssetCategory assetCategory = null;
496    
497                    try {
498                            assetCategory = AssetCategoryUtil.findByG_P_N_First(
499                                    groupId, parentCategoryId, name, 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, ++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            /**
545             * @see com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl#getFileEntryTypeName(
546             *      String, long, String, int)
547             * @see com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl#getFolderName(
548             *      String, long, long, String, int)
549             */
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(), 2);
690    
691                                    serviceContext.setUuid(assetCategory.getUuid());
692    
693                                    if (global) {
694                                            if (AssetPermission.contains(
695                                                            PermissionThreadLocal.getPermissionChecker(),
696                                                            groupId, ActionKeys.ADD_CATEGORY)) {
697    
698                                                    serviceContext.setScopeGroupId(groupId);
699                                            }
700                                            else {
701                                                    StringBundler sb = new StringBundler(6);
702    
703                                                    sb.append("Category ");
704                                                    sb.append(assetCategory.getName());
705                                                    sb.append(" could not be imported to the Global ");
706                                                    sb.append("scope because the user does not have ");
707                                                    sb.append("permissions. It will be imported into the ");
708                                                    sb.append("current scope.");
709    
710                                                    _log.error(sb.toString());
711                                            }
712                                    }
713    
714                                    importedAssetCategory =
715                                            AssetCategoryLocalServiceUtil.addCategory(
716                                                    userId, parentAssetCategoryId,
717                                                    getAssetCategoryTitleMap(assetCategory, name),
718                                                    assetCategory.getDescriptionMap(), assetVocabularyId,
719                                                    properties, serviceContext);
720                            }
721                            else {
722                                    String name = getAssetCategoryName(
723                                            assetCategory.getUuid(), assetCategory.getGroupId(),
724                                            parentAssetCategoryId, assetCategory.getName(), 2);
725    
726                                    boolean updateAssetCategory = true;
727    
728                                    if (global) {
729                                            PermissionChecker permissionChecker =
730                                                    PermissionThreadLocal.getPermissionChecker();
731    
732                                            if (permissionChecker.hasPermission(
733                                                            groupId, AssetCategory.class.getName(),
734                                                            existingAssetCategory.getCategoryId(),
735                                                            ActionKeys.UPDATE)) {
736    
737                                                    serviceContext.setScopeGroupId(groupId);
738                                            }
739                                            else {
740                                                    updateAssetCategory = false;
741                                            }
742                                    }
743    
744                                    if (updateAssetCategory) {
745                                            importedAssetCategory =
746                                                    AssetCategoryLocalServiceUtil.updateCategory(
747                                                            userId, existingAssetCategory.getCategoryId(),
748                                                            parentAssetCategoryId,
749                                                            getAssetCategoryTitleMap(assetCategory, name),
750                                                            assetCategory.getDescriptionMap(),
751                                                            assetVocabularyId, properties, serviceContext);
752                                    }
753                                    else {
754                                            StringBundler sb = new StringBundler(4);
755    
756                                            sb.append("Category ");
757                                            sb.append(existingAssetCategory.getName());
758                                            sb.append(" could not be updated in the Global scope ");
759                                            sb.append("because the user does not have permissions.");
760    
761                                            _log.error(sb.toString());
762    
763                                            return;
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                                    return;
969                            }
970                    }
971    
972                    assetVocabularyPKs.put(
973                            assetVocabulary.getVocabularyId(),
974                            importedAssetVocabulary.getVocabularyId());
975    
976                    portletDataContext.importPermissions(
977                            AssetVocabulary.class, assetVocabulary.getVocabularyId(),
978                            importedAssetVocabulary.getVocabularyId());
979            }
980    
981            protected void importPortletData(
982                            PortletDataContext portletDataContext, String portletId, long plid,
983                            Element portletDataElement)
984                    throws Exception {
985    
986                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
987                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
988    
989                    PortletPreferences portletPreferences =
990                            PortletPreferencesUtil.fetchByO_O_P_P(
991                                    ownerId, ownerType, plid, portletId);
992    
993                    if (portletPreferences == null) {
994                            portletPreferences =
995                                    new com.liferay.portal.model.impl.PortletPreferencesImpl();
996                    }
997    
998                    String xml = importPortletData(
999                            portletDataContext, portletId, portletPreferences,
1000                            portletDataElement);
1001    
1002                    if (Validator.isNotNull(xml)) {
1003                            PortletPreferencesLocalServiceUtil.updatePreferences(
1004                                    ownerId, ownerType, plid, portletId, xml);
1005                    }
1006            }
1007    
1008            protected String importPortletData(
1009                            PortletDataContext portletDataContext, String portletId,
1010                            PortletPreferences portletPreferences, Element portletDataElement)
1011                    throws Exception {
1012    
1013                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
1014                            portletDataContext.getCompanyId(), portletId);
1015    
1016                    if (portlet == null) {
1017                            if (_log.isDebugEnabled()) {
1018                                    _log.debug(
1019                                            "Do not import portlet data for " + portletId +
1020                                                    " because the portlet does not exist");
1021                            }
1022    
1023                            return null;
1024                    }
1025    
1026                    PortletDataHandler portletDataHandler =
1027                            portlet.getPortletDataHandlerInstance();
1028    
1029                    if (portletDataHandler == null) {
1030                            if (_log.isDebugEnabled()) {
1031                                    _log.debug(
1032                                            "Do not import portlet data for " + portletId +
1033                                                    " because the portlet does not have a " +
1034                                                            "PortletDataHandler");
1035                            }
1036    
1037                            return null;
1038                    }
1039    
1040                    if (_log.isDebugEnabled()) {
1041                            _log.debug("Importing data for " + portletId);
1042                    }
1043    
1044                    PortletPreferencesImpl portletPreferencesImpl = null;
1045    
1046                    if (portletPreferences != null) {
1047                            portletPreferencesImpl =
1048                                    (PortletPreferencesImpl)
1049                                            PortletPreferencesFactoryUtil.fromDefaultXML(
1050                                                    portletPreferences.getPreferences());
1051                    }
1052    
1053                    String portletData = portletDataContext.getZipEntryAsString(
1054                            portletDataElement.attributeValue("path"));
1055    
1056                    portletPreferencesImpl =
1057                            (PortletPreferencesImpl)portletDataHandler.importData(
1058                                    portletDataContext, portletId, portletPreferencesImpl,
1059                                    portletData);
1060    
1061                    if (portletPreferencesImpl == null) {
1062                            return null;
1063                    }
1064    
1065                    return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
1066            }
1067    
1068            protected void importPortletPreferences(
1069                            PortletDataContext portletDataContext, long companyId, long groupId,
1070                            Layout layout, String portletId, Element parentElement,
1071                            boolean importPortletSetup, boolean importPortletArchivedSetups,
1072                            boolean importPortletUserPreferences, boolean preserveScopeLayoutId,
1073                            boolean importPortletData)
1074                    throws Exception {
1075    
1076                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
1077                    long plid = 0;
1078                    String scopeType = StringPool.BLANK;
1079                    String scopeLayoutUuid = StringPool.BLANK;
1080    
1081                    if (layout != null) {
1082                            plid = layout.getPlid();
1083    
1084                            if (preserveScopeLayoutId && (portletId != null)) {
1085                                    javax.portlet.PortletPreferences jxPreferences =
1086                                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
1087                                                    layout, portletId);
1088    
1089                                    scopeType = GetterUtil.getString(
1090                                            jxPreferences.getValue("lfrScopeType", null));
1091                                    scopeLayoutUuid = GetterUtil.getString(
1092                                            jxPreferences.getValue("lfrScopeLayoutUuid", null));
1093    
1094                                    portletDataContext.setScopeType(scopeType);
1095                                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1096                            }
1097                    }
1098    
1099                    List<Element> portletPreferencesElements = parentElement.elements(
1100                            "portlet-preferences");
1101    
1102                    for (Element portletPreferencesElement : portletPreferencesElements) {
1103                            String path = portletPreferencesElement.attributeValue("path");
1104    
1105                            if (portletDataContext.isPathNotProcessed(path)) {
1106                                    String xml = null;
1107    
1108                                    Element element = null;
1109    
1110                                    try {
1111                                            xml = portletDataContext.getZipEntryAsString(path);
1112    
1113                                            Document preferencesDocument = SAXReaderUtil.read(xml);
1114    
1115                                            element = preferencesDocument.getRootElement();
1116                                    }
1117                                    catch (DocumentException de) {
1118                                            throw new SystemException(de);
1119                                    }
1120    
1121                                    long ownerId = GetterUtil.getLong(
1122                                            element.attributeValue("owner-id"));
1123                                    int ownerType = GetterUtil.getInteger(
1124                                            element.attributeValue("owner-type"));
1125    
1126                                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
1127                                            continue;
1128                                    }
1129    
1130                                    if (((ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) ||
1131                                             (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT)) &&
1132                                            !importPortletSetup) {
1133    
1134                                            continue;
1135                                    }
1136    
1137                                    if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) &&
1138                                            !importPortletArchivedSetups) {
1139    
1140                                            continue;
1141                                    }
1142    
1143                                    if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) &&
1144                                            (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT) &&
1145                                            !importPortletUserPreferences) {
1146    
1147                                            continue;
1148                                    }
1149    
1150                                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
1151                                            plid = PortletKeys.PREFS_PLID_SHARED;
1152                                            ownerId = portletDataContext.getScopeGroupId();
1153                                    }
1154    
1155                                    boolean defaultUser = GetterUtil.getBoolean(
1156                                            element.attributeValue("default-user"));
1157    
1158                                    if (portletId == null) {
1159                                            portletId = element.attributeValue("portlet-id");
1160                                    }
1161    
1162                                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1163                                            portletId = PortletConstants.getRootPortletId(portletId);
1164    
1165                                            String userUuid = element.attributeValue(
1166                                                    "archive-user-uuid");
1167                                            String name = element.attributeValue("archive-name");
1168    
1169                                            long userId = portletDataContext.getUserId(userUuid);
1170    
1171                                            PortletItem portletItem =
1172                                                    PortletItemLocalServiceUtil.updatePortletItem(
1173                                                            userId, groupId, name, portletId,
1174                                                            PortletPreferences.class.getName());
1175    
1176                                            plid = 0;
1177                                            ownerId = portletItem.getPortletItemId();
1178                                    }
1179                                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1180                                            String userUuid = element.attributeValue("user-uuid");
1181    
1182                                            ownerId = portletDataContext.getUserId(userUuid);
1183                                    }
1184    
1185                                    if (defaultUser) {
1186                                            ownerId = defaultUserId;
1187                                    }
1188    
1189                                    String rootPotletId = PortletConstants.getRootPortletId(
1190                                            portletId);
1191    
1192                                    if (rootPotletId.equals(PortletKeys.ASSET_PUBLISHER)) {
1193                                            xml = updateAssetPublisherPortletPreferences(
1194                                                    portletDataContext, companyId, ownerId, ownerType, plid,
1195                                                    portletId, xml);
1196                                    }
1197                                    else if (rootPotletId.equals(
1198                                                            PortletKeys.TAGS_CATEGORIES_NAVIGATION)) {
1199    
1200                                            xml = updateAssetCategoriesNavigationPortletPreferences(
1201                                                    portletDataContext, companyId, ownerId, ownerType, plid,
1202                                                    portletId, xml);
1203                                    }
1204    
1205                                    updatePortletPreferences(
1206                                            portletDataContext, ownerId, ownerType, plid, portletId,
1207                                            xml, importPortletData);
1208                            }
1209                    }
1210    
1211                    if (preserveScopeLayoutId && (layout != null)) {
1212                            javax.portlet.PortletPreferences jxPreferences =
1213                                    PortletPreferencesFactoryUtil.getLayoutPortletSetup(
1214                                            layout, portletId);
1215    
1216                            try {
1217                                    jxPreferences.setValue("lfrScopeType", scopeType);
1218                                    jxPreferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);
1219    
1220                                    jxPreferences.store();
1221                            }
1222                            finally {
1223                                    portletDataContext.setScopeType(scopeType);
1224                                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1225                            }
1226                    }
1227            }
1228    
1229            protected void readAssetCategories(PortletDataContext portletDataContext)
1230                    throws Exception {
1231    
1232                    String xml = portletDataContext.getZipEntryAsString(
1233                            portletDataContext.getSourceRootPath() +
1234                                    "/categories-hierarchy.xml");
1235    
1236                    if (xml == null) {
1237                            return;
1238                    }
1239    
1240                    Document document = SAXReaderUtil.read(xml);
1241    
1242                    Element rootElement = document.getRootElement();
1243    
1244                    Element assetVocabulariesElement = rootElement.element("vocabularies");
1245    
1246                    List<Element> assetVocabularyElements =
1247                            assetVocabulariesElement.elements("vocabulary");
1248    
1249                    Map<Long, Long> assetVocabularyPKs =
1250                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1251                                    AssetVocabulary.class);
1252    
1253                    for (Element assetVocabularyElement : assetVocabularyElements) {
1254                            String path = assetVocabularyElement.attributeValue("path");
1255    
1256                            if (!portletDataContext.isPathNotProcessed(path)) {
1257                                    continue;
1258                            }
1259    
1260                            AssetVocabulary assetVocabulary =
1261                                    (AssetVocabulary)portletDataContext.getZipEntryAsObject(path);
1262    
1263                            importAssetVocabulary(
1264                                    portletDataContext, assetVocabularyPKs, assetVocabularyElement,
1265                                    assetVocabulary);
1266                    }
1267    
1268                    Element assetCategoriesElement = rootElement.element("categories");
1269    
1270                    List<Element> assetCategoryElements = assetCategoriesElement.elements(
1271                            "category");
1272    
1273                    Map<Long, Long> assetCategoryPKs =
1274                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1275                                    AssetCategory.class);
1276    
1277                    Map<String, String> assetCategoryUuids =
1278                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
1279                                    AssetCategory.class.getName() + "uuid");
1280    
1281                    for (Element assetCategoryElement : assetCategoryElements) {
1282                            String path = assetCategoryElement.attributeValue("path");
1283    
1284                            if (!portletDataContext.isPathNotProcessed(path)) {
1285                                    continue;
1286                            }
1287    
1288                            AssetCategory assetCategory =
1289                                    (AssetCategory)portletDataContext.getZipEntryAsObject(path);
1290    
1291                            importAssetCategory(
1292                                    portletDataContext, assetVocabularyPKs, assetCategoryPKs,
1293                                    assetCategoryUuids, assetCategoryElement, assetCategory);
1294                    }
1295    
1296                    Element assetsElement = rootElement.element("assets");
1297    
1298                    List<Element> assetElements = assetsElement.elements("asset");
1299    
1300                    for (Element assetElement : assetElements) {
1301                            String className = GetterUtil.getString(
1302                                    assetElement.attributeValue("class-name"));
1303                            long classPK = GetterUtil.getLong(
1304                                    assetElement.attributeValue("class-pk"));
1305                            String[] assetCategoryUuidArray = StringUtil.split(
1306                                    GetterUtil.getString(
1307                                            assetElement.attributeValue("category-uuids")));
1308    
1309                            long[] assetCategoryIds = new long[0];
1310    
1311                            for (String assetCategoryUuid : assetCategoryUuidArray) {
1312                                    assetCategoryUuid = MapUtil.getString(
1313                                            assetCategoryUuids, assetCategoryUuid, assetCategoryUuid);
1314    
1315                                    AssetCategory assetCategory = AssetCategoryUtil.fetchByUUID_G(
1316                                            assetCategoryUuid, portletDataContext.getScopeGroupId());
1317    
1318                                    if (assetCategory == null) {
1319                                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
1320                                                    portletDataContext.getCompanyId());
1321    
1322                                            assetCategory = AssetCategoryUtil.fetchByUUID_G(
1323                                                    assetCategoryUuid, companyGroup.getGroupId());
1324                                    }
1325    
1326                                    if (assetCategory != null) {
1327                                            assetCategoryIds = ArrayUtil.append(
1328                                                    assetCategoryIds, assetCategory.getCategoryId());
1329                                    }
1330                            }
1331    
1332                            portletDataContext.addAssetCategories(
1333                                    className, classPK, assetCategoryIds);
1334                    }
1335            }
1336    
1337            protected void readAssetLinks(PortletDataContext portletDataContext)
1338                    throws Exception {
1339    
1340                    String xml = portletDataContext.getZipEntryAsString(
1341                            portletDataContext.getSourceRootPath() + "/links.xml");
1342    
1343                    if (xml == null) {
1344                            return;
1345                    }
1346    
1347                    Document document = SAXReaderUtil.read(xml);
1348    
1349                    Element rootElement = document.getRootElement();
1350    
1351                    List<Element> assetLinkElements = rootElement.elements("asset-link");
1352    
1353                    for (Element assetLinkElement : assetLinkElements) {
1354                            String sourceUuid = GetterUtil.getString(
1355                                    assetLinkElement.attributeValue("source-uuid"));
1356                            String[] assetEntryUuidArray = StringUtil.split(
1357                                    GetterUtil.getString(
1358                                            assetLinkElement.attributeValue("target-uuids")));
1359                            int assetLinkType = GetterUtil.getInteger(
1360                                    assetLinkElement.attributeValue("type"));
1361    
1362                            List<Long> assetEntryIds = new ArrayList<Long>();
1363    
1364                            for (String assetEntryUuid : assetEntryUuidArray) {
1365                                    try {
1366                                            AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
1367                                                    portletDataContext.getScopeGroupId(), assetEntryUuid);
1368    
1369                                            assetEntryIds.add(assetEntry.getEntryId());
1370                                    }
1371                                    catch (NoSuchEntryException nsee) {
1372                                    }
1373                            }
1374    
1375                            if (assetEntryIds.isEmpty()) {
1376                                    continue;
1377                            }
1378    
1379                            long[] assetEntryIdsArray = ArrayUtil.toArray(
1380                                    assetEntryIds.toArray(new Long[assetEntryIds.size()]));
1381    
1382                            try {
1383                                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
1384                                            portletDataContext.getScopeGroupId(), sourceUuid);
1385    
1386                                    AssetLinkLocalServiceUtil.updateLinks(
1387                                            assetEntry.getUserId(), assetEntry.getEntryId(),
1388                                            assetEntryIdsArray, assetLinkType);
1389                            }
1390                            catch (NoSuchEntryException nsee) {
1391                            }
1392                    }
1393            }
1394    
1395            protected void readAssetTags(PortletDataContext portletDataContext)
1396                    throws Exception {
1397    
1398                    String xml = portletDataContext.getZipEntryAsString(
1399                            portletDataContext.getSourceRootPath() + "/tags.xml");
1400    
1401                    if (xml == null) {
1402                            return;
1403                    }
1404    
1405                    Document document = SAXReaderUtil.read(xml);
1406    
1407                    Element rootElement = document.getRootElement();
1408    
1409                    List<Element> assetTagElements = rootElement.elements("tag");
1410    
1411                    for (Element assetTagElement : assetTagElements) {
1412                            String path = assetTagElement.attributeValue("path");
1413    
1414                            if (!portletDataContext.isPathNotProcessed(path)) {
1415                                    continue;
1416                            }
1417    
1418                            AssetTag assetTag =
1419                                    (AssetTag)portletDataContext.getZipEntryAsObject(path);
1420    
1421                            Map<Long, Long> assetTagPKs =
1422                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1423                                            AssetTag.class);
1424    
1425                            importAssetTag(
1426                                    portletDataContext, assetTagPKs, assetTagElement, assetTag);
1427                    }
1428    
1429                    List<Element> assetElements = rootElement.elements("asset");
1430    
1431                    for (Element assetElement : assetElements) {
1432                            String className = GetterUtil.getString(
1433                                    assetElement.attributeValue("class-name"));
1434                            long classPK = GetterUtil.getLong(
1435                                    assetElement.attributeValue("class-pk"));
1436                            String assetTagNames = GetterUtil.getString(
1437                                    assetElement.attributeValue("tags"));
1438    
1439                            portletDataContext.addAssetTags(
1440                                    className, classPK, StringUtil.split(assetTagNames));
1441                    }
1442            }
1443    
1444            protected void readComments(PortletDataContext portletDataContext)
1445                    throws Exception {
1446    
1447                    String xml = portletDataContext.getZipEntryAsString(
1448                            portletDataContext.getSourceRootPath() + "/comments.xml");
1449    
1450                    if (xml == null) {
1451                            return;
1452                    }
1453    
1454                    Document document = SAXReaderUtil.read(xml);
1455    
1456                    Element rootElement = document.getRootElement();
1457    
1458                    List<Element> assetElements = rootElement.elements("asset");
1459    
1460                    for (Element assetElement : assetElements) {
1461                            String path = assetElement.attributeValue("path");
1462                            String className = assetElement.attributeValue("class-name");
1463                            long classPK = GetterUtil.getLong(
1464                                    assetElement.attributeValue("class-pk"));
1465    
1466                            List<String> zipFolderEntries =
1467                                    portletDataContext.getZipFolderEntries(path);
1468    
1469                            List<MBMessage> mbMessages = new ArrayList<MBMessage>();
1470    
1471                            for (String zipFolderEntry : zipFolderEntries) {
1472                                    MBMessage mbMessage =
1473                                            (MBMessage)portletDataContext.getZipEntryAsObject(
1474                                                    zipFolderEntry);
1475    
1476                                    if (mbMessage != null) {
1477                                            mbMessages.add(mbMessage);
1478                                    }
1479                            }
1480    
1481                            portletDataContext.addComments(className, classPK, mbMessages);
1482                    }
1483            }
1484    
1485            protected void readExpandoTables(PortletDataContext portletDataContext)
1486                    throws Exception {
1487    
1488                    String xml = portletDataContext.getZipEntryAsString(
1489                            portletDataContext.getSourceRootPath() + "/expando-tables.xml");
1490    
1491                    if (xml == null) {
1492                            return;
1493                    }
1494    
1495                    Document document = SAXReaderUtil.read(xml);
1496    
1497                    Element rootElement = document.getRootElement();
1498    
1499                    List<Element> expandoTableElements = rootElement.elements(
1500                            "expando-table");
1501    
1502                    for (Element expandoTableElement : expandoTableElements) {
1503                            String className = expandoTableElement.attributeValue("class-name");
1504    
1505                            ExpandoTable expandoTable = null;
1506    
1507                            try {
1508                                    expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(
1509                                            portletDataContext.getCompanyId(), className);
1510                            }
1511                            catch (NoSuchTableException nste) {
1512                                    expandoTable = ExpandoTableLocalServiceUtil.addDefaultTable(
1513                                            portletDataContext.getCompanyId(), className);
1514                            }
1515    
1516                            List<Element> expandoColumnElements = expandoTableElement.elements(
1517                                    "expando-column");
1518    
1519                            for (Element expandoColumnElement : expandoColumnElements) {
1520                                    long columnId = GetterUtil.getLong(
1521                                            expandoColumnElement.attributeValue("column-id"));
1522                                    String name = expandoColumnElement.attributeValue("name");
1523                                    int type = GetterUtil.getInteger(
1524                                            expandoColumnElement.attributeValue("type"));
1525                                    String defaultData = expandoColumnElement.elementText(
1526                                            "default-data");
1527                                    String typeSettings = expandoColumnElement.elementText(
1528                                            "type-settings");
1529    
1530                                    Serializable defaultDataObject =
1531                                            ExpandoConverterUtil.getAttributeFromString(
1532                                                    type, defaultData);
1533    
1534                                    ExpandoColumn expandoColumn =
1535                                            ExpandoColumnLocalServiceUtil.getColumn(
1536                                                    expandoTable.getTableId(), name);
1537    
1538                                    if (expandoColumn != null) {
1539                                            ExpandoColumnLocalServiceUtil.updateColumn(
1540                                                    expandoColumn.getColumnId(), name, type,
1541                                                    defaultDataObject);
1542                                    }
1543                                    else {
1544                                            expandoColumn = ExpandoColumnLocalServiceUtil.addColumn(
1545                                                    expandoTable.getTableId(), name, type,
1546                                                    defaultDataObject);
1547                                    }
1548    
1549                                    ExpandoColumnLocalServiceUtil.updateTypeSettings(
1550                                            expandoColumn.getColumnId(), typeSettings);
1551    
1552                                    portletDataContext.importPermissions(
1553                                            ExpandoColumn.class, columnId, expandoColumn.getColumnId());
1554                            }
1555                    }
1556            }
1557    
1558            protected void readLocks(PortletDataContext portletDataContext)
1559                    throws Exception {
1560    
1561                    String xml = portletDataContext.getZipEntryAsString(
1562                            portletDataContext.getSourceRootPath() + "/locks.xml");
1563    
1564                    if (xml == null) {
1565                            return;
1566                    }
1567    
1568                    Document document = SAXReaderUtil.read(xml);
1569    
1570                    Element rootElement = document.getRootElement();
1571    
1572                    List<Element> assetElements = rootElement.elements("asset");
1573    
1574                    for (Element assetElement : assetElements) {
1575                            String path = assetElement.attributeValue("path");
1576                            String className = assetElement.attributeValue("class-name");
1577                            String key = assetElement.attributeValue("key");
1578    
1579                            Lock lock = (Lock)portletDataContext.getZipEntryAsObject(path);
1580    
1581                            if (lock != null) {
1582                                    portletDataContext.addLocks(className, key, lock);
1583                            }
1584                    }
1585            }
1586    
1587            protected void readRatingsEntries(PortletDataContext portletDataContext)
1588                    throws Exception {
1589    
1590                    String xml = portletDataContext.getZipEntryAsString(
1591                            portletDataContext.getSourceRootPath() + "/ratings.xml");
1592    
1593                    if (xml == null) {
1594                            return;
1595                    }
1596    
1597                    Document document = SAXReaderUtil.read(xml);
1598    
1599                    Element rootElement = document.getRootElement();
1600    
1601                    List<Element> assetElements = rootElement.elements("asset");
1602    
1603                    for (Element assetElement : assetElements) {
1604                            String path = assetElement.attributeValue("path");
1605                            String className = assetElement.attributeValue("class-name");
1606                            long classPK = GetterUtil.getLong(
1607                                    assetElement.attributeValue("class-pk"));
1608    
1609                            List<String> zipFolderEntries =
1610                                    portletDataContext.getZipFolderEntries(path);
1611    
1612                            List<RatingsEntry> ratingsEntries = new ArrayList<RatingsEntry>();
1613    
1614                            for (String zipFolderEntry : zipFolderEntries) {
1615                                    RatingsEntry ratingsEntry =
1616                                            (RatingsEntry)portletDataContext.getZipEntryAsObject(
1617                                                    zipFolderEntry);
1618    
1619                                    if (ratingsEntry != null) {
1620                                            ratingsEntries.add(ratingsEntry);
1621                                    }
1622                            }
1623    
1624                            portletDataContext.addRatingsEntries(
1625                                    className, classPK, ratingsEntries);
1626                    }
1627            }
1628    
1629            protected void resetPortletScope(
1630                    PortletDataContext portletDataContext, long groupId) {
1631    
1632                    portletDataContext.setScopeGroupId(groupId);
1633                    portletDataContext.setScopeLayoutUuid(StringPool.BLANK);
1634                    portletDataContext.setScopeType(StringPool.BLANK);
1635            }
1636    
1637            protected void setPortletScope(
1638                    PortletDataContext portletDataContext, Element portletElement) {
1639    
1640                    // Portlet data scope
1641    
1642                    String scopeLayoutUuid = GetterUtil.getString(
1643                            portletElement.attributeValue("scope-layout-uuid"));
1644                    String scopeLayoutType = GetterUtil.getString(
1645                            portletElement.attributeValue("scope-layout-type"));
1646    
1647                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1648                    portletDataContext.setScopeType(scopeLayoutType);
1649    
1650                    // Layout scope
1651    
1652                    try {
1653                            Group scopeGroup = null;
1654    
1655                            if (scopeLayoutType.equals("company")) {
1656                                    scopeGroup = GroupLocalServiceUtil.getCompanyGroup(
1657                                            portletDataContext.getCompanyId());
1658                            }
1659                            else if (Validator.isNotNull(scopeLayoutUuid)) {
1660                                    Layout scopeLayout =
1661                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1662                                                    scopeLayoutUuid, portletDataContext.getGroupId());
1663    
1664                                    if (scopeLayout.hasScopeGroup()) {
1665                                            scopeGroup = scopeLayout.getScopeGroup();
1666                                    }
1667                                    else {
1668                                            String name = String.valueOf(scopeLayout.getPlid());
1669    
1670                                            scopeGroup = GroupLocalServiceUtil.addGroup(
1671                                                    portletDataContext.getUserId(null),
1672                                                    GroupConstants.DEFAULT_PARENT_GROUP_ID,
1673                                                    Layout.class.getName(), scopeLayout.getPlid(),
1674                                                    GroupConstants.DEFAULT_LIVE_GROUP_ID, name, null, 0,
1675                                                    null, false, true, null);
1676                                    }
1677    
1678                                    Group group = scopeLayout.getGroup();
1679    
1680                                    if (group.isStaged() && !group.isStagedRemotely()) {
1681                                            try {
1682                                                    Layout oldLayout =
1683                                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1684                                                                    scopeLayoutUuid,
1685                                                                    portletDataContext.getSourceGroupId());
1686    
1687                                                    Group oldScopeGroup = oldLayout.getScopeGroup();
1688    
1689                                                    oldScopeGroup.setLiveGroupId(scopeGroup.getGroupId());
1690    
1691                                                    GroupLocalServiceUtil.updateGroup(oldScopeGroup);
1692                                            }
1693                                            catch (NoSuchLayoutException nsle) {
1694                                                    if (_log.isWarnEnabled()) {
1695                                                            _log.warn(nsle);
1696                                                    }
1697                                            }
1698                                    }
1699                            }
1700    
1701                            if (scopeGroup != null) {
1702                                    portletDataContext.setScopeGroupId(scopeGroup.getGroupId());
1703                            }
1704                    }
1705                    catch (PortalException pe) {
1706                    }
1707                    catch (Exception e) {
1708                            _log.error(e, e);
1709                    }
1710            }
1711    
1712            protected String updateAssetCategoriesNavigationPortletPreferences(
1713                            PortletDataContext portletDataContext, long companyId, long ownerId,
1714                            int ownerType, long plid, String portletId, String xml)
1715                    throws Exception {
1716    
1717                    Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
1718    
1719                    Group companyGroup = company.getGroup();
1720    
1721                    javax.portlet.PortletPreferences jxPreferences =
1722                            PortletPreferencesFactoryUtil.fromXML(
1723                                    companyId, ownerId, ownerType, plid, portletId, xml);
1724    
1725                    Enumeration<String> enu = jxPreferences.getNames();
1726    
1727                    while (enu.hasMoreElements()) {
1728                            String name = enu.nextElement();
1729    
1730                            if (name.equals("assetVocabularyIds")) {
1731                                    updatePreferencesClassPKs(
1732                                            portletDataContext, jxPreferences, name,
1733                                            AssetVocabulary.class, companyGroup.getGroupId());
1734                            }
1735                    }
1736    
1737                    return PortletPreferencesFactoryUtil.toXML(jxPreferences);
1738            }
1739    
1740            protected void updateAssetPublisherClassNameIds(
1741                            javax.portlet.PortletPreferences jxPreferences, String key)
1742                    throws Exception {
1743    
1744                    String[] oldValues = jxPreferences.getValues(key, null);
1745    
1746                    if (oldValues == null) {
1747                            return;
1748                    }
1749    
1750                    String[] newValues = new String[oldValues.length];
1751    
1752                    int i = 0;
1753    
1754                    for (String oldValue : oldValues) {
1755                            if (key.equals("anyAssetType") &&
1756                                    (oldValue.equals("false") || oldValue.equals("true"))) {
1757    
1758                                    newValues[i++] = oldValue;
1759    
1760                                    continue;
1761                            }
1762    
1763                            try {
1764                                    long classNameId = PortalUtil.getClassNameId(oldValue);
1765    
1766                                    newValues[i++] = String.valueOf(classNameId);
1767                            }
1768                            catch (Exception e) {
1769                                    if (_log.isWarnEnabled()) {
1770                                            _log.warn(
1771                                                    "Unable to find class name ID for class name " +
1772                                                            oldValue);
1773                                    }
1774                            }
1775                    }
1776    
1777                    jxPreferences.setValues(key, newValues);
1778            }
1779    
1780            protected void updateAssetPublisherGlobalScopeId(
1781                            javax.portlet.PortletPreferences jxPreferences, String key,
1782                            long groupId)
1783                    throws Exception {
1784    
1785                    String[] oldValues = jxPreferences.getValues(key, null);
1786    
1787                    if (oldValues == null) {
1788                            return;
1789                    }
1790    
1791                    String newValue = "Group_" + groupId;
1792    
1793                    String[] newValues = new String[oldValues.length];
1794    
1795                    for (int i = 0; i < oldValues.length; i++) {
1796                            String oldValue = oldValues[i];
1797    
1798                            newValues[i] = StringUtil.replace(
1799                                    oldValue, "Group_Company", newValue);
1800                    }
1801    
1802                    jxPreferences.setValues(key, newValues);
1803            }
1804    
1805            protected String updateAssetPublisherPortletPreferences(
1806                            PortletDataContext portletDataContext, long companyId, long ownerId,
1807                            int ownerType, long plid, String portletId, String xml)
1808                    throws Exception {
1809    
1810                    Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
1811    
1812                    Group companyGroup = company.getGroup();
1813    
1814                    javax.portlet.PortletPreferences jxPreferences =
1815                            PortletPreferencesFactoryUtil.fromXML(
1816                                    companyId, ownerId, ownerType, plid, portletId, xml);
1817    
1818                    Enumeration<String> enu = jxPreferences.getNames();
1819    
1820                    while (enu.hasMoreElements()) {
1821                            String name = enu.nextElement();
1822    
1823                            String value = GetterUtil.getString(
1824                                    jxPreferences.getValue(name, null));
1825    
1826                            if (name.equals(
1827                                            "anyClassTypeJournalArticleAssetRendererFactory") ||
1828                                    name.equals(
1829                                            "classTypeIdsJournalArticleAssetRendererFactory") ||
1830                                    name.equals("classTypeIds")) {
1831    
1832                                    updatePreferencesClassPKs(
1833                                            portletDataContext, jxPreferences, name,
1834                                            JournalStructure.class, companyGroup.getGroupId());
1835                            }
1836                            else if (name.equals("anyAssetType") ||
1837                                             name.equals("classNameIds")) {
1838    
1839                                    updateAssetPublisherClassNameIds(jxPreferences, name);
1840                            }
1841                            else if (name.equals("defaultScope") || name.equals("scopeIds")) {
1842                                    updateAssetPublisherGlobalScopeId(
1843                                            jxPreferences, name, companyGroup.getGroupId());
1844                            }
1845                            else if (name.startsWith("queryName") &&
1846                                             value.equalsIgnoreCase("assetCategories")) {
1847    
1848                                    String index = name.substring(9, name.length());
1849    
1850                                    updatePreferencesClassPKs(
1851                                            portletDataContext, jxPreferences, "queryValues" + index,
1852                                            AssetCategory.class, companyGroup.getGroupId());
1853                            }
1854                    }
1855    
1856                    return PortletPreferencesFactoryUtil.toXML(jxPreferences);
1857            }
1858    
1859            protected void updatePortletPreferences(
1860                            PortletDataContext portletDataContext, long ownerId, int ownerType,
1861                            long plid, String portletId, String xml, boolean importData)
1862                    throws Exception {
1863    
1864                    if (importData) {
1865                            PortletPreferencesLocalServiceUtil.updatePreferences(
1866                                    ownerId, ownerType, plid, portletId, xml);
1867                    }
1868                    else {
1869                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
1870                                    portletDataContext.getCompanyId(), portletId);
1871    
1872                            if (portlet == null) {
1873                                    if (_log.isDebugEnabled()) {
1874                                            _log.debug(
1875                                                    "Do not update portlet preferences for " + portletId +
1876                                                            " because the portlet does not exist");
1877                                    }
1878    
1879                                    return;
1880                            }
1881    
1882                            PortletDataHandler portletDataHandler =
1883                                    portlet.getPortletDataHandlerInstance();
1884    
1885                            if (portletDataHandler == null) {
1886                                    PortletPreferencesLocalServiceUtil.updatePreferences(
1887                                            ownerId, ownerType, plid, portletId, xml);
1888    
1889                                    return;
1890                            }
1891    
1892                            // Portlet preferences to be updated only when importing data
1893    
1894                            String[] dataPortletPreferences =
1895                                    portletDataHandler.getDataPortletPreferences();
1896    
1897                            // Current portlet preferences
1898    
1899                            javax.portlet.PortletPreferences portletPreferences =
1900                                    PortletPreferencesLocalServiceUtil.getPreferences(
1901                                            portletDataContext.getCompanyId(), ownerId, ownerType, plid,
1902                                            portletId);
1903    
1904                            // New portlet preferences
1905    
1906                            javax.portlet.PortletPreferences jxPreferences =
1907                                    PortletPreferencesFactoryUtil.fromXML(
1908                                            portletDataContext.getCompanyId(), ownerId, ownerType, plid,
1909                                            portletId, xml);
1910    
1911                            Enumeration<String> enu = jxPreferences.getNames();
1912    
1913                            while (enu.hasMoreElements()) {
1914                                    String name = enu.nextElement();
1915    
1916                                    if (!ArrayUtil.contains(dataPortletPreferences, name)) {
1917                                            String value = GetterUtil.getString(
1918                                                    jxPreferences.getValue(name, null));
1919    
1920                                            portletPreferences.setValue(name, value);
1921                                    }
1922                            }
1923    
1924                            PortletPreferencesLocalServiceUtil.updatePreferences(
1925                                    ownerId, ownerType, plid, portletId, portletPreferences);
1926                    }
1927            }
1928    
1929            protected void updatePreferencesClassPKs(
1930                            PortletDataContext portletDataContext,
1931                            javax.portlet.PortletPreferences jxPreferences, String key,
1932                            Class<?> clazz, long companyGroupId)
1933                    throws Exception {
1934    
1935                    String[] oldValues = jxPreferences.getValues(key, null);
1936    
1937                    if (oldValues == null) {
1938                            return;
1939                    }
1940    
1941                    Map<Long, Long> primaryKeysMap =
1942                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(clazz);
1943    
1944                    String[] newValues = new String[oldValues.length];
1945    
1946                    for (int i = 0; i < oldValues.length; i++) {
1947                            String oldValue = oldValues[i];
1948    
1949                            String newValue = oldValue;
1950    
1951                            String[] uuids = StringUtil.split(oldValue);
1952    
1953                            for (String uuid : uuids) {
1954                                    Long newPrimaryKey = null;
1955    
1956                                    if (Validator.isNumber(uuid)) {
1957                                            long oldPrimaryKey = GetterUtil.getLong(uuid);
1958    
1959                                            newPrimaryKey = MapUtil.getLong(
1960                                                    primaryKeysMap, oldPrimaryKey, oldPrimaryKey);
1961                                    }
1962                                    else {
1963                                            String className = clazz.getName();
1964    
1965                                            if (className.equals(AssetCategory.class.getName())) {
1966                                                    AssetCategory assetCategory =
1967                                                            AssetCategoryUtil.fetchByUUID_G(
1968                                                                    uuid, portletDataContext.getScopeGroupId());
1969    
1970                                                    if (assetCategory == null) {
1971                                                            assetCategory = AssetCategoryUtil.fetchByUUID_G(
1972                                                                    uuid, companyGroupId);
1973                                                    }
1974    
1975                                                    if (assetCategory != null) {
1976                                                            newPrimaryKey = assetCategory.getCategoryId();
1977                                                    }
1978                                            }
1979                                            else if (className.equals(
1980                                                            AssetVocabulary.class.getName())) {
1981    
1982                                                    AssetVocabulary assetVocabulary =
1983                                                            AssetVocabularyUtil.fetchByUUID_G(
1984                                                                    uuid, portletDataContext.getScopeGroupId());
1985    
1986                                                    if (assetVocabulary == null) {
1987                                                            assetVocabulary = AssetVocabularyUtil.fetchByUUID_G(
1988                                                                    uuid, companyGroupId);
1989                                                    }
1990    
1991                                                    if (assetVocabulary != null) {
1992                                                            newPrimaryKey = assetVocabulary.getVocabularyId();
1993                                                    }
1994                                            }
1995                                            else if (className.equals(
1996                                                            JournalStructure.class.getName())) {
1997    
1998                                                    JournalStructure journalStructure =
1999                                                            JournalStructureUtil.fetchByUUID_G(
2000                                                                    uuid, portletDataContext.getScopeGroupId());
2001    
2002                                                    if (journalStructure == null) {
2003                                                            journalStructure =
2004                                                                    JournalStructureUtil.fetchByUUID_G(
2005                                                                            uuid, companyGroupId);
2006                                                    }
2007    
2008                                                    if (journalStructure != null) {
2009                                                            newPrimaryKey = journalStructure.getId();
2010                                                    }
2011                                            }
2012                                    }
2013    
2014                                    if (Validator.isNull(newPrimaryKey)) {
2015                                            if (_log.isWarnEnabled()) {
2016                                                    StringBundler sb = new StringBundler(8);
2017    
2018                                                    sb.append("Unable to get primary key for ");
2019                                                    sb.append(clazz);
2020                                                    sb.append(" with UUID ");
2021                                                    sb.append(uuid);
2022                                                    sb.append(" in company group ");
2023                                                    sb.append(companyGroupId);
2024                                                    sb.append(" or in group ");
2025                                                    sb.append(portletDataContext.getScopeGroupId());
2026    
2027                                                    _log.warn(sb.toString());
2028                                            }
2029                                    }
2030                                    else {
2031                                            newValue = StringUtil.replace(
2032                                                    newValue, uuid, newPrimaryKey.toString());
2033                                    }
2034                            }
2035    
2036                            newValues[i] = newValue;
2037                    }
2038    
2039                    jxPreferences.setValues(key, newValues);
2040            }
2041    
2042            private static Log _log = LogFactoryUtil.getLog(PortletImporter.class);
2043    
2044            private PermissionImporter _permissionImporter = new PermissionImporter();
2045    
2046    }