001    /**
002     * Copyright (c) 2000-2013 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.LayoutImportException;
018    import com.liferay.portal.NoSuchPortletPreferencesException;
019    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskThreadLocal;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.language.LanguageUtil;
023    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
024    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
025    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
026    import com.liferay.portal.kernel.lar.ManifestSummary;
027    import com.liferay.portal.kernel.lar.PortletDataContext;
028    import com.liferay.portal.kernel.lar.PortletDataContextFactoryUtil;
029    import com.liferay.portal.kernel.lar.PortletDataException;
030    import com.liferay.portal.kernel.lar.PortletDataHandler;
031    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
032    import com.liferay.portal.kernel.lar.PortletDataHandlerStatusMessageSenderUtil;
033    import com.liferay.portal.kernel.log.Log;
034    import com.liferay.portal.kernel.log.LogFactoryUtil;
035    import com.liferay.portal.kernel.staging.StagingUtil;
036    import com.liferay.portal.kernel.util.CharPool;
037    import com.liferay.portal.kernel.util.FileUtil;
038    import com.liferay.portal.kernel.util.GetterUtil;
039    import com.liferay.portal.kernel.util.MapUtil;
040    import com.liferay.portal.kernel.util.ReleaseInfo;
041    import com.liferay.portal.kernel.util.StringBundler;
042    import com.liferay.portal.kernel.util.StringPool;
043    import com.liferay.portal.kernel.util.StringUtil;
044    import com.liferay.portal.kernel.util.Time;
045    import com.liferay.portal.kernel.util.UnicodeProperties;
046    import com.liferay.portal.kernel.util.Validator;
047    import com.liferay.portal.kernel.xml.Document;
048    import com.liferay.portal.kernel.xml.Element;
049    import com.liferay.portal.kernel.xml.Node;
050    import com.liferay.portal.kernel.xml.SAXReaderUtil;
051    import com.liferay.portal.kernel.zip.ZipWriter;
052    import com.liferay.portal.kernel.zip.ZipWriterFactoryUtil;
053    import com.liferay.portal.model.Group;
054    import com.liferay.portal.model.Layout;
055    import com.liferay.portal.model.LayoutConstants;
056    import com.liferay.portal.model.LayoutTypePortlet;
057    import com.liferay.portal.model.Lock;
058    import com.liferay.portal.model.Portlet;
059    import com.liferay.portal.model.PortletConstants;
060    import com.liferay.portal.model.PortletItem;
061    import com.liferay.portal.model.PortletPreferences;
062    import com.liferay.portal.model.User;
063    import com.liferay.portal.service.GroupLocalServiceUtil;
064    import com.liferay.portal.service.LayoutLocalServiceUtil;
065    import com.liferay.portal.service.PortletItemLocalServiceUtil;
066    import com.liferay.portal.service.PortletLocalServiceUtil;
067    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
068    import com.liferay.portal.service.ServiceContext;
069    import com.liferay.portal.service.ServiceContextThreadLocal;
070    import com.liferay.portal.service.UserLocalServiceUtil;
071    import com.liferay.portal.util.PortalUtil;
072    import com.liferay.portal.util.PortletKeys;
073    import com.liferay.portlet.PortletPreferencesFactoryUtil;
074    import com.liferay.portlet.asset.model.AssetCategory;
075    import com.liferay.portlet.asset.model.AssetCategoryConstants;
076    import com.liferay.portlet.asset.model.AssetCategoryProperty;
077    import com.liferay.portlet.asset.model.AssetEntry;
078    import com.liferay.portlet.asset.model.AssetLink;
079    import com.liferay.portlet.asset.model.AssetTag;
080    import com.liferay.portlet.asset.model.AssetTagProperty;
081    import com.liferay.portlet.asset.model.AssetVocabulary;
082    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
083    import com.liferay.portlet.asset.service.AssetCategoryPropertyLocalServiceUtil;
084    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
085    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
086    import com.liferay.portlet.asset.service.AssetTagPropertyLocalServiceUtil;
087    import com.liferay.portlet.asset.service.persistence.AssetCategoryUtil;
088    import com.liferay.portlet.asset.service.persistence.AssetVocabularyUtil;
089    import com.liferay.portlet.expando.model.ExpandoColumn;
090    import com.liferay.portlet.messageboards.model.MBDiscussion;
091    import com.liferay.portlet.messageboards.model.MBMessage;
092    import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
093    import com.liferay.portlet.ratings.model.RatingsEntry;
094    import com.liferay.util.xml.DocUtil;
095    
096    import java.io.File;
097    import java.io.IOException;
098    
099    import java.util.Date;
100    import java.util.List;
101    import java.util.Map;
102    import java.util.Map.Entry;
103    
104    import org.apache.commons.lang.time.StopWatch;
105    
106    /**
107     * @author Brian Wing Shun Chan
108     * @author Joel Kozikowski
109     * @author Charles May
110     * @author Raymond Aug??
111     * @author Jorge Ferrer
112     * @author Bruno Farache
113     * @author Zsigmond Rab
114     * @author Douglas Wong
115     * @author Mate Thurzo
116     */
117    public class PortletExporter {
118    
119            public void exportPortletData(
120                            PortletDataContext portletDataContext, Portlet portlet,
121                            Layout layout,
122                            javax.portlet.PortletPreferences jxPortletPreferences,
123                            Element parentElement)
124                    throws Exception {
125    
126                    if (portlet == null) {
127                            return;
128                    }
129    
130                    PortletDataHandler portletDataHandler =
131                            portlet.getPortletDataHandlerInstance();
132    
133                    if ((portletDataHandler == null) ||
134                            portletDataHandler.isDataPortletInstanceLevel()) {
135    
136                            return;
137                    }
138    
139                    String portletId = portlet.getPortletId();
140    
141                    Group liveGroup = layout.getGroup();
142    
143                    if (liveGroup.isStagingGroup()) {
144                            liveGroup = liveGroup.getLiveGroup();
145                    }
146    
147                    boolean staged = liveGroup.isStagedPortlet(portlet.getRootPortletId());
148    
149                    if (!staged && ExportImportThreadLocal.isLayoutExportInProcess()) {
150                            if (_log.isDebugEnabled()) {
151                                    _log.debug(
152                                            "Not exporting data for " + portletId +
153                                                    " because it is configured not to be staged");
154                            }
155    
156                            return;
157                    }
158    
159                    if (_log.isDebugEnabled()) {
160                            _log.debug("Exporting data for " + portletId);
161                    }
162    
163                    StringBundler sb = new StringBundler(4);
164    
165                    sb.append(
166                            ExportImportPathUtil.getPortletPath(portletDataContext, portletId));
167                    sb.append(StringPool.SLASH);
168    
169                    if (portlet.isPreferencesUniquePerLayout()) {
170                            sb.append(layout.getPlid());
171                    }
172                    else {
173                            sb.append(portletDataContext.getScopeGroupId());
174                    }
175    
176                    sb.append("/portlet-data.xml");
177    
178                    String path = sb.toString();
179    
180                    if (portletDataContext.isPathProcessed(path)) {
181                            return;
182                    }
183    
184                    long lastPublishDate = GetterUtil.getLong(
185                            jxPortletPreferences.getValue(
186                                    "last-publish-date", StringPool.BLANK));
187    
188                    Date startDate = portletDataContext.getStartDate();
189    
190                    if ((lastPublishDate > 0) && (startDate != null) &&
191                            (lastPublishDate < startDate.getTime())) {
192    
193                            portletDataContext.setStartDate(new Date(lastPublishDate));
194                    }
195    
196                    String data = null;
197    
198                    long groupId = portletDataContext.getGroupId();
199    
200                    portletDataContext.setGroupId(portletDataContext.getScopeGroupId());
201    
202                    portletDataContext.clearScopedPrimaryKeys();
203    
204                    try {
205                            data = portletDataHandler.exportData(
206                                    portletDataContext, portletId, jxPortletPreferences);
207                    }
208                    catch (PortletDataException pde) {
209                            throw pde;
210                    }
211                    catch (Exception e) {
212                            throw new SystemException(e);
213                    }
214                    finally {
215                            portletDataContext.setGroupId(groupId);
216                            portletDataContext.setStartDate(startDate);
217                    }
218    
219                    if (Validator.isNull(data)) {
220                            if (_log.isDebugEnabled()) {
221                                    _log.debug(
222                                            "Not exporting data for " + portletId +
223                                                    " because null data was returned");
224                            }
225    
226                            return;
227                    }
228    
229                    Element portletDataElement = parentElement.addElement("portlet-data");
230    
231                    portletDataElement.addAttribute("path", path);
232    
233                    portletDataContext.addZipEntry(path, data);
234    
235                    Date endDate = portletDataContext.getEndDate();
236    
237                    boolean updateLastPublishDate = MapUtil.getBoolean(
238                            portletDataContext.getParameterMap(),
239                            PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);
240    
241                    if (updateLastPublishDate) {
242                            StagingUtil.updateLastPublishDate(
243                                    portletId, jxPortletPreferences, endDate);
244                    }
245            }
246    
247            @Deprecated
248            public byte[] exportPortletInfo(
249                            long plid, long groupId, String portletId,
250                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
251                    throws Exception {
252    
253                    File file = exportPortletInfoAsFile(
254                            plid, groupId, portletId, parameterMap, startDate, endDate);
255    
256                    try {
257                            return FileUtil.getBytes(file);
258                    }
259                    catch (IOException ioe) {
260                            throw new SystemException(ioe);
261                    }
262                    finally {
263                            file.delete();
264                    }
265            }
266    
267            public File exportPortletInfoAsFile(
268                            long plid, long groupId, String portletId,
269                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
270                    throws Exception {
271    
272                    try {
273                            ExportImportThreadLocal.setPortletExportInProcess(true);
274    
275                            return doExportPortletInfoAsFile(
276                                    plid, groupId, portletId, parameterMap, startDate, endDate);
277                    }
278                    finally {
279                            ExportImportThreadLocal.setPortletExportInProcess(false);
280                    }
281            }
282    
283            protected File doExportPortletInfoAsFile(
284                            long plid, long groupId, String portletId,
285                            Map<String, String[]> parameterMap, Date startDate, Date endDate)
286                    throws Exception {
287    
288                    boolean exportPermissions = MapUtil.getBoolean(
289                            parameterMap, PortletDataHandlerKeys.PERMISSIONS);
290    
291                    if (_log.isDebugEnabled()) {
292                            _log.debug("Export permissions " + exportPermissions);
293                    }
294    
295                    StopWatch stopWatch = new StopWatch();
296    
297                    stopWatch.start();
298    
299                    LayoutCache layoutCache = new LayoutCache();
300    
301                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
302    
303                    if (!layout.isTypeControlPanel() && !layout.isTypePanel() &&
304                            !layout.isTypePortlet()) {
305    
306                            throw new LayoutImportException(
307                                    "Layout type " + layout.getType() + " is not valid");
308                    }
309    
310                    ServiceContext serviceContext =
311                            ServiceContextThreadLocal.getServiceContext();
312    
313                    if (serviceContext == null) {
314                            serviceContext = new ServiceContext();
315    
316                            serviceContext.setCompanyId(layout.getCompanyId());
317                            serviceContext.setSignedIn(false);
318    
319                            long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
320                                    layout.getCompanyId());
321    
322                            serviceContext.setUserId(defaultUserId);
323    
324                            ServiceContextThreadLocal.pushServiceContext(serviceContext);
325                    }
326    
327                    long layoutSetBranchId = MapUtil.getLong(
328                            parameterMap, "layoutSetBranchId");
329    
330                    serviceContext.setAttribute("layoutSetBranchId", layoutSetBranchId);
331    
332                    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();
333    
334                    long scopeGroupId = groupId;
335    
336                    javax.portlet.PortletPreferences jxPortletPreferences =
337                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
338                                    layout, portletId);
339    
340                    String scopeType = GetterUtil.getString(
341                            jxPortletPreferences.getValue("lfrScopeType", null));
342                    String scopeLayoutUuid = GetterUtil.getString(
343                            jxPortletPreferences.getValue("lfrScopeLayoutUuid", null));
344    
345                    if (Validator.isNotNull(scopeType)) {
346                            Group scopeGroup = null;
347    
348                            if (scopeType.equals("company")) {
349                                    scopeGroup = GroupLocalServiceUtil.getCompanyGroup(
350                                            layout.getCompanyId());
351                            }
352                            else if (Validator.isNotNull(scopeLayoutUuid)) {
353                                    scopeGroup = layout.getScopeGroup();
354                            }
355    
356                            if (scopeGroup != null) {
357                                    scopeGroupId = scopeGroup.getGroupId();
358                            }
359                    }
360    
361                    PortletDataContext portletDataContext =
362                            PortletDataContextFactoryUtil.createExportPortletDataContext(
363                                    layout.getCompanyId(), scopeGroupId, parameterMap, startDate,
364                                    endDate, zipWriter);
365    
366                    portletDataContext.setPortetDataContextListener(
367                            new PortletDataContextListenerImpl(portletDataContext));
368    
369                    portletDataContext.setPlid(plid);
370                    portletDataContext.setOldPlid(plid);
371                    portletDataContext.setScopeType(scopeType);
372                    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
373    
374                    Document document = SAXReaderUtil.createDocument();
375    
376                    Element rootElement = document.addElement("root");
377    
378                    Element headerElement = rootElement.addElement("header");
379    
380                    headerElement.addAttribute(
381                            "available-locales",
382                            StringUtil.merge(
383                                    LanguageUtil.getAvailableLocales(
384                                            PortalUtil.getSiteGroupId(
385                                                    portletDataContext.getScopeGroupId()))));
386                    headerElement.addAttribute(
387                            "build-number", String.valueOf(ReleaseInfo.getBuildNumber()));
388                    headerElement.addAttribute("export-date", Time.getRFC822());
389    
390                    if (portletDataContext.hasDateRange()) {
391                            headerElement.addAttribute(
392                                    "start-date",
393                                    String.valueOf(portletDataContext.getStartDate()));
394                            headerElement.addAttribute(
395                                    "end-date", String.valueOf(portletDataContext.getEndDate()));
396                    }
397    
398                    headerElement.addAttribute("type", "portlet");
399                    headerElement.addAttribute(
400                            "company-id", String.valueOf(portletDataContext.getCompanyId()));
401                    headerElement.addAttribute(
402                            "company-group-id",
403                            String.valueOf(portletDataContext.getCompanyGroupId()));
404                    headerElement.addAttribute("group-id", String.valueOf(scopeGroupId));
405                    headerElement.addAttribute(
406                            "user-personal-site-group-id",
407                            String.valueOf(portletDataContext.getUserPersonalSiteGroupId()));
408                    headerElement.addAttribute(
409                            "private-layout", String.valueOf(layout.isPrivateLayout()));
410                    headerElement.addAttribute(
411                            "root-portlet-id", PortletConstants.getRootPortletId(portletId));
412    
413                    Element missingReferencesElement = rootElement.addElement(
414                            "missing-references");
415    
416                    portletDataContext.setMissingReferencesElement(
417                            missingReferencesElement);
418    
419                    Map<String, Boolean> exportPortletControlsMap =
420                            LayoutExporter.getExportPortletControlsMap(
421                                    layout.getCompanyId(), portletId, parameterMap);
422    
423                    exportPortlet(
424                            portletDataContext, layoutCache, portletId, layout, rootElement,
425                            exportPermissions,
426                            exportPortletControlsMap.get(
427                                    PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS),
428                            exportPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_DATA),
429                            exportPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_SETUP),
430                            exportPortletControlsMap.get(
431                                    PortletDataHandlerKeys.PORTLET_USER_PREFERENCES));
432    
433                    exportAssetCategories(portletDataContext);
434                    exportAssetLinks(portletDataContext);
435                    exportAssetTags(portletDataContext);
436                    exportComments(portletDataContext);
437                    exportExpandoTables(portletDataContext);
438                    exportLocks(portletDataContext);
439    
440                    _deletionSystemEventExporter.exportDeletionSystemEvents(
441                            portletDataContext);
442    
443                    if (exportPermissions) {
444                            _permissionExporter.exportPortletDataPermissions(
445                                    portletDataContext);
446                    }
447    
448                    exportRatingsEntries(portletDataContext, rootElement);
449    
450                    ExportImportHelperUtil.writeManifestSummary(
451                            document, portletDataContext.getManifestSummary());
452    
453                    if (_log.isInfoEnabled()) {
454                            _log.info("Exporting portlet took " + stopWatch.getTime() + " ms");
455                    }
456    
457                    try {
458                            portletDataContext.addZipEntry(
459                                    "/manifest.xml", document.formattedString());
460                    }
461                    catch (IOException ioe) {
462                            throw new SystemException(ioe);
463                    }
464    
465                    return zipWriter.getFile();
466            }
467    
468            protected void exportAssetCategories(PortletDataContext portletDataContext)
469                    throws Exception {
470    
471                    Document document = SAXReaderUtil.createDocument();
472    
473                    Element rootElement = document.addElement("categories-hierarchy");
474    
475                    exportAssetCategories(portletDataContext, rootElement);
476    
477                    portletDataContext.addZipEntry(
478                            ExportImportPathUtil.getRootPath(portletDataContext) +
479                                    "/categories-hierarchy.xml",
480                            document.formattedString());
481            }
482    
483            protected void exportAssetCategories(
484                            PortletDataContext portletDataContext, Element rootElement)
485                    throws Exception {
486    
487                    Element assetsElement = rootElement.element("assets");
488    
489                    if (assetsElement == null) {
490                            assetsElement = rootElement.addElement("assets");
491                    }
492    
493                    Element assetCategoriesElement = rootElement.element("categories");
494    
495                    if (assetCategoriesElement == null) {
496                            assetCategoriesElement = rootElement.addElement("categories");
497                    }
498    
499                    Element assetVocabulariesElement = rootElement.element("vocabularies");
500    
501                    if (assetVocabulariesElement == null) {
502                            assetVocabulariesElement = rootElement.addElement("vocabularies");
503                    }
504    
505                    Map<String, String[]> assetCategoryUuidsMap =
506                            portletDataContext.getAssetCategoryUuidsMap();
507    
508                    for (Map.Entry<String, String[]> entry :
509                                    assetCategoryUuidsMap.entrySet()) {
510    
511                            String[] assetCategoryEntryParts = StringUtil.split(
512                                    entry.getKey(), CharPool.POUND);
513    
514                            String className = assetCategoryEntryParts[0];
515                            String classPK = assetCategoryEntryParts[1];
516    
517                            Element assetElement = assetsElement.addElement("asset");
518    
519                            assetElement.addAttribute("class-name", className);
520                            assetElement.addAttribute("class-pk", classPK);
521                            assetElement.addAttribute(
522                                    "category-uuids", StringUtil.merge(entry.getValue()));
523    
524                            List<AssetCategory> assetCategories =
525                                    AssetCategoryLocalServiceUtil.getCategories(
526                                            className, GetterUtil.getLong(classPK));
527    
528                            for (AssetCategory assestCategory : assetCategories) {
529                                    exportAssetCategory(
530                                            portletDataContext, assetVocabulariesElement,
531                                            assetCategoriesElement, assestCategory);
532                            }
533                    }
534            }
535    
536            protected void exportAssetCategory(
537                            PortletDataContext portletDataContext,
538                            Element assetVocabulariesElement, Element assetCategoriesElement,
539                            AssetCategory assetCategory)
540                    throws Exception {
541    
542                    exportAssetVocabulary(
543                            portletDataContext, assetVocabulariesElement,
544                            assetCategory.getVocabularyId());
545    
546                    if (assetCategory.getParentCategoryId() !=
547                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
548    
549                            exportAssetCategory(
550                                    portletDataContext, assetVocabulariesElement,
551                                    assetCategoriesElement, assetCategory.getParentCategoryId());
552                    }
553    
554                    String path = getAssetCategoryPath(
555                            portletDataContext, assetCategory.getCategoryId());
556    
557                    if (!portletDataContext.isPathNotProcessed(path)) {
558                            return;
559                    }
560    
561                    Element assetCategoryElement = assetCategoriesElement.addElement(
562                            "category");
563    
564                    assetCategoryElement.addAttribute("path", path);
565    
566                    assetCategory.setUserUuid(assetCategory.getUserUuid());
567    
568                    portletDataContext.addZipEntry(path, assetCategory);
569    
570                    List<AssetCategoryProperty> assetCategoryProperties =
571                            AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(
572                                    assetCategory.getCategoryId());
573    
574                    for (AssetCategoryProperty assetCategoryProperty :
575                                    assetCategoryProperties) {
576    
577                            Element propertyElement = assetCategoryElement.addElement(
578                                    "property");
579    
580                            propertyElement.addAttribute(
581                                    "userUuid", assetCategoryProperty.getUserUuid());
582                            propertyElement.addAttribute("key", assetCategoryProperty.getKey());
583                            propertyElement.addAttribute(
584                                    "value", assetCategoryProperty.getValue());
585                    }
586    
587                    portletDataContext.addPermissions(
588                            AssetCategory.class, assetCategory.getCategoryId());
589    
590                    portletDataContext.cleanUpMissingReferences(assetCategory);
591            }
592    
593            protected void exportAssetCategory(
594                            PortletDataContext portletDataContext,
595                            Element assetVocabulariesElement, Element assetCategoriesElement,
596                            long assetCategoryId)
597                    throws Exception {
598    
599                    AssetCategory assetCategory = AssetCategoryUtil.fetchByPrimaryKey(
600                            assetCategoryId);
601    
602                    if (assetCategory != null) {
603                            exportAssetCategory(
604                                    portletDataContext, assetVocabulariesElement,
605                                    assetCategoriesElement, assetCategory);
606                    }
607            }
608    
609            protected void exportAssetLinks(PortletDataContext portletDataContext)
610                    throws Exception {
611    
612                    Document document = SAXReaderUtil.createDocument();
613    
614                    Element rootElement = document.addElement("links");
615    
616                    Map<String, List<AssetLink>> assetLinksMap =
617                            portletDataContext.getAssetLinksMap();
618    
619                    for (Entry<String, List<AssetLink>> entry : assetLinksMap.entrySet()) {
620                            String[] assetLinkNameParts = StringUtil.split(
621                                    entry.getKey(), CharPool.POUND);
622    
623                            List<AssetLink> assetLinks = entry.getValue();
624    
625                            String sourceAssetEntryUuid = assetLinkNameParts[0];
626    
627                            Element assetElement = rootElement.addElement("asset-link-group");
628    
629                            assetElement.addAttribute("source-uuid", sourceAssetEntryUuid);
630    
631                            for (AssetLink assetLink : assetLinks) {
632                                    String path = getAssetLinkPath(
633                                            portletDataContext, assetLink.getLinkId());
634    
635                                    if (!portletDataContext.isPathNotProcessed(path)) {
636                                            return;
637                                    }
638    
639                                    Element assetLinkElement = assetElement.addElement(
640                                            "asset-link");
641    
642                                    assetLinkElement.addAttribute("path", path);
643    
644                                    AssetEntry targetAssetEntry =
645                                            AssetEntryLocalServiceUtil.fetchAssetEntry(
646                                                    assetLink.getEntryId2());
647    
648                                    assetLinkElement.addAttribute(
649                                            "target-uuid", targetAssetEntry.getClassUuid());
650    
651                                    portletDataContext.addZipEntry(path, assetLink);
652                            }
653                    }
654    
655                    portletDataContext.addZipEntry(
656                            ExportImportPathUtil.getRootPath(portletDataContext) + "/links.xml",
657                            document.formattedString());
658            }
659    
660            protected void exportAssetTag(
661                            PortletDataContext portletDataContext, AssetTag assetTag,
662                            Element assetTagsElement)
663                    throws PortalException, SystemException {
664    
665                    String path = getAssetTagPath(portletDataContext, assetTag.getTagId());
666    
667                    if (!portletDataContext.isPathNotProcessed(path)) {
668                            return;
669                    }
670    
671                    Element assetTagElement = assetTagsElement.addElement("tag");
672    
673                    assetTagElement.addAttribute("path", path);
674    
675                    assetTag.setUserUuid(assetTag.getUserUuid());
676    
677                    portletDataContext.addZipEntry(path, assetTag);
678    
679                    List<AssetTagProperty> assetTagProperties =
680                            AssetTagPropertyLocalServiceUtil.getTagProperties(
681                                    assetTag.getTagId());
682    
683                    for (AssetTagProperty assetTagProperty : assetTagProperties) {
684                            Element propertyElement = assetTagElement.addElement("property");
685    
686                            propertyElement.addAttribute("key", assetTagProperty.getKey());
687                            propertyElement.addAttribute("value", assetTagProperty.getValue());
688                    }
689    
690                    portletDataContext.addPermissions(AssetTag.class, assetTag.getTagId());
691            }
692    
693            protected void exportAssetTags(PortletDataContext portletDataContext)
694                    throws Exception {
695    
696                    Document document = SAXReaderUtil.createDocument();
697    
698                    Element rootElement = document.addElement("tags");
699    
700                    Map<String, String[]> assetTagNamesMap =
701                            portletDataContext.getAssetTagNamesMap();
702    
703                    if (assetTagNamesMap.isEmpty()) {
704                            return;
705                    }
706    
707                    for (Map.Entry<String, String[]> entry : assetTagNamesMap.entrySet()) {
708                            String[] assetTagNameParts = StringUtil.split(
709                                    entry.getKey(), CharPool.POUND);
710    
711                            String className = assetTagNameParts[0];
712                            String classPK = assetTagNameParts[1];
713    
714                            Element assetElement = rootElement.addElement("asset");
715    
716                            assetElement.addAttribute("class-name", className);
717                            assetElement.addAttribute("class-pk", classPK);
718                            assetElement.addAttribute(
719                                    "tags", StringUtil.merge(entry.getValue()));
720    
721                            List<AssetTag> assetTags = AssetTagLocalServiceUtil.getTags(
722                                    className, GetterUtil.getLong(classPK));
723    
724                            for (AssetTag assetTag : assetTags) {
725                                    exportAssetTag(portletDataContext, assetTag, rootElement);
726                            }
727                    }
728    
729                    portletDataContext.addZipEntry(
730                            ExportImportPathUtil.getRootPath(portletDataContext) + "/tags.xml",
731                            document.formattedString());
732            }
733    
734            protected void exportAssetVocabulary(
735                            PortletDataContext portletDataContext,
736                            Element assetVocabulariesElement, AssetVocabulary assetVocabulary)
737                    throws Exception {
738    
739                    String path = getAssetVocabulariesPath(
740                            portletDataContext, assetVocabulary.getVocabularyId());
741    
742                    if (!portletDataContext.isPathNotProcessed(path)) {
743                            return;
744                    }
745    
746                    Element assetVocabularyElement = assetVocabulariesElement.addElement(
747                            "vocabulary");
748    
749                    assetVocabularyElement.addAttribute("path", path);
750    
751                    assetVocabulary.setUserUuid(assetVocabulary.getUserUuid());
752    
753                    portletDataContext.addZipEntry(path, assetVocabulary);
754    
755                    portletDataContext.addPermissions(
756                            AssetVocabulary.class, assetVocabulary.getVocabularyId());
757    
758                    portletDataContext.cleanUpMissingReferences(assetVocabulary);
759            }
760    
761            protected void exportAssetVocabulary(
762                            PortletDataContext portletDataContext,
763                            Element assetVocabulariesElement, long assetVocabularyId)
764                    throws Exception {
765    
766                    AssetVocabulary assetVocabulary = AssetVocabularyUtil.findByPrimaryKey(
767                            assetVocabularyId);
768    
769                    exportAssetVocabulary(
770                            portletDataContext, assetVocabulariesElement, assetVocabulary);
771            }
772    
773            protected void exportComments(PortletDataContext portletDataContext)
774                    throws Exception {
775    
776                    Document document = SAXReaderUtil.createDocument();
777    
778                    Element rootElement = document.addElement("comments");
779    
780                    Map<String, List<MBMessage>> commentsMap =
781                            portletDataContext.getComments();
782    
783                    for (Map.Entry<String, List<MBMessage>> entry :
784                                    commentsMap.entrySet()) {
785    
786                            String[] commentParts = StringUtil.split(
787                                    entry.getKey(), CharPool.POUND);
788    
789                            String className = commentParts[0];
790                            String classPK = commentParts[1];
791    
792                            String commentsPath = getCommentsPath(
793                                    portletDataContext, className, classPK);
794    
795                            Element assetElement = rootElement.addElement("asset");
796    
797                            assetElement.addAttribute("path", commentsPath);
798                            assetElement.addAttribute("class-name", className);
799                            assetElement.addAttribute("class-pk", classPK);
800    
801                            List<MBMessage> mbMessages = entry.getValue();
802    
803                            for (MBMessage mbMessage : mbMessages) {
804                                    String commentPath = getCommentPath(
805                                            portletDataContext, className, classPK, mbMessage);
806    
807                                    if (portletDataContext.isPathNotProcessed(commentPath)) {
808                                            User user = UserLocalServiceUtil.fetchUser(
809                                                    mbMessage.getUserId());
810    
811                                            if (user == null) {
812                                                    continue;
813                                            }
814    
815                                            portletDataContext.addZipEntry(commentPath, mbMessage);
816    
817                                            MBDiscussion mbDiscussion =
818                                                    MBDiscussionLocalServiceUtil.getDiscussion(
819                                                            className, GetterUtil.getLong(classPK));
820    
821                                            portletDataContext.addReferenceElement(
822                                                    mbDiscussion, assetElement, user,
823                                                    PortletDataContext.REFERENCE_TYPE_WEAK, true);
824                                    }
825                            }
826                    }
827    
828                    portletDataContext.addZipEntry(
829                            ExportImportPathUtil.getRootPath(portletDataContext) +
830                                    "/comments.xml",
831                            document.formattedString());
832            }
833    
834            protected void exportExpandoTables(PortletDataContext portletDataContext)
835                    throws Exception {
836    
837                    Document document = SAXReaderUtil.createDocument();
838    
839                    Element rootElement = document.addElement("expando-tables");
840    
841                    Map<String, List<ExpandoColumn>> expandoColumnsMap =
842                            portletDataContext.getExpandoColumns();
843    
844                    for (Map.Entry<String, List<ExpandoColumn>> entry :
845                                    expandoColumnsMap.entrySet()) {
846    
847                            String className = entry.getKey();
848    
849                            Element expandoTableElement = rootElement.addElement(
850                                    "expando-table");
851    
852                            expandoTableElement.addAttribute("class-name", className);
853    
854                            List<ExpandoColumn> expandoColumns = entry.getValue();
855    
856                            for (ExpandoColumn expandoColumn : expandoColumns) {
857                                    Element expandoColumnElement = expandoTableElement.addElement(
858                                            "expando-column");
859    
860                                    expandoColumnElement.addAttribute(
861                                            "column-id", String.valueOf(expandoColumn.getColumnId()));
862                                    expandoColumnElement.addAttribute(
863                                            "name", expandoColumn.getName());
864                                    expandoColumnElement.addAttribute(
865                                            "type", String.valueOf(expandoColumn.getType()));
866    
867                                    DocUtil.add(
868                                            expandoColumnElement, "default-data",
869                                            expandoColumn.getDefaultData());
870    
871                                    Element typeSettingsElement = expandoColumnElement.addElement(
872                                            "type-settings");
873    
874                                    UnicodeProperties typeSettingsProperties =
875                                            expandoColumn.getTypeSettingsProperties();
876    
877                                    typeSettingsElement.addCDATA(typeSettingsProperties.toString());
878                            }
879                    }
880    
881                    portletDataContext.addZipEntry(
882                            ExportImportPathUtil.getRootPath(portletDataContext) +
883                                    "/expando-tables.xml",
884                            document.formattedString());
885            }
886    
887            protected void exportLocks(PortletDataContext portletDataContext)
888                    throws Exception {
889    
890                    Document document = SAXReaderUtil.createDocument();
891    
892                    Element rootElement = document.addElement("locks");
893    
894                    Map<String, Lock> locksMap = portletDataContext.getLocks();
895    
896                    for (Map.Entry<String, Lock> entry : locksMap.entrySet()) {
897                            Lock lock = entry.getValue();
898    
899                            String entryKey = entry.getKey();
900    
901                            int pos = entryKey.indexOf(CharPool.POUND);
902    
903                            String className = entryKey.substring(0, pos);
904                            String key = entryKey.substring(pos + 1);
905    
906                            String path = getLockPath(portletDataContext, className, key, lock);
907    
908                            Element assetElement = rootElement.addElement("asset");
909    
910                            assetElement.addAttribute("path", path);
911                            assetElement.addAttribute("class-name", className);
912                            assetElement.addAttribute("key", key);
913    
914                            if (portletDataContext.isPathNotProcessed(path)) {
915                                    portletDataContext.addZipEntry(path, lock);
916                            }
917                    }
918    
919                    portletDataContext.addZipEntry(
920                            ExportImportPathUtil.getRootPath(portletDataContext) + "/locks.xml",
921                            document.formattedString());
922            }
923    
924            protected void exportPortlet(
925                            PortletDataContext portletDataContext, LayoutCache layoutCache,
926                            String portletId, Layout layout, Element parentElement,
927                            boolean exportPermissions, boolean exportPortletArchivedSetups,
928                            boolean exportPortletData, boolean exportPortletSetup,
929                            boolean exportPortletUserPreferences)
930                    throws Exception {
931    
932                    long plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
933                    long layoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
934    
935                    if (layout != null) {
936                            plid = layout.getPlid();
937                            layoutId = layout.getLayoutId();
938                    }
939    
940                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
941                            portletDataContext.getCompanyId(), portletId);
942    
943                    if (portlet == null) {
944                            if (_log.isDebugEnabled()) {
945                                    _log.debug(
946                                            "Do not export portlet " + portletId +
947                                                    " because the portlet does not exist");
948                            }
949    
950                            return;
951                    }
952    
953                    if (!portlet.isInstanceable() &&
954                            !portlet.isPreferencesUniquePerLayout() &&
955                            portletDataContext.hasNotUniquePerLayout(portletId)) {
956    
957                            return;
958                    }
959    
960                    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
961                            PortletDataContext clonedPortletDataContext =
962                                    PortletDataContextFactoryUtil.clonePortletDataContext(
963                                            portletDataContext);
964    
965                            ManifestSummary manifestSummary =
966                                    clonedPortletDataContext.getManifestSummary();
967    
968                            manifestSummary.resetCounters();
969    
970                            PortletDataHandler portletDataHandler =
971                                    portlet.getPortletDataHandlerInstance();
972    
973                            portletDataHandler.prepareManifestSummary(clonedPortletDataContext);
974    
975                            PortletDataHandlerStatusMessageSenderUtil.sendStatusMessage(
976                                    "portlet", portletId, manifestSummary);
977                    }
978    
979                    Document document = SAXReaderUtil.createDocument();
980    
981                    Element portletElement = document.addElement("portlet");
982    
983                    portletElement.addAttribute("portlet-id", portletId);
984                    portletElement.addAttribute(
985                            "root-portlet-id", PortletConstants.getRootPortletId(portletId));
986                    portletElement.addAttribute("old-plid", String.valueOf(plid));
987                    portletElement.addAttribute(
988                            "scope-layout-type", portletDataContext.getScopeType());
989                    portletElement.addAttribute(
990                            "scope-layout-uuid", portletDataContext.getScopeLayoutUuid());
991                    portletElement.addAttribute(
992                            "private-layout", String.valueOf(layout.isPrivateLayout()));
993    
994                    // Data
995    
996                    if (exportPortletData) {
997                            javax.portlet.PortletPreferences jxPortletPreferences =
998                                    PortletPreferencesFactoryUtil.getStrictPortletSetup(
999                                            layout, portletId);
1000    
1001                            if (!portlet.isPreferencesUniquePerLayout()) {
1002                                    StringBundler sb = new StringBundler(5);
1003    
1004                                    sb.append(portletId);
1005                                    sb.append(StringPool.AT);
1006                                    sb.append(portletDataContext.getScopeType());
1007                                    sb.append(StringPool.AT);
1008                                    sb.append(portletDataContext.getScopeLayoutUuid());
1009    
1010                                    String dataKey = sb.toString();
1011    
1012                                    if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
1013                                            portletDataContext.putNotUniquePerLayout(dataKey);
1014    
1015                                            exportPortletData(
1016                                                    portletDataContext, portlet, layout,
1017                                                    jxPortletPreferences, portletElement);
1018                                    }
1019                            }
1020                            else {
1021                                    exportPortletData(
1022                                            portletDataContext, portlet, layout, jxPortletPreferences,
1023                                            portletElement);
1024                            }
1025                    }
1026    
1027                    // Portlet preferences
1028    
1029                    if (exportPortletSetup) {
1030    
1031                            // Company
1032    
1033                            exportPortletPreferences(
1034                                    portletDataContext, portletDataContext.getCompanyId(),
1035                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY, false, layout, plid,
1036                                    portlet.getRootPortletId(), portletElement);
1037    
1038                            // Group
1039    
1040                            exportPortletPreferences(
1041                                    portletDataContext, portletDataContext.getScopeGroupId(),
1042                                    PortletKeys.PREFS_OWNER_TYPE_GROUP, false, layout,
1043                                    PortletKeys.PREFS_PLID_SHARED, portlet.getRootPortletId(),
1044                                    portletElement);
1045    
1046                            // Layout
1047    
1048                            exportPortletPreferences(
1049                                    portletDataContext, PortletKeys.PREFS_OWNER_ID_DEFAULT,
1050                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false, layout, plid,
1051                                    portletId, portletElement);
1052                    }
1053    
1054                    // Portlet user preferences
1055    
1056                    if (exportPortletUserPreferences) {
1057                            List<PortletPreferences> portletPreferencesList =
1058                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
1059                                            PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId);
1060    
1061                            for (PortletPreferences portletPreferences :
1062                                            portletPreferencesList) {
1063    
1064                                    boolean defaultUser = false;
1065    
1066                                    if (portletPreferences.getOwnerId() ==
1067                                                    PortletKeys.PREFS_OWNER_ID_DEFAULT) {
1068    
1069                                            defaultUser = true;
1070                                    }
1071    
1072                                    exportPortletPreferences(
1073                                            portletDataContext, portletPreferences.getOwnerId(),
1074                                            PortletKeys.PREFS_OWNER_TYPE_USER, defaultUser, layout,
1075                                            plid, portletId, portletElement);
1076                            }
1077    
1078                            try {
1079                                    PortletPreferences groupPortletPreferences =
1080                                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1081                                                    portletDataContext.getScopeGroupId(),
1082                                                    PortletKeys.PREFS_OWNER_TYPE_GROUP,
1083                                                    PortletKeys.PREFS_PLID_SHARED,
1084                                                    portlet.getRootPortletId());
1085    
1086                                    exportPortletPreference(
1087                                            portletDataContext, portletDataContext.getScopeGroupId(),
1088                                            PortletKeys.PREFS_OWNER_TYPE_GROUP, false,
1089                                            groupPortletPreferences, portlet.getRootPortletId(),
1090                                            PortletKeys.PREFS_PLID_SHARED, portletElement);
1091                            }
1092                            catch (NoSuchPortletPreferencesException nsppe) {
1093                            }
1094                    }
1095    
1096                    // Archived setups
1097    
1098                    if (exportPortletArchivedSetups) {
1099                            String rootPortletId = PortletConstants.getRootPortletId(portletId);
1100    
1101                            List<PortletItem> portletItems =
1102                                    PortletItemLocalServiceUtil.getPortletItems(
1103                                            portletDataContext.getGroupId(), rootPortletId,
1104                                            PortletPreferences.class.getName());
1105    
1106                            for (PortletItem portletItem : portletItems) {
1107                                    exportPortletPreferences(
1108                                            portletDataContext, portletItem.getPortletItemId(),
1109                                            PortletKeys.PREFS_OWNER_TYPE_ARCHIVED, false, null, plid,
1110                                            portletItem.getPortletId(), portletElement);
1111                            }
1112                    }
1113    
1114                    // Permissions
1115    
1116                    if (exportPermissions) {
1117                            _permissionExporter.exportPortletPermissions(
1118                                    portletDataContext, layoutCache, portletId, layout,
1119                                    portletElement);
1120                    }
1121    
1122                    // Zip
1123    
1124                    StringBundler pathSB = new StringBundler(4);
1125    
1126                    pathSB.append(
1127                            ExportImportPathUtil.getPortletPath(portletDataContext, portletId));
1128                    pathSB.append(StringPool.SLASH);
1129                    pathSB.append(plid);
1130                    pathSB.append("/portlet.xml");
1131    
1132                    String path = pathSB.toString();
1133    
1134                    Element element = parentElement.addElement("portlet");
1135    
1136                    element.addAttribute("portlet-id", portletId);
1137                    element.addAttribute("layout-id", String.valueOf(layoutId));
1138                    element.addAttribute("path", path);
1139                    element.addAttribute("portlet-data", String.valueOf(exportPortletData));
1140    
1141                    StringBundler configurationOptionsSB = new StringBundler(6);
1142    
1143                    if (exportPortletSetup) {
1144                            configurationOptionsSB.append("setup");
1145                            configurationOptionsSB.append(StringPool.COMMA);
1146                    }
1147    
1148                    if (exportPortletArchivedSetups) {
1149                            configurationOptionsSB.append("archived-setups");
1150                            configurationOptionsSB.append(StringPool.COMMA);
1151                    }
1152    
1153                    if (exportPortletUserPreferences) {
1154                            configurationOptionsSB.append("user-preferences");
1155                            configurationOptionsSB.append(StringPool.COMMA);
1156                    }
1157    
1158                    if (configurationOptionsSB.index() > 0) {
1159                            configurationOptionsSB.setIndex(configurationOptionsSB.index() -1);
1160                    }
1161    
1162                    element.addAttribute(
1163                            "portlet-configuration", configurationOptionsSB.toString());
1164    
1165                    if (portletDataContext.isPathNotProcessed(path)) {
1166                            try {
1167                                    portletDataContext.addZipEntry(
1168                                            path, document.formattedString());
1169                            }
1170                            catch (IOException ioe) {
1171                                    if (_log.isWarnEnabled()) {
1172                                            _log.warn(ioe.getMessage());
1173                                    }
1174                            }
1175    
1176                            portletDataContext.addPrimaryKey(String.class, path);
1177                    }
1178            }
1179    
1180            protected void exportPortletPreference(
1181                            PortletDataContext portletDataContext, long ownerId, int ownerType,
1182                            boolean defaultUser, PortletPreferences portletPreferences,
1183                            String portletId, long plid, Element parentElement)
1184                    throws Exception {
1185    
1186                    String preferencesXML = portletPreferences.getPreferences();
1187    
1188                    if (Validator.isNull(preferencesXML)) {
1189                            preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
1190                    }
1191    
1192                    javax.portlet.PortletPreferences jxPortletPreferences =
1193                            PortletPreferencesFactoryUtil.fromDefaultXML(preferencesXML);
1194    
1195                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
1196                            portletDataContext.getCompanyId(), portletId);
1197    
1198                    Element portletPreferencesElement = parentElement.addElement(
1199                            "portlet-preferences");
1200    
1201                    if ((portlet != null) &&
1202                            (portlet.getPortletDataHandlerInstance() != null)) {
1203    
1204                            Element exportDataRootElement =
1205                                    portletDataContext.getExportDataRootElement();
1206    
1207                            try {
1208                                    portletDataContext.clearScopedPrimaryKeys();
1209    
1210                                    Element preferenceDataElement =
1211                                            portletPreferencesElement.addElement("preference-data");
1212    
1213                                    portletDataContext.setExportDataRootElement(
1214                                            preferenceDataElement);
1215    
1216                                    PortletDataHandler portletDataHandler =
1217                                            portlet.getPortletDataHandlerInstance();
1218    
1219                                    jxPortletPreferences =
1220                                            portletDataHandler.processExportPortletPreferences(
1221                                                    portletDataContext, portletId, jxPortletPreferences);
1222                            }
1223                            finally {
1224                                    portletDataContext.setExportDataRootElement(
1225                                            exportDataRootElement);
1226                            }
1227                    }
1228    
1229                    Document document = SAXReaderUtil.read(
1230                            PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));
1231    
1232                    Element rootElement = document.getRootElement();
1233    
1234                    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
1235                    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
1236                    rootElement.addAttribute("default-user", String.valueOf(defaultUser));
1237                    rootElement.addAttribute("plid", String.valueOf(plid));
1238                    rootElement.addAttribute("portlet-id", portletId);
1239    
1240                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1241                            PortletItem portletItem =
1242                                    PortletItemLocalServiceUtil.getPortletItem(ownerId);
1243    
1244                            rootElement.addAttribute(
1245                                    "archive-user-uuid", portletItem.getUserUuid());
1246                            rootElement.addAttribute("archive-name", portletItem.getName());
1247                    }
1248                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1249                            User user = UserLocalServiceUtil.fetchUserById(ownerId);
1250    
1251                            if (user == null) {
1252                                    return;
1253                            }
1254    
1255                            rootElement.addAttribute("user-uuid", user.getUserUuid());
1256                    }
1257    
1258                    List<Node> nodes = document.selectNodes(
1259                            "/portlet-preferences/preference[name/text() = " +
1260                                    "'last-publish-date']");
1261    
1262                    for (Node node : nodes) {
1263                            document.remove(node);
1264                    }
1265    
1266                    String path = getPortletPreferencesPath(
1267                            portletDataContext, portletId, ownerId, ownerType, plid);
1268    
1269                    portletPreferencesElement.addAttribute("path", path);
1270    
1271                    if (portletDataContext.isPathNotProcessed(path)) {
1272                            portletDataContext.addZipEntry(
1273                                    path, document.formattedString(StringPool.TAB, false, false));
1274                    }
1275            }
1276    
1277            protected void exportPortletPreferences(
1278                            PortletDataContext portletDataContext, long ownerId, int ownerType,
1279                            boolean defaultUser, Layout layout, long plid, String portletId,
1280                            Element parentElement)
1281                    throws Exception {
1282    
1283                    PortletPreferences portletPreferences = null;
1284    
1285                    try {
1286                            if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) ||
1287                                    (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) ||
1288                                    (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED)) {
1289    
1290                                    portletPreferences =
1291                                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1292                                                    ownerId, ownerType, LayoutConstants.DEFAULT_PLID,
1293                                                    portletId);
1294                            }
1295                            else {
1296                                    portletPreferences =
1297                                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1298                                                    ownerId, ownerType, plid, portletId);
1299                            }
1300                    }
1301                    catch (NoSuchPortletPreferencesException nsppe) {
1302                            return;
1303                    }
1304    
1305                    LayoutTypePortlet layoutTypePortlet = null;
1306    
1307                    if (layout != null) {
1308                            layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
1309                    }
1310    
1311                    if ((layoutTypePortlet == null) ||
1312                            layoutTypePortlet.hasPortletId(portletId)) {
1313    
1314                            exportPortletPreference(
1315                                    portletDataContext, ownerId, ownerType, defaultUser,
1316                                    portletPreferences, portletId, plid, parentElement);
1317                    }
1318            }
1319    
1320            protected void exportRatingsEntries(
1321                            PortletDataContext portletDataContext, Element parentElement)
1322                    throws Exception {
1323    
1324                    Document document = SAXReaderUtil.createDocument();
1325    
1326                    Element rootElement = document.addElement("ratings");
1327    
1328                    Map<String, List<RatingsEntry>> ratingsEntriesMap =
1329                            portletDataContext.getRatingsEntries();
1330    
1331                    for (Map.Entry<String, List<RatingsEntry>> entry :
1332                                    ratingsEntriesMap.entrySet()) {
1333    
1334                            String[] ratingsEntryParts = StringUtil.split(
1335                                    entry.getKey(), CharPool.POUND);
1336    
1337                            String className = ratingsEntryParts[0];
1338                            String classPK = ratingsEntryParts[1];
1339    
1340                            String ratingsEntriesPath = getRatingsEntriesPath(
1341                                    portletDataContext, className, classPK);
1342    
1343                            Element assetElement = rootElement.addElement("asset");
1344    
1345                            assetElement.addAttribute("path", ratingsEntriesPath);
1346                            assetElement.addAttribute("class-name", className);
1347                            assetElement.addAttribute("class-pk", classPK);
1348    
1349                            List<RatingsEntry> ratingsEntries = entry.getValue();
1350    
1351                            for (RatingsEntry ratingsEntry : ratingsEntries) {
1352                                    String ratingsEntryPath = getRatingsEntryPath(
1353                                            portletDataContext, className, classPK, ratingsEntry);
1354    
1355                                    User user = UserLocalServiceUtil.fetchUser(
1356                                            ratingsEntry.getUserId());
1357    
1358                                    if (user == null) {
1359                                            continue;
1360                                    }
1361    
1362                                    portletDataContext.addZipEntry(ratingsEntryPath, ratingsEntry);
1363    
1364                                    portletDataContext.addReferenceElement(
1365                                            ratingsEntry, assetElement, user,
1366                                            PortletDataContext.REFERENCE_TYPE_WEAK, true);
1367                            }
1368                    }
1369    
1370                    portletDataContext.addZipEntry(
1371                            ExportImportPathUtil.getRootPath(portletDataContext) +
1372                                    "/ratings.xml",
1373                            document.formattedString());
1374            }
1375    
1376            protected String getAssetCategoryPath(
1377                    PortletDataContext portletDataContext, long assetCategoryId) {
1378    
1379                    StringBundler sb = new StringBundler(4);
1380    
1381                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1382                    sb.append("/categories/");
1383                    sb.append(assetCategoryId);
1384                    sb.append(".xml");
1385    
1386                    return sb.toString();
1387            }
1388    
1389            protected String getAssetLinkPath(
1390                    PortletDataContext portletDataContext, long assetLinkId) {
1391    
1392                    StringBundler sb = new StringBundler(4);
1393    
1394                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1395                    sb.append("/links/");
1396                    sb.append(assetLinkId);
1397                    sb.append(".xml");
1398    
1399                    return sb.toString();
1400            }
1401    
1402            protected String getAssetTagPath(
1403                    PortletDataContext portletDataContext, long assetCategoryId) {
1404    
1405                    StringBundler sb = new StringBundler(4);
1406    
1407                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1408                    sb.append("/tags/");
1409                    sb.append(assetCategoryId);
1410                    sb.append(".xml");
1411    
1412                    return sb.toString();
1413            }
1414    
1415            protected String getAssetVocabulariesPath(
1416                    PortletDataContext portletDataContext, long assetVocabularyId) {
1417    
1418                    StringBundler sb = new StringBundler(4);
1419    
1420                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1421                    sb.append("/vocabularies/");
1422                    sb.append(assetVocabularyId);
1423                    sb.append(".xml");
1424    
1425                    return sb.toString();
1426            }
1427    
1428            protected String getCommentPath(
1429                    PortletDataContext portletDataContext, String className, String classPK,
1430                    MBMessage mbMessage) {
1431    
1432                    StringBundler sb = new StringBundler(8);
1433    
1434                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1435                    sb.append("/comments/");
1436                    sb.append(PortalUtil.getClassNameId(className));
1437                    sb.append(CharPool.FORWARD_SLASH);
1438                    sb.append(classPK);
1439                    sb.append(CharPool.FORWARD_SLASH);
1440                    sb.append(mbMessage.getMessageId());
1441                    sb.append(".xml");
1442    
1443                    return sb.toString();
1444            }
1445    
1446            protected String getCommentsPath(
1447                    PortletDataContext portletDataContext, String className,
1448                    String classPK) {
1449    
1450                    StringBundler sb = new StringBundler(6);
1451    
1452                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1453                    sb.append("/comments/");
1454                    sb.append(PortalUtil.getClassNameId(className));
1455                    sb.append(CharPool.FORWARD_SLASH);
1456                    sb.append(classPK);
1457                    sb.append(CharPool.FORWARD_SLASH);
1458    
1459                    return sb.toString();
1460            }
1461    
1462            protected String getLockPath(
1463                    PortletDataContext portletDataContext, String className, String key,
1464                    Lock lock) {
1465    
1466                    StringBundler sb = new StringBundler(8);
1467    
1468                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1469                    sb.append("/locks/");
1470                    sb.append(PortalUtil.getClassNameId(className));
1471                    sb.append(CharPool.FORWARD_SLASH);
1472                    sb.append(key);
1473                    sb.append(CharPool.FORWARD_SLASH);
1474                    sb.append(lock.getLockId());
1475                    sb.append(".xml");
1476    
1477                    return sb.toString();
1478            }
1479    
1480            protected String getPortletPreferencesPath(
1481                    PortletDataContext portletDataContext, String portletId, long ownerId,
1482                    int ownerType, long plid) {
1483    
1484                    StringBundler sb = new StringBundler(8);
1485    
1486                    sb.append(
1487                            ExportImportPathUtil.getPortletPath(portletDataContext, portletId));
1488                    sb.append("/preferences/");
1489    
1490                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
1491                            sb.append("company/");
1492                    }
1493                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
1494                            sb.append("group/");
1495                    }
1496                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT) {
1497                            sb.append("layout/");
1498                    }
1499                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1500                            sb.append("user/");
1501                    }
1502                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1503                            sb.append("archived/");
1504                    }
1505    
1506                    sb.append(ownerId);
1507                    sb.append(CharPool.FORWARD_SLASH);
1508                    sb.append(plid);
1509                    sb.append(CharPool.FORWARD_SLASH);
1510                    sb.append("portlet-preferences.xml");
1511    
1512                    return sb.toString();
1513            }
1514    
1515            protected String getRatingsEntriesPath(
1516                    PortletDataContext portletDataContext, String className,
1517                    String classPK) {
1518    
1519                    StringBundler sb = new StringBundler(6);
1520    
1521                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1522                    sb.append("/ratings/");
1523                    sb.append(PortalUtil.getClassNameId(className));
1524                    sb.append(CharPool.FORWARD_SLASH);
1525                    sb.append(classPK);
1526                    sb.append(CharPool.FORWARD_SLASH);
1527    
1528                    return sb.toString();
1529            }
1530    
1531            protected String getRatingsEntryPath(
1532                    PortletDataContext portletDataContext, String className, String classPK,
1533                    RatingsEntry ratingsEntry) {
1534    
1535                    StringBundler sb = new StringBundler(8);
1536    
1537                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1538                    sb.append("/ratings/");
1539                    sb.append(PortalUtil.getClassNameId(className));
1540                    sb.append(CharPool.FORWARD_SLASH);
1541                    sb.append(classPK);
1542                    sb.append(CharPool.FORWARD_SLASH);
1543                    sb.append(ratingsEntry.getEntryId());
1544                    sb.append(".xml");
1545    
1546                    return sb.toString();
1547            }
1548    
1549            private static Log _log = LogFactoryUtil.getLog(PortletExporter.class);
1550    
1551            private DeletionSystemEventExporter _deletionSystemEventExporter =
1552                    new DeletionSystemEventExporter();
1553            private PermissionExporter _permissionExporter = new PermissionExporter();
1554    
1555    }