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.lar.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
027    import com.liferay.portlet.documentlibrary.model.DLFileRank;
028    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
031    
032    import java.util.Map;
033    
034    /**
035     * @author Mate Thurzo
036     */
037    public class DLFileRankStagedModelDataHandler
038            extends BaseStagedModelDataHandler<DLFileRank> {
039    
040            public static final String[] CLASS_NAMES = {DLFileRank.class.getName()};
041    
042            @Override
043            public String[] getClassNames() {
044                    return CLASS_NAMES;
045            }
046    
047            @Override
048            protected void doExportStagedModel(
049                            PortletDataContext portletDataContext, DLFileRank fileRank)
050                    throws Exception {
051    
052                    DLFileEntry dlFileEntry = DLFileEntryUtil.fetchByPrimaryKey(
053                            fileRank.getFileEntryId());
054    
055                    StagedModelDataHandlerUtil.exportStagedModel(
056                            portletDataContext, dlFileEntry);
057    
058                    Element fileRankElement =
059                            portletDataContext.getExportDataStagedModelElement(fileRank);
060    
061                    portletDataContext.addClassedModel(
062                            fileRankElement, ExportImportPathUtil.getModelPath(fileRank),
063                            fileRank, DLPortletDataHandler.NAMESPACE);
064            }
065    
066            @Override
067            protected void doImportStagedModel(
068                            PortletDataContext portletDataContext, DLFileRank fileRank)
069                    throws Exception {
070    
071                    long userId = portletDataContext.getUserId(fileRank.getUserUuid());
072    
073                    String fileEntryPath = ExportImportPathUtil.getModelPath(
074                            portletDataContext, DLFileEntry.class.getName(),
075                            fileRank.getFileEntryId());
076    
077                    DLFileEntry dlFileEntry =
078                            (DLFileEntry)portletDataContext.getZipEntryAsObject(fileEntryPath);
079    
080                    StagedModelDataHandlerUtil.importStagedModel(
081                            portletDataContext, dlFileEntry);
082    
083                    Map<Long, Long> fileEntryIds =
084                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
085                                    DLFileEntry.class);
086    
087                    long fileEntryId = MapUtil.getLong(
088                            fileEntryIds, fileRank.getFileEntryId(), fileRank.getFileEntryId());
089    
090                    if (DLFileEntryLocalServiceUtil.fetchDLFileEntry(fileEntryId) == null) {
091                            if (_log.isWarnEnabled()) {
092                                    _log.warn("Unable to retrieve file to import file rank");
093                            }
094    
095                            return;
096                    }
097    
098                    ServiceContext serviceContext = new ServiceContext();
099    
100                    serviceContext.setCreateDate(fileRank.getCreateDate());
101    
102                    DLAppLocalServiceUtil.updateFileRank(
103                            portletDataContext.getScopeGroupId(),
104                            portletDataContext.getCompanyId(), userId, fileEntryId,
105                            serviceContext);
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(
109                    DLFileRankStagedModelDataHandler.class);
110    
111    }