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.asset;
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.portal.util.PortalUtil;
021    import com.liferay.portlet.asset.model.BaseDDMFormValuesReader;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
023    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
024    import com.liferay.portlet.dynamicdatamapping.DDMFormValues;
025    import com.liferay.portlet.dynamicdatamapping.DDMStructure;
026    import com.liferay.portlet.dynamicdatamapping.DDMStructureManagerUtil;
027    import com.liferay.portlet.dynamicdatamapping.StorageEngineManagerUtil;
028    
029    import java.util.List;
030    
031    /**
032     * @author Adolfo P??rez
033     */
034    public class DLFileEntryDDMFormValuesReader extends BaseDDMFormValuesReader {
035    
036            public DLFileEntryDDMFormValuesReader(
037                    FileEntry dlFileEntry, FileVersion fileVersion) {
038    
039                    _fileEntry = dlFileEntry;
040                    _fileVersion = fileVersion;
041            }
042    
043            @Override
044            public DDMFormValues getDDMFormValues() throws PortalException {
045                    DLFileEntryMetadata dlFileEntryMetadata = getDLFileEntryMetadata();
046    
047                    if (dlFileEntryMetadata == null) {
048                            return new DDMFormValues(null);
049                    }
050    
051                    return StorageEngineManagerUtil.getDDMFormValues(
052                            dlFileEntryMetadata.getDDMStorageId());
053            }
054    
055            protected DLFileEntryMetadata getDLFileEntryMetadata() {
056                    List<DDMStructure> ddmStructures =
057                            DDMStructureManagerUtil.getClassStructures(
058                                    _fileEntry.getCompanyId(),
059                                    PortalUtil.getClassNameId(DLFileEntryMetadata.class));
060    
061                    DLFileEntryMetadata dlFileEntryMetadata = null;
062    
063                    for (DDMStructure ddmStructure : ddmStructures) {
064                            dlFileEntryMetadata =
065                                    DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(
066                                            ddmStructure.getStructureId(),
067                                            _fileVersion.getFileVersionId());
068    
069                            if (dlFileEntryMetadata != null) {
070                                    break;
071                            }
072                    }
073    
074                    return dlFileEntryMetadata;
075            }
076    
077            private final FileEntry _fileEntry;
078            private final FileVersion _fileVersion;
079    
080    }