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