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             */
061            public static void generateMetadata(FileVersion fileVersion)
062                    throws PortalException {
063    
064                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
065    
066                    if (rawMetadataProcessor != null) {
067                            rawMetadataProcessor.generateMetadata(fileVersion);
068                    }
069            }
070    
071            public static RawMetadataProcessor getRawMetadataProcessor() {
072                    return (RawMetadataProcessor)DLProcessorRegistryUtil.getDLProcessor(
073                            DLProcessorConstants.RAW_METADATA_PROCESSOR);
074            }
075    
076            public static boolean isSupported(FileVersion fileVersion) {
077                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
078    
079                    if (rawMetadataProcessor == null) {
080                            return false;
081                    }
082    
083                    return rawMetadataProcessor.isSupported(fileVersion);
084            }
085    
086            public static boolean isSupported(String mimeType) {
087                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
088    
089                    if (rawMetadataProcessor == null) {
090                            return false;
091                    }
092    
093                    return rawMetadataProcessor.isSupported(mimeType);
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             */
107            public static void saveMetadata(FileVersion fileVersion)
108                    throws PortalException {
109    
110                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
111    
112                    if (rawMetadataProcessor != null) {
113                            rawMetadataProcessor.saveMetadata(fileVersion);
114                    }
115            }
116    
117            /**
118             * Launches extraction of raw metadata from the file version.
119             *
120             * <p>
121             * The raw metadata extraction is done asynchronously.
122             * </p>
123             *
124             * @param fileVersion the latest file version from which the raw metadata is
125             *        to be generated
126             */
127            public static void trigger(FileVersion fileVersion) {
128                    RawMetadataProcessor rawMetadataProcessor = getRawMetadataProcessor();
129    
130                    if (rawMetadataProcessor != null) {
131                            rawMetadataProcessor.trigger(fileVersion);
132                    }
133            }
134    
135            /**
136             * @deprecated As of 6.2.0
137             */
138            @Deprecated
139            public void setRawMetadataProcessor(
140                    RawMetadataProcessor rawMetadataProcessor) {
141            }
142    
143    }