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.dynamicdatamapping.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.GetterUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.LocalizationUtil;
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.portal.service.UserLocalServiceUtil;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
032    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
033    
034    import java.util.Locale;
035    import java.util.Map;
036    
037    /**
038     * @author Mate Thurzo
039     * @author Daniel Kocsis
040     */
041    public class DDMStructureStagedModelDataHandler
042            extends BaseStagedModelDataHandler<DDMStructure> {
043    
044            public static final String[] CLASS_NAMES = {DDMStructure.class.getName()};
045    
046            @Override
047            public void deleteStagedModel(
048                            String uuid, long groupId, String className, String extraData)
049                    throws PortalException, SystemException {
050    
051                    DDMStructure ddmStructure =
052                            DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
053                                    uuid, groupId);
054    
055                    if (ddmStructure != null) {
056                            DDMStructureLocalServiceUtil.deleteStructure(ddmStructure);
057                    }
058            }
059    
060            @Override
061            public String[] getClassNames() {
062                    return CLASS_NAMES;
063            }
064    
065            @Override
066            public String getDisplayName(DDMStructure structure) {
067                    return structure.getNameCurrentValue();
068            }
069    
070            @Override
071            protected void doExportStagedModel(
072                            PortletDataContext portletDataContext, DDMStructure structure)
073                    throws Exception {
074    
075                    Element structureElement = portletDataContext.getExportDataElement(
076                            structure);
077    
078                    if (structure.getParentStructureId() !=
079                                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
080    
081                            DDMStructure parentStructure =
082                                    DDMStructureLocalServiceUtil.getStructure(
083                                            structure.getParentStructureId());
084    
085                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
086                                    portletDataContext, structure, parentStructure,
087                                    PortletDataContext.REFERENCE_TYPE_PARENT);
088                    }
089    
090                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
091                            structure.getCompanyId());
092    
093                    if (defaultUserId == structure.getUserId()) {
094                            structureElement.addAttribute("preloaded", "true");
095                    }
096    
097                    portletDataContext.addClassedModel(
098                            structureElement, ExportImportPathUtil.getModelPath(structure),
099                            structure, DDMPortletDataHandler.NAMESPACE);
100            }
101    
102            @Override
103            protected void doImportStagedModel(
104                            PortletDataContext portletDataContext, DDMStructure structure)
105                    throws Exception {
106    
107                    prepareLanguagesForImport(structure);
108    
109                    long userId = portletDataContext.getUserId(structure.getUserUuid());
110    
111                    if (structure.getParentStructureId() !=
112                                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
113    
114                            Element structureElement =
115                                    portletDataContext.getReferenceDataElement(
116                                            structure, DDMStructure.class,
117                                            structure.getParentStructureId());
118    
119                            StagedModelDataHandlerUtil.importStagedModel(
120                                    portletDataContext, structureElement);
121                    }
122    
123                    Map<Long, Long> structureIds =
124                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
125                                    DDMStructure.class);
126    
127                    long parentStructureId = MapUtil.getLong(
128                            structureIds, structure.getParentStructureId(),
129                            structure.getParentStructureId());
130    
131                    Map<String, String> structureKeys =
132                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
133                                    DDMStructure.class + ".ddmStructureKey");
134    
135                    ServiceContext serviceContext = portletDataContext.createServiceContext(
136                            structure, DDMPortletDataHandler.NAMESPACE);
137    
138                    DDMStructure importedStructure = null;
139    
140                    if (portletDataContext.isDataStrategyMirror()) {
141                            Element element =
142                                    portletDataContext.getImportDataStagedModelElement(structure);
143    
144                            boolean preloaded = GetterUtil.getBoolean(
145                                    element.attributeValue("preloaded"));
146    
147                            DDMStructure existingStructure = null;
148    
149                            if (!preloaded) {
150                                    existingStructure =
151                                            DDMStructureLocalServiceUtil.
152                                                    fetchDDMStructureByUuidAndGroupId(
153                                                            structure.getUuid(),
154                                                            portletDataContext.getScopeGroupId());
155                            }
156                            else {
157                                    existingStructure = DDMStructureLocalServiceUtil.fetchStructure(
158                                            portletDataContext.getScopeGroupId(),
159                                            structure.getClassNameId(), structure.getStructureKey());
160                            }
161    
162                            if (existingStructure == null) {
163                                    existingStructure =
164                                            DDMStructureLocalServiceUtil.
165                                                    fetchDDMStructureByUuidAndGroupId(
166                                                            structure.getUuid(),
167                                                            portletDataContext.getCompanyGroupId());
168                            }
169    
170                            if (existingStructure == null) {
171                                    serviceContext.setUuid(structure.getUuid());
172    
173                                    importedStructure = DDMStructureLocalServiceUtil.addStructure(
174                                            userId, portletDataContext.getScopeGroupId(),
175                                            parentStructureId, structure.getClassNameId(),
176                                            structure.getStructureKey(), structure.getNameMap(),
177                                            structure.getDescriptionMap(), structure.getXsd(),
178                                            structure.getStorageType(), structure.getType(),
179                                            serviceContext);
180                            }
181                            else if (portletDataContext.isCompanyStagedGroupedModel(
182                                                    existingStructure)) {
183    
184                                    structureIds.put(
185                                            structure.getStructureId(),
186                                            existingStructure.getStructureId());
187    
188                                    structureKeys.put(
189                                            structure.getStructureKey(),
190                                            existingStructure.getStructureKey());
191    
192                                    return;
193                            }
194                            else {
195                                    importedStructure =
196                                            DDMStructureLocalServiceUtil.updateStructure(
197                                                    existingStructure.getStructureId(), parentStructureId,
198                                                    structure.getNameMap(), structure.getDescriptionMap(),
199                                                    structure.getXsd(), serviceContext);
200                            }
201                    }
202                    else {
203                            importedStructure = DDMStructureLocalServiceUtil.addStructure(
204                                    userId, portletDataContext.getScopeGroupId(), parentStructureId,
205                                    structure.getClassNameId(), structure.getStructureKey(),
206                                    structure.getNameMap(), structure.getDescriptionMap(),
207                                    structure.getXsd(), structure.getStorageType(),
208                                    structure.getType(), serviceContext);
209                    }
210    
211                    portletDataContext.importClassedModel(
212                            structure, importedStructure, DDMPortletDataHandler.NAMESPACE);
213    
214                    structureKeys.put(
215                            structure.getStructureKey(), importedStructure.getStructureKey());
216            }
217    
218            protected void prepareLanguagesForImport(DDMStructure structure)
219                    throws PortalException {
220    
221                    Locale defaultLocale = LocaleUtil.fromLanguageId(
222                            structure.getDefaultLanguageId());
223    
224                    Locale[] availableLocales = LocaleUtil.fromLanguageIds(
225                            structure.getAvailableLanguageIds());
226    
227                    Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
228                            DDMStructure.class.getName(), structure.getPrimaryKey(),
229                            defaultLocale, availableLocales);
230    
231                    structure.prepareLocalizedFieldsForImport(defaultImportLocale);
232            }
233    
234    }