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.messageboards.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.trash.TrashHandler;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.messageboards.model.MBCategory;
028    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
029    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
030    
031    import java.util.Map;
032    
033    /**
034     * @author Daniel Kocsis
035     */
036    public class MBCategoryStagedModelDataHandler
037            extends BaseStagedModelDataHandler<MBCategory> {
038    
039            public static final String[] CLASS_NAMES = {MBCategory.class.getName()};
040    
041            @Override
042            public void deleteStagedModel(
043                            String uuid, long groupId, String className, String extraData)
044                    throws PortalException, SystemException {
045    
046                    MBCategory category =
047                            MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
048                                    uuid, groupId);
049    
050                    if (category != null) {
051                            MBCategoryLocalServiceUtil.deleteCategory(category);
052                    }
053            }
054    
055            @Override
056            public String[] getClassNames() {
057                    return CLASS_NAMES;
058            }
059    
060            @Override
061            public String getDisplayName(MBCategory category) {
062                    return category.getName();
063            }
064    
065            @Override
066            protected void doExportStagedModel(
067                            PortletDataContext portletDataContext, MBCategory category)
068                    throws Exception {
069    
070                    if ((category.getCategoryId() ==
071                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
072                            (category.getCategoryId() ==
073                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
074    
075                            return;
076                    }
077    
078                    if (category.getParentCategory() != null) {
079                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
080                                    portletDataContext, category, category.getParentCategory(),
081                                    PortletDataContext.REFERENCE_TYPE_PARENT);
082                    }
083    
084                    Element categoryElement = portletDataContext.getExportDataElement(
085                            category);
086    
087                    portletDataContext.addClassedModel(
088                            categoryElement, ExportImportPathUtil.getModelPath(category),
089                            category);
090            }
091    
092            @Override
093            protected void doImportStagedModel(
094                            PortletDataContext portletDataContext, MBCategory category)
095                    throws Exception {
096    
097                    long userId = portletDataContext.getUserId(category.getUserUuid());
098    
099                    Map<Long, Long> categoryIds =
100                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
101                                    MBCategory.class);
102    
103                    long parentCategoryId = MapUtil.getLong(
104                            categoryIds, category.getParentCategoryId(),
105                            category.getParentCategoryId());
106    
107                    String emailAddress = null;
108                    String inProtocol = null;
109                    String inServerName = null;
110                    int inServerPort = 0;
111                    boolean inUseSSL = false;
112                    String inUserName = null;
113                    String inPassword = null;
114                    int inReadInterval = 0;
115                    String outEmailAddress = null;
116                    boolean outCustom = false;
117                    String outServerName = null;
118                    int outServerPort = 0;
119                    boolean outUseSSL = false;
120                    String outUserName = null;
121                    String outPassword = null;
122                    boolean allowAnonymous = false;
123                    boolean mailingListActive = false;
124    
125                    // Parent category
126    
127                    if ((parentCategoryId !=
128                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
129                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID) &&
130                            (parentCategoryId == category.getParentCategoryId())) {
131    
132                            String parentCategoryPath = ExportImportPathUtil.getModelPath(
133                                    portletDataContext, MBCategory.class.getName(),
134                                    parentCategoryId);
135    
136                            MBCategory parentCategory =
137                                    (MBCategory)portletDataContext.getZipEntryAsObject(
138                                            parentCategoryPath);
139    
140                            StagedModelDataHandlerUtil.importReferenceStagedModel(
141                                    portletDataContext, parentCategory);
142    
143                            parentCategoryId = MapUtil.getLong(
144                                    categoryIds, category.getParentCategoryId(),
145                                    category.getParentCategoryId());
146                    }
147    
148                    ServiceContext serviceContext = portletDataContext.createServiceContext(
149                            category);
150    
151                    MBCategory importedCategory = null;
152    
153                    if (portletDataContext.isDataStrategyMirror()) {
154                            MBCategory existingCategory =
155                                    MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
156                                            category.getUuid(), portletDataContext.getScopeGroupId());
157    
158                            if (existingCategory == null) {
159                                    serviceContext.setUuid(category.getUuid());
160    
161                                    importedCategory = MBCategoryLocalServiceUtil.addCategory(
162                                            userId, parentCategoryId, category.getName(),
163                                            category.getDescription(), category.getDisplayStyle(),
164                                            emailAddress, inProtocol, inServerName, inServerPort,
165                                            inUseSSL, inUserName, inPassword, inReadInterval,
166                                            outEmailAddress, outCustom, outServerName, outServerPort,
167                                            outUseSSL, outUserName, outPassword, allowAnonymous,
168                                            mailingListActive, serviceContext);
169                            }
170                            else {
171                                    importedCategory = MBCategoryLocalServiceUtil.updateCategory(
172                                            existingCategory.getCategoryId(), parentCategoryId,
173                                            category.getName(), category.getDescription(),
174                                            category.getDisplayStyle(), emailAddress, inProtocol,
175                                            inServerName, inServerPort, inUseSSL, inUserName,
176                                            inPassword, inReadInterval, outEmailAddress, outCustom,
177                                            outServerName, outServerPort, outUseSSL, outUserName,
178                                            outPassword, allowAnonymous, mailingListActive, false,
179                                            serviceContext);
180                            }
181                    }
182                    else {
183                            importedCategory = MBCategoryLocalServiceUtil.addCategory(
184                                    userId, parentCategoryId, category.getName(),
185                                    category.getDescription(), category.getDisplayStyle(),
186                                    emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
187                                    inUserName, inPassword, inReadInterval, outEmailAddress,
188                                    outCustom, outServerName, outServerPort, outUseSSL, outUserName,
189                                    outPassword, allowAnonymous, mailingListActive, serviceContext);
190                    }
191    
192                    portletDataContext.importClassedModel(category, importedCategory);
193            }
194    
195            @Override
196            protected void doRestoreStagedModel(
197                            PortletDataContext portletDataContext, MBCategory category)
198                    throws Exception {
199    
200                    long userId = portletDataContext.getUserId(category.getUserUuid());
201    
202                    MBCategory existingCategory =
203                            MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
204                                    category.getUuid(), portletDataContext.getScopeGroupId());
205    
206                    if ((existingCategory == null) || !existingCategory.isInTrash()) {
207                            return;
208                    }
209    
210                    TrashHandler trashHandler = existingCategory.getTrashHandler();
211    
212                    if (trashHandler.isRestorable(existingCategory.getCategoryId())) {
213                            trashHandler.restoreTrashEntry(
214                                    userId, existingCategory.getCategoryId());
215                    }
216            }
217    
218    }