001    /**
002     * Copyright (c) 2000-2012 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.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.upload.UploadRequest;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MimeTypesUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StreamUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.model.BaseModel;
030    import com.liferay.portal.model.CompanyConstants;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
033    import com.liferay.portlet.documentlibrary.DuplicateFileException;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadataModel;
036    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
037    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
038    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
039    import com.liferay.portlet.dynamicdatalists.model.DDLRecordModel;
040    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
041    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
042    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
043    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
044    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
045    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
046    import com.liferay.portlet.dynamicdatamapping.storage.Field;
047    import com.liferay.portlet.dynamicdatamapping.storage.FieldConstants;
048    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
049    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
050    
051    import java.io.InputStream;
052    import java.io.Serializable;
053    
054    import java.util.Set;
055    
056    import javax.servlet.http.HttpServletRequest;
057    import javax.servlet.http.HttpServletResponse;
058    
059    /**
060     * @author Eduardo Lundgren
061     */
062    public class DDMImpl implements DDM {
063    
064            public static final String TYPE_CHECKBOX = "checkbox";
065    
066            public static final String TYPE_DDM_DOCUMENTLIBRARY = "ddm-documentlibrary";
067    
068            public static final String TYPE_DDM_FILEUPLOAD = "ddm-fileupload";
069    
070            public static final String TYPE_RADIO = "radio";
071    
072            public static final String TYPE_SELECT = "select";
073    
074            public Fields getFields(
075                            long ddmStructureId, long ddmTemplateId,
076                            ServiceContext serviceContext)
077                    throws PortalException, SystemException {
078    
079                    return getFields(
080                            ddmStructureId, ddmTemplateId, StringPool.BLANK, serviceContext);
081            }
082    
083            public Fields getFields(
084                            long ddmStructureId, long ddmTemplateId, String fieldNamespace,
085                            ServiceContext serviceContext)
086                    throws PortalException, SystemException {
087    
088                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(
089                            ddmStructureId);
090    
091                    try {
092                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
093                                    ddmTemplateId);
094    
095                            ddmStructure.setXsd(ddmTemplate.getScript());
096                    }
097                    catch (NoSuchTemplateException nste) {
098                    }
099    
100                    Set<String> fieldNames = ddmStructure.getFieldNames();
101    
102                    Fields fields = new Fields();
103    
104                    for (String fieldName : fieldNames) {
105                            Field field = new Field();
106    
107                            field.setName(fieldName);
108    
109                            String fieldDataType = ddmStructure.getFieldDataType(fieldName);
110                            String fieldType = ddmStructure.getFieldType(fieldName);
111                            String fieldValue = ParamUtil.getString(
112                                    serviceContext, fieldNamespace + fieldName);
113    
114                            if (fieldDataType.equals(FieldConstants.FILE_UPLOAD)) {
115                                    continue;
116                            }
117    
118                            if (fieldType.equals(DDMImpl.TYPE_RADIO) ||
119                                    fieldType.equals(DDMImpl.TYPE_SELECT)) {
120    
121                                    Object value = serviceContext.getAttribute(
122                                            fieldNamespace + fieldName);
123    
124                                    String[] fieldValues = {};
125    
126                                    if (value instanceof String) {
127                                            fieldValues = new String[] {String.valueOf(value)};
128                                    }
129                                    else if (value instanceof String[]) {
130                                            fieldValues = (String[])value;
131                                    }
132    
133                                    fieldValue = JSONFactoryUtil.serialize(fieldValues);
134                            }
135    
136                            if (fieldValue == null) {
137                                    continue;
138                            }
139    
140                            Serializable fieldValueSerializable =
141                                    FieldConstants.getSerializable(
142                                            fieldDataType, GetterUtil.getString(fieldValue));
143    
144                            field.setValue(fieldValueSerializable);
145    
146                            fields.put(field);
147                    }
148    
149                    return fields;
150            }
151    
152            public Fields getFields(long ddmStructureId, ServiceContext serviceContext)
153                    throws PortalException, SystemException {
154    
155                    return getFields(ddmStructureId, 0, serviceContext);
156            }
157    
158            public Fields getFields(
159                            long ddmStructureId, String fieldNamespace,
160                            ServiceContext serviceContext)
161                    throws PortalException, SystemException {
162    
163                    return getFields(ddmStructureId, 0, fieldNamespace, serviceContext);
164            }
165    
166            public String getFileUploadPath(BaseModel<?> baseModel) {
167                    StringBundler sb = new StringBundler(7);
168    
169                    try {
170                            long primaryKey = 0;
171    
172                            String version = StringPool.BLANK;
173    
174                            if (baseModel instanceof DDLRecordModel) {
175                                    DDLRecord record = (DDLRecord)baseModel;
176    
177                                    primaryKey = record.getPrimaryKey();
178    
179                                    DDLRecordVersion recordVersion =
180                                            record.getLatestRecordVersion();
181    
182                                    version = recordVersion.getVersion();
183                            }
184                            else if (baseModel instanceof DLFileEntryMetadataModel) {
185                                    DLFileEntryMetadata fileEntryMetadata =
186                                            (DLFileEntryMetadata)baseModel;
187    
188                                    primaryKey = fileEntryMetadata.getPrimaryKey();
189    
190                                    DLFileVersion fileVersion = fileEntryMetadata.getFileVersion();
191    
192                                    version = fileVersion.getVersion();
193                            }
194    
195                            sb.append("ddm");
196                            sb.append(StringPool.SLASH);
197                            sb.append(baseModel.getModelClassName());
198                            sb.append(StringPool.SLASH);
199                            sb.append(primaryKey);
200                            sb.append(StringPool.SLASH);
201                            sb.append(version);
202                    }
203                    catch (Exception e) {
204                    }
205    
206                    return sb.toString();
207            }
208    
209            public void sendFieldFile(
210                            HttpServletRequest request, HttpServletResponse response,
211                            Field field)
212                    throws Exception {
213    
214                    if (field == null) {
215                            return;
216                    }
217    
218                    DDMStructure structure = field.getDDMStructure();
219    
220                    Serializable fieldValue = field.getValue();
221    
222                    JSONObject fileJSONObject = JSONFactoryUtil.createJSONObject(
223                            String.valueOf(fieldValue));
224    
225                    String fileName = fileJSONObject.getString("name");
226                    String filePath = fileJSONObject.getString("path");
227    
228                    InputStream is = DLStoreUtil.getFileAsStream(
229                            structure.getCompanyId(), CompanyConstants.SYSTEM, filePath);
230                    long contentLength = DLStoreUtil.getFileSize(
231                            structure.getCompanyId(), CompanyConstants.SYSTEM, filePath);
232                    String contentType = MimeTypesUtil.getContentType(fileName);
233    
234                    ServletResponseUtil.sendFile(
235                            request, response, fileName, is, contentLength, contentType);
236            }
237    
238            public String uploadFieldFile(
239                            long structureId, long storageId, BaseModel<?> baseModel,
240                            String fieldName, ServiceContext serviceContext)
241                    throws Exception {
242    
243                    return uploadFieldFile(
244                            structureId, storageId, baseModel, fieldName, StringPool.BLANK,
245                            serviceContext);
246            }
247    
248            public String uploadFieldFile(
249                            long structureId, long storageId, BaseModel<?> baseModel,
250                            String fieldName, String fieldNamespace,
251                            ServiceContext serviceContext)
252                    throws Exception {
253    
254                    HttpServletRequest request = serviceContext.getRequest();
255    
256                    if (!(request instanceof UploadRequest)) {
257                            return StringPool.BLANK;
258                    }
259    
260                    UploadRequest uploadRequest = (UploadRequest)request;
261    
262                    String fileName = uploadRequest.getFileName(fieldNamespace + fieldName);
263    
264                    String fieldValue = StringPool.BLANK;
265    
266                    InputStream inputStream = null;
267    
268                    try {
269                            inputStream = uploadRequest.getFileAsStream(
270                                    fieldNamespace + fieldName, true);
271    
272                            if (inputStream != null) {
273                                    String filePath = storeFieldFile(
274                                            baseModel, fieldName, inputStream, serviceContext);
275    
276                                    JSONObject recordFileJSONObject =
277                                            JSONFactoryUtil.createJSONObject();
278    
279                                    recordFileJSONObject.put("name", fileName);
280                                    recordFileJSONObject.put("path", filePath);
281                                    recordFileJSONObject.put(
282                                            "className", baseModel.getModelClassName());
283                                    recordFileJSONObject.put(
284                                            "classPK", String.valueOf(baseModel.getPrimaryKeyObj()));
285    
286                                    fieldValue = recordFileJSONObject.toString();
287    
288                                    Fields fields = new Fields();
289    
290                                    Field field = new Field(structureId, fieldName, fieldValue);
291    
292                                    fields.put(field);
293    
294                                    StorageEngineUtil.update(
295                                            storageId, fields, true, serviceContext);
296                            }
297                    }
298                    finally {
299                            StreamUtil.cleanUp(inputStream);
300                    }
301    
302                    return fieldValue;
303            }
304    
305            protected String storeFieldFile(
306                            BaseModel<?> baseModel, String fieldName, InputStream inputStream,
307                            ServiceContext serviceContext)
308                    throws Exception {
309    
310                    String dirName = getFileUploadPath(baseModel);
311    
312                    try {
313                            DLStoreUtil.addDirectory(
314                                    serviceContext.getCompanyId(), CompanyConstants.SYSTEM,
315                                    dirName);
316                    }
317                    catch (DuplicateDirectoryException dde) {
318                    }
319    
320                    String fileName = dirName + StringPool.SLASH + fieldName;
321    
322                    try {
323                            DLStoreUtil.addFile(
324                                    serviceContext.getCompanyId(), CompanyConstants.SYSTEM,
325                                    fileName, inputStream);
326                    }
327                    catch (DuplicateFileException dfe) {
328                    }
329    
330                    return fileName;
331            }
332    
333    }