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