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