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