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.asset.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.Property;
019    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.adapter.ModelAdapterUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.asset.model.AssetTag;
026    import com.liferay.portlet.asset.model.adapter.StagedAssetTag;
027    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
028    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
029    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
030    import com.liferay.portlet.exportimport.lar.PortletDataContext;
031    
032    import java.util.List;
033    
034    /**
035     * @author Daniel Kocsis
036     */
037    public class StagedAssetTagStagedModelDataHandler
038            extends BaseStagedModelDataHandler<StagedAssetTag> {
039    
040            public static final String[] CLASS_NAMES = {StagedAssetTag.class.getName()};
041    
042            @Override
043            public void deleteStagedModel(StagedAssetTag stagedAssetTag)
044                    throws PortalException {
045    
046                    AssetTagLocalServiceUtil.deleteTag(stagedAssetTag);
047            }
048    
049            @Override
050            public void deleteStagedModel(
051                            String uuid, long groupId, String className, String extraData)
052                    throws PortalException {
053    
054                    StagedAssetTag stagedAssetTag = fetchStagedModelByUuidAndGroupId(
055                            uuid, groupId);
056    
057                    if (stagedAssetTag != null) {
058                            deleteStagedModel(stagedAssetTag);
059                    }
060            }
061    
062            @Override
063            public StagedAssetTag fetchStagedModelByUuidAndGroupId(
064                    String uuid, long groupId) {
065    
066                    AssetTag assetTag = AssetTagLocalServiceUtil.fetchTag(groupId, uuid);
067    
068                    if (assetTag == null) {
069                            return null;
070                    }
071    
072                    return ModelAdapterUtil.adapt(
073                            assetTag, AssetTag.class, StagedAssetTag.class);
074            }
075    
076            @Override
077            public List<StagedAssetTag> fetchStagedModelsByUuidAndCompanyId(
078                    String uuid, long companyId) {
079    
080                    DynamicQuery dynamicQuery = AssetTagLocalServiceUtil.dynamicQuery();
081    
082                    Property companyIdProperty = PropertyFactoryUtil.forName("companyId");
083    
084                    dynamicQuery.add(companyIdProperty.eq(companyId));
085    
086                    Property nameProperty = PropertyFactoryUtil.forName("name");
087    
088                    dynamicQuery.add(nameProperty.eq(uuid));
089    
090                    List<AssetTag> assetTags = AssetTagLocalServiceUtil.dynamicQuery(
091                            dynamicQuery);
092    
093                    if (ListUtil.isEmpty(assetTags)) {
094                            return null;
095                    }
096    
097                    return ModelAdapterUtil.adapt(
098                            assetTags, AssetTag.class, StagedAssetTag.class);
099            }
100    
101            @Override
102            public String[] getClassNames() {
103                    return CLASS_NAMES;
104            }
105    
106            @Override
107            public String getDisplayName(StagedAssetTag stagedAssetTag) {
108                    return stagedAssetTag.getName();
109            }
110    
111            protected ServiceContext createServiceContext(
112                    PortletDataContext portletDataContext, StagedAssetTag stagedAssetTag) {
113    
114                    ServiceContext serviceContext = new ServiceContext();
115    
116                    serviceContext.setAddGroupPermissions(true);
117                    serviceContext.setAddGuestPermissions(true);
118                    serviceContext.setCreateDate(stagedAssetTag.getCreateDate());
119                    serviceContext.setModifiedDate(stagedAssetTag.getModifiedDate());
120                    serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());
121    
122                    return serviceContext;
123            }
124    
125            @Override
126            protected void doExportStagedModel(
127                            PortletDataContext portletDataContext,
128                            StagedAssetTag stagedAssetTag)
129                    throws Exception {
130    
131                    Element assetTagElement = portletDataContext.getExportDataElement(
132                            stagedAssetTag);
133    
134                    portletDataContext.addClassedModel(
135                            assetTagElement, ExportImportPathUtil.getModelPath(stagedAssetTag),
136                            stagedAssetTag);
137            }
138    
139            @Override
140            protected void doImportStagedModel(
141                            PortletDataContext portletDataContext,
142                            StagedAssetTag stagedAssetTag)
143                    throws Exception {
144    
145                    long userId = portletDataContext.getUserId(
146                            stagedAssetTag.getUserUuid());
147    
148                    ServiceContext serviceContext = createServiceContext(
149                            portletDataContext, stagedAssetTag);
150    
151                    AssetTag existingAssetTag = fetchStagedModelByUuidAndGroupId(
152                            stagedAssetTag.getName(), portletDataContext.getScopeGroupId());
153    
154                    AssetTag importedAssetTag = null;
155    
156                    if (existingAssetTag == null) {
157                            importedAssetTag = AssetTagLocalServiceUtil.addTag(
158                                    userId, portletDataContext.getScopeGroupId(),
159                                    stagedAssetTag.getName(), serviceContext);
160                    }
161                    else {
162                            importedAssetTag = AssetTagLocalServiceUtil.updateTag(
163                                    userId, existingAssetTag.getTagId(), stagedAssetTag.getName(),
164                                    serviceContext);
165                    }
166    
167                    portletDataContext.importClassedModel(stagedAssetTag, importedAssetTag);
168            }
169    
170    }