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.StagedModel;
025    import com.liferay.portal.model.TypedModel;
026    import com.liferay.portal.util.PortalUtil;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class StagedModelDataHandlerUtil {
032    
033            public static void deleteStagedModel(
034                            PortletDataContext portletDataContext, Element deletionElement)
035                    throws PortalException, SystemException {
036    
037                    String className = deletionElement.attributeValue("class-name");
038                    String extraData = deletionElement.attributeValue("extra-data");
039                    String uuid = deletionElement.attributeValue("uuid");
040    
041                    StagedModelDataHandler<?> stagedModelDataHandler =
042                            StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
043                                    className);
044    
045                    if (stagedModelDataHandler != null) {
046                            stagedModelDataHandler.deleteStagedModel(
047                                    uuid, portletDataContext.getScopeGroupId(), className,
048                                    extraData);
049                    }
050            }
051    
052            public static <T extends StagedModel> void exportStagedModel(
053                            PortletDataContext portletDataContext, T stagedModel)
054                    throws PortletDataException {
055    
056                    StagedModelDataHandler<T> stagedModelDataHandler =
057                            _getStagedModelDataHandler(stagedModel);
058    
059                    stagedModelDataHandler.exportStagedModel(
060                            portletDataContext, stagedModel);
061            }
062    
063            public static <T extends StagedModel> String getDisplayName(T stagedModel) {
064                    StagedModelDataHandler<T> stagedModelDataHandler =
065                            _getStagedModelDataHandler(stagedModel);
066    
067                    if (stagedModelDataHandler == null) {
068                            return StringPool.BLANK;
069                    }
070    
071                    return stagedModelDataHandler.getDisplayName(stagedModel);
072            }
073    
074            public static void importStagedModel(
075                            PortletDataContext portletDataContext, Element element)
076                    throws PortletDataException {
077    
078                    String path = element.attributeValue("path");
079    
080                    StagedModel stagedModel =
081                            (StagedModel)portletDataContext.getZipEntryAsObject(element, path);
082    
083                    Attribute classNameAttribute = element.attribute("class-name");
084    
085                    if ((classNameAttribute != null) &&
086                            (stagedModel instanceof TypedModel)) {
087    
088                            String className = classNameAttribute.getValue();
089    
090                            if (Validator.isNotNull(className)) {
091                                    long classNameId = PortalUtil.getClassNameId(className);
092    
093                                    TypedModel typedModel = (TypedModel)stagedModel;
094    
095                                    typedModel.setClassNameId(classNameId);
096                            }
097                    }
098    
099                    importStagedModel(portletDataContext, stagedModel);
100            }
101    
102            public static <T extends StagedModel> void importStagedModel(
103                            PortletDataContext portletDataContext, T stagedModel)
104                    throws PortletDataException {
105    
106                    StagedModelDataHandler<T> stagedModelDataHandler =
107                            _getStagedModelDataHandler(stagedModel);
108    
109                    stagedModelDataHandler.importStagedModel(
110                            portletDataContext, stagedModel);
111            }
112    
113            private static <T extends StagedModel> StagedModelDataHandler<T>
114                    _getStagedModelDataHandler(T stagedModel) {
115    
116                    ClassedModel classedModel = stagedModel;
117    
118                    StagedModelDataHandler<T> stagedModelDataHandler =
119                            (StagedModelDataHandler<T>)
120                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
121                                            classedModel.getModelClassName());
122    
123                    return stagedModelDataHandler;
124            }
125    
126    }