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                            StagedModelDataHandlerUtil.importReferenceStagedModel(
133                                    portletDataContext, category, MBCategory.class,
134                                    parentCategoryId);
135    
136                            parentCategoryId = MapUtil.getLong(
137                                    categoryIds, category.getParentCategoryId(),
138                                    category.getParentCategoryId());
139                    }
140    
141                    ServiceContext serviceContext = portletDataContext.createServiceContext(
142                            category);
143    
144                    MBCategory importedCategory = null;
145    
146                    if (portletDataContext.isDataStrategyMirror()) {
147                            MBCategory existingCategory =
148                                    MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
149                                            category.getUuid(), portletDataContext.getScopeGroupId());
150    
151                            if (existingCategory == null) {
152                                    serviceContext.setUuid(category.getUuid());
153    
154                                    importedCategory = MBCategoryLocalServiceUtil.addCategory(
155                                            userId, parentCategoryId, category.getName(),
156                                            category.getDescription(), category.getDisplayStyle(),
157                                            emailAddress, inProtocol, inServerName, inServerPort,
158                                            inUseSSL, inUserName, inPassword, inReadInterval,
159                                            outEmailAddress, outCustom, outServerName, outServerPort,
160                                            outUseSSL, outUserName, outPassword, allowAnonymous,
161                                            mailingListActive, serviceContext);
162                            }
163                            else {
164                                    importedCategory = MBCategoryLocalServiceUtil.updateCategory(
165                                            existingCategory.getCategoryId(), parentCategoryId,
166                                            category.getName(), category.getDescription(),
167                                            category.getDisplayStyle(), emailAddress, inProtocol,
168                                            inServerName, inServerPort, inUseSSL, inUserName,
169                                            inPassword, inReadInterval, outEmailAddress, outCustom,
170                                            outServerName, outServerPort, outUseSSL, outUserName,
171                                            outPassword, allowAnonymous, mailingListActive, false,
172                                            serviceContext);
173                            }
174                    }
175                    else {
176                            importedCategory = MBCategoryLocalServiceUtil.addCategory(
177                                    userId, parentCategoryId, category.getName(),
178                                    category.getDescription(), category.getDisplayStyle(),
179                                    emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
180                                    inUserName, inPassword, inReadInterval, outEmailAddress,
181                                    outCustom, outServerName, outServerPort, outUseSSL, outUserName,
182                                    outPassword, allowAnonymous, mailingListActive, serviceContext);
183                    }
184    
185                    portletDataContext.importClassedModel(category, importedCategory);
186            }
187    
188            @Override
189            protected void doRestoreStagedModel(
190                            PortletDataContext portletDataContext, MBCategory category)
191                    throws Exception {
192    
193                    long userId = portletDataContext.getUserId(category.getUserUuid());
194    
195                    MBCategory existingCategory =
196                            MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
197                                    category.getUuid(), portletDataContext.getScopeGroupId());
198    
199                    if ((existingCategory == null) || !existingCategory.isInTrash()) {
200                            return;
201                    }
202    
203                    TrashHandler trashHandler = existingCategory.getTrashHandler();
204    
205                    if (trashHandler.isRestorable(existingCategory.getCategoryId())) {
206                            trashHandler.restoreTrashEntry(
207                                    userId, existingCategory.getCategoryId());
208                    }
209            }
210    
211    }