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.util;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.messaging.DestinationNames;
024    import com.liferay.portal.kernel.messaging.MessageBusException;
025    import com.liferay.portal.kernel.messaging.MessageBusUtil;
026    import com.liferay.portal.kernel.metadata.RawMetadataProcessorUtil;
027    import com.liferay.portal.kernel.repository.model.FileEntry;
028    import com.liferay.portal.kernel.repository.model.FileVersion;
029    import com.liferay.portal.kernel.search.Indexer;
030    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
031    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
032    import com.liferay.portal.kernel.util.StreamUtil;
033    import com.liferay.portal.kernel.xml.Element;
034    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
035    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
036    import com.liferay.portal.service.ServiceContext;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PropsValues;
039    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
040    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
041    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
042    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
043    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
044    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
045    
046    import java.io.File;
047    import java.io.InputStream;
048    
049    import java.util.List;
050    import java.util.Map;
051    
052    /**
053     * @author Alexander Chow
054     * @author Mika Koivisto
055     * @author Miguel Pastor
056     */
057    @DoPrivileged
058    public class RawMetadataProcessorImpl
059            implements DLProcessor, RawMetadataProcessor {
060    
061            @Override
062            public void afterPropertiesSet() {
063            }
064    
065            @Override
066            public void cleanUp(FileEntry fileEntry) {
067            }
068    
069            @Override
070            public void cleanUp(FileVersion fileVersion) {
071            }
072    
073            @Override
074            public void exportGeneratedFiles(
075                            PortletDataContext portletDataContext, FileEntry fileEntry,
076                            Element fileEntryElement)
077                    throws Exception {
078    
079                    return;
080            }
081    
082            @Override
083            public void generateMetadata(FileVersion fileVersion)
084                    throws SystemException {
085    
086                    long fileVersionId = fileVersion.getFileVersionId();
087    
088                    long fileEntryMetadataCount =
089                            DLFileEntryMetadataLocalServiceUtil.
090                                    getFileVersionFileEntryMetadatasCount(fileVersionId);
091    
092                    if (fileEntryMetadataCount == 0) {
093                            trigger(fileVersion);
094                    }
095            }
096    
097            @Override
098            public void importGeneratedFiles(
099                            PortletDataContext portletDataContext, FileEntry fileEntry,
100                            FileEntry importedFileEntry, Element fileEntryElement)
101                    throws Exception {
102    
103                    return;
104            }
105    
106            @Override
107            public boolean isSupported(FileVersion fileVersion) {
108                    return true;
109            }
110    
111            @Override
112            public boolean isSupported(String mimeType) {
113                    return true;
114            }
115    
116            @Override
117            public void saveMetadata(FileVersion fileVersion)
118                    throws PortalException, SystemException {
119    
120                    Map<String, Fields> rawMetadataMap = null;
121    
122                    if (fileVersion instanceof LiferayFileVersion) {
123                            try {
124                                    LiferayFileVersion liferayFileVersion =
125                                            (LiferayFileVersion)fileVersion;
126    
127                                    File file = liferayFileVersion.getFile(false);
128    
129                                    rawMetadataMap = RawMetadataProcessorUtil.getRawMetadataMap(
130                                            fileVersion.getExtension(), fileVersion.getMimeType(),
131                                            file);
132                            }
133                            catch (UnsupportedOperationException uoe) {
134                            }
135                    }
136    
137                    if (rawMetadataMap == null) {
138                            InputStream inputStream = null;
139    
140                            try {
141                                    inputStream = fileVersion.getContentStream(false);
142    
143                                    if (inputStream == null) {
144                                            if (_log.isWarnEnabled()) {
145                                                    _log.warn(
146                                                            "No metadata is available for file version " +
147                                                                    fileVersion.getFileVersionId());
148                                            }
149    
150                                            return;
151                                    }
152    
153                                    rawMetadataMap = RawMetadataProcessorUtil.getRawMetadataMap(
154                                            fileVersion.getExtension(), fileVersion.getMimeType(),
155                                            inputStream);
156                            }
157                            finally {
158                                    StreamUtil.cleanUp(inputStream);
159                            }
160                    }
161    
162                    List<DDMStructure> ddmStructures =
163                            DDMStructureLocalServiceUtil.getClassStructures(
164                                    fileVersion.getCompanyId(),
165                                    PortalUtil.getClassNameId(DLFileEntry.class), QueryUtil.ALL_POS,
166                                    QueryUtil.ALL_POS);
167    
168                    ServiceContext serviceContext = new ServiceContext();
169    
170                    serviceContext.setScopeGroupId(fileVersion.getGroupId());
171                    serviceContext.setUserId(fileVersion.getUserId());
172    
173                    DLFileEntryMetadataLocalServiceUtil.updateFileEntryMetadata(
174                            fileVersion.getCompanyId(), ddmStructures, 0L,
175                            fileVersion.getFileEntryId(), fileVersion.getFileVersionId(),
176                            rawMetadataMap, serviceContext);
177    
178                    FileEntry fileEntry = fileVersion.getFileEntry();
179    
180                    if (fileEntry instanceof LiferayFileEntry) {
181                            Indexer indexer = IndexerRegistryUtil.getIndexer(
182                                    DLFileEntryConstants.getClassName());
183    
184                            LiferayFileEntry liferayFileEntry = (LiferayFileEntry)fileEntry;
185    
186                            indexer.reindex(liferayFileEntry.getDLFileEntry());
187                    }
188            }
189    
190            @Override
191            public void trigger(FileVersion fileVersion) {
192                    if (PropsValues.DL_FILE_ENTRY_PROCESSORS_TRIGGER_SYNCHRONOUSLY) {
193                            try {
194                                    MessageBusUtil.sendSynchronousMessage(
195                                            DestinationNames.DOCUMENT_LIBRARY_RAW_METADATA_PROCESSOR,
196                                            fileVersion);
197                            }
198                            catch (MessageBusException mbe) {
199                                    if (_log.isWarnEnabled()) {
200                                            _log.warn(mbe, mbe);
201                                    }
202                            }
203                    }
204                    else {
205                            MessageBusUtil.sendMessage(
206                                    DestinationNames.DOCUMENT_LIBRARY_RAW_METADATA_PROCESSOR,
207                                    fileVersion);
208                    }
209            }
210    
211            private static Log _log = LogFactoryUtil.getLog(
212                    RawMetadataProcessorImpl.class);
213    
214    }