001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.lar;
016    
017    import com.liferay.portal.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                    String rootPortletId = PortletConstants.getRootPortletId(portletId);
961    
962                    portletDataContext.setRootPortletId(rootPortletId);
963    
964                    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
965                            PortletDataContext clonedPortletDataContext =
966                                    PortletDataContextFactoryUtil.clonePortletDataContext(
967                                            portletDataContext);
968    
969                            ManifestSummary manifestSummary =
970                                    clonedPortletDataContext.getManifestSummary();
971    
972                            manifestSummary.resetCounters();
973    
974                            PortletDataHandler portletDataHandler =
975                                    portlet.getPortletDataHandlerInstance();
976    
977                            portletDataHandler.prepareManifestSummary(clonedPortletDataContext);
978    
979                            PortletDataHandlerStatusMessageSenderUtil.sendStatusMessage(
980                                    "portlet", portletId, manifestSummary);
981                    }
982    
983                    Document document = SAXReaderUtil.createDocument();
984    
985                    Element portletElement = document.addElement("portlet");
986    
987                    portletElement.addAttribute("portlet-id", portletId);
988                    portletElement.addAttribute("root-portlet-id", rootPortletId);
989                    portletElement.addAttribute("old-plid", String.valueOf(plid));
990                    portletElement.addAttribute(
991                            "scope-layout-type", portletDataContext.getScopeType());
992                    portletElement.addAttribute(
993                            "scope-layout-uuid", portletDataContext.getScopeLayoutUuid());
994                    portletElement.addAttribute(
995                            "private-layout", String.valueOf(layout.isPrivateLayout()));
996    
997                    // Data
998    
999                    if (exportPortletData) {
1000                            javax.portlet.PortletPreferences jxPortletPreferences =
1001                                    PortletPreferencesFactoryUtil.getStrictPortletSetup(
1002                                            layout, portletId);
1003    
1004                            if (!portlet.isPreferencesUniquePerLayout()) {
1005                                    StringBundler sb = new StringBundler(5);
1006    
1007                                    sb.append(portletId);
1008                                    sb.append(StringPool.AT);
1009                                    sb.append(portletDataContext.getScopeType());
1010                                    sb.append(StringPool.AT);
1011                                    sb.append(portletDataContext.getScopeLayoutUuid());
1012    
1013                                    String dataKey = sb.toString();
1014    
1015                                    if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
1016                                            portletDataContext.putNotUniquePerLayout(dataKey);
1017    
1018                                            exportPortletData(
1019                                                    portletDataContext, portlet, layout,
1020                                                    jxPortletPreferences, portletElement);
1021                                    }
1022                            }
1023                            else {
1024                                    exportPortletData(
1025                                            portletDataContext, portlet, layout, jxPortletPreferences,
1026                                            portletElement);
1027                            }
1028                    }
1029    
1030                    // Portlet preferences
1031    
1032                    if (exportPortletSetup) {
1033    
1034                            // Company
1035    
1036                            exportPortletPreferences(
1037                                    portletDataContext, portletDataContext.getCompanyId(),
1038                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY, false, layout, plid,
1039                                    portlet.getRootPortletId(), portletElement);
1040    
1041                            // Group
1042    
1043                            exportPortletPreferences(
1044                                    portletDataContext, portletDataContext.getScopeGroupId(),
1045                                    PortletKeys.PREFS_OWNER_TYPE_GROUP, false, layout,
1046                                    PortletKeys.PREFS_PLID_SHARED, portlet.getRootPortletId(),
1047                                    portletElement);
1048    
1049                            // Layout
1050    
1051                            exportPortletPreferences(
1052                                    portletDataContext, PortletKeys.PREFS_OWNER_ID_DEFAULT,
1053                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false, layout, plid,
1054                                    portletId, portletElement);
1055                    }
1056    
1057                    // Portlet user preferences
1058    
1059                    if (exportPortletUserPreferences) {
1060                            List<PortletPreferences> portletPreferencesList =
1061                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
1062                                            PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId);
1063    
1064                            for (PortletPreferences portletPreferences :
1065                                            portletPreferencesList) {
1066    
1067                                    boolean defaultUser = false;
1068    
1069                                    if (portletPreferences.getOwnerId() ==
1070                                                    PortletKeys.PREFS_OWNER_ID_DEFAULT) {
1071    
1072                                            defaultUser = true;
1073                                    }
1074    
1075                                    exportPortletPreferences(
1076                                            portletDataContext, portletPreferences.getOwnerId(),
1077                                            PortletKeys.PREFS_OWNER_TYPE_USER, defaultUser, layout,
1078                                            plid, portletId, portletElement);
1079                            }
1080    
1081                            try {
1082                                    PortletPreferences groupPortletPreferences =
1083                                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1084                                                    portletDataContext.getScopeGroupId(),
1085                                                    PortletKeys.PREFS_OWNER_TYPE_GROUP,
1086                                                    PortletKeys.PREFS_PLID_SHARED,
1087                                                    portlet.getRootPortletId());
1088    
1089                                    exportPortletPreference(
1090                                            portletDataContext, portletDataContext.getScopeGroupId(),
1091                                            PortletKeys.PREFS_OWNER_TYPE_GROUP, false,
1092                                            groupPortletPreferences, portlet.getRootPortletId(),
1093                                            PortletKeys.PREFS_PLID_SHARED, portletElement);
1094                            }
1095                            catch (NoSuchPortletPreferencesException nsppe) {
1096                            }
1097                    }
1098    
1099                    // Archived setups
1100    
1101                    if (exportPortletArchivedSetups) {
1102                            List<PortletItem> portletItems =
1103                                    PortletItemLocalServiceUtil.getPortletItems(
1104                                            portletDataContext.getGroupId(), rootPortletId,
1105                                            PortletPreferences.class.getName());
1106    
1107                            for (PortletItem portletItem : portletItems) {
1108                                    exportPortletPreferences(
1109                                            portletDataContext, portletItem.getPortletItemId(),
1110                                            PortletKeys.PREFS_OWNER_TYPE_ARCHIVED, false, null, plid,
1111                                            portletItem.getPortletId(), portletElement);
1112                            }
1113                    }
1114    
1115                    // Permissions
1116    
1117                    if (exportPermissions) {
1118                            _permissionExporter.exportPortletPermissions(
1119                                    portletDataContext, layoutCache, portletId, layout,
1120                                    portletElement);
1121                    }
1122    
1123                    // Zip
1124    
1125                    StringBundler pathSB = new StringBundler(4);
1126    
1127                    pathSB.append(
1128                            ExportImportPathUtil.getPortletPath(portletDataContext, portletId));
1129                    pathSB.append(StringPool.SLASH);
1130                    pathSB.append(plid);
1131                    pathSB.append("/portlet.xml");
1132    
1133                    String path = pathSB.toString();
1134    
1135                    Element element = parentElement.addElement("portlet");
1136    
1137                    element.addAttribute("portlet-id", portletId);
1138                    element.addAttribute("layout-id", String.valueOf(layoutId));
1139                    element.addAttribute("path", path);
1140                    element.addAttribute("portlet-data", String.valueOf(exportPortletData));
1141    
1142                    StringBundler configurationOptionsSB = new StringBundler(6);
1143    
1144                    if (exportPortletSetup) {
1145                            configurationOptionsSB.append("setup");
1146                            configurationOptionsSB.append(StringPool.COMMA);
1147                    }
1148    
1149                    if (exportPortletArchivedSetups) {
1150                            configurationOptionsSB.append("archived-setups");
1151                            configurationOptionsSB.append(StringPool.COMMA);
1152                    }
1153    
1154                    if (exportPortletUserPreferences) {
1155                            configurationOptionsSB.append("user-preferences");
1156                            configurationOptionsSB.append(StringPool.COMMA);
1157                    }
1158    
1159                    if (configurationOptionsSB.index() > 0) {
1160                            configurationOptionsSB.setIndex(configurationOptionsSB.index() -1);
1161                    }
1162    
1163                    element.addAttribute(
1164                            "portlet-configuration", configurationOptionsSB.toString());
1165    
1166                    if (portletDataContext.isPathNotProcessed(path)) {
1167                            try {
1168                                    portletDataContext.addZipEntry(
1169                                            path, document.formattedString());
1170                            }
1171                            catch (IOException ioe) {
1172                                    if (_log.isWarnEnabled()) {
1173                                            _log.warn(ioe.getMessage());
1174                                    }
1175                            }
1176    
1177                            portletDataContext.addPrimaryKey(String.class, path);
1178                    }
1179    
1180                    portletDataContext.setRootPortletId(StringPool.BLANK);
1181            }
1182    
1183            protected void exportPortletPreference(
1184                            PortletDataContext portletDataContext, long ownerId, int ownerType,
1185                            boolean defaultUser, PortletPreferences portletPreferences,
1186                            String portletId, long plid, Element parentElement)
1187                    throws Exception {
1188    
1189                    String preferencesXML = portletPreferences.getPreferences();
1190    
1191                    if (Validator.isNull(preferencesXML)) {
1192                            preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
1193                    }
1194    
1195                    javax.portlet.PortletPreferences jxPortletPreferences =
1196                            PortletPreferencesFactoryUtil.fromDefaultXML(preferencesXML);
1197    
1198                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
1199                            portletDataContext.getCompanyId(), portletId);
1200    
1201                    Element portletPreferencesElement = parentElement.addElement(
1202                            "portlet-preferences");
1203    
1204                    if ((portlet != null) &&
1205                            (portlet.getPortletDataHandlerInstance() != null)) {
1206    
1207                            Element exportDataRootElement =
1208                                    portletDataContext.getExportDataRootElement();
1209    
1210                            try {
1211                                    portletDataContext.clearScopedPrimaryKeys();
1212    
1213                                    Element preferenceDataElement =
1214                                            portletPreferencesElement.addElement("preference-data");
1215    
1216                                    portletDataContext.setExportDataRootElement(
1217                                            preferenceDataElement);
1218    
1219                                    PortletDataHandler portletDataHandler =
1220                                            portlet.getPortletDataHandlerInstance();
1221    
1222                                    jxPortletPreferences =
1223                                            portletDataHandler.processExportPortletPreferences(
1224                                                    portletDataContext, portletId, jxPortletPreferences);
1225                            }
1226                            finally {
1227                                    portletDataContext.setExportDataRootElement(
1228                                            exportDataRootElement);
1229                            }
1230                    }
1231    
1232                    Document document = SAXReaderUtil.read(
1233                            PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));
1234    
1235                    Element rootElement = document.getRootElement();
1236    
1237                    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
1238                    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
1239                    rootElement.addAttribute("default-user", String.valueOf(defaultUser));
1240                    rootElement.addAttribute("plid", String.valueOf(plid));
1241                    rootElement.addAttribute("portlet-id", portletId);
1242    
1243                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1244                            PortletItem portletItem =
1245                                    PortletItemLocalServiceUtil.getPortletItem(ownerId);
1246    
1247                            rootElement.addAttribute(
1248                                    "archive-user-uuid", portletItem.getUserUuid());
1249                            rootElement.addAttribute("archive-name", portletItem.getName());
1250                    }
1251                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1252                            User user = UserLocalServiceUtil.fetchUserById(ownerId);
1253    
1254                            if (user == null) {
1255                                    return;
1256                            }
1257    
1258                            rootElement.addAttribute("user-uuid", user.getUserUuid());
1259                    }
1260    
1261                    List<Node> nodes = document.selectNodes(
1262                            "/portlet-preferences/preference[name/text() = " +
1263                                    "'last-publish-date']");
1264    
1265                    for (Node node : nodes) {
1266                            document.remove(node);
1267                    }
1268    
1269                    String path = getPortletPreferencesPath(
1270                            portletDataContext, portletId, ownerId, ownerType, plid);
1271    
1272                    portletPreferencesElement.addAttribute("path", path);
1273    
1274                    if (portletDataContext.isPathNotProcessed(path)) {
1275                            portletDataContext.addZipEntry(
1276                                    path, document.formattedString(StringPool.TAB, false, false));
1277                    }
1278            }
1279    
1280            protected void exportPortletPreferences(
1281                            PortletDataContext portletDataContext, long ownerId, int ownerType,
1282                            boolean defaultUser, Layout layout, long plid, String portletId,
1283                            Element parentElement)
1284                    throws Exception {
1285    
1286                    PortletPreferences portletPreferences = null;
1287    
1288                    try {
1289                            if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) ||
1290                                    (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) ||
1291                                    (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED)) {
1292    
1293                                    portletPreferences =
1294                                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1295                                                    ownerId, ownerType, LayoutConstants.DEFAULT_PLID,
1296                                                    portletId);
1297                            }
1298                            else {
1299                                    portletPreferences =
1300                                            PortletPreferencesLocalServiceUtil.getPortletPreferences(
1301                                                    ownerId, ownerType, plid, portletId);
1302                            }
1303                    }
1304                    catch (NoSuchPortletPreferencesException nsppe) {
1305                            return;
1306                    }
1307    
1308                    LayoutTypePortlet layoutTypePortlet = null;
1309    
1310                    if (layout != null) {
1311                            layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
1312                    }
1313    
1314                    if ((layoutTypePortlet == null) ||
1315                            layoutTypePortlet.hasPortletId(portletId)) {
1316    
1317                            exportPortletPreference(
1318                                    portletDataContext, ownerId, ownerType, defaultUser,
1319                                    portletPreferences, portletId, plid, parentElement);
1320                    }
1321            }
1322    
1323            protected void exportRatingsEntries(
1324                            PortletDataContext portletDataContext, Element parentElement)
1325                    throws Exception {
1326    
1327                    Document document = SAXReaderUtil.createDocument();
1328    
1329                    Element rootElement = document.addElement("ratings");
1330    
1331                    Map<String, List<RatingsEntry>> ratingsEntriesMap =
1332                            portletDataContext.getRatingsEntries();
1333    
1334                    for (Map.Entry<String, List<RatingsEntry>> entry :
1335                                    ratingsEntriesMap.entrySet()) {
1336    
1337                            String[] ratingsEntryParts = StringUtil.split(
1338                                    entry.getKey(), CharPool.POUND);
1339    
1340                            String className = ratingsEntryParts[0];
1341                            String classPK = ratingsEntryParts[1];
1342    
1343                            String ratingsEntriesPath = getRatingsEntriesPath(
1344                                    portletDataContext, className, classPK);
1345    
1346                            Element assetElement = rootElement.addElement("asset");
1347    
1348                            assetElement.addAttribute("path", ratingsEntriesPath);
1349                            assetElement.addAttribute("class-name", className);
1350                            assetElement.addAttribute("class-pk", classPK);
1351    
1352                            List<RatingsEntry> ratingsEntries = entry.getValue();
1353    
1354                            for (RatingsEntry ratingsEntry : ratingsEntries) {
1355                                    String ratingsEntryPath = getRatingsEntryPath(
1356                                            portletDataContext, className, classPK, ratingsEntry);
1357    
1358                                    User user = UserLocalServiceUtil.fetchUser(
1359                                            ratingsEntry.getUserId());
1360    
1361                                    if (user == null) {
1362                                            continue;
1363                                    }
1364    
1365                                    portletDataContext.addZipEntry(ratingsEntryPath, ratingsEntry);
1366    
1367                                    portletDataContext.addReferenceElement(
1368                                            ratingsEntry, assetElement, user,
1369                                            PortletDataContext.REFERENCE_TYPE_WEAK, true);
1370                            }
1371                    }
1372    
1373                    portletDataContext.addZipEntry(
1374                            ExportImportPathUtil.getRootPath(portletDataContext) +
1375                                    "/ratings.xml",
1376                            document.formattedString());
1377            }
1378    
1379            protected String getAssetCategoryPath(
1380                    PortletDataContext portletDataContext, long assetCategoryId) {
1381    
1382                    StringBundler sb = new StringBundler(4);
1383    
1384                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1385                    sb.append("/categories/");
1386                    sb.append(assetCategoryId);
1387                    sb.append(".xml");
1388    
1389                    return sb.toString();
1390            }
1391    
1392            protected String getAssetLinkPath(
1393                    PortletDataContext portletDataContext, long assetLinkId) {
1394    
1395                    StringBundler sb = new StringBundler(4);
1396    
1397                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1398                    sb.append("/links/");
1399                    sb.append(assetLinkId);
1400                    sb.append(".xml");
1401    
1402                    return sb.toString();
1403            }
1404    
1405            protected String getAssetTagPath(
1406                    PortletDataContext portletDataContext, long assetCategoryId) {
1407    
1408                    StringBundler sb = new StringBundler(4);
1409    
1410                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1411                    sb.append("/tags/");
1412                    sb.append(assetCategoryId);
1413                    sb.append(".xml");
1414    
1415                    return sb.toString();
1416            }
1417    
1418            protected String getAssetVocabulariesPath(
1419                    PortletDataContext portletDataContext, long assetVocabularyId) {
1420    
1421                    StringBundler sb = new StringBundler(4);
1422    
1423                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1424                    sb.append("/vocabularies/");
1425                    sb.append(assetVocabularyId);
1426                    sb.append(".xml");
1427    
1428                    return sb.toString();
1429            }
1430    
1431            protected String getCommentPath(
1432                    PortletDataContext portletDataContext, String className, String classPK,
1433                    MBMessage mbMessage) {
1434    
1435                    StringBundler sb = new StringBundler(8);
1436    
1437                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1438                    sb.append("/comments/");
1439                    sb.append(PortalUtil.getClassNameId(className));
1440                    sb.append(CharPool.FORWARD_SLASH);
1441                    sb.append(classPK);
1442                    sb.append(CharPool.FORWARD_SLASH);
1443                    sb.append(mbMessage.getMessageId());
1444                    sb.append(".xml");
1445    
1446                    return sb.toString();
1447            }
1448    
1449            protected String getCommentsPath(
1450                    PortletDataContext portletDataContext, String className,
1451                    String classPK) {
1452    
1453                    StringBundler sb = new StringBundler(6);
1454    
1455                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1456                    sb.append("/comments/");
1457                    sb.append(PortalUtil.getClassNameId(className));
1458                    sb.append(CharPool.FORWARD_SLASH);
1459                    sb.append(classPK);
1460                    sb.append(CharPool.FORWARD_SLASH);
1461    
1462                    return sb.toString();
1463            }
1464    
1465            protected String getLockPath(
1466                    PortletDataContext portletDataContext, String className, String key,
1467                    Lock lock) {
1468    
1469                    StringBundler sb = new StringBundler(8);
1470    
1471                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1472                    sb.append("/locks/");
1473                    sb.append(PortalUtil.getClassNameId(className));
1474                    sb.append(CharPool.FORWARD_SLASH);
1475                    sb.append(key);
1476                    sb.append(CharPool.FORWARD_SLASH);
1477                    sb.append(lock.getLockId());
1478                    sb.append(".xml");
1479    
1480                    return sb.toString();
1481            }
1482    
1483            protected String getPortletPreferencesPath(
1484                    PortletDataContext portletDataContext, String portletId, long ownerId,
1485                    int ownerType, long plid) {
1486    
1487                    StringBundler sb = new StringBundler(8);
1488    
1489                    sb.append(
1490                            ExportImportPathUtil.getPortletPath(portletDataContext, portletId));
1491                    sb.append("/preferences/");
1492    
1493                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
1494                            sb.append("company/");
1495                    }
1496                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
1497                            sb.append("group/");
1498                    }
1499                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT) {
1500                            sb.append("layout/");
1501                    }
1502                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
1503                            sb.append("user/");
1504                    }
1505                    else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
1506                            sb.append("archived/");
1507                    }
1508    
1509                    sb.append(ownerId);
1510                    sb.append(CharPool.FORWARD_SLASH);
1511                    sb.append(plid);
1512                    sb.append(CharPool.FORWARD_SLASH);
1513                    sb.append("portlet-preferences.xml");
1514    
1515                    return sb.toString();
1516            }
1517    
1518            protected String getRatingsEntriesPath(
1519                    PortletDataContext portletDataContext, String className,
1520                    String classPK) {
1521    
1522                    StringBundler sb = new StringBundler(6);
1523    
1524                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1525                    sb.append("/ratings/");
1526                    sb.append(PortalUtil.getClassNameId(className));
1527                    sb.append(CharPool.FORWARD_SLASH);
1528                    sb.append(classPK);
1529                    sb.append(CharPool.FORWARD_SLASH);
1530    
1531                    return sb.toString();
1532            }
1533    
1534            protected String getRatingsEntryPath(
1535                    PortletDataContext portletDataContext, String className, String classPK,
1536                    RatingsEntry ratingsEntry) {
1537    
1538                    StringBundler sb = new StringBundler(8);
1539    
1540                    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
1541                    sb.append("/ratings/");
1542                    sb.append(PortalUtil.getClassNameId(className));
1543                    sb.append(CharPool.FORWARD_SLASH);
1544                    sb.append(classPK);
1545                    sb.append(CharPool.FORWARD_SLASH);
1546                    sb.append(ratingsEntry.getEntryId());
1547                    sb.append(".xml");
1548    
1549                    return sb.toString();
1550            }
1551    
1552            private static Log _log = LogFactoryUtil.getLog(PortletExporter.class);
1553    
1554            private DeletionSystemEventExporter _deletionSystemEventExporter =
1555                    new DeletionSystemEventExporter();
1556            private PermissionExporter _permissionExporter = new PermissionExporter();
1557    
1558    }