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.portal.kernel.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.xml.Attribute;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.ClassedModel;
024    import com.liferay.portal.model.StagedGroupedModel;
025    import com.liferay.portal.model.StagedModel;
026    import com.liferay.portal.model.TypedModel;
027    import com.liferay.portal.util.PortalUtil;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class StagedModelDataHandlerUtil {
033    
034            public static void deleteStagedModel(
035                            PortletDataContext portletDataContext, Element deletionElement)
036                    throws PortalException, SystemException {
037    
038                    String className = deletionElement.attributeValue("class-name");
039                    String extraData = deletionElement.attributeValue("extra-data");
040                    String uuid = deletionElement.attributeValue("uuid");
041    
042                    StagedModelDataHandler<?> stagedModelDataHandler =
043                            StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
044                                    className);
045    
046                    if (stagedModelDataHandler != null) {
047                            stagedModelDataHandler.deleteStagedModel(
048                                    uuid, portletDataContext.getScopeGroupId(), className,
049                                    extraData);
050                    }
051            }
052    
053            public static <T extends StagedModel> void exportReferenceStagedModel(
054                            PortletDataContext portletDataContext, String referrerPortletId,
055                            T stagedModel)
056                    throws PortletDataException {
057    
058                    if (stagedModel instanceof StagedGroupedModel) {
059                            StagedGroupedModel stagedGroupedModel =
060                                    (StagedGroupedModel)stagedModel;
061    
062                            if (portletDataContext.isCompanyStagedGroupedModel(
063                                            stagedGroupedModel)) {
064    
065                                    portletDataContext.addMissingReferenceElement(
066                                            referrerPortletId, stagedModel);
067    
068                                    return;
069                            }
070                    }
071    
072                    exportStagedModel(portletDataContext, stagedModel);
073            }
074    
075            public static <T extends StagedModel, U extends StagedModel> Element
076                    exportReferenceStagedModel(
077                            PortletDataContext portletDataContext, T referrerStagedModel,
078                            Class<?> referrerStagedModelClass, U stagedModel,
079                            Class<?> stagedModelClass, String referenceType)
080                    throws PortletDataException {
081    
082                    Element referrerStagedModelElement =
083                            portletDataContext.getExportDataElement(
084                                    referrerStagedModel, referrerStagedModelClass);
085    
086                    return exportReferenceStagedModel(
087                            portletDataContext, referrerStagedModel, referrerStagedModelElement,
088                            stagedModel, stagedModelClass, referenceType);
089            }
090    
091            public static <T extends StagedModel, U extends StagedModel> Element
092                    exportReferenceStagedModel(
093                            PortletDataContext portletDataContext, T referrerStagedModel,
094                            Element referrerStagedModelElement, U stagedModel,
095                            Class<?> stagedModelClass, String referenceType)
096                    throws PortletDataException {
097    
098                    Element referenceElement = null;
099    
100                    if (stagedModel instanceof StagedGroupedModel) {
101                            StagedGroupedModel stagedGroupedModel =
102                                    (StagedGroupedModel)stagedModel;
103    
104                            if (portletDataContext.isCompanyStagedGroupedModel(
105                                            stagedGroupedModel)) {
106    
107                                    referenceElement = portletDataContext.addReferenceElement(
108                                            referrerStagedModel, referrerStagedModelElement,
109                                            stagedModel, stagedModelClass, referenceType, true);
110    
111                                    return referenceElement;
112                            }
113                    }
114    
115                    exportStagedModel(portletDataContext, stagedModel);
116    
117                    referenceElement = portletDataContext.addReferenceElement(
118                            referrerStagedModel, referrerStagedModelElement, stagedModel,
119                            stagedModelClass, referenceType, false);
120    
121                    return referenceElement;
122            }
123    
124            public static <T extends StagedModel, U extends StagedModel> Element
125                    exportReferenceStagedModel(
126                            PortletDataContext portletDataContext, T referrerStagedModel,
127                            U stagedModel, String referenceType)
128                    throws PortletDataException {
129    
130                    return exportReferenceStagedModel(
131                            portletDataContext, referrerStagedModel,
132                            referrerStagedModel.getModelClass(), stagedModel,
133                            stagedModel.getModelClass(), referenceType);
134            }
135    
136            public static <T extends StagedModel> void exportStagedModel(
137                            PortletDataContext portletDataContext, T stagedModel)
138                    throws PortletDataException {
139    
140                    StagedModelDataHandler<T> stagedModelDataHandler =
141                            _getStagedModelDataHandler(stagedModel);
142    
143                    stagedModelDataHandler.exportStagedModel(
144                            portletDataContext, stagedModel);
145            }
146    
147            public static <T extends StagedModel> String getDisplayName(T stagedModel) {
148                    StagedModelDataHandler<T> stagedModelDataHandler =
149                            _getStagedModelDataHandler(stagedModel);
150    
151                    if (stagedModelDataHandler == null) {
152                            return StringPool.BLANK;
153                    }
154    
155                    return stagedModelDataHandler.getDisplayName(stagedModel);
156            }
157    
158            public static void importStagedModel(
159                            PortletDataContext portletDataContext, Element element)
160                    throws PortletDataException {
161    
162                    String path = element.attributeValue("path");
163    
164                    StagedModel stagedModel =
165                            (StagedModel)portletDataContext.getZipEntryAsObject(element, path);
166    
167                    Attribute classNameAttribute = element.attribute("class-name");
168    
169                    if ((classNameAttribute != null) &&
170                            (stagedModel instanceof TypedModel)) {
171    
172                            String className = classNameAttribute.getValue();
173    
174                            if (Validator.isNotNull(className)) {
175                                    long classNameId = PortalUtil.getClassNameId(className);
176    
177                                    TypedModel typedModel = (TypedModel)stagedModel;
178    
179                                    typedModel.setClassNameId(classNameId);
180                            }
181                    }
182    
183                    importStagedModel(portletDataContext, stagedModel);
184            }
185    
186            public static <T extends StagedModel> void importStagedModel(
187                            PortletDataContext portletDataContext, T stagedModel)
188                    throws PortletDataException {
189    
190                    StagedModelDataHandler<T> stagedModelDataHandler =
191                            _getStagedModelDataHandler(stagedModel);
192    
193                    stagedModelDataHandler.importStagedModel(
194                            portletDataContext, stagedModel);
195            }
196    
197            private static <T extends StagedModel> StagedModelDataHandler<T>
198                    _getStagedModelDataHandler(T stagedModel) {
199    
200                    ClassedModel classedModel = stagedModel;
201    
202                    StagedModelDataHandler<T> stagedModelDataHandler =
203                            (StagedModelDataHandler<T>)
204                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
205                                            classedModel.getModelClassName());
206    
207                    return stagedModelDataHandler;
208            }
209    
210    }