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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.repository.model.FileVersion;
021    import com.liferay.portlet.documentlibrary.model.DLProcessorConstants;
022    
023    /**
024     * Document library processor responsible for the generation of raw metadata
025     * associated with all of the the files stored in the document library.
026     *
027     * <p>
028     * This processor automatically and assynchronously extracts the metadata from
029     * all of the files stored in the document library. The metadata extraction is
030     * done with the help of {@link
031     * com.liferay.portal.metadata.TikaRawMetadataProcessor}
032     * </p>
033     *
034     * @author Alexander Chow
035     * @author Mika Koivisto
036     * @author Miguel Pastor
037     */
038    public class RawMetadataProcessorUtil {
039    
040            public static void cleanUp(FileEntry fileEntry) {
041                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
042    
043                    if (rawMetadataProcessor != null) {
044                            rawMetadataProcessor.cleanUp(fileEntry);
045                    }
046            }
047    
048            public static void cleanUp(FileVersion fileVersion) {
049                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
050    
051                    if (rawMetadataProcessor != null) {
052                            rawMetadataProcessor.cleanUp(fileVersion);
053                    }
054            }
055    
056            /**
057             * Generates the raw metadata associated with the file entry.
058             *
059             * @param  fileVersion the file version from which the raw metatada is to be
060             *         generated
061             * @throws PortalException if an error occurred in the metadata extraction
062             * @throws SystemException if a system exception occurred
063             */
064            public static void generateMetadata(FileVersion fileVersion)
065                    throws PortalException, SystemException {
066    
067                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
068    
069                    if (rawMetadataProcessor != null) {
070                            rawMetadataProcessor.generateMetadata(fileVersion);
071                    }
072            }
073    
074            public static RawMetadataProcessor getRawMetadataProcessor() {
075                    return (RawMetadataProcessor)DLProcessorRegistryUtil.getDLProcessor(
076                            DLProcessorConstants.RAW_METADATA_PROCESSOR);
077            }
078    
079            public static boolean isSupported(FileVersion fileVersion) {
080                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
081    
082                    if (rawMetadataProcessor == null) {
083                            return false;
084                    }
085    
086                    return rawMetadataProcessor.isSupported(fileVersion);
087            }
088    
089            public static boolean isSupported(String mimeType) {
090                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
091    
092                    if (rawMetadataProcessor == null) {
093                            return false;
094                    }
095    
096                    return rawMetadataProcessor.isSupported(mimeType);
097            }
098    
099            /**
100             * Saves the raw metadata present in the file version.
101             *
102             * <p>
103             * The raw metadata present in the file version is extracted and persisted
104             * using {@link com.liferay.portal.metadata.TikaRawMetadataProcessor}.
105             * </p>
106             *
107             * @param  fileVersion the file version from which the raw metatada is to be
108             *         extracted and persisted
109             * @throws PortalException if an error occurred in the metadata extraction
110             * @throws SystemException if a system exception occurred
111             */
112            public static void saveMetadata(FileVersion fileVersion)
113                    throws PortalException, SystemException {
114    
115                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
116    
117                    if (rawMetadataProcessor != null) {
118                            rawMetadataProcessor.saveMetadata(fileVersion);
119                    }
120            }
121    
122            /**
123             * Launches extraction of raw metadata from the file version.
124             *
125             * <p>
126             * The raw metadata extraction is done asynchronously.
127             * </p>
128             *
129             * @param fileVersion the latest file version from which the raw metadata is
130             *        to be generated
131             */
132            public static void trigger(FileVersion fileVersion) {
133                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
134    
135                    if (rawMetadataProcessor != null) {
136                            rawMetadataProcessor.trigger(fileVersion);
137                    }
138            }
139    
140            /**
141             * @deprecated As of 6.2.0
142             */
143            public void setRawMetadataProcessor(
144                    RawMetadataProcessor rawMetadataProcessor) {
145            }
146    
147    }