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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.service.ServiceContext;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
023    import com.liferay.portlet.documentlibrary.service.base.DLFileEntryMetadataLocalServiceBaseImpl;
024    import com.liferay.portlet.dynamicdatamapping.StorageException;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
026    import com.liferay.portlet.dynamicdatamapping.storage.FieldConstants;
027    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
028    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
029    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
030    
031    import java.util.List;
032    import java.util.Map;
033    
034    /**
035     * @author Alexander Chow
036     */
037    public class DLFileEntryMetadataLocalServiceImpl
038            extends DLFileEntryMetadataLocalServiceBaseImpl {
039    
040            @Override
041            public void deleteFileEntryMetadata(long fileEntryId)
042                    throws PortalException, SystemException {
043    
044                    List<DLFileEntryMetadata> fileEntryMetadatas =
045                            dlFileEntryMetadataPersistence.findByFileEntryId(fileEntryId);
046    
047                    for (DLFileEntryMetadata fileEntryMetadata : fileEntryMetadatas) {
048                            deleteFileEntryMetadata(fileEntryMetadata);
049                    }
050            }
051    
052            @Override
053            public void deleteFileVersionFileEntryMetadata(long fileVersionId)
054                    throws PortalException, SystemException {
055    
056                    List<DLFileEntryMetadata> fileEntryMetadatas =
057                            dlFileEntryMetadataPersistence.findByFileVersionId(fileVersionId);
058    
059                    for (DLFileEntryMetadata fileEntryMetadata : fileEntryMetadatas) {
060                            deleteFileEntryMetadata(fileEntryMetadata);
061                    }
062            }
063    
064            @Override
065            public DLFileEntryMetadata getFileEntryMetadata(long fileEntryMetadataId)
066                    throws PortalException, SystemException {
067    
068                    return dlFileEntryMetadataPersistence.findByPrimaryKey(
069                            fileEntryMetadataId);
070            }
071    
072            @Override
073            public DLFileEntryMetadata getFileEntryMetadata(
074                            long ddmStructureId, long fileVersionId)
075                    throws PortalException, SystemException {
076    
077                    return dlFileEntryMetadataPersistence.findByD_F(
078                            ddmStructureId, fileVersionId);
079            }
080    
081            /**
082             * @deprecated {@link #getFileVersionFileEntryMetadatasCount(long)}
083             */
084            @Override
085            public long getFileEntryMetadataCount(long fileEntryId, long fileVersionId)
086                    throws SystemException {
087    
088                    return getFileVersionFileEntryMetadatasCount(fileVersionId);
089            }
090    
091            @Override
092            public List<DLFileEntryMetadata> getFileVersionFileEntryMetadatas(
093                            long fileVersionId)
094                    throws SystemException {
095    
096                    return dlFileEntryMetadataPersistence.findByFileVersionId(
097                            fileVersionId);
098            }
099    
100            @Override
101            public long getFileVersionFileEntryMetadatasCount(long fileVersionId)
102                    throws SystemException {
103    
104                    return dlFileEntryMetadataPersistence.countByFileVersionId(
105                            fileVersionId);
106            }
107    
108            @Override
109            public void updateFileEntryMetadata(
110                            long companyId, List<DDMStructure> ddmStructures,
111                            long fileEntryTypeId, long fileEntryId, long fileVersionId,
112                            Map<String, Fields> fieldsMap, ServiceContext serviceContext)
113                    throws PortalException, SystemException {
114    
115                    for (DDMStructure ddmStructure : ddmStructures) {
116                            Fields fields = fieldsMap.get(ddmStructure.getStructureKey());
117    
118                            if (fields != null) {
119                                    updateFileEntryMetadata(
120                                            companyId, ddmStructure, fileEntryTypeId, fileEntryId,
121                                            fileVersionId, fields, serviceContext);
122                            }
123                    }
124            }
125    
126            @Override
127            public void updateFileEntryMetadata(
128                            long fileEntryTypeId, long fileEntryId, long fileVersionId,
129                            Map<String, Fields> fieldsMap, ServiceContext serviceContext)
130                    throws PortalException, SystemException {
131    
132                    DLFileEntryType fileEntryType =
133                            dlFileEntryTypeLocalService.getFileEntryType(fileEntryTypeId);
134    
135                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
136    
137                    updateFileEntryMetadata(
138                            fileEntryType.getCompanyId(), ddmStructures, fileEntryTypeId,
139                            fileEntryId, fileVersionId, fieldsMap, serviceContext);
140            }
141    
142            protected void deleteFileEntryMetadata(
143                            DLFileEntryMetadata fileEntryMetadata)
144                    throws PortalException, SystemException {
145    
146                    // FileEntry metadata
147    
148                    dlFileEntryMetadataPersistence.remove(fileEntryMetadata);
149    
150                    // Dynamic data mapping storage
151    
152                    StorageEngineUtil.deleteByClass(fileEntryMetadata.getDDMStorageId());
153    
154                    // Dynamic data mapping structure link
155    
156                    ddmStructureLinkLocalService.deleteClassStructureLink(
157                            fileEntryMetadata.getFileEntryMetadataId());
158            }
159    
160            protected void updateFileEntryMetadata(
161                            long companyId, DDMStructure ddmStructure, long fileEntryTypeId,
162                            long fileEntryId, long fileVersionId, Fields fields,
163                            ServiceContext serviceContext)
164                    throws StorageException, SystemException {
165    
166                    DLFileEntryMetadata fileEntryMetadata =
167                            dlFileEntryMetadataPersistence.fetchByD_F(
168                                    ddmStructure.getStructureId(), fileVersionId);
169    
170                    if (fileEntryMetadata != null) {
171                            StorageEngineUtil.update(
172                                    fileEntryMetadata.getDDMStorageId(), fields, serviceContext);
173                    }
174                    else {
175    
176                            // File entry metadata
177    
178                            long fileEntryMetadataId = counterLocalService.increment();
179    
180                            fileEntryMetadata = dlFileEntryMetadataPersistence.create(
181                                    fileEntryMetadataId);
182    
183                            long ddmStorageId = StorageEngineUtil.create(
184                                    companyId, ddmStructure.getStructureId(), fields,
185                                    serviceContext);
186    
187                            fileEntryMetadata.setDDMStorageId(ddmStorageId);
188    
189                            fileEntryMetadata.setDDMStructureId(ddmStructure.getStructureId());
190                            fileEntryMetadata.setFileEntryTypeId(fileEntryTypeId);
191                            fileEntryMetadata.setFileEntryId(fileEntryId);
192                            fileEntryMetadata.setFileVersionId(fileVersionId);
193    
194                            dlFileEntryMetadataPersistence.update(fileEntryMetadata, false);
195    
196                            // Dynamic data mapping structure link
197    
198                            long classNameId = PortalUtil.getClassNameId(
199                                    DLFileEntryMetadata.class);
200    
201                            ddmStructureLinkLocalService.addStructureLink(
202                                    classNameId, fileEntryMetadata.getFileEntryMetadataId(),
203                                    ddmStructure.getStructureId(), serviceContext);
204                    }
205    
206                    try {
207                            String namespace = String.valueOf(ddmStructure.getStructureId());
208    
209                            for (String fieldName : ddmStructure.getFieldNames()) {
210                                    String fieldDataType = ddmStructure.getFieldDataType(fieldName);
211    
212                                    if (fieldDataType.equals(FieldConstants.FILE_UPLOAD)) {
213                                            DDMUtil.uploadFieldFile(
214                                                    fileEntryMetadata.getDDMStructureId(),
215                                                    fileEntryMetadata.getDDMStorageId(), fileEntryMetadata,
216                                                    fieldName, namespace, serviceContext);
217                                    }
218                            }
219                    }
220                    catch (Exception e) {
221                    }
222            }
223    
224    }