001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.repository.model.Folder;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.xml.Element;
028    import com.liferay.portal.model.Repository;
029    import com.liferay.portal.model.RepositoryEntry;
030    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
031    import com.liferay.portal.service.RepositoryEntryLocalServiceUtil;
032    import com.liferay.portal.service.RepositoryLocalServiceUtil;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portlet.documentlibrary.model.DLFolder;
036    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
037    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
038    
039    import java.util.List;
040    
041    /**
042     * @author Mate Thurzo
043     */
044    public class RepositoryStagedModelDataHandler
045            extends BaseStagedModelDataHandler<Repository> {
046    
047            public static final String[] CLASS_NAMES = {Repository.class.getName()};
048    
049            @Override
050            public void deleteStagedModel(
051                            String uuid, long groupId, String className, String extraData)
052                    throws PortalException, SystemException {
053    
054                    Repository repository =
055                            RepositoryLocalServiceUtil.fetchRepositoryByUuidAndGroupId(
056                                    uuid, groupId);
057    
058                    if (repository != null) {
059                            RepositoryLocalServiceUtil.deleteRepository(
060                                    repository.getRepositoryId());
061                    }
062            }
063    
064            @Override
065            public String[] getClassNames() {
066                    return CLASS_NAMES;
067            }
068    
069            @Override
070            public String getDisplayName(Repository repository) {
071                    return repository.getName();
072            }
073    
074            @Override
075            protected void doExportStagedModel(
076                            PortletDataContext portletDataContext, Repository repository)
077                    throws Exception {
078    
079                    Element repositoryElement = portletDataContext.getExportDataElement(
080                            repository);
081    
082                    Folder folder = DLAppLocalServiceUtil.getFolder(
083                            repository.getDlFolderId());
084    
085                    if (folder.getModel() instanceof DLFolder) {
086                            DLFolder dlFolder = (DLFolder)folder.getModel();
087    
088                            repositoryElement.addAttribute(
089                                    "hidden", String.valueOf(dlFolder.isHidden()));
090                    }
091    
092                    portletDataContext.addClassedModel(
093                            repositoryElement, ExportImportPathUtil.getModelPath(repository),
094                            repository, DLPortletDataHandler.NAMESPACE);
095    
096                    List<RepositoryEntry> repositoryEntries =
097                            RepositoryEntryLocalServiceUtil.getRepositoryEntries(
098                                    repository.getRepositoryId());
099    
100                    for (RepositoryEntry repositoryEntry : repositoryEntries) {
101                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
102                                    portletDataContext, repository, repositoryEntry,
103                                    PortletDataContext.REFERENCE_TYPE_CHILD);
104                    }
105            }
106    
107            @Override
108            protected void doImportStagedModel(
109                            PortletDataContext portletDataContext, Repository repository)
110                    throws Exception {
111    
112                    long userId = portletDataContext.getUserId(repository.getUserUuid());
113    
114                    ServiceContext serviceContext = portletDataContext.createServiceContext(
115                            repository, DLPortletDataHandler.NAMESPACE);
116    
117                    Repository importedRepository = null;
118    
119                    Element repositoryElement =
120                            portletDataContext.getImportDataStagedModelElement(repository);
121    
122                    try {
123                            boolean hidden = GetterUtil.getBoolean(
124                                    repositoryElement.attributeValue("hidden"));
125    
126                            if (portletDataContext.isDataStrategyMirror()) {
127                                    Repository existingRepository =
128                                            RepositoryLocalServiceUtil.fetchRepositoryByUuidAndGroupId(
129                                                    repository.getUuid(),
130                                                    portletDataContext.getScopeGroupId());
131    
132                                    if (existingRepository == null) {
133                                            existingRepository =
134                                                    RepositoryLocalServiceUtil.fetchRepository(
135                                                            portletDataContext.getScopeGroupId(),
136                                                            repository.getName());
137                                    }
138    
139                                    long classNameId = 0;
140    
141                                    if (existingRepository != null) {
142                                            classNameId = existingRepository.getClassNameId();
143                                    }
144    
145                                    if ((existingRepository == null) ||
146                                            (classNameId !=
147                                                    PortalUtil.getClassNameId(LiferayRepository.class))) {
148    
149                                            serviceContext.setUuid(repository.getUuid());
150    
151                                            importedRepository =
152                                                    RepositoryLocalServiceUtil.addRepository(
153                                                            userId, portletDataContext.getScopeGroupId(),
154                                                            repository.getClassNameId(),
155                                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
156                                                            repository.getName(), repository.getDescription(),
157                                                            repository.getPortletId(),
158                                                            repository.getTypeSettingsProperties(), hidden,
159                                                            serviceContext);
160                                    }
161                                    else {
162                                            RepositoryLocalServiceUtil.updateRepository(
163                                                    existingRepository.getRepositoryId(),
164                                                    repository.getName(), repository.getDescription());
165    
166                                            importedRepository = existingRepository;
167                                    }
168                            }
169                            else {
170                                    importedRepository = RepositoryLocalServiceUtil.addRepository(
171                                            userId, portletDataContext.getScopeGroupId(),
172                                            repository.getClassNameId(),
173                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
174                                            repository.getName(), repository.getDescription(),
175                                            repository.getPortletId(),
176                                            repository.getTypeSettingsProperties(), hidden,
177                                            serviceContext);
178                            }
179                    }
180                    catch (Exception e) {
181                            if (_log.isWarnEnabled()) {
182                                    _log.warn(
183                                            "Unable to connect to repository {name=" +
184                                                    repository.getName() + ", typeSettings=" +
185                                                            repository.getTypeSettingsProperties() + "}",
186                                            e);
187                            }
188                    }
189    
190                    portletDataContext.importClassedModel(
191                            repository, importedRepository, DLPortletDataHandler.NAMESPACE);
192    
193                    List<Element> repositoryEntryElements =
194                            portletDataContext.getReferenceDataElements(
195                                    repository, RepositoryEntry.class);
196    
197                    for (Element repositoryEntryElement : repositoryEntryElements) {
198                            StagedModelDataHandlerUtil.importReferenceStagedModel(
199                                    portletDataContext, repositoryEntryElement);
200                    }
201            }
202    
203            private static Log _log = LogFactoryUtil.getLog(
204                    RepositoryStagedModelDataHandler.class);
205    
206    }