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