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.trash.TrashHandler;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
024    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
025    import com.liferay.portlet.exportimport.lar.PortletDataContext;
026    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
027    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
028    import com.liferay.portlet.messageboards.model.MBCategory;
029    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
030    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
031    
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * @author Daniel Kocsis
037     */
038    public class MBCategoryStagedModelDataHandler
039            extends BaseStagedModelDataHandler<MBCategory> {
040    
041            public static final String[] CLASS_NAMES = {MBCategory.class.getName()};
042    
043            @Override
044            public void deleteStagedModel(MBCategory category) throws PortalException {
045                    MBCategoryLocalServiceUtil.deleteCategory(category);
046            }
047    
048            @Override
049            public void deleteStagedModel(
050                            String uuid, long groupId, String className, String extraData)
051                    throws PortalException {
052    
053                    MBCategory category = fetchStagedModelByUuidAndGroupId(uuid, groupId);
054    
055                    if (category != null) {
056                            deleteStagedModel(category);
057                    }
058            }
059    
060            @Override
061            public MBCategory fetchStagedModelByUuidAndGroupId(
062                    String uuid, long groupId) {
063    
064                    return MBCategoryLocalServiceUtil.fetchMBCategoryByUuidAndGroupId(
065                            uuid, groupId);
066            }
067    
068            @Override
069            public List<MBCategory> fetchStagedModelsByUuidAndCompanyId(
070                    String uuid, long companyId) {
071    
072                    return MBCategoryLocalServiceUtil.getMBCategoriesByUuidAndCompanyId(
073                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
074                            new StagedModelModifiedDateComparator<MBCategory>());
075            }
076    
077            @Override
078            public String[] getClassNames() {
079                    return CLASS_NAMES;
080            }
081    
082            @Override
083            public String getDisplayName(MBCategory category) {
084                    return category.getName();
085            }
086    
087            @Override
088            protected void doExportStagedModel(
089                            PortletDataContext portletDataContext, MBCategory category)
090                    throws Exception {
091    
092                    if ((category.getCategoryId() ==
093                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
094                            (category.getCategoryId() ==
095                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
096    
097                            return;
098                    }
099    
100                    if (category.getParentCategory() != null) {
101                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
102                                    portletDataContext, category, category.getParentCategory(),
103                                    PortletDataContext.REFERENCE_TYPE_PARENT);
104                    }
105    
106                    Element categoryElement = portletDataContext.getExportDataElement(
107                            category);
108    
109                    portletDataContext.addClassedModel(
110                            categoryElement, ExportImportPathUtil.getModelPath(category),
111                            category);
112            }
113    
114            @Override
115            protected void doImportStagedModel(
116                            PortletDataContext portletDataContext, MBCategory category)
117                    throws Exception {
118    
119                    long userId = portletDataContext.getUserId(category.getUserUuid());
120    
121                    String emailAddress = null;
122                    String inProtocol = null;
123                    String inServerName = null;
124                    int inServerPort = 0;
125                    boolean inUseSSL = false;
126                    String inUserName = null;
127                    String inPassword = null;
128                    int inReadInterval = 0;
129                    String outEmailAddress = null;
130                    boolean outCustom = false;
131                    String outServerName = null;
132                    int outServerPort = 0;
133                    boolean outUseSSL = false;
134                    String outUserName = null;
135                    String outPassword = null;
136                    boolean allowAnonymous = false;
137                    boolean mailingListActive = false;
138    
139                    // Parent category
140    
141                    Map<Long, Long> categoryIds =
142                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
143                                    MBCategory.class);
144    
145                    long parentCategoryId = MapUtil.getLong(
146                            categoryIds, category.getParentCategoryId(),
147                            category.getParentCategoryId());
148    
149                    ServiceContext serviceContext = portletDataContext.createServiceContext(
150                            category);
151    
152                    MBCategory importedCategory = null;
153    
154                    if (portletDataContext.isDataStrategyMirror()) {
155                            MBCategory existingCategory = fetchStagedModelByUuidAndGroupId(
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 = fetchStagedModelByUuidAndGroupId(
203                            category.getUuid(), portletDataContext.getScopeGroupId());
204    
205                    if ((existingCategory == null) || !existingCategory.isInTrash()) {
206                            return;
207                    }
208    
209                    TrashHandler trashHandler = existingCategory.getTrashHandler();
210    
211                    if (trashHandler.isRestorable(existingCategory.getCategoryId())) {
212                            trashHandler.restoreTrashEntry(
213                                    userId, existingCategory.getCategoryId());
214                    }
215            }
216    
217    }