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