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