001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.NoSuchLayoutException;
021    import com.liferay.portal.PortletIdException;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.lar.ImportExportThreadLocal;
025    import com.liferay.portal.kernel.lar.PortletDataContext;
026    import com.liferay.portal.kernel.lar.PortletDataHandler;
027    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
028    import com.liferay.portal.kernel.lar.UserIdStrategy;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.search.Indexer;
032    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
033    import com.liferay.portal.kernel.util.ArrayUtil;
034    import com.liferay.portal.kernel.util.GetterUtil;
035    import com.liferay.portal.kernel.util.LocaleUtil;
036    import com.liferay.portal.kernel.util.MapUtil;
037    import com.liferay.portal.kernel.util.ReleaseInfo;
038    import com.liferay.portal.kernel.util.StringBundler;
039    import com.liferay.portal.kernel.util.StringPool;
040    import com.liferay.portal.kernel.util.StringUtil;
041    import com.liferay.portal.kernel.util.Validator;
042    import com.liferay.portal.kernel.xml.Document;
043    import com.liferay.portal.kernel.xml.DocumentException;
044    import com.liferay.portal.kernel.xml.Element;
045    import com.liferay.portal.kernel.xml.Node;
046    import com.liferay.portal.kernel.xml.SAXReaderUtil;
047    import com.liferay.portal.kernel.zip.ZipReader;
048    import com.liferay.portal.kernel.zip.ZipReaderFactoryUtil;
049    import com.liferay.portal.model.Group;
050    import com.liferay.portal.model.Layout;
051    import com.liferay.portal.model.Lock;
052    import com.liferay.portal.model.Portlet;
053    import com.liferay.portal.model.PortletConstants;
054    import com.liferay.portal.model.PortletItem;
055    import com.liferay.portal.model.PortletPreferences;
056    import com.liferay.portal.model.User;
057    import com.liferay.portal.security.permission.PermissionCacheUtil;
058    import com.liferay.portal.service.GroupLocalServiceUtil;
059    import com.liferay.portal.service.LayoutLocalServiceUtil;
060    import com.liferay.portal.service.PortletItemLocalServiceUtil;
061    import com.liferay.portal.service.PortletLocalServiceUtil;
062    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
063    import com.liferay.portal.service.ServiceContext;
064    import com.liferay.portal.service.UserLocalServiceUtil;
065    import com.liferay.portal.service.persistence.PortletPreferencesUtil;
066    import com.liferay.portal.service.persistence.UserUtil;
067    import com.liferay.portal.servlet.filters.cache.CacheUtil;
068    import com.liferay.portal.util.PortletKeys;
069    import com.liferay.portal.util.PropsValues;
070    import com.liferay.portlet.PortletPreferencesFactoryUtil;
071    import com.liferay.portlet.PortletPreferencesImpl;
072    import com.liferay.portlet.asset.NoSuchCategoryException;
073    import com.liferay.portlet.asset.NoSuchEntryException;
074    import com.liferay.portlet.asset.model.AssetCategory;
075    import com.liferay.portlet.asset.model.AssetCategoryConstants;
076    import com.liferay.portlet.asset.model.AssetEntry;
077    import com.liferay.portlet.asset.model.AssetVocabulary;
078    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
079    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
080    import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
081    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
082    import com.liferay.portlet.asset.service.persistence.AssetCategoryUtil;
083    import com.liferay.portlet.asset.service.persistence.AssetVocabularyUtil;
084    import com.liferay.portlet.expando.NoSuchTableException;
085    import com.liferay.portlet.expando.model.ExpandoColumn;
086    import com.liferay.portlet.expando.model.ExpandoTable;
087    import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
088    import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
089    import com.liferay.portlet.expando.util.ExpandoConverterUtil;
090    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
091    import com.liferay.portlet.messageboards.model.MBMessage;
092    import com.liferay.portlet.ratings.model.RatingsEntry;
093    
094    import java.io.File;
095    import java.io.Serializable;
096    
097    import java.util.ArrayList;
098    import java.util.Enumeration;
099    import java.util.HashMap;
100    import java.util.HashSet;
101    import java.util.List;
102    import java.util.Locale;
103    import java.util.Map;
104    
105    import org.apache.commons.lang.time.StopWatch;
106    
107    /**
108     * @author Brian Wing Shun Chan
109     * @author Joel Kozikowski
110     * @author Charles May
111     * @author Raymond Augé
112     * @author Jorge Ferrer
113     * @author Bruno Farache
114     * @author Zsigmond Rab
115     * @author Douglas Wong
116     */
117    public class PortletImporter {
118    
119            public void importPortletInfo(
120                            long userId, long plid, long groupId, String portletId,
121                            Map<String, String[]> parameterMap, File file)
122                    throws Exception {
123    
124                    try {
125                            ImportExportThreadLocal.setPortletImportInProcess(true);
126    
127                            doImportPortletInfo(
128                                    userId, plid, groupId, portletId, parameterMap, file);
129                    }
130                    finally {
131                            ImportExportThreadLocal.setPortletImportInProcess(false);
132    
133                            CacheUtil.clearCache();
134                            JournalContentUtil.clearCache();
135                            PermissionCacheUtil.clearCache();
136                    }
137            }
138    
139            protected String checkPortletPreferences(
140                            PortletDataContext portletDataContext, long companyId,
141                            long ownerId, int ownerType, long plid, String portletId,
142                            String xml)
143                    throws Exception {
144    
145                    String rootPotletId = PortletConstants.getRootPortletId(portletId);
146    
147                    if (!rootPotletId.equals(PortletKeys.ASSET_PUBLISHER)) {
148                            return xml;
149                    }
150    
151                    PortletPreferencesImpl portletPreferences =
152                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.
153                                    fromXML(companyId, ownerId, ownerType, plid, portletId, xml);
154    
155                     Enumeration<String> enu = portletPreferences.getNames();
156    
157                     while (enu.hasMoreElements()) {
158                            String name = enu.nextElement();
159    
160                            String value = GetterUtil.getString(
161                                    portletPreferences.getValue(name, StringPool.BLANK));
162    
163                            String prefix = "queryName";
164    
165                            if (value.equalsIgnoreCase("assetCategories") &&
166                                    name.startsWith(prefix)) {
167    
168                                    String index = name.substring(prefix.length(), name.length());
169    
170                                    String queryValuesName = "queryValues" + index;
171    
172                                    String[] importedCategoryPKs = portletPreferences.getValues(
173                                            queryValuesName, null);
174    
175                                    Map<Long, Long> assetCategoryPKs =
176                                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
177                                                    AssetCategory.class);
178    
179                                    String[] newCategoryPKs = new String[
180                                            importedCategoryPKs.length];
181    
182                                    int i = 0;
183    
184                                    for (String importedCategoryPK : importedCategoryPKs) {
185                                            newCategoryPKs[i++] = StringUtil.valueOf(
186                                                    assetCategoryPKs.get(new Long(importedCategoryPK)));
187                                    }
188    
189                                    portletPreferences.setValues(queryValuesName, newCategoryPKs);
190                            }
191                    }
192    
193                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
194            }
195    
196            protected void deletePortletData(
197                            PortletDataContext portletDataContext, String portletId, long plid)
198                    throws Exception {
199    
200                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
201                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
202    
203                    PortletPreferences portletPreferences =
204                            PortletPreferencesUtil.fetchByO_O_P_P(
205                                    ownerId, ownerType, plid, portletId);
206    
207                    if (portletPreferences == null) {
208                            portletPreferences =
209                                    new com.liferay.portal.model.impl.PortletPreferencesImpl();
210                    }
211    
212                    String xml = deletePortletData(
213                            portletDataContext, portletId, portletPreferences);
214    
215                    if (xml != null) {
216                            PortletPreferencesLocalServiceUtil.updatePreferences(
217                                    ownerId, ownerType, plid, portletId, xml);
218                    }
219            }
220    
221            protected String deletePortletData(
222                            PortletDataContext portletDataContext, String portletId,
223                            PortletPreferences portletPreferences)
224                    throws Exception {
225    
226                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
227                            portletDataContext.getCompanyId(), portletId);
228    
229                    if (portlet == null) {
230                            if (_log.isDebugEnabled()) {
231                                    _log.debug(
232                                            "Do not delete portlet data for " + portletId +
233                                                    " because the portlet does not exist");
234                            }
235    
236                            return null;
237                    }
238    
239                    PortletDataHandler portletDataHandler =
240                            portlet.getPortletDataHandlerInstance();
241    
242                    if (portletDataHandler == null) {
243                            if (_log.isDebugEnabled()) {
244                                    _log.debug(
245                                            "Do not delete portlet data for " + portletId +
246                                                    " because the portlet does not have a " +
247                                                            "PortletDataHandler");
248                            }
249    
250                            return null;
251                    }
252    
253                    if (_log.isDebugEnabled()) {
254                            _log.debug("Deleting data for " + portletId);
255                    }
256    
257                    PortletPreferencesImpl portletPreferencesImpl =
258                            (PortletPreferencesImpl)
259                                    PortletPreferencesFactoryUtil.fromDefaultXML(
260                                            portletPreferences.getPreferences());
261    
262                    try {
263                            portletPreferencesImpl =
264                                    (PortletPreferencesImpl)portletDataHandler.deleteData(
265                                            portletDataContext, portletId, portletPreferencesImpl);
266                    }
267                    finally {
268                            portletDataContext.setGroupId(portletDataContext.getScopeGroupId());
269                    }
270    
271                    if (portletPreferencesImpl == null) {
272                            return null;
273                    }
274    
275                    return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
276            }
277    
278            protected void doImportPortletInfo(
279                            long userId, long plid, long groupId, String portletId,
280                            Map<String, String[]> parameterMap, File file)
281                    throws Exception {
282    
283                    boolean deletePortletData = MapUtil.getBoolean(
284                            parameterMap, PortletDataHandlerKeys.DELETE_PORTLET_DATA);
285                    boolean importPermissions = MapUtil.getBoolean(
286                            parameterMap, PortletDataHandlerKeys.PERMISSIONS);
287                    boolean importUserPermissions = MapUtil.getBoolean(
288                            parameterMap, PortletDataHandlerKeys.PERMISSIONS);
289                    boolean importPortletData = MapUtil.getBoolean(
290                            parameterMap, PortletDataHandlerKeys.PORTLET_DATA);
291                    boolean importPortletArchivedSetups = MapUtil.getBoolean(
292                            parameterMap, PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS);
293                    boolean importPortletSetup = MapUtil.getBoolean(
294                            parameterMap, PortletDataHandlerKeys.PORTLET_SETUP);
295                    boolean importPortletUserPreferences = MapUtil.getBoolean(
296                            parameterMap, PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
297                    String userIdStrategyString = MapUtil.getString(
298                            parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY);
299    
300                    StopWatch stopWatch = null;
301    
302                    if (_log.isInfoEnabled()) {
303                            stopWatch = new StopWatch();
304    
305                            stopWatch.start();
306                    }
307    
308                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
309    
310                    User user = UserUtil.findByPrimaryKey(userId);
311    
312                    UserIdStrategy userIdStrategy = getUserIdStrategy(
313                            user, userIdStrategyString);
314    
315                    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);
316    
317                    PortletDataContext portletDataContext = new PortletDataContextImpl(
318                            layout.getCompanyId(), groupId, parameterMap, new HashSet<String>(),
319                            userIdStrategy, zipReader);
320    
321                    portletDataContext.setPortetDataContextListener(
322                            new PortletDataContextListenerImpl(portletDataContext));
323    
324                    portletDataContext.setPlid(plid);
325                    portletDataContext.setPrivateLayout(layout.isPrivateLayout());
326    
327                    // Manifest
328    
329                    String xml = portletDataContext.getZipEntryAsString("/manifest.xml");
330    
331                    Element rootElement = null;
332    
333                    try {
334                            Document document = SAXReaderUtil.read(xml);
335    
336                            rootElement = document.getRootElement();
337                    }
338                    catch (Exception e) {
339                            throw new LARFileException("Unable to read /manifest.xml");
340                    }
341    
342                    // Build compatibility
343    
344                    Element headerElement = rootElement.element("header");
345    
346                    int buildNumber = ReleaseInfo.getBuildNumber();
347    
348                    int importBuildNumber = GetterUtil.getInteger(
349                            headerElement.attributeValue("build-number"));
350    
351                    if (buildNumber != importBuildNumber) {
352                            throw new LayoutImportException(
353                                    "LAR build number " + importBuildNumber + " does not match " +
354                                            "portal build number " + buildNumber);
355                    }
356    
357                    // Type compatibility
358    
359                    String type = headerElement.attributeValue("type");
360    
361                    if (!type.equals("portlet")) {
362                            throw new LARTypeException(
363                                    "Invalid type of LAR file (" + type + ")");
364                    }
365    
366                    // Portlet compatibility
367    
368                    String rootPortletId = headerElement.attributeValue("root-portlet-id");
369    
370                    if (!PortletConstants.getRootPortletId(portletId).equals(
371                                    rootPortletId)) {
372    
373                            throw new PortletIdException("Invalid portlet id " + rootPortletId);
374                    }
375    
376                    // Import group id
377    
378                    long sourceGroupId = GetterUtil.getLong(
379                            headerElement.attributeValue("group-id"));
380    
381                    portletDataContext.setSourceGroupId(sourceGroupId);
382    
383                    // Read asset categories, asset tags, comments, locks, and ratings
384                    // entries to make them available to the data handlers through the
385                    // context
386    
387                    if (importPermissions) {
388                            _permissionImporter.readPortletDataPermissions(portletDataContext);
389                    }
390    
391                    readAssetCategories(portletDataContext);
392                    readAssetTags(portletDataContext);
393                    readComments(portletDataContext);
394                    readExpandoTables(portletDataContext);
395                    readLocks(portletDataContext);
396                    readRatingsEntries(portletDataContext);
397    
398                    // Delete portlet data
399    
400                    if (_log.isDebugEnabled()) {
401                            _log.debug("Deleting portlet data");
402                    }
403    
404                    if (deletePortletData) {
405                            deletePortletData(portletDataContext, portletId, plid);
406                    }
407    
408                    Element portletElement = null;
409    
410                    try {
411                            portletElement = rootElement.element("portlet");
412    
413                            Document portletDocument = SAXReaderUtil.read(
414                                    portletDataContext.getZipEntryAsString(
415                                            portletElement.attributeValue("path")));
416    
417                            portletElement = portletDocument.getRootElement();
418                    }
419                    catch (DocumentException de) {
420                            throw new SystemException(de);
421                    }
422    
423                    setPortletScope(portletDataContext, portletElement);
424    
425                    try {
426    
427                            // Portlet preferences
428    
429                            importPortletPreferences(
430                                    portletDataContext, layout.getCompanyId(), groupId, layout,
431                                    portletId, portletElement, importPortletSetup,
432                                    importPortletArchivedSetups, importPortletUserPreferences,
433                                    true);
434    
435                            // Portlet data
436    
437                            Element portletDataElement = portletElement.element("portlet-data");
438    
439                            if (importPortletData && (portletDataElement != null)) {
440                                    if (_log.isDebugEnabled()) {
441                                            _log.debug("Importing portlet data");
442                                    }
443    
444                                    importPortletData(
445                                            portletDataContext, portletId, plid, portletDataElement);
446                            }
447                    }
448                    finally {
449                            resetPortletScope(portletDataContext, groupId);
450                    }
451    
452                    // Portlet permissions
453    
454                    if (importPermissions) {
455                            if (_log.isDebugEnabled()) {
456                                    _log.debug("Importing portlet permissions");
457                            }
458    
459                            LayoutCache layoutCache = new LayoutCache();
460    
461                            _permissionImporter.importPortletPermissions(
462                                    layoutCache, layout.getCompanyId(), groupId, userId, layout,
463                                    portletElement, portletId, importUserPermissions);
464    
465                            if ((userId > 0) &&
466                                    ((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) ||
467                                     (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6))) {
468    
469                                    Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
470    
471                                    indexer.reindex(userId);
472                            }
473                    }
474    
475                    // Asset links
476    
477                    if (_log.isDebugEnabled()) {
478                            _log.debug("Importing asset links");
479                    }
480    
481                    readAssetLinks(portletDataContext);
482    
483                    if (_log.isInfoEnabled()) {
484                            _log.info(
485                                    "Importing portlet takes " + stopWatch.getTime() + " ms");
486                    }
487    
488                    zipReader.close();
489            }
490    
491            protected String getAssetCategoryPath(
492                    PortletDataContext portletDataContext, long assetCategoryId) {
493    
494                    StringBundler sb = new StringBundler(6);
495    
496                    sb.append(portletDataContext.getSourceRootPath());
497                    sb.append("/categories/");
498                    sb.append(assetCategoryId);
499                    sb.append(".xml");
500    
501                    return sb.toString();
502            }
503    
504            protected Map<Locale, String> getAssetCategoryTitleMap(
505                    AssetCategory assetCategory) {
506    
507                    Map<Locale, String> titleMap = assetCategory.getTitleMap();
508    
509                    if (titleMap == null) {
510                            titleMap = new HashMap<Locale, String>();
511                    }
512    
513                    Locale locale = LocaleUtil.getDefault();
514    
515                    String title = titleMap.get(locale);
516    
517                    if (Validator.isNull(title)) {
518                            titleMap.put(locale, assetCategory.getName());
519                    }
520    
521                    return titleMap;
522            }
523    
524            protected Map<Locale, String> getAssetVocabularyTitleMap(
525                    AssetVocabulary assetVocabulary) {
526    
527                    Map<Locale, String> titleMap = assetVocabulary.getTitleMap();
528    
529                    if (titleMap == null) {
530                            titleMap = new HashMap<Locale, String>();
531                    }
532    
533                    Locale locale = LocaleUtil.getDefault();
534    
535                    String title = titleMap.get(locale);
536    
537                    if (Validator.isNull(title)) {
538                            titleMap.put(locale, assetVocabulary.getName());
539                    }
540    
541                    return titleMap;
542            }
543    
544            protected UserIdStrategy getUserIdStrategy(
545                    User user, String userIdStrategy) {
546    
547                    if (UserIdStrategy.ALWAYS_CURRENT_USER_ID.equals(userIdStrategy)) {
548                            return new AlwaysCurrentUserIdStrategy(user);
549                    }
550    
551                    return new CurrentUserIdStrategy(user);
552            }
553    
554            protected void importAssetCategory(
555                            PortletDataContext portletDataContext,
556                            Map<Long, Long> assetVocabularyPKs,
557                            Map<Long, Long> assetCategoryPKs,
558                            Map<String, String> assetCategoryUuids,
559                            Element assetCategoryElement, AssetCategory assetCategory)
560                    throws Exception {
561    
562                    long userId = portletDataContext.getUserId(assetCategory.getUserUuid());
563                    long assetVocabularyId = MapUtil.getLong(
564                            assetVocabularyPKs, assetCategory.getVocabularyId(),
565                            assetCategory.getVocabularyId());
566                    long parentAssetCategoryId = MapUtil.getLong(
567                            assetCategoryPKs, assetCategory.getParentCategoryId(),
568                            assetCategory.getParentCategoryId());
569    
570                    if ((parentAssetCategoryId !=
571                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
572                            (parentAssetCategoryId == assetCategory.getParentCategoryId())) {
573    
574                            String path = getAssetCategoryPath(
575                                    portletDataContext, parentAssetCategoryId);
576    
577                            AssetCategory parentAssetCategory =
578                                    (AssetCategory)portletDataContext.getZipEntryAsObject(path);
579    
580                            Node parentCategoryNode =
581                                    assetCategoryElement.getParent().selectSingleNode(
582                                            "./category[@path='" + path + "']");
583    
584                            if (parentCategoryNode != null) {
585                                    importAssetCategory(
586                                            portletDataContext, assetVocabularyPKs, assetCategoryPKs,
587                                            assetCategoryUuids, (Element)parentCategoryNode,
588                                            parentAssetCategory);
589    
590                                    parentAssetCategoryId = MapUtil.getLong(
591                                            assetCategoryPKs, assetCategory.getParentCategoryId(),
592                                            assetCategory.getParentCategoryId());
593                            }
594                    }
595    
596                    ServiceContext serviceContext = new ServiceContext();
597    
598                    serviceContext.setAddGroupPermissions(true);
599                    serviceContext.setAddGuestPermissions(true);
600                    serviceContext.setCreateDate(assetCategory.getCreateDate());
601                    serviceContext.setModifiedDate(assetCategory.getModifiedDate());
602                    serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
603    
604                    AssetCategory importedAssetCategory = null;
605    
606                    try {
607                            if (parentAssetCategoryId !=
608                                            AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
609    
610                                    AssetCategoryUtil.findByPrimaryKey(parentAssetCategoryId);
611                            }
612    
613                            List<Element> propertyElements = assetCategoryElement.elements(
614                                    "property");
615    
616                            String[] properties = new String[propertyElements.size()];
617    
618                            for (int i = 0; i < propertyElements.size(); i++) {
619                                    Element propertyElement = propertyElements.get(i);
620    
621                                    String key = propertyElement.attributeValue("key");
622                                    String value = propertyElement.attributeValue("value");
623    
624                                    properties[i] = key.concat(StringPool.COLON).concat(value);
625                            }
626    
627                            AssetCategory existingAssetCategory =
628                                    AssetCategoryUtil.fetchByP_N_V(
629                                            parentAssetCategoryId, assetCategory.getName(),
630                                            assetVocabularyId);
631    
632                            if (existingAssetCategory == null) {
633                                    serviceContext.setUuid(assetCategory.getUuid());
634    
635                                    importedAssetCategory =
636                                            AssetCategoryLocalServiceUtil.addCategory(
637                                                    userId, parentAssetCategoryId,
638                                                    getAssetCategoryTitleMap(assetCategory),
639                                                    assetCategory.getDescriptionMap(), assetVocabularyId,
640                                                    properties, serviceContext);
641                            }
642                            else {
643                                    importedAssetCategory =
644                                            AssetCategoryLocalServiceUtil.updateCategory(
645                                                    userId, existingAssetCategory.getCategoryId(),
646                                                    parentAssetCategoryId,
647                                                    getAssetCategoryTitleMap(assetCategory),
648                                                    assetCategory.getDescriptionMap(), assetVocabularyId,
649                                                    properties, serviceContext);
650                            }
651    
652                            assetCategoryPKs.put(
653                                    assetCategory.getCategoryId(),
654                                    importedAssetCategory.getCategoryId());
655    
656                            assetCategoryUuids.put(
657                                    assetCategory.getUuid(), importedAssetCategory.getUuid());
658    
659                            portletDataContext.importPermissions(
660                                    AssetCategory.class, assetCategory.getCategoryId(),
661                                    importedAssetCategory.getCategoryId());
662                    }
663                    catch (NoSuchCategoryException nsce) {
664                            _log.error(
665                                    "Could not find the parent category for category " +
666                                            assetCategory.getCategoryId());
667                    }
668            }
669    
670            protected void importAssetVocabulary(
671                            PortletDataContext portletDataContext,
672                            Map<Long, Long> assetVocabularyPKs, Element assetVocabularyElement,
673                            AssetVocabulary assetVocabulary)
674                    throws Exception {
675    
676                    long userId = portletDataContext.getUserId(
677                            assetVocabulary.getUserUuid());
678                    long groupId = portletDataContext.getScopeGroupId();
679    
680                    ServiceContext serviceContext = new ServiceContext();
681    
682                    serviceContext.setAddGroupPermissions(true);
683                    serviceContext.setAddGuestPermissions(true);
684                    serviceContext.setCreateDate(assetVocabulary.getCreateDate());
685                    serviceContext.setModifiedDate(assetVocabulary.getModifiedDate());
686                    serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
687    
688                    AssetVocabulary importedAssetVocabulary = null;
689    
690                    AssetVocabulary existingAssetVocabulary =
691                            AssetVocabularyUtil.fetchByG_N(groupId, assetVocabulary.getName());
692    
693                    if (existingAssetVocabulary == null) {
694                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
695                                    portletDataContext.getCompanyId());
696    
697                            existingAssetVocabulary = AssetVocabularyUtil.fetchByG_N(
698                                    companyGroup.getGroupId(), assetVocabulary.getName());
699                    }
700    
701                    if (existingAssetVocabulary == null) {
702                            serviceContext.setUuid(assetVocabulary.getUuid());
703    
704                            importedAssetVocabulary =
705                                    AssetVocabularyLocalServiceUtil.addVocabulary(
706                                            userId, assetVocabulary.getTitle(),
707                                            getAssetVocabularyTitleMap(assetVocabulary),
708                                            assetVocabulary.getDescriptionMap(),
709                                            assetVocabulary.getSettings(), serviceContext);
710                    }
711                    else {
712                            importedAssetVocabulary =
713                                    AssetVocabularyLocalServiceUtil.updateVocabulary(
714                                            existingAssetVocabulary.getVocabularyId(),
715                                            assetVocabulary.getTitle(),
716                                            getAssetVocabularyTitleMap(assetVocabulary),
717                                            assetVocabulary.getDescriptionMap(),
718                                            assetVocabulary.getSettings(), serviceContext);
719                    }
720    
721                    assetVocabularyPKs.put(
722                            assetVocabulary.getVocabularyId(),
723                            importedAssetVocabulary.getVocabularyId());
724    
725                    portletDataContext.importPermissions(
726                            AssetVocabulary.class, assetVocabulary.getVocabularyId(),
727                            importedAssetVocabulary.getVocabularyId());
728            }
729    
730            protected void importPortletData(
731                            PortletDataContext portletDataContext, String portletId, long plid,
732                            Element portletDataElement)
733                    throws Exception {
734    
735                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
736                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
737    
738                    PortletPreferences portletPreferences =
739                            PortletPreferencesUtil.fetchByO_O_P_P(
740                                    ownerId, ownerType, plid, portletId);
741    
742                    if (portletPreferences == null) {
743                            portletPreferences =
744                                    new com.liferay.portal.model.impl.PortletPreferencesImpl();
745                    }
746    
747                    String xml = importPortletData(
748                            portletDataContext, portletId, portletPreferences,
749                            portletDataElement);
750    
751                    if (xml != null) {
752                            PortletPreferencesLocalServiceUtil.updatePreferences(
753                                    ownerId, ownerType, plid, portletId, xml);
754                    }
755            }
756    
757            protected String importPortletData(
758                            PortletDataContext portletDataContext, String portletId,
759                            PortletPreferences portletPreferences, Element portletDataElement)
760                    throws Exception {
761    
762                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
763                            portletDataContext.getCompanyId(), portletId);
764    
765                    if (portlet == null) {
766                            if (_log.isDebugEnabled()) {
767                                    _log.debug(
768                                            "Do not import portlet data for " + portletId +
769                                                    " because the portlet does not exist");
770                            }
771    
772                            return null;
773                    }
774    
775                    PortletDataHandler portletDataHandler =
776                            portlet.getPortletDataHandlerInstance();
777    
778                    if (portletDataHandler == null) {
779                            if (_log.isDebugEnabled()) {
780                                    _log.debug(
781                                            "Do not import portlet data for " + portletId +
782                                                    " because the portlet does not have a " +
783                                                            "PortletDataHandler");
784                            }
785    
786                            return null;
787                    }
788    
789                    if (_log.isDebugEnabled()) {
790                            _log.debug("Importing data for " + portletId);
791                    }
792    
793                    PortletPreferencesImpl portletPreferencesImpl = null;
794    
795                    if (portletPreferences != null) {
796                            portletPreferencesImpl =
797                                    (PortletPreferencesImpl)
798                                            PortletPreferencesFactoryUtil.fromDefaultXML(
799                                                    portletPreferences.getPreferences());
800                    }
801    
802                    String portletData = portletDataContext.getZipEntryAsString(
803                            portletDataElement.attributeValue("path"));
804    
805                    try {
806                            portletPreferencesImpl =
807                                    (PortletPreferencesImpl)portletDataHandler.importData(
808                                            portletDataContext, portletId, portletPreferencesImpl,
809                                            portletData);
810                    }
811                    catch (Exception e) {
812                            throw e;
813                    }
814    
815                    if (portletPreferencesImpl == null) {
816                            return null;
817                    }
818    
819                    return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
820            }
821    
822            protected void importPortletPreferences(
823                            PortletDataContext portletDataContext, long companyId, long groupId,
824                            Layout layout, String portletId, Element parentElement,
825                            boolean importPortletSetup, boolean importPortletArchivedSetups,
826                            boolean importPortletUserPreferences, boolean preserveScopeLayoutId)
827                    throws Exception {
828    
829                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
830                    long plid = 0;
831                    String scopeType = StringPool.BLANK;
832                    String scopeLayoutUuid = StringPool.BLANK;
833    
834                    if (layout != null) {
835                            plid = layout.getPlid();
836    
837                            if (preserveScopeLayoutId && (portletId != null)) {
838                                    javax.portlet.PortletPreferences jxPreferences =
839                                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
840                                                    layout, portletId);
841    
842                                    scopeType = GetterUtil.getString(
843                                            jxPreferences.getValue("lfrScopeType", null));
844                                    scopeLayoutUuid = GetterUtil.getString(
845                                            jxPreferences.getValue("lfrScopeLayoutUuid", null));
846    
847                                    portletDataContext.setScopeType(scopeType);
848                                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
849                            }
850                    }
851    
852                    List<Element> portletPreferencesElements = parentElement.elements(
853                            "portlet-preferences");
854    
855                    for (Element portletPreferencesElement : portletPreferencesElements) {
856                            String path = portletPreferencesElement.attributeValue("path");
857    
858                            if (portletDataContext.isPathNotProcessed(path)) {
859                                    String xml = null;
860    
861                                    Element element = null;
862    
863                                    try {
864                                            xml = portletDataContext.getZipEntryAsString(path);
865    
866                                            Document preferencesDocument = SAXReaderUtil.read(xml);
867    
868                                            element = preferencesDocument.getRootElement();
869                                    }
870                                    catch (DocumentException de) {
871                                            throw new SystemException(de);
872                                    }
873    
874                                    long ownerId = GetterUtil.getLong(
875                                            element.attributeValue("owner-id"));
876                                    int ownerType = GetterUtil.getInteger(
877                                            element.attributeValue("owner-type"));
878    
879                                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
880                                            continue;
881                                    }
882    
883                                    if (((ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) ||
884                                            (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT)) &&
885                                            !importPortletSetup) {
886    
887                                            continue;
888                                    }
889    
890                                    if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) &&
891                                            !importPortletArchivedSetups) {
892    
893                                            continue;
894                                    }
895    
896                                    if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) &&
897                                            (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT) &&
898                                            !importPortletUserPreferences) {
899    
900                                            continue;
901                                    }
902    
903                                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
904                                            plid = PortletKeys.PREFS_PLID_SHARED;
905                                            ownerId = portletDataContext.getScopeGroupId();
906                                    }
907    
908                                    boolean defaultUser = GetterUtil.getBoolean(
909                                            element.attributeValue("default-user"));
910    
911                                    if (portletId == null) {
912                                            portletId = element.attributeValue("portlet-id");
913                                    }
914    
915                                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
916                                            portletId = PortletConstants.getRootPortletId(portletId);
917    
918                                            String userUuid = element.attributeValue(
919                                                    "archive-user-uuid");
920                                            String name = element.attributeValue("archive-name");
921    
922                                            long userId = portletDataContext.getUserId(userUuid);
923    
924                                            PortletItem portletItem =
925                                                    PortletItemLocalServiceUtil.updatePortletItem(
926                                                            userId, groupId, name, portletId,
927                                                            PortletPreferences.class.getName());
928    
929                                            plid = 0;
930                                            ownerId = portletItem.getPortletItemId();
931                                    }
932    
933                                    if (defaultUser) {
934                                            ownerId = defaultUserId;
935                                    }
936    
937                                    xml = checkPortletPreferences(
938                                            portletDataContext, companyId, ownerId, ownerType, plid,
939                                            portletId, xml);
940    
941                                    PortletPreferencesLocalServiceUtil.updatePreferences(
942                                            ownerId, ownerType, plid, portletId, xml);
943                            }
944                    }
945    
946                    if (preserveScopeLayoutId && (layout != null)) {
947                            javax.portlet.PortletPreferences jxPreferences =
948                                    PortletPreferencesFactoryUtil.getLayoutPortletSetup(
949                                            layout, portletId);
950    
951                            try {
952                                    jxPreferences.setValue("lfrScopeType", scopeType);
953                                    jxPreferences.setValue(
954                                            "lfrScopeLayoutUuid", scopeLayoutUuid);
955    
956                                    jxPreferences.store();
957                            }
958                            finally {
959                                    portletDataContext.setScopeType(scopeType);
960                                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
961                            }
962                    }
963            }
964    
965            protected void readAssetCategories(PortletDataContext portletDataContext)
966                    throws Exception {
967    
968                    String xml = portletDataContext.getZipEntryAsString(
969                            portletDataContext.getSourceRootPath() +
970                                    "/categories-hierarchy.xml");
971    
972                    if (xml == null) {
973                            return;
974                    }
975    
976                    Document document = SAXReaderUtil.read(xml);
977    
978                    Element rootElement = document.getRootElement();
979    
980                    Element assetVocabulariesElement = rootElement.element("vocabularies");
981    
982                    List<Element> assetVocabularyElements =
983                            assetVocabulariesElement.elements("vocabulary");
984    
985                    Map<Long, Long> assetVocabularyPKs =
986                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
987                                    AssetVocabulary.class);
988    
989                    for (Element assetVocabularyElement : assetVocabularyElements) {
990                            String path = assetVocabularyElement.attributeValue("path");
991    
992                            if (!portletDataContext.isPathNotProcessed(path)) {
993                                    continue;
994                            }
995    
996                            AssetVocabulary assetVocabulary =
997                                    (AssetVocabulary)portletDataContext.getZipEntryAsObject(path);
998    
999                            importAssetVocabulary(
1000                                    portletDataContext, assetVocabularyPKs, assetVocabularyElement,
1001                                    assetVocabulary);
1002                    }
1003    
1004                    Element assetCategoriesElement = rootElement.element("categories");
1005    
1006                    List<Element> assetCategoryElements = assetCategoriesElement.elements(
1007                            "category");
1008    
1009                    Map<Long, Long> assetCategoryPKs =
1010                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1011                                    AssetCategory.class);
1012    
1013                    Map<String, String> assetCategoryUuids =
1014                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
1015                                    AssetCategory.class.getName() + "uuid");
1016    
1017                    for (Element assetCategoryElement : assetCategoryElements) {
1018                            String path = assetCategoryElement.attributeValue("path");
1019    
1020                            if (!portletDataContext.isPathNotProcessed(path)) {
1021                                    continue;
1022                            }
1023    
1024                            AssetCategory assetCategory =
1025                                    (AssetCategory)portletDataContext.getZipEntryAsObject(path);
1026    
1027                            importAssetCategory(
1028                                    portletDataContext, assetVocabularyPKs, assetCategoryPKs,
1029                                    assetCategoryUuids, assetCategoryElement, assetCategory);
1030                    }
1031    
1032                    Element assetsElement = rootElement.element("assets");
1033    
1034                    List<Element> assetElements = assetsElement.elements("asset");
1035    
1036                    for (Element assetElement : assetElements) {
1037                            String className = GetterUtil.getString(
1038                                    assetElement.attributeValue("class-name"));
1039                            long classPK = GetterUtil.getLong(
1040                                    assetElement.attributeValue("class-pk"));
1041                            String[] assetCategoryUuidArray = StringUtil.split(
1042                                    GetterUtil.getString(
1043                                            assetElement.attributeValue("category-uuids")));
1044    
1045                            long[] assetCategoryIds = new long[0];
1046    
1047                            for (String assetCategoryUuid : assetCategoryUuidArray) {
1048                                    assetCategoryUuid = MapUtil.getString(
1049                                            assetCategoryUuids, assetCategoryUuid,
1050                                            assetCategoryUuid);
1051    
1052                                    AssetCategory assetCategory = AssetCategoryUtil.fetchByUUID_G(
1053                                            assetCategoryUuid, portletDataContext.getScopeGroupId());
1054    
1055                                    if (assetCategory == null) {
1056                                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
1057                                                    portletDataContext.getCompanyId());
1058    
1059                                            assetCategory = AssetCategoryUtil.fetchByUUID_G(
1060                                                    assetCategoryUuid, companyGroup.getGroupId());
1061                                    }
1062    
1063                                    if (assetCategory != null) {
1064                                            assetCategoryIds = ArrayUtil.append(
1065                                                    assetCategoryIds, assetCategory.getCategoryId());
1066                                    }
1067                            }
1068    
1069                            portletDataContext.addAssetCategories(
1070                                    className, classPK, assetCategoryIds);
1071                    }
1072            }
1073    
1074            protected void readAssetLinks(PortletDataContext portletDataContext)
1075                    throws Exception {
1076    
1077                    String xml = portletDataContext.getZipEntryAsString(
1078                            portletDataContext.getSourceRootPath() + "/links.xml");
1079    
1080                    if (xml == null) {
1081                            return;
1082                    }
1083    
1084                    Document document = SAXReaderUtil.read(xml);
1085    
1086                    Element rootElement = document.getRootElement();
1087    
1088                    List<Element> assetLinkElements = rootElement.elements("asset-link");
1089    
1090                    for (Element assetLinkElement : assetLinkElements) {
1091                            String sourceUuid = GetterUtil.getString(
1092                                    assetLinkElement.attributeValue("source-uuid"));
1093                            String[] assetEntryUuidArray = StringUtil.split(
1094                                    GetterUtil.getString(
1095                                            assetLinkElement.attributeValue("target-uuids")));
1096                            int assetLinkType = GetterUtil.getInteger(
1097                                    assetLinkElement.attributeValue("type"));
1098    
1099                            List<Long> assetEntryIds = new ArrayList<Long>();
1100    
1101                            for (String assetEntryUuid : assetEntryUuidArray) {
1102                                    try {
1103                                            AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
1104                                                    portletDataContext.getScopeGroupId(), assetEntryUuid);
1105    
1106                                            assetEntryIds.add(assetEntry.getEntryId());
1107                                    }
1108                                    catch (NoSuchEntryException nsee) {
1109                                    }
1110                            }
1111    
1112                            if (assetEntryIds.isEmpty()) {
1113                                    continue;
1114                            }
1115    
1116                            long[] assetEntryIdsArray = ArrayUtil.toArray(
1117                                    assetEntryIds.toArray(new Long[assetEntryIds.size()]));
1118    
1119                            try {
1120                                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(
1121                                            portletDataContext.getScopeGroupId(), sourceUuid);
1122    
1123                                    AssetLinkLocalServiceUtil.updateLinks(
1124                                            assetEntry.getUserId(), assetEntry.getEntryId(),
1125                                            assetEntryIdsArray, assetLinkType);
1126                            }
1127                            catch (NoSuchEntryException nsee) {
1128                            }
1129                    }
1130            }
1131    
1132            protected void readAssetTags(PortletDataContext portletDataContext)
1133                    throws Exception {
1134    
1135                    String xml = portletDataContext.getZipEntryAsString(
1136                            portletDataContext.getSourceRootPath() + "/tags.xml");
1137    
1138                    if (xml == null) {
1139                            return;
1140                    }
1141    
1142                    Document document = SAXReaderUtil.read(xml);
1143    
1144                    Element rootElement = document.getRootElement();
1145    
1146                    List<Element> assetElements = rootElement.elements("asset");
1147    
1148                    for (Element assetElement : assetElements) {
1149                            String className = GetterUtil.getString(
1150                                    assetElement.attributeValue("class-name"));
1151                            long classPK = GetterUtil.getLong(
1152                                    assetElement.attributeValue("class-pk"));
1153                            String assetTagNames = GetterUtil.getString(
1154                                    assetElement.attributeValue("tags"));
1155    
1156                            portletDataContext.addAssetTags(
1157                                    className, classPK, StringUtil.split(assetTagNames));
1158                    }
1159            }
1160    
1161            protected void readComments(PortletDataContext portletDataContext)
1162                    throws Exception {
1163    
1164                    String xml = portletDataContext.getZipEntryAsString(
1165                            portletDataContext.getSourceRootPath() + "/comments.xml");
1166    
1167                    if (xml == null) {
1168                            return;
1169                    }
1170    
1171                    Document document = SAXReaderUtil.read(xml);
1172    
1173                    Element rootElement = document.getRootElement();
1174    
1175                    List<Element> assetElements = rootElement.elements("asset");
1176    
1177                    for (Element assetElement : assetElements) {
1178                            String path = assetElement.attributeValue("path");
1179                            String className = assetElement.attributeValue("class-name");
1180                            long classPK = GetterUtil.getLong(
1181                                    assetElement.attributeValue("class-pk"));
1182    
1183                            List<String> zipFolderEntries =
1184                                    portletDataContext.getZipFolderEntries(path);
1185    
1186                            List<MBMessage> mbMessages = new ArrayList<MBMessage>();
1187    
1188                            for (String zipFolderEntry : zipFolderEntries) {
1189                                    MBMessage mbMessage =
1190                                            (MBMessage)portletDataContext.getZipEntryAsObject(
1191                                                    zipFolderEntry);
1192    
1193                                    if (mbMessage != null) {
1194                                            mbMessages.add(mbMessage);
1195                                    }
1196                            }
1197    
1198                            portletDataContext.addComments(className, classPK, mbMessages);
1199                    }
1200            }
1201    
1202            protected void readExpandoTables(PortletDataContext portletDataContext)
1203                    throws Exception {
1204    
1205                    String xml = portletDataContext.getZipEntryAsString(
1206                            portletDataContext.getSourceRootPath() + "/expando-tables.xml");
1207    
1208                    if (xml == null) {
1209                            return;
1210                    }
1211    
1212                    Document document = SAXReaderUtil.read(xml);
1213    
1214                    Element rootElement = document.getRootElement();
1215    
1216                    List<Element> expandoTableElements = rootElement.elements(
1217                            "expando-table");
1218    
1219                    for (Element expandoTableElement : expandoTableElements) {
1220                            String className = expandoTableElement.attributeValue(
1221                                    "class-name");
1222    
1223                            ExpandoTable expandoTable = null;
1224    
1225                            try {
1226                                    expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(
1227                                            portletDataContext.getCompanyId(), className);
1228                            }
1229                            catch (NoSuchTableException nste) {
1230                                    expandoTable =
1231                                            ExpandoTableLocalServiceUtil.addDefaultTable(
1232                                                    portletDataContext.getCompanyId(), className);
1233                            }
1234    
1235                            List<Element> expandoColumnElements =
1236                                    expandoTableElement.elements("expando-column");
1237    
1238                            for (Element expandoColumnElement : expandoColumnElements) {
1239                                    long columnId = GetterUtil.getLong(
1240                                            expandoColumnElement.attributeValue("column-id"));
1241                                    String name = expandoColumnElement.attributeValue("name");
1242                                    int type = GetterUtil.getInteger(
1243                                            expandoColumnElement.attributeValue("type"));
1244                                    String defaultData = expandoColumnElement.elementText(
1245                                            "default-data");
1246                                    String typeSettings = expandoColumnElement.elementText(
1247                                            "type-settings");
1248    
1249                                    Serializable defaultDataObject =
1250                                            ExpandoConverterUtil.getAttributeFromString(
1251                                                    type, defaultData);
1252    
1253                                    ExpandoColumn expandoColumn =
1254                                            ExpandoColumnLocalServiceUtil.getColumn(
1255                                                    expandoTable.getTableId(), name);
1256    
1257                                    if (expandoColumn != null) {
1258                                            ExpandoColumnLocalServiceUtil.updateColumn(
1259                                                    expandoColumn.getColumnId(), name, type,
1260                                                    defaultDataObject);
1261                                    }
1262                                    else {
1263                                            expandoColumn = ExpandoColumnLocalServiceUtil.addColumn(
1264                                                    expandoTable.getTableId(), name, type,
1265                                                    defaultDataObject);
1266                                    }
1267    
1268                                    ExpandoColumnLocalServiceUtil.updateTypeSettings(
1269                                            expandoColumn.getColumnId(), typeSettings);
1270    
1271                                    portletDataContext.importPermissions(
1272                                            ExpandoColumn.class, columnId, expandoColumn.getColumnId());
1273                            }
1274                    }
1275            }
1276    
1277            protected void readLocks(PortletDataContext portletDataContext)
1278                    throws Exception {
1279    
1280                    String xml = portletDataContext.getZipEntryAsString(
1281                            portletDataContext.getSourceRootPath() + "/locks.xml");
1282    
1283                    if (xml == null) {
1284                            return;
1285                    }
1286    
1287                    Document document = SAXReaderUtil.read(xml);
1288    
1289                    Element rootElement = document.getRootElement();
1290    
1291                    List<Element> assetElements = rootElement.elements("asset");
1292    
1293                    for (Element assetElement : assetElements) {
1294                            String path = assetElement.attributeValue("path");
1295                            String className = assetElement.attributeValue("class-name");
1296                            String key = assetElement.attributeValue("key");
1297    
1298                            Lock lock = (Lock)portletDataContext.getZipEntryAsObject(path);
1299    
1300                            if (lock != null) {
1301                                    portletDataContext.addLocks(className, key, lock);
1302                            }
1303                    }
1304            }
1305    
1306            protected void readRatingsEntries(PortletDataContext portletDataContext)
1307                    throws Exception {
1308    
1309                    String xml = portletDataContext.getZipEntryAsString(
1310                            portletDataContext.getSourceRootPath() + "/ratings.xml");
1311    
1312                    if (xml == null) {
1313                            return;
1314                    }
1315    
1316                    Document document = SAXReaderUtil.read(xml);
1317    
1318                    Element rootElement = document.getRootElement();
1319    
1320                    List<Element> assetElements = rootElement.elements("asset");
1321    
1322                    for (Element assetElement : assetElements) {
1323                            String path = assetElement.attributeValue("path");
1324                            String className = assetElement.attributeValue("class-name");
1325                            long classPK = GetterUtil.getLong(
1326                                    assetElement.attributeValue("class-pk"));
1327    
1328                            List<String> zipFolderEntries =
1329                                    portletDataContext.getZipFolderEntries(path);
1330    
1331                            List<RatingsEntry> ratingsEntries = new ArrayList<RatingsEntry>();
1332    
1333                            for (String zipFolderEntry : zipFolderEntries) {
1334                                    RatingsEntry ratingsEntry =
1335                                            (RatingsEntry)portletDataContext.getZipEntryAsObject(
1336                                                    zipFolderEntry);
1337    
1338                                    if (ratingsEntry != null) {
1339                                            ratingsEntries.add(ratingsEntry);
1340                                    }
1341                            }
1342    
1343                            portletDataContext.addRatingsEntries(
1344                                    className, classPK, ratingsEntries);
1345                    }
1346            }
1347    
1348            protected void resetPortletScope(
1349                    PortletDataContext portletDataContext, long groupId) {
1350    
1351                    portletDataContext.setScopeGroupId(groupId);
1352                    portletDataContext.setScopeLayoutUuid(StringPool.BLANK);
1353                    portletDataContext.setScopeType(StringPool.BLANK);
1354            }
1355    
1356            protected void setPortletScope(
1357                    PortletDataContext portletDataContext, Element portletElement) {
1358    
1359                    // Portlet data scope
1360    
1361                    String scopeLayoutUuid = GetterUtil.getString(
1362                            portletElement.attributeValue("scope-layout-uuid"));
1363                    String scopeLayoutType = GetterUtil.getString(
1364                            portletElement.attributeValue("scope-layout-type"));
1365    
1366                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
1367                    portletDataContext.setScopeType(scopeLayoutType);
1368    
1369                    // Layout scope
1370    
1371                    try {
1372                            Group scopeGroup = null;
1373    
1374                            if (scopeLayoutType.equals("company")) {
1375                                    scopeGroup = GroupLocalServiceUtil.getCompanyGroup(
1376                                            portletDataContext.getCompanyId());
1377                            }
1378                            else if (Validator.isNotNull(scopeLayoutUuid)) {
1379                                    Layout scopeLayout =
1380                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1381                                                    scopeLayoutUuid, portletDataContext.getGroupId());
1382    
1383                                    if (scopeLayout.hasScopeGroup()) {
1384                                            scopeGroup = scopeLayout.getScopeGroup();
1385                                    }
1386                                    else {
1387                                            String name = String.valueOf(scopeLayout.getPlid());
1388    
1389                                            scopeGroup = GroupLocalServiceUtil.addGroup(
1390                                                    portletDataContext.getUserId(null),
1391                                                    Layout.class.getName(), scopeLayout.getPlid(), name,
1392                                                    null, 0, null, false, true, null);
1393                                    }
1394    
1395                                    Group group = scopeLayout.getGroup();
1396    
1397                                    if (group.isStaged() && !group.isStagedRemotely()) {
1398                                            try {
1399                                                    Layout oldLayout =
1400                                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
1401                                                                    scopeLayoutUuid,
1402                                                                    portletDataContext.getSourceGroupId());
1403    
1404                                                    Group oldScopeGroup = oldLayout.getScopeGroup();
1405    
1406                                                    oldScopeGroup.setLiveGroupId(scopeGroup.getGroupId());
1407    
1408                                                    GroupLocalServiceUtil.updateGroup(oldScopeGroup, true);
1409                                            }
1410                                            catch (NoSuchLayoutException nsle) {
1411                                                    if (_log.isWarnEnabled()) {
1412                                                            _log.warn(nsle);
1413                                                    }
1414                                            }
1415                                    }
1416    
1417                                    portletDataContext.setScopeGroupId(scopeGroup.getGroupId());
1418                            }
1419                    }
1420                    catch (PortalException pe) {
1421                    }
1422                    catch (Exception e) {
1423                            _log.error(e, e);
1424                    }
1425            }
1426    
1427            private static Log _log = LogFactoryUtil.getLog(PortletImporter.class);
1428    
1429            private PermissionImporter _permissionImporter = new PermissionImporter();
1430    
1431    }