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.exportStagedModel(
102                                    portletDataContext, repositoryEntry);
103    
104                            portletDataContext.addReferenceElement(
105                                    repository, repositoryElement, repositoryEntry,
106                                    PortletDataContext.REFERENCE_TYPE_CHILD, false);
107                    }
108            }
109    
110            @Override
111            protected void doImportStagedModel(
112                            PortletDataContext portletDataContext, Repository repository)
113                    throws Exception {
114    
115                    long userId = portletDataContext.getUserId(repository.getUserUuid());
116    
117                    ServiceContext serviceContext = portletDataContext.createServiceContext(
118                            repository, DLPortletDataHandler.NAMESPACE);
119    
120                    Repository importedRepository = null;
121    
122                    Element repositoryElement =
123                            portletDataContext.getImportDataStagedModelElement(repository);
124    
125                    try {
126                            boolean hidden = GetterUtil.getBoolean(
127                                    repositoryElement.attributeValue("hidden"));
128    
129                            if (portletDataContext.isDataStrategyMirror()) {
130                                    Repository existingRepository =
131                                            RepositoryLocalServiceUtil.fetchRepositoryByUuidAndGroupId(
132                                                    repository.getUuid(),
133                                                    portletDataContext.getScopeGroupId());
134    
135                                    if (existingRepository == null) {
136                                            existingRepository =
137                                                    RepositoryLocalServiceUtil.fetchRepository(
138                                                            portletDataContext.getScopeGroupId(),
139                                                            repository.getName());
140                                    }
141    
142                                    long classNameId = 0;
143    
144                                    if (existingRepository != null) {
145                                            classNameId = existingRepository.getClassNameId();
146                                    }
147    
148                                    if ((existingRepository == null) ||
149                                            (classNameId !=
150                                                    PortalUtil.getClassNameId(LiferayRepository.class))) {
151    
152                                            serviceContext.setUuid(repository.getUuid());
153    
154                                            importedRepository =
155                                                    RepositoryLocalServiceUtil.addRepository(
156                                                            userId, portletDataContext.getScopeGroupId(),
157                                                            repository.getClassNameId(),
158                                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
159                                                            repository.getName(), repository.getDescription(),
160                                                            repository.getPortletId(),
161                                                            repository.getTypeSettingsProperties(), hidden,
162                                                            serviceContext);
163                                    }
164                                    else {
165                                            RepositoryLocalServiceUtil.updateRepository(
166                                                    existingRepository.getRepositoryId(),
167                                                    repository.getName(), repository.getDescription());
168    
169                                            importedRepository = existingRepository;
170                                    }
171                            }
172                            else {
173                                    importedRepository = RepositoryLocalServiceUtil.addRepository(
174                                            userId, portletDataContext.getScopeGroupId(),
175                                            repository.getClassNameId(),
176                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
177                                            repository.getName(), repository.getDescription(),
178                                            repository.getPortletId(),
179                                            repository.getTypeSettingsProperties(), hidden,
180                                            serviceContext);
181                            }
182                    }
183                    catch (Exception e) {
184                            if (_log.isWarnEnabled()) {
185                                    _log.warn(
186                                            "Unable to connect to repository {name=" +
187                                                    repository.getName() + ", typeSettings=" +
188                                                            repository.getTypeSettingsProperties() + "}",
189                                            e);
190                            }
191                    }
192    
193                    portletDataContext.importClassedModel(
194                            repository, importedRepository, DLPortletDataHandler.NAMESPACE);
195    
196                    List<Element> repositoryEntryElements =
197                            portletDataContext.getReferenceDataElements(
198                                    repository, RepositoryEntry.class);
199    
200                    for (Element repositoryEntryElement : repositoryEntryElements) {
201                            StagedModelDataHandlerUtil.importStagedModel(
202                                    portletDataContext, repositoryEntryElement);
203                    }
204            }
205    
206            private static Log _log = LogFactoryUtil.getLog(
207                    RepositoryStagedModelDataHandler.class);
208    
209    }