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