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