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.dynamicdatamapping.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
025    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
026    import com.liferay.portlet.documentlibrary.util.DLUtil;
027    import com.liferay.portlet.dynamicdatamapping.model.Value;
028    
029    import java.util.Locale;
030    
031    /**
032     * @author Marcellus Tavares
033     */
034    public class DocumentLibraryDDMFormFieldValueTransformer
035            implements DDMFormFieldValueTransformer {
036    
037            @Override
038            public String getFieldType() {
039                    return "ddm-documentlibrary";
040            }
041    
042            @Override
043            public void transform(Value value) throws PortalException {
044                    for (Locale locale : value.getAvailableLocales()) {
045                            FileEntry tempFileEntry = fetchTempFileEntry(
046                                    value.getString(locale));
047    
048                            if (tempFileEntry == null) {
049                                    continue;
050                            }
051    
052                            FileEntry fileEntry = addFileEntry(tempFileEntry);
053    
054                            value.addString(locale, toJSON(fileEntry));
055                    }
056            }
057    
058            protected FileEntry addFileEntry(FileEntry tempFileEntry)
059                    throws PortalException {
060    
061                    String fileName = DLUtil.getFileName(
062                            tempFileEntry.getGroupId(), tempFileEntry.getFolderId(),
063                            tempFileEntry.getFileName());
064    
065                    return DLAppServiceUtil.addFileEntry(
066                            tempFileEntry.getGroupId(), 0, fileName,
067                            tempFileEntry.getMimeType(), fileName, StringPool.BLANK,
068                            StringPool.BLANK, tempFileEntry.getContentStream(),
069                            tempFileEntry.getSize(), new ServiceContext());
070            }
071    
072            protected FileEntry fetchTempFileEntry(String value)
073                    throws PortalException {
074    
075                    if (Validator.isNull(value)) {
076                            return null;
077                    }
078    
079                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);
080    
081                    boolean tempFile = jsonObject.getBoolean("tempFile");
082    
083                    if (tempFile == false) {
084                            return null;
085                    }
086    
087                    return DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
088                            jsonObject.getString("uuid"), jsonObject.getLong("groupId"));
089            }
090    
091            protected String toJSON(FileEntry fileEntry) {
092                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
093    
094                    jsonObject.put("groupId", fileEntry.getGroupId());
095                    jsonObject.put("title", fileEntry.getTitle());
096                    jsonObject.put("uuid", fileEntry.getUuid());
097    
098                    return jsonObject.toString();
099            }
100    
101    }