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.PortletDataException;
023    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.LocalizationUtil;
027    import com.liferay.portal.kernel.util.MapUtil;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
034    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
035    
036    import java.util.HashMap;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Mate Thurzo
042     * @author Daniel Kocsis
043     */
044    public class DDMStructureStagedModelDataHandler
045            extends BaseStagedModelDataHandler<DDMStructure> {
046    
047            public static final String[] CLASS_NAMES = {DDMStructure.class.getName()};
048    
049            @Override
050            public void deleteStagedModel(
051                            String uuid, long groupId, String className, String extraData)
052                    throws PortalException, SystemException {
053    
054                    DDMStructure ddmStructure =
055                            DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
056                                    uuid, groupId);
057    
058                    if (ddmStructure != null) {
059                            DDMStructureLocalServiceUtil.deleteStructure(ddmStructure);
060                    }
061            }
062    
063            @Override
064            public String[] getClassNames() {
065                    return CLASS_NAMES;
066            }
067    
068            @Override
069            public String getDisplayName(DDMStructure structure) {
070                    return structure.getNameCurrentValue();
071            }
072    
073            @Override
074            public Map<String, String> getReferenceAttributes(
075                    PortletDataContext portletDataContext, DDMStructure structure) {
076    
077                    Map<String, String> referenceAttributes = new HashMap<String, String>();
078    
079                    referenceAttributes.put(
080                            "referenced-class-name", structure.getClassName());
081                    referenceAttributes.put("structure-key", structure.getStructureKey());
082    
083                    long defaultUserId = 0;
084    
085                    try {
086                            defaultUserId = UserLocalServiceUtil.getDefaultUserId(
087                                    structure.getCompanyId());
088                    }
089                    catch (Exception e) {
090                            return referenceAttributes;
091                    }
092    
093                    boolean preloaded = false;
094    
095                    if (defaultUserId == structure.getUserId()) {
096                            preloaded = true;
097                    }
098    
099                    referenceAttributes.put("preloaded", String.valueOf(preloaded));
100    
101                    return referenceAttributes;
102            }
103    
104            @Override
105            public void importCompanyStagedModel(
106                            PortletDataContext portletDataContext, Element element)
107                    throws PortletDataException {
108    
109                    String uuid = element.attributeValue("uuid");
110                    long classNameId = PortalUtil.getClassNameId(
111                            element.attributeValue("referenced-class-name"));
112                    String structureKey = element.attributeValue("structure-key");
113                    boolean preloaded = GetterUtil.getBoolean(
114                            element.attributeValue("preloaded"));
115    
116                    DDMStructure existingStructure = null;
117    
118                    try {
119                            existingStructure = getExistingStructure(
120                                    uuid, portletDataContext.getCompanyGroupId(), classNameId,
121                                    structureKey, preloaded);
122                    }
123                    catch (Exception e) {
124                            throw new PortletDataException(e);
125                    }
126    
127                    Map<Long, Long> structureIds =
128                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
129                                    DDMStructure.class);
130    
131                    long structureId = GetterUtil.getLong(
132                            element.attributeValue("class-pk"));
133    
134                    structureIds.put(structureId, existingStructure.getStructureId());
135    
136                    Map<String, String> structureKeys =
137                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
138                                    DDMStructure.class + ".ddmStructureKey");
139    
140                    structureKeys.put(structureKey, existingStructure.getStructureKey());
141            }
142    
143            @Override
144            public boolean validateReference(
145                    PortletDataContext portletDataContext, Element referenceElement) {
146    
147                    String uuid = referenceElement.attributeValue("uuid");
148                    long classNameId = PortalUtil.getClassNameId(
149                            referenceElement.attributeValue("referenced-class-name"));
150                    String structureKey = referenceElement.attributeValue("structure-key");
151                    boolean preloaded = GetterUtil.getBoolean(
152                            referenceElement.attributeValue("preloaded"));
153    
154                    try {
155                            DDMStructure existingStructure = getExistingStructure(
156                                    uuid, portletDataContext.getScopeGroupId(), classNameId,
157                                    structureKey, preloaded);
158    
159                            if (existingStructure == null) {
160                                    existingStructure = getExistingStructure(
161                                            uuid, portletDataContext.getCompanyGroupId(), classNameId,
162                                            structureKey, preloaded);
163                            }
164    
165                            if (existingStructure == null) {
166                                    return false;
167                            }
168    
169                            return true;
170                    }
171                    catch (Exception e) {
172                            return false;
173                    }
174            }
175    
176            @Override
177            protected void doExportStagedModel(
178                            PortletDataContext portletDataContext, DDMStructure structure)
179                    throws Exception {
180    
181                    Element structureElement = portletDataContext.getExportDataElement(
182                            structure);
183    
184                    if (structure.getParentStructureId() !=
185                                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
186    
187                            DDMStructure parentStructure =
188                                    DDMStructureLocalServiceUtil.getStructure(
189                                            structure.getParentStructureId());
190    
191                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
192                                    portletDataContext, structure, parentStructure,
193                                    PortletDataContext.REFERENCE_TYPE_PARENT);
194                    }
195    
196                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
197                            structure.getCompanyId());
198    
199                    if (defaultUserId == structure.getUserId()) {
200                            structureElement.addAttribute("preloaded", "true");
201                    }
202    
203                    portletDataContext.addClassedModel(
204                            structureElement, ExportImportPathUtil.getModelPath(structure),
205                            structure);
206            }
207    
208            @Override
209            protected void doImportStagedModel(
210                            PortletDataContext portletDataContext, DDMStructure structure)
211                    throws Exception {
212    
213                    prepareLanguagesForImport(structure);
214    
215                    long userId = portletDataContext.getUserId(structure.getUserUuid());
216    
217                    if (structure.getParentStructureId() !=
218                                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {
219    
220                            StagedModelDataHandlerUtil.importReferenceStagedModel(
221                                    portletDataContext, structure, DDMStructure.class,
222                                    structure.getParentStructureId());
223                    }
224    
225                    Map<Long, Long> structureIds =
226                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
227                                    DDMStructure.class);
228    
229                    long parentStructureId = MapUtil.getLong(
230                            structureIds, structure.getParentStructureId(),
231                            structure.getParentStructureId());
232    
233                    Map<String, String> structureKeys =
234                            (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
235                                    DDMStructure.class + ".ddmStructureKey");
236    
237                    ServiceContext serviceContext = portletDataContext.createServiceContext(
238                            structure);
239    
240                    DDMStructure importedStructure = null;
241    
242                    if (portletDataContext.isDataStrategyMirror()) {
243                            Element element =
244                                    portletDataContext.getImportDataStagedModelElement(structure);
245    
246                            boolean preloaded = GetterUtil.getBoolean(
247                                    element.attributeValue("preloaded"));
248    
249                            DDMStructure existingStructure = getExistingStructure(
250                                    structure.getUuid(), portletDataContext.getScopeGroupId(),
251                                    structure.getClassNameId(), structure.getStructureKey(),
252                                    preloaded);
253    
254                            if (existingStructure == null) {
255                                    serviceContext.setUuid(structure.getUuid());
256    
257                                    importedStructure = DDMStructureLocalServiceUtil.addStructure(
258                                            userId, portletDataContext.getScopeGroupId(),
259                                            parentStructureId, structure.getClassNameId(),
260                                            structure.getStructureKey(), structure.getNameMap(),
261                                            structure.getDescriptionMap(), structure.getXsd(),
262                                            structure.getStorageType(), structure.getType(),
263                                            serviceContext);
264                            }
265                            else {
266                                    importedStructure =
267                                            DDMStructureLocalServiceUtil.updateStructure(
268                                                    existingStructure.getStructureId(), parentStructureId,
269                                                    structure.getNameMap(), structure.getDescriptionMap(),
270                                                    structure.getXsd(), serviceContext);
271                            }
272                    }
273                    else {
274                            importedStructure = DDMStructureLocalServiceUtil.addStructure(
275                                    userId, portletDataContext.getScopeGroupId(), parentStructureId,
276                                    structure.getClassNameId(), structure.getStructureKey(),
277                                    structure.getNameMap(), structure.getDescriptionMap(),
278                                    structure.getXsd(), structure.getStorageType(),
279                                    structure.getType(), serviceContext);
280                    }
281    
282                    portletDataContext.importClassedModel(structure, importedStructure);
283    
284                    structureKeys.put(
285                            structure.getStructureKey(), importedStructure.getStructureKey());
286            }
287    
288            protected DDMStructure getExistingStructure(
289                            String uuid, long groupId, long classNameId, String structureKey,
290                            boolean preloaded)
291                    throws Exception {
292    
293                    DDMStructure existingStructure = null;
294    
295                    if (!preloaded) {
296                            existingStructure =
297                                    DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
298                                            uuid, groupId);
299                    }
300                    else {
301                            existingStructure = DDMStructureLocalServiceUtil.fetchStructure(
302                                    groupId, classNameId, structureKey);
303                    }
304    
305                    return existingStructure;
306            }
307    
308            protected void prepareLanguagesForImport(DDMStructure structure)
309                    throws PortalException {
310    
311                    Locale defaultLocale = LocaleUtil.fromLanguageId(
312                            structure.getDefaultLanguageId());
313    
314                    Locale[] availableLocales = LocaleUtil.fromLanguageIds(
315                            structure.getAvailableLanguageIds());
316    
317                    Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale(
318                            DDMStructure.class.getName(), structure.getPrimaryKey(),
319                            defaultLocale, availableLocales);
320    
321                    structure.prepareLocalizedFieldsForImport(defaultImportLocale);
322            }
323    
324    }