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