001    /**
002     * Copyright (c) 2000-present 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.portlet.exportimport.service.impl;
016    
017    import com.liferay.portal.LocaleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.DateRange;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.model.BackgroundTask;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.documentlibrary.util.DLValidatorUtil;
028    import com.liferay.portlet.dynamicdatamapping.StructureDuplicateStructureKeyException;
029    import com.liferay.portlet.exportimport.LARFileNameException;
030    import com.liferay.portlet.exportimport.backgroundtask.LayoutExportBackgroundTaskExecutor;
031    import com.liferay.portlet.exportimport.backgroundtask.LayoutImportBackgroundTaskExecutor;
032    import com.liferay.portlet.exportimport.backgroundtask.PortletExportBackgroundTaskExecutor;
033    import com.liferay.portlet.exportimport.backgroundtask.PortletImportBackgroundTaskExecutor;
034    import com.liferay.portlet.exportimport.lar.ExportImportDateUtil;
035    import com.liferay.portlet.exportimport.lar.LayoutExporter;
036    import com.liferay.portlet.exportimport.lar.LayoutImporter;
037    import com.liferay.portlet.exportimport.lar.MissingReferences;
038    import com.liferay.portlet.exportimport.lar.PortletDataException;
039    import com.liferay.portlet.exportimport.lar.PortletExporter;
040    import com.liferay.portlet.exportimport.lar.PortletImporter;
041    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
042    import com.liferay.portlet.exportimport.service.base.ExportImportLocalServiceBaseImpl;
043    
044    import java.io.File;
045    import java.io.IOException;
046    import java.io.InputStream;
047    import java.io.Serializable;
048    
049    import java.util.HashMap;
050    import java.util.Map;
051    
052    /**
053     * @author Daniel Kocsis
054     */
055    public class ExportImportLocalServiceImpl
056            extends ExportImportLocalServiceBaseImpl {
057    
058            @Override
059            public File exportLayoutsAsFile(
060                            ExportImportConfiguration exportImportConfiguration)
061                    throws PortalException {
062    
063                    try {
064                            LayoutExporter layoutExporter = LayoutExporter.getInstance();
065    
066                            Map<String, Serializable> settingsMap =
067                                    exportImportConfiguration.getSettingsMap();
068    
069                            long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");
070                            boolean privateLayout = MapUtil.getBoolean(
071                                    settingsMap, "privateLayout");
072                            long[] layoutIds = GetterUtil.getLongValues(
073                                    settingsMap.get("layoutIds"));
074                            Map<String, String[]> parameterMap =
075                                    (Map<String, String[]>)settingsMap.get("parameterMap");
076                            DateRange dateRange = ExportImportDateUtil.getDateRange(
077                                    exportImportConfiguration);
078    
079                            return layoutExporter.exportLayoutsAsFile(
080                                    sourceGroupId, privateLayout, layoutIds, parameterMap,
081                                    dateRange.getStartDate(), dateRange.getEndDate());
082                    }
083                    catch (PortalException pe) {
084                            throw pe;
085                    }
086                    catch (SystemException se) {
087                            throw se;
088                    }
089                    catch (Exception e) {
090                            throw new SystemException(e);
091                    }
092            }
093    
094            @Override
095            public long exportLayoutsAsFileInBackground(
096                            long userId, ExportImportConfiguration exportImportConfiguration)
097                    throws PortalException {
098    
099                    if (!DLValidatorUtil.isValidName(exportImportConfiguration.getName())) {
100                            throw new LARFileNameException(exportImportConfiguration.getName());
101                    }
102    
103                    Map<String, Serializable> taskContextMap = new HashMap<>();
104    
105                    taskContextMap.put(Constants.CMD, Constants.EXPORT);
106                    taskContextMap.put(
107                            "exportImportConfigurationId",
108                            exportImportConfiguration.getExportImportConfigurationId());
109    
110                    BackgroundTask backgroundTask =
111                            backgroundTaskLocalService.addBackgroundTask(
112                                    userId, exportImportConfiguration.getGroupId(),
113                                    exportImportConfiguration.getName(), null,
114                                    LayoutExportBackgroundTaskExecutor.class, taskContextMap,
115                                    new ServiceContext());
116    
117                    return backgroundTask.getBackgroundTaskId();
118            }
119    
120            @Override
121            public long exportLayoutsAsFileInBackground(
122                            long userId, long exportImportConfigurationId)
123                    throws PortalException {
124    
125                    ExportImportConfiguration exportImportConfiguration =
126                            exportImportConfigurationLocalService.getExportImportConfiguration(
127                                    exportImportConfigurationId);
128    
129                    return exportLayoutsAsFileInBackground(
130                            userId, exportImportConfiguration);
131            }
132    
133            @Override
134            public File exportPortletInfoAsFile(
135                            ExportImportConfiguration exportImportConfiguration)
136                    throws PortalException {
137    
138                    try {
139                            PortletExporter portletExporter = PortletExporter.getInstance();
140    
141                            Map<String, Serializable> settingsMap =
142                                    exportImportConfiguration.getSettingsMap();
143    
144                            long sourcePlid = MapUtil.getLong(settingsMap, "sourcePlid");
145                            long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");
146                            String portletId = MapUtil.getString(settingsMap, "portletId");
147                            Map<String, String[]> parameterMap =
148                                    (Map<String, String[]>)settingsMap.get("parameterMap");
149                            DateRange dateRange = ExportImportDateUtil.getDateRange(
150                                    exportImportConfiguration);
151    
152                            return portletExporter.exportPortletInfoAsFile(
153                                    sourcePlid, sourceGroupId, portletId, parameterMap,
154                                    dateRange.getStartDate(), dateRange.getEndDate());
155                    }
156                    catch (PortalException pe) {
157                            throw pe;
158                    }
159                    catch (SystemException se) {
160                            throw se;
161                    }
162                    catch (Exception e) {
163                            throw new SystemException(e);
164                    }
165            }
166    
167            @Override
168            public long exportPortletInfoAsFileInBackground(
169                            long userId, ExportImportConfiguration exportImportConfiguration)
170                    throws PortalException {
171    
172                    Map<String, Serializable> settingsMap =
173                            exportImportConfiguration.getSettingsMap();
174    
175                    String fileName = MapUtil.getString(settingsMap, "fileName");
176    
177                    if (!DLValidatorUtil.isValidName(fileName)) {
178                            throw new LARFileNameException(fileName);
179                    }
180    
181                    Map<String, Serializable> taskContextMap = new HashMap<>();
182    
183                    taskContextMap.put(Constants.CMD, Constants.EXPORT);
184                    taskContextMap.put(
185                            "exportImportConfigurationId",
186                            exportImportConfiguration.getExportImportConfigurationId());
187    
188                    BackgroundTask backgroundTask =
189                            backgroundTaskLocalService.addBackgroundTask(
190                                    userId, exportImportConfiguration.getGroupId(),
191                                    exportImportConfiguration.getName(), null,
192                                    PortletExportBackgroundTaskExecutor.class, taskContextMap,
193                                    new ServiceContext());
194    
195                    return backgroundTask.getBackgroundTaskId();
196            }
197    
198            @Override
199            public long exportPortletInfoAsFileInBackground(
200                            long userId, long exportImportConfigurationId)
201                    throws PortalException {
202    
203                    ExportImportConfiguration exportImportConfiguration =
204                            exportImportConfigurationLocalService.getExportImportConfiguration(
205                                    exportImportConfigurationId);
206    
207                    return exportPortletInfoAsFileInBackground(
208                            userId, exportImportConfiguration);
209            }
210    
211            @Override
212            public void importLayouts(
213                            ExportImportConfiguration exportImportConfiguration, File file)
214                    throws PortalException {
215    
216                    try {
217                            LayoutImporter layoutImporter = LayoutImporter.getInstance();
218    
219                            Map<String, Serializable> settingsMap =
220                                    exportImportConfiguration.getSettingsMap();
221    
222                            long userId = MapUtil.getLong(settingsMap, "userId");
223                            long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
224                            boolean privateLayout = MapUtil.getBoolean(
225                                    settingsMap, "privateLayout");
226                            Map<String, String[]> parameterMap =
227                                    (Map<String, String[]>)settingsMap.get("parameterMap");
228    
229                            layoutImporter.importLayouts(
230                                    userId, targetGroupId, privateLayout, parameterMap, file);
231                    }
232                    catch (PortalException pe) {
233                            Throwable cause = pe.getCause();
234    
235                            if (cause instanceof LocaleException) {
236                                    throw (PortalException)cause;
237                            }
238    
239                            throw pe;
240                    }
241                    catch (SystemException se) {
242                            throw se;
243                    }
244                    catch (Exception e) {
245                            throw new SystemException(e);
246                    }
247            }
248    
249            @Override
250            public void importLayouts(
251                            ExportImportConfiguration exportImportConfiguration,
252                            InputStream inputStream)
253                    throws PortalException {
254    
255                    File file = null;
256    
257                    try {
258                            file = FileUtil.createTempFile("lar");
259    
260                            FileUtil.write(file, inputStream);
261    
262                            importLayouts(exportImportConfiguration, file);
263                    }
264                    catch (IOException ioe) {
265                            throw new SystemException(ioe);
266                    }
267                    finally {
268                            FileUtil.delete(file);
269                    }
270            }
271    
272            @Override
273            public void importLayoutsDataDeletions(
274                            ExportImportConfiguration exportImportConfiguration, File file)
275                    throws PortalException {
276    
277                    try {
278                            LayoutImporter layoutImporter = LayoutImporter.getInstance();
279    
280                            Map<String, Serializable> settingsMap =
281                                    exportImportConfiguration.getSettingsMap();
282    
283                            long userId = MapUtil.getLong(settingsMap, "userId");
284                            long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
285                            boolean privateLayout = MapUtil.getBoolean(
286                                    settingsMap, "privateLayout");
287                            Map<String, String[]> parameterMap =
288                                    (Map<String, String[]>)settingsMap.get("parameterMap");
289    
290                            layoutImporter.importLayoutsDataDeletions(
291                                    userId, targetGroupId, privateLayout, parameterMap, file);
292                    }
293                    catch (PortalException pe) {
294                            Throwable cause = pe.getCause();
295    
296                            if (cause instanceof LocaleException) {
297                                    throw (PortalException)cause;
298                            }
299    
300                            throw pe;
301                    }
302                    catch (SystemException se) {
303                            throw se;
304                    }
305                    catch (Exception e) {
306                            throw new SystemException(e);
307                    }
308            }
309    
310            @Override
311            public long importLayoutsInBackground(
312                            long userId, ExportImportConfiguration exportImportConfiguration,
313                            File file)
314                    throws PortalException {
315    
316                    Map<String, Serializable> taskContextMap = new HashMap<>();
317    
318                    taskContextMap.put(Constants.CMD, Constants.IMPORT);
319                    taskContextMap.put(
320                            "exportImportConfigurationId",
321                            exportImportConfiguration.getExportImportConfigurationId());
322    
323                    BackgroundTask backgroundTask =
324                            backgroundTaskLocalService.addBackgroundTask(
325                                    userId, exportImportConfiguration.getGroupId(),
326                                    exportImportConfiguration.getName(), null,
327                                    LayoutImportBackgroundTaskExecutor.class, taskContextMap,
328                                    new ServiceContext());
329    
330                    backgroundTaskLocalService.addBackgroundTaskAttachment(
331                            userId, backgroundTask.getBackgroundTaskId(), file.getName(), file);
332    
333                    return backgroundTask.getBackgroundTaskId();
334            }
335    
336            @Override
337            public long importLayoutsInBackground(
338                            long userId, ExportImportConfiguration exportImportConfiguration,
339                            InputStream inputStream)
340                    throws PortalException {
341    
342                    File file = null;
343    
344                    try {
345                            file = FileUtil.createTempFile("lar");
346    
347                            FileUtil.write(file, inputStream);
348    
349                            return importLayoutsInBackground(
350                                    userId, exportImportConfiguration, file);
351                    }
352                    catch (IOException ioe) {
353                            throw new SystemException(ioe);
354                    }
355                    finally {
356                            FileUtil.delete(file);
357                    }
358            }
359    
360            @Override
361            public long importLayoutsInBackground(
362                            long userId, long exportImportConfigurationId, File file)
363                    throws PortalException {
364    
365                    ExportImportConfiguration exportImportConfiguration =
366                            exportImportConfigurationLocalService.getExportImportConfiguration(
367                                    exportImportConfigurationId);
368    
369                    return importPortletInfoInBackground(
370                            userId, exportImportConfiguration, file);
371            }
372    
373            @Override
374            public long importLayoutsInBackground(
375                            long userId, long exportImportConfigurationId,
376                            InputStream inputStream)
377                    throws PortalException {
378    
379                    ExportImportConfiguration exportImportConfiguration =
380                            exportImportConfigurationLocalService.getExportImportConfiguration(
381                                    exportImportConfigurationId);
382    
383                    return importLayoutsInBackground(
384                            userId, exportImportConfiguration, inputStream);
385            }
386    
387            @Override
388            public void importPortletDataDeletions(
389                            ExportImportConfiguration exportImportConfiguration, File file)
390                    throws PortalException {
391    
392                    try {
393                            PortletImporter portletImporter = PortletImporter.getInstance();
394    
395                            Map<String, Serializable> settingsMap =
396                                    exportImportConfiguration.getSettingsMap();
397    
398                            long userId = MapUtil.getLong(settingsMap, "userId");
399                            long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
400                            long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
401                            String portletId = MapUtil.getString(settingsMap, "portletId");
402                            Map<String, String[]> parameterMap =
403                                    (Map<String, String[]>)settingsMap.get("parameterMap");
404    
405                            portletImporter.importPortletDataDeletions(
406                                    userId, targetPlid, targetGroupId, portletId, parameterMap,
407                                    file);
408                    }
409                    catch (PortalException pe) {
410                            Throwable cause = pe.getCause();
411    
412                            if (cause instanceof LocaleException) {
413                                    throw (PortalException)cause;
414                            }
415    
416                            throw pe;
417                    }
418                    catch (SystemException se) {
419                            throw se;
420                    }
421                    catch (Exception e) {
422                            throw new SystemException(e);
423                    }
424            }
425    
426            @Override
427            public void importPortletInfo(
428                            ExportImportConfiguration exportImportConfiguration, File file)
429                    throws PortalException {
430    
431                    try {
432                            PortletImporter portletImporter = PortletImporter.getInstance();
433    
434                            Map<String, Serializable> settingsMap =
435                                    exportImportConfiguration.getSettingsMap();
436    
437                            long userId = MapUtil.getLong(settingsMap, "userId");
438                            long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
439                            long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
440                            String portletId = MapUtil.getString(settingsMap, "portletId");
441                            Map<String, String[]> parameterMap =
442                                    (Map<String, String[]>)settingsMap.get("parameterMap");
443    
444                            portletImporter.importPortletInfo(
445                                    userId, targetPlid, targetGroupId, portletId, parameterMap,
446                                    file);
447                    }
448                    catch (PortalException pe) {
449                            Throwable cause = pe.getCause();
450    
451                            while (true) {
452                                    if (cause == null) {
453                                            break;
454                                    }
455    
456                                    if ((cause instanceof LocaleException) ||
457                                            (cause instanceof
458                                                    StructureDuplicateStructureKeyException)) {
459    
460                                            throw (PortalException)cause;
461                                    }
462    
463                                    if (cause instanceof PortletDataException) {
464                                            cause = cause.getCause();
465                                    }
466                                    else {
467                                            break;
468                                    }
469                            }
470    
471                            throw pe;
472                    }
473                    catch (SystemException se) {
474                            throw se;
475                    }
476                    catch (Exception e) {
477                            throw new SystemException(e);
478                    }
479            }
480    
481            @Override
482            public void importPortletInfo(
483                            ExportImportConfiguration exportImportConfiguration,
484                            InputStream inputStream)
485                    throws PortalException {
486    
487                    File file = null;
488    
489                    try {
490                            file = FileUtil.createTempFile("lar");
491    
492                            FileUtil.write(file, inputStream);
493    
494                            importPortletInfo(exportImportConfiguration, file);
495                    }
496                    catch (IOException ioe) {
497                            throw new SystemException(ioe);
498                    }
499                    finally {
500                            FileUtil.delete(file);
501                    }
502            }
503    
504            @Override
505            public long importPortletInfoInBackground(
506                            long userId, ExportImportConfiguration exportImportConfiguration,
507                            File file)
508                    throws PortalException {
509    
510                    Map<String, Serializable> taskContextMap = new HashMap<>();
511    
512                    taskContextMap.put(Constants.CMD, Constants.IMPORT);
513                    taskContextMap.put(
514                            "exportImportConfigurationId",
515                            exportImportConfiguration.getExportImportConfigurationId());
516    
517                    BackgroundTask backgroundTask =
518                            backgroundTaskLocalService.addBackgroundTask(
519                                    userId, exportImportConfiguration.getGroupId(),
520                                    exportImportConfiguration.getName(), null,
521                                    PortletImportBackgroundTaskExecutor.class, taskContextMap,
522                                    new ServiceContext());
523    
524                    backgroundTaskLocalService.addBackgroundTaskAttachment(
525                            userId, backgroundTask.getBackgroundTaskId(), file.getName(), file);
526    
527                    return backgroundTask.getBackgroundTaskId();
528            }
529    
530            @Override
531            public long importPortletInfoInBackground(
532                            long userId, ExportImportConfiguration exportImportConfiguration,
533                            InputStream inputStream)
534                    throws PortalException {
535    
536                    File file = null;
537    
538                    try {
539                            file = FileUtil.createTempFile("lar");
540    
541                            FileUtil.write(file, inputStream);
542    
543                            return importPortletInfoInBackground(
544                                    userId, exportImportConfiguration, file);
545                    }
546                    catch (IOException ioe) {
547                            throw new SystemException(ioe);
548                    }
549                    finally {
550                            FileUtil.delete(file);
551                    }
552            }
553    
554            @Override
555            public long importPortletInfoInBackground(
556                            long userId, long exportImportConfigurationId, File file)
557                    throws PortalException {
558    
559                    ExportImportConfiguration exportImportConfiguration =
560                            exportImportConfigurationLocalService.getExportImportConfiguration(
561                                    exportImportConfigurationId);
562    
563                    return importPortletInfoInBackground(
564                            userId, exportImportConfiguration, file);
565            }
566    
567            @Override
568            public long importPortletInfoInBackground(
569                            long userId, long exportImportConfigurationId,
570                            InputStream inputStream)
571                    throws PortalException {
572    
573                    ExportImportConfiguration exportImportConfiguration =
574                            exportImportConfigurationLocalService.getExportImportConfiguration(
575                                    exportImportConfigurationId);
576    
577                    return importPortletInfoInBackground(
578                            userId, exportImportConfiguration, inputStream);
579            }
580    
581            @Override
582            public MissingReferences validateImportLayoutsFile(
583                            ExportImportConfiguration exportImportConfiguration, File file)
584                    throws PortalException {
585    
586                    try {
587                            LayoutImporter layoutImporter = LayoutImporter.getInstance();
588    
589                            Map<String, Serializable> settingsMap =
590                                    exportImportConfiguration.getSettingsMap();
591    
592                            long userId = MapUtil.getLong(settingsMap, "userId");
593                            long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
594                            boolean privateLayout = MapUtil.getBoolean(
595                                    settingsMap, "privateLayout");
596                            Map<String, String[]> parameterMap =
597                                    (Map<String, String[]>)settingsMap.get("parameterMap");
598    
599                            return layoutImporter.validateFile(
600                                    userId, targetGroupId, privateLayout, parameterMap, file);
601                    }
602                    catch (PortalException pe) {
603                            Throwable cause = pe.getCause();
604    
605                            if (cause instanceof LocaleException) {
606                                    throw (PortalException)cause;
607                            }
608    
609                            throw pe;
610                    }
611                    catch (SystemException se) {
612                            throw se;
613                    }
614                    catch (Exception e) {
615                            throw new SystemException(e);
616                    }
617            }
618    
619            @Override
620            public MissingReferences validateImportLayoutsFile(
621                            ExportImportConfiguration exportImportConfiguration,
622                            InputStream inputStream)
623                    throws PortalException {
624    
625                    File file = null;
626    
627                    try {
628                            file = FileUtil.createTempFile("lar");
629    
630                            FileUtil.write(file, inputStream);
631    
632                            return validateImportLayoutsFile(exportImportConfiguration, file);
633                    }
634                    catch (IOException ioe) {
635                            throw new SystemException(ioe);
636                    }
637                    finally {
638                            FileUtil.delete(file);
639                    }
640            }
641    
642            @Override
643            public MissingReferences validateImportPortletInfo(
644                            ExportImportConfiguration exportImportConfiguration, File file)
645                    throws PortalException {
646    
647                    try {
648                            PortletImporter portletImporter = PortletImporter.getInstance();
649    
650                            Map<String, Serializable> settingsMap =
651                                    exportImportConfiguration.getSettingsMap();
652    
653                            long userId = MapUtil.getLong(settingsMap, "userId");
654                            long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
655                            long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
656                            String portletId = MapUtil.getString(settingsMap, "portletId");
657                            Map<String, String[]> parameterMap =
658                                    (Map<String, String[]>)settingsMap.get("parameterMap");
659    
660                            return portletImporter.validateFile(
661                                    userId, targetPlid, targetGroupId, portletId, parameterMap,
662                                    file);
663                    }
664                    catch (PortalException pe) {
665                            Throwable cause = pe.getCause();
666    
667                            if (cause instanceof LocaleException) {
668                                    throw (PortalException)cause;
669                            }
670    
671                            throw pe;
672                    }
673                    catch (SystemException se) {
674                            throw se;
675                    }
676                    catch (Exception e) {
677                            throw new SystemException(e);
678                    }
679            }
680    
681            @Override
682            public MissingReferences validateImportPortletInfo(
683                            ExportImportConfiguration exportImportConfiguration,
684                            InputStream inputStream)
685                    throws PortalException {
686    
687                    File file = null;
688    
689                    try {
690                            file = FileUtil.createTempFile("lar");
691    
692                            FileUtil.write(file, inputStream);
693    
694                            return validateImportPortletInfo(exportImportConfiguration, file);
695                    }
696                    catch (IOException ioe) {
697                            throw new SystemException(ioe);
698                    }
699                    finally {
700                            FileUtil.delete(file);
701                    }
702            }
703    
704    }