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.documentlibrary.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.MapUtil;
026    import com.liferay.portal.kernel.util.StringUtil;
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.documentlibrary.model.DLFileEntryType;
031    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.util.DLUtil;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
035    
036    import java.util.HashMap;
037    import java.util.List;
038    import java.util.Map;
039    
040    /**
041     * @author Mate Thurzo
042     */
043    public class DLFileEntryTypeStagedModelDataHandler
044            extends BaseStagedModelDataHandler<DLFileEntryType> {
045    
046            public static final String[] CLASS_NAMES =
047                    {DLFileEntryType.class.getName()};
048    
049            @Override
050            public void deleteStagedModel(
051                            String uuid, long groupId, String className, String extraData)
052                    throws PortalException, SystemException {
053    
054                    DLFileEntryType dlFileEntryType =
055                            DLFileEntryTypeLocalServiceUtil.
056                                    fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
057    
058                    if (dlFileEntryType != null) {
059                            DLFileEntryTypeLocalServiceUtil.deleteFileEntryType(
060                                    dlFileEntryType);
061                    }
062            }
063    
064            @Override
065            public String[] getClassNames() {
066                    return CLASS_NAMES;
067            }
068    
069            @Override
070            public Map<String, String> getReferenceAttributes(
071                    PortletDataContext portletDataContext, DLFileEntryType fileEntryType) {
072    
073                    Map<String, String> referenceAttributes = new HashMap<String, String>();
074    
075                    referenceAttributes.put(
076                            "file-entry-type-key", fileEntryType.getFileEntryTypeKey());
077    
078                    long defaultUserId = 0;
079    
080                    try {
081                            defaultUserId = UserLocalServiceUtil.getDefaultUserId(
082                                    fileEntryType.getCompanyId());
083                    }
084                    catch (Exception e) {
085                            return referenceAttributes;
086                    }
087    
088                    boolean preloaded = false;
089    
090                    if (defaultUserId == fileEntryType.getUserId()) {
091                            preloaded = true;
092                    }
093    
094                    referenceAttributes.put("preloaded", String.valueOf(preloaded));
095    
096                    return referenceAttributes;
097            }
098    
099            @Override
100            public void importCompanyStagedModel(
101                            PortletDataContext portletDataContext, Element element)
102                    throws PortletDataException {
103    
104                    String uuid = element.attributeValue("uuid");
105                    String fileEntryTypeKey = element.attributeValue("file-entry-type-key");
106                    boolean preloaded = GetterUtil.getBoolean(
107                            element.attributeValue("preloaded"));
108    
109                    DLFileEntryType existingFileEntryType = null;
110    
111                    try {
112                            existingFileEntryType = getExistingFileEntryType(
113                                    uuid, portletDataContext.getCompanyGroupId(), fileEntryTypeKey,
114                                    preloaded);
115                    }
116                    catch (Exception e) {
117                            throw new PortletDataException(e);
118                    }
119    
120                    Map<Long, Long> fileEntryTypeIds =
121                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
122                                    DLFileEntryType.class);
123    
124                    long fileEntryTypeId = GetterUtil.getLong(
125                            element.attributeValue("class-pk"));
126    
127                    fileEntryTypeIds.put(
128                            fileEntryTypeId, existingFileEntryType.getFileEntryTypeId());
129            }
130    
131            @Override
132            public boolean validateReference(
133                    PortletDataContext portletDataContext, Element referenceElement) {
134    
135                    String uuid = referenceElement.attributeValue("uuid");
136                    String fileEntryTypeKey = referenceElement.attributeValue(
137                            "file-entry-type-key");
138                    boolean preloaded = GetterUtil.getBoolean(
139                            referenceElement.attributeValue("preloaded"));
140    
141                    try {
142                            DLFileEntryType existingFileEntryType =
143                                    getExistingFileEntryType(
144                                            uuid, portletDataContext.getScopeGroupId(),
145                                            fileEntryTypeKey, preloaded);
146    
147                            if (existingFileEntryType == null) {
148                                    existingFileEntryType = getExistingFileEntryType(
149                                            uuid, portletDataContext.getCompanyGroupId(),
150                                            fileEntryTypeKey, preloaded);
151                            }
152    
153                            if (existingFileEntryType == null) {
154                                    return false;
155                            }
156    
157                            return true;
158                    }
159                    catch (Exception e) {
160                            return false;
161                    }
162            }
163    
164            @Override
165            protected void doExportStagedModel(
166                            PortletDataContext portletDataContext,
167                            DLFileEntryType fileEntryType)
168                    throws Exception {
169    
170                    Element fileEntryTypeElement = portletDataContext.getExportDataElement(
171                            fileEntryType);
172    
173                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
174    
175                    for (DDMStructure ddmStructure : ddmStructures) {
176                            Element referenceElement =
177                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
178                                            portletDataContext, fileEntryType, ddmStructure,
179                                            PortletDataContext.REFERENCE_TYPE_STRONG);
180    
181                            referenceElement.addAttribute(
182                                    "structure-id",
183                                    StringUtil.valueOf(ddmStructure.getStructureId()));
184                    }
185    
186                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
187                            fileEntryType.getCompanyId());
188    
189                    if (defaultUserId == fileEntryType.getUserId()) {
190                            fileEntryTypeElement.addAttribute("preloaded", "true");
191                    }
192    
193                    portletDataContext.addClassedModel(
194                            fileEntryTypeElement,
195                            ExportImportPathUtil.getModelPath(fileEntryType), fileEntryType);
196            }
197    
198            @Override
199            protected void doImportStagedModel(
200                            PortletDataContext portletDataContext,
201                            DLFileEntryType fileEntryType)
202                    throws Exception {
203    
204                    long userId = portletDataContext.getUserId(fileEntryType.getUserUuid());
205    
206                    StagedModelDataHandlerUtil.importReferenceStagedModels(
207                            portletDataContext, fileEntryType, DDMStructure.class);
208    
209                    List<Element> ddmStructureReferenceElements =
210                            portletDataContext.getReferenceElements(
211                                    fileEntryType, DDMStructure.class);
212    
213                    long[] ddmStructureIdsArray =
214                            new long[ddmStructureReferenceElements.size()];
215    
216                    Map<Long, Long> ddmStructureIds =
217                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
218                                    DDMStructure.class);
219    
220                    for (int i = 0; i < ddmStructureReferenceElements.size(); i++) {
221                            Element ddmStructureReferenceElement =
222                                    ddmStructureReferenceElements.get(i);
223    
224                            long ddmStructureId = GetterUtil.getLong(
225                                    ddmStructureReferenceElement.attributeValue("class-pk"));
226    
227                            ddmStructureIdsArray[i] = MapUtil.getLong(
228                                    ddmStructureIds, ddmStructureId);
229                    }
230    
231                    ServiceContext serviceContext = portletDataContext.createServiceContext(
232                            fileEntryType);
233    
234                    DLFileEntryType importedDLFileEntryType = null;
235    
236                    Element element = portletDataContext.getImportDataStagedModelElement(
237                            fileEntryType);
238    
239                    boolean preloaded = GetterUtil.getBoolean(
240                            element.attributeValue("preloaded"));
241    
242                    if (portletDataContext.isDataStrategyMirror()) {
243                            DLFileEntryType existingDLFileEntryType =
244                                    getExistingFileEntryType(
245                                            fileEntryType.getUuid(),
246                                            portletDataContext.getScopeGroupId(),
247                                            fileEntryType.getFileEntryTypeKey(), preloaded);
248    
249                            if (existingDLFileEntryType == null) {
250                                    serviceContext.setUuid(fileEntryType.getUuid());
251    
252                                    importedDLFileEntryType =
253                                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
254                                                    userId, portletDataContext.getScopeGroupId(),
255                                                    fileEntryType.getFileEntryTypeKey(),
256                                                    fileEntryType.getNameMap(),
257                                                    fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
258                                                    serviceContext);
259                            }
260                            else {
261                                    DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
262                                            userId, existingDLFileEntryType.getFileEntryTypeId(),
263                                            fileEntryType.getNameMap(),
264                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
265                                            serviceContext);
266    
267                                    importedDLFileEntryType =
268                                            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
269                                                    existingDLFileEntryType.getFileEntryTypeId());
270                            }
271                    }
272                    else {
273                            importedDLFileEntryType =
274                                    DLFileEntryTypeLocalServiceUtil.addFileEntryType(
275                                            userId, portletDataContext.getScopeGroupId(),
276                                            fileEntryType.getFileEntryTypeKey(),
277                                            fileEntryType.getNameMap(),
278                                            fileEntryType.getDescriptionMap(), ddmStructureIdsArray,
279                                            serviceContext);
280                    }
281    
282                    portletDataContext.importClassedModel(
283                            fileEntryType, importedDLFileEntryType);
284    
285                    if (preloaded) {
286                            return;
287                    }
288    
289                    String importedDLFileEntryDDMStructureKey = DLUtil.getDDMStructureKey(
290                            importedDLFileEntryType);
291    
292                    List<DDMStructure> importedDDMStructures =
293                            importedDLFileEntryType.getDDMStructures();
294    
295                    for (DDMStructure importedDDMStructure : importedDDMStructures) {
296                            String ddmStructureKey = importedDDMStructure.getStructureKey();
297    
298                            if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(
299                                            ddmStructureKey)) {
300    
301                                    continue;
302                            }
303    
304                            if (ddmStructureKey.equals(importedDLFileEntryDDMStructureKey)) {
305                                    continue;
306                            }
307    
308                            importedDDMStructure.setStructureKey(
309                                    importedDLFileEntryDDMStructureKey);
310    
311                            DDMStructureLocalServiceUtil.updateDDMStructure(
312                                    importedDDMStructure);
313                    }
314            }
315    
316            protected DLFileEntryType getExistingFileEntryType(
317                            String uuid, long groupId, String fileEntryTypeKey,
318                            boolean preloaded)
319                    throws Exception {
320    
321                    DLFileEntryType existingDLFileEntryType = null;
322    
323                    if (!preloaded) {
324                            existingDLFileEntryType =
325                                    DLFileEntryTypeLocalServiceUtil.
326                                            fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
327                    }
328                    else {
329                            existingDLFileEntryType =
330                                    DLFileEntryTypeLocalServiceUtil.fetchFileEntryType(
331                                            groupId, fileEntryTypeKey);
332                    }
333    
334                    return existingDLFileEntryType;
335            }
336    
337    }