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.usersadmin.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.model.Organization;
024    import com.liferay.portal.model.OrganizationConstants;
025    import com.liferay.portal.service.OrganizationLocalServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    
028    import java.util.LinkedList;
029    import java.util.Map;
030    import java.util.Queue;
031    
032    /**
033     * @author David Mendez Gonzalez
034     */
035    public class OrganizationStagedModelDataHandler
036            extends BaseStagedModelDataHandler<Organization> {
037    
038            public static final String[] CLASS_NAMES = {Organization.class.getName()};
039    
040            @Override
041            public String[] getClassNames() {
042                    return CLASS_NAMES;
043            }
044    
045            @Override
046            protected void doExportStagedModel(
047                            PortletDataContext portletDataContext, Organization organization)
048                    throws Exception {
049    
050                    Queue<Organization> organizations = new LinkedList<Organization>();
051    
052                    organizations.add(organization);
053    
054                    while (!organizations.isEmpty()) {
055                            Organization exportedOrganization = organizations.remove();
056    
057                            Element organizationElement =
058                                    portletDataContext.getExportDataStagedModelElement(
059                                            exportedOrganization);
060    
061                            portletDataContext.addClassedModel(
062                                    organizationElement,
063                                    ExportImportPathUtil.getModelPath(exportedOrganization),
064                                    exportedOrganization, UsersAdminPortletDataHandler.NAMESPACE);
065    
066                            organizations.addAll(exportedOrganization.getSuborganizations());
067                    }
068            }
069    
070            @Override
071            protected void doImportStagedModel(
072                            PortletDataContext portletDataContext, Organization organization)
073                    throws Exception {
074    
075                    long userId = portletDataContext.getUserId(organization.getUserUuid());
076    
077                    Map<Long, Long> organizationIds =
078                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
079                                    Organization.class);
080    
081                    long parentOrganizationId = MapUtil.getLong(
082                            organizationIds, organization.getParentOrganizationId(),
083                            organization.getParentOrganizationId());
084    
085                    if ((parentOrganizationId !=
086                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) &&
087                            (parentOrganizationId == organization.getParentOrganizationId())) {
088    
089                            String parentOrganizationPath = ExportImportPathUtil.getModelPath(
090                                    portletDataContext, Organization.class.getName(),
091                                    parentOrganizationId);
092    
093                            Organization parentOrganization =
094                                    (Organization)portletDataContext.getZipEntryAsObject(
095                                            parentOrganizationPath);
096    
097                            StagedModelDataHandlerUtil.importStagedModel(
098                                    portletDataContext, parentOrganization);
099    
100                            parentOrganizationId = MapUtil.getLong(
101                                    organizationIds, organization.getParentOrganizationId(),
102                                    organization.getParentOrganizationId());
103                    }
104    
105                    ServiceContext serviceContext = portletDataContext.createServiceContext(
106                            organization, UsersAdminPortletDataHandler.NAMESPACE);
107    
108                    serviceContext.setUserId(userId);
109    
110                    Organization existingOrganization = OrganizationLocalServiceUtil
111                            .fetchOrganizationByUuidAndCompanyId(
112                                    organization.getUuid(), portletDataContext.getCompanyId());
113    
114                    if (existingOrganization == null) {
115                            existingOrganization =
116                                    OrganizationLocalServiceUtil.fetchOrganization(
117                                            portletDataContext.getCompanyId(), organization.getName());
118                    }
119    
120                    Organization importedOrganization = null;
121    
122                    if (existingOrganization == null) {
123                            serviceContext.setUuid(organization.getUuid());
124    
125                            importedOrganization =
126                                    OrganizationLocalServiceUtil.addOrganization(
127                                            userId, parentOrganizationId, organization.getName(),
128                                            organization.getType(), organization.getRegionId(),
129                                            organization.getCountryId(), organization.getStatusId(),
130                                            organization.getComments(), false, serviceContext);
131                    }
132                    else {
133                            importedOrganization =
134                                    OrganizationLocalServiceUtil.updateOrganization(
135                                            portletDataContext.getCompanyId(),
136                                            existingOrganization.getOrganizationId(),
137                                            parentOrganizationId, organization.getName(),
138                                            organization.getType(), organization.getRegionId(),
139                                            organization.getCountryId(), organization.getStatusId(),
140                                            organization.getComments(), false, serviceContext);
141                    }
142    
143                    portletDataContext.importClassedModel(
144                            organization, importedOrganization,
145                            UsersAdminPortletDataHandler.NAMESPACE);
146            }
147    
148    }