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.portletdisplaytemplate.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
023    import com.liferay.portal.kernel.lar.ManifestSummary;
024    import com.liferay.portal.kernel.lar.PortletDataContext;
025    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
026    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
027    import com.liferay.portal.kernel.lar.StagedModelType;
028    import com.liferay.portal.kernel.template.TemplateHandlerRegistryUtil;
029    import com.liferay.portal.kernel.util.ArrayUtil;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
034    import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMTemplateExportActionableDynamicQuery;
035    
036    import java.util.ArrayList;
037    import java.util.List;
038    
039    import javax.portlet.PortletPreferences;
040    
041    /**
042     * @author Juan Fern??ndez
043     */
044    public class PortletDisplayTemplatePortletDataHandler
045            extends BasePortletDataHandler {
046    
047            public static final String NAMESPACE = "portlet_display_templates";
048    
049            public PortletDisplayTemplatePortletDataHandler() {
050                    setExportControls(
051                            new PortletDataHandlerBoolean(
052                                    NAMESPACE, "application-display-templates", true, true));
053            }
054    
055            @Override
056            public StagedModelType[] getDeletionSystemEventStagedModelTypes() {
057                    return getStagedModelTypes();
058            }
059    
060            @Override
061            public long getExportModelCount(ManifestSummary manifestSummary) {
062                    long totalModelCount = -1;
063    
064                    for (StagedModelType stagedModelType : getStagedModelTypes()) {
065                            long modelCount = manifestSummary.getModelAdditionCount(
066                                    stagedModelType.getClassName(),
067                                    stagedModelType.getReferrerClassName());
068    
069                            if (modelCount == -1) {
070                                    continue;
071                            }
072    
073                            if (totalModelCount == -1) {
074                                    totalModelCount = modelCount;
075                            }
076                            else {
077                                    totalModelCount += modelCount;
078                            }
079                    }
080    
081                    return totalModelCount;
082            }
083    
084            @Override
085            protected String doExportData(
086                            PortletDataContext portletDataContext, String portletId,
087                            PortletPreferences portletPreferences)
088                    throws Exception {
089    
090                    Element rootElement = addExportDataRootElement(portletDataContext);
091    
092                    long[] classNameIds = TemplateHandlerRegistryUtil.getClassNameIds();
093    
094                    ActionableDynamicQuery actionableDynamicQuery =
095                            getDDMTemplateActionableDynamicQuery(
096                                    portletDataContext, ArrayUtil.toArray(classNameIds),
097                                    new StagedModelType(
098                                            PortalUtil.getClassNameId(DDMTemplate.class),
099                                            StagedModelType.REFERRER_CLASS_NAME_ID_ALL));
100    
101                    actionableDynamicQuery.performActions();
102    
103                    return getExportDataRootElementString(rootElement);
104            }
105    
106            @Override
107            protected PortletPreferences doImportData(
108                            PortletDataContext portletDataContext, String portletId,
109                            PortletPreferences portletPreferences, String data)
110                    throws Exception {
111    
112                    Element ddmTemplatesElement =
113                            portletDataContext.getImportDataGroupElement(DDMTemplate.class);
114    
115                    List<Element> ddmTemplateElements = ddmTemplatesElement.elements();
116    
117                    for (Element ddmTemplateElement : ddmTemplateElements) {
118                            StagedModelDataHandlerUtil.importStagedModel(
119                                    portletDataContext, ddmTemplateElement);
120                    }
121    
122                    return null;
123            }
124    
125            @Override
126            protected void doPrepareManifestSummary(
127                            PortletDataContext portletDataContext,
128                            PortletPreferences portletPreferences)
129                    throws Exception {
130    
131                    for (StagedModelType stagedModelType : getStagedModelTypes()) {
132                            ActionableDynamicQuery actionableDynamicQuery =
133                                    getDDMTemplateActionableDynamicQuery(
134                                            portletDataContext,
135                                            new Long[] {stagedModelType.getReferrerClassNameId()},
136                                            stagedModelType);
137    
138                            actionableDynamicQuery.performCount();
139                    }
140            }
141    
142            protected ActionableDynamicQuery getDDMTemplateActionableDynamicQuery(
143                            final PortletDataContext portletDataContext,
144                            final Long[] classNameIds, final StagedModelType stagedModelType)
145                    throws SystemException {
146    
147                    return new DDMTemplateExportActionableDynamicQuery(
148                            portletDataContext) {
149    
150                            @Override
151                            protected void addCriteria(DynamicQuery dynamicQuery) {
152                                    super.addCriteria(dynamicQuery);
153    
154                                    Property classNameIdProperty = PropertyFactoryUtil.forName(
155                                            "classNameId");
156    
157                                    dynamicQuery.add(classNameIdProperty.in(classNameIds));
158    
159                                    Property classPKProperty = PropertyFactoryUtil.forName(
160                                            "classPK");
161    
162                                    dynamicQuery.add(classPKProperty.eq(0L));
163    
164                                    Property typeProperty = PropertyFactoryUtil.forName("type");
165    
166                                    dynamicQuery.add(
167                                            typeProperty.eq(
168                                                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY));
169                            }
170    
171                            @Override
172                            protected StagedModelType getStagedModelType() {
173                                    return stagedModelType;
174                            }
175                    };
176            }
177    
178            protected StagedModelType[] getStagedModelTypes() {
179                    if (_stagedModelTypes != null) {
180                            return _stagedModelTypes;
181                    }
182    
183                    List<StagedModelType> stagedModelTypes =
184                            new ArrayList<StagedModelType>();
185    
186                    long ddmTemplateClassNameId = PortalUtil.getClassNameId(
187                            DDMTemplate.class);
188    
189                    for (long classNameId : TemplateHandlerRegistryUtil.getClassNameIds()) {
190                            stagedModelTypes.add(
191                                    new StagedModelType(ddmTemplateClassNameId, classNameId));
192                    }
193    
194                    _stagedModelTypes = stagedModelTypes.toArray(
195                            new StagedModelType[stagedModelTypes.size()]);
196    
197                    return _stagedModelTypes;
198            }
199    
200            private StagedModelType[] _stagedModelTypes;
201    
202    }