001    /**
002     * Copyright (c) 2000-2011 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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.messaging.DestinationNames;
023    import com.liferay.portal.kernel.messaging.MessageBusException;
024    import com.liferay.portal.kernel.messaging.MessageBusUtil;
025    import com.liferay.portal.kernel.metadata.RawMetadataProcessorUtil;
026    import com.liferay.portal.kernel.repository.model.FileEntry;
027    import com.liferay.portal.kernel.repository.model.FileVersion;
028    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PropsValues;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
033    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
035    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
036    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
037    
038    import java.io.File;
039    import java.io.InputStream;
040    
041    import java.util.List;
042    import java.util.Map;
043    
044    /**
045     * Document library processor responsible for the generation of raw metadata
046     * associated with all of the the files stored in the document library.
047     *
048     * <p>
049     * This processor automatically and assynchronously extracts the metadata from
050     * all of the files stored in the document library. The metadata extraction is
051     * done with the help of {@link
052     * com.liferay.portal.metadata.TikaRawMetadataProcessor}
053     * </p>
054     *
055     * @author Alexander Chow
056     * @author Mika Koivisto
057     * @author Miguel Pastor
058     */
059    public class RawMetadataProcessor implements DLProcessor {
060    
061            public void cleanUp(FileEntry fileEntry) {
062            }
063    
064            public void cleanUp(FileVersion fileVersion) {
065            }
066    
067            /**
068             * Generates the raw metadata associated with the file entry.
069             *
070             * @param  fileVersion the file version from which the raw metatada is to be
071             *         generated
072             * @throws PortalException if an error occurred in the metadata extraction
073             * @throws SystemException if a system exception occurred
074             */
075            public static void generateMetadata(FileVersion fileVersion)
076                    throws PortalException, SystemException {
077    
078                    long fileEntryMetadataCount =
079                            DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadataCount(
080                                    fileVersion.getFileEntryId(),
081                                    fileVersion.getFileVersionId());
082    
083                    if (fileEntryMetadataCount == 0) {
084                            _instance.trigger(fileVersion);
085                    }
086            }
087    
088            public boolean isSupported(FileVersion fileVersion) {
089                    return true;
090            }
091    
092            public boolean isSupported(String mimeType) {
093                    return true;
094            }
095    
096            /**
097             * Saves the raw metadata present in the file version.
098             *
099             * <p>
100             * The raw metadata present in the file version is extracted and persisted
101             * using {@link com.liferay.portal.metadata.TikaRawMetadataProcessor}.
102             * </p>
103             *
104             * @param  fileVersion the file version from which the raw metatada is to be
105             *         extracted and persisted
106             * @throws PortalException if an error occurred in the metadata extraction
107             * @throws SystemException if a system exception occurred
108             */
109            public static void saveMetadata(FileVersion fileVersion)
110                    throws PortalException, SystemException {
111    
112                    Map<String, Fields> rawMetadataMap = null;
113    
114                    if (fileVersion instanceof LiferayFileVersion) {
115                            try {
116                                    LiferayFileVersion liferayFileVersion =
117                                            (LiferayFileVersion)fileVersion;
118    
119                                    File file = liferayFileVersion.getFile(false);
120    
121                                    rawMetadataMap = RawMetadataProcessorUtil.getRawMetadataMap(
122                                            fileVersion.getExtension(), fileVersion.getMimeType(),
123                                            file);
124                            }
125                            catch (UnsupportedOperationException uoe) {
126                            }
127                    }
128    
129                    if (rawMetadataMap == null) {
130                            InputStream inputStream = fileVersion.getContentStream(false);
131    
132                            rawMetadataMap = RawMetadataProcessorUtil.getRawMetadataMap(
133                                    fileVersion.getExtension(), fileVersion.getMimeType(),
134                                    inputStream);
135                    }
136    
137                    List<DDMStructure> ddmStructures =
138                            DDMStructureLocalServiceUtil.getClassStructures(
139                                    PortalUtil.getClassNameId(DLFileEntry.class),
140                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
141    
142                    ServiceContext serviceContext = new ServiceContext();
143    
144                    serviceContext.setScopeGroupId(fileVersion.getGroupId());
145                    serviceContext.setUserId(fileVersion.getUserId());
146    
147                    DLFileEntryMetadataLocalServiceUtil.updateFileEntryMetadata(
148                            fileVersion.getCompanyId(), ddmStructures, 0L,
149                            fileVersion.getFileEntryId(), fileVersion.getFileVersionId(),
150                            rawMetadataMap, serviceContext);
151            }
152    
153            /**
154             * Launches extraction of raw metadata from the file version.
155             *
156             * <p>
157             * The raw metadata extraction is done asynchronously.
158             * </p>
159             *
160             * @param fileVersion the latest file version from which the raw metadata is
161             *        to be generated
162             */
163            public void trigger(FileVersion fileVersion) {
164                    if (PropsValues.DL_FILE_ENTRY_PROCESSORS_TRIGGER_SYNCHRONOUSLY) {
165                            try {
166                                    MessageBusUtil.sendSynchronousMessage(
167                                            DestinationNames.DOCUMENT_LIBRARY_RAW_METADATA_PROCESSOR,
168                                            fileVersion);
169                            }
170                            catch (MessageBusException mbe) {
171                                    if (_log.isWarnEnabled()) {
172                                            _log.warn(mbe, mbe);
173                                    }
174                            }
175                    }
176                    else {
177                            MessageBusUtil.sendMessage(
178                                    DestinationNames.DOCUMENT_LIBRARY_RAW_METADATA_PROCESSOR,
179                                    fileVersion);
180                    }
181            }
182    
183            private static Log _log = LogFactoryUtil.getLog(RawMetadataProcessor.class);
184    
185            private static RawMetadataProcessor _instance = new RawMetadataProcessor();
186    
187    }