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