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