001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.StreamUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.model.BaseModel;
029    import com.liferay.portal.model.CompanyConstants;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.util.PortalUtil;
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.Date;
055    import java.util.Set;
056    
057    import javax.servlet.http.HttpServletRequest;
058    import javax.servlet.http.HttpServletResponse;
059    
060    /**
061     * @author Eduardo Lundgren
062     */
063    public class DDMImpl implements DDM {
064    
065            public static final String TYPE_CHECKBOX = "checkbox";
066    
067            public static final String TYPE_DDM_DOCUMENTLIBRARY = "ddm-documentlibrary";
068    
069            public static final String TYPE_DDM_FILEUPLOAD = "ddm-fileupload";
070    
071            public static final String TYPE_RADIO = "radio";
072    
073            public static final String TYPE_SELECT = "select";
074    
075            public Fields getFields(
076                            long ddmStructureId, long ddmTemplateId,
077                            ServiceContext serviceContext)
078                    throws PortalException, SystemException {
079    
080                    return getFields(
081                            ddmStructureId, ddmTemplateId, StringPool.BLANK, serviceContext);
082            }
083    
084            public Fields getFields(
085                            long ddmStructureId, long ddmTemplateId, String fieldNamespace,
086                            ServiceContext serviceContext)
087                    throws PortalException, SystemException {
088    
089                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(
090                            ddmStructureId);
091    
092                    try {
093                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
094                                    ddmTemplateId);
095    
096                            ddmStructure.setXsd(ddmTemplate.getScript());
097                    }
098                    catch (NoSuchTemplateException nste) {
099                    }
100    
101                    Set<String> fieldNames = ddmStructure.getFieldNames();
102    
103                    Fields fields = new Fields();
104    
105                    for (String fieldName : fieldNames) {
106                            Field field = new Field();
107    
108                            field.setName(fieldName);
109    
110                            String fieldDataType = ddmStructure.getFieldDataType(fieldName);
111                            String fieldType = ddmStructure.getFieldType(fieldName);
112                            Serializable fieldValue = serviceContext.getAttribute(
113                                    fieldNamespace + fieldName);
114    
115                            if (fieldDataType.equals(FieldConstants.DATE)) {
116                                    int fieldValueMonth = GetterUtil.getInteger(
117                                            serviceContext.getAttribute(
118                                                    fieldNamespace + fieldName + "Month"));
119                                    int fieldValueYear = GetterUtil.getInteger(
120                                            serviceContext.getAttribute(
121                                                    fieldNamespace + fieldName + "Year"));
122                                    int fieldValueDay = GetterUtil.getInteger(
123                                            serviceContext.getAttribute(
124                                                    fieldNamespace + fieldName + "Day"));
125    
126                                    Date fieldValueDate = PortalUtil.getDate(
127                                            fieldValueMonth, fieldValueDay, fieldValueYear);
128    
129                                    if (fieldValueDate != null) {
130                                            fieldValue = String.valueOf(fieldValueDate.getTime());
131                                    }
132                            }
133    
134                            if ((fieldValue == null) ||
135                                    fieldDataType.equals(FieldConstants.FILE_UPLOAD)) {
136    
137                                    continue;
138                            }
139    
140                            if (fieldType.equals(DDMImpl.TYPE_RADIO) ||
141                                    fieldType.equals(DDMImpl.TYPE_SELECT)) {
142    
143                                    if (fieldValue instanceof String) {
144                                            fieldValue = new String[] {String.valueOf(fieldValue)};
145                                    }
146    
147                                    fieldValue = JSONFactoryUtil.serialize(fieldValue);
148                            }
149    
150                            Serializable fieldValueSerializable =
151                                    FieldConstants.getSerializable(
152                                            fieldDataType, GetterUtil.getString(fieldValue));
153    
154                            field.setValue(fieldValueSerializable);
155    
156                            fields.put(field);
157                    }
158    
159                    return fields;
160            }
161    
162            public Fields getFields(long ddmStructureId, ServiceContext serviceContext)
163                    throws PortalException, SystemException {
164    
165                    return getFields(ddmStructureId, 0, serviceContext);
166            }
167    
168            public Fields getFields(
169                            long ddmStructureId, String fieldNamespace,
170                            ServiceContext serviceContext)
171                    throws PortalException, SystemException {
172    
173                    return getFields(ddmStructureId, 0, fieldNamespace, serviceContext);
174            }
175    
176            public String getFileUploadPath(BaseModel<?> baseModel) {
177                    StringBundler sb = new StringBundler(7);
178    
179                    try {
180                            long primaryKey = 0;
181    
182                            String version = StringPool.BLANK;
183    
184                            if (baseModel instanceof DDLRecordModel) {
185                                    DDLRecord record = (DDLRecord)baseModel;
186    
187                                    primaryKey = record.getPrimaryKey();
188    
189                                    DDLRecordVersion recordVersion =
190                                            record.getLatestRecordVersion();
191    
192                                    version = recordVersion.getVersion();
193                            }
194                            else if (baseModel instanceof DLFileEntryMetadataModel) {
195                                    DLFileEntryMetadata fileEntryMetadata =
196                                            (DLFileEntryMetadata)baseModel;
197    
198                                    primaryKey = fileEntryMetadata.getPrimaryKey();
199    
200                                    DLFileVersion fileVersion = fileEntryMetadata.getFileVersion();
201    
202                                    version = fileVersion.getVersion();
203                            }
204    
205                            sb.append("ddm");
206                            sb.append(StringPool.SLASH);
207                            sb.append(baseModel.getModelClassName());
208                            sb.append(StringPool.SLASH);
209                            sb.append(primaryKey);
210                            sb.append(StringPool.SLASH);
211                            sb.append(version);
212                    }
213                    catch (Exception e) {
214                    }
215    
216                    return sb.toString();
217            }
218    
219            public void sendFieldFile(
220                            HttpServletRequest request, HttpServletResponse response,
221                            Field field)
222                    throws Exception {
223    
224                    if (field == null) {
225                            return;
226                    }
227    
228                    DDMStructure structure = field.getDDMStructure();
229    
230                    Serializable fieldValue = field.getValue();
231    
232                    JSONObject fileJSONObject = JSONFactoryUtil.createJSONObject(
233                            String.valueOf(fieldValue));
234    
235                    String fileName = fileJSONObject.getString("name");
236                    String filePath = fileJSONObject.getString("path");
237    
238                    InputStream is = DLStoreUtil.getFileAsStream(
239                            structure.getCompanyId(), CompanyConstants.SYSTEM, filePath);
240                    long contentLength = DLStoreUtil.getFileSize(
241                            structure.getCompanyId(), CompanyConstants.SYSTEM, filePath);
242                    String contentType = MimeTypesUtil.getContentType(fileName);
243    
244                    ServletResponseUtil.sendFile(
245                            request, response, fileName, is, contentLength, contentType);
246            }
247    
248            public String uploadFieldFile(
249                            long structureId, long storageId, BaseModel<?> baseModel,
250                            String fieldName, ServiceContext serviceContext)
251                    throws Exception {
252    
253                    return uploadFieldFile(
254                            structureId, storageId, baseModel, fieldName, StringPool.BLANK,
255                            serviceContext);
256            }
257    
258            public String uploadFieldFile(
259                            long structureId, long storageId, BaseModel<?> baseModel,
260                            String fieldName, String fieldNamespace,
261                            ServiceContext serviceContext)
262                    throws Exception {
263    
264                    HttpServletRequest request = serviceContext.getRequest();
265    
266                    if (!(request instanceof UploadRequest)) {
267                            return StringPool.BLANK;
268                    }
269    
270                    UploadRequest uploadRequest = (UploadRequest)request;
271    
272                    String fileName = uploadRequest.getFileName(fieldNamespace + fieldName);
273    
274                    String fieldValue = StringPool.BLANK;
275    
276                    InputStream inputStream = null;
277    
278                    Fields fields = StorageEngineUtil.getFields(storageId);
279    
280                    try {
281                            inputStream = uploadRequest.getFileAsStream(
282                                    fieldNamespace + fieldName, true);
283    
284                            if (inputStream != null) {
285                                    String filePath = storeFieldFile(
286                                            baseModel, fieldName, inputStream, serviceContext);
287    
288                                    JSONObject recordFileJSONObject =
289                                            JSONFactoryUtil.createJSONObject();
290    
291                                    recordFileJSONObject.put("name", fileName);
292                                    recordFileJSONObject.put("path", filePath);
293                                    recordFileJSONObject.put(
294                                            "className", baseModel.getModelClassName());
295                                    recordFileJSONObject.put(
296                                            "classPK", String.valueOf(baseModel.getPrimaryKeyObj()));
297    
298                                    fieldValue = recordFileJSONObject.toString();
299                            }
300                            else if (fields.contains(fieldName)) {
301                                    return StringPool.BLANK;
302                            }
303    
304                            Field field = new Field(structureId, fieldName, fieldValue);
305    
306                            fields.put(field);
307    
308                            StorageEngineUtil.update(storageId, fields, true, serviceContext);
309                    }
310                    finally {
311                            StreamUtil.cleanUp(inputStream);
312                    }
313    
314                    return fieldValue;
315            }
316    
317            protected String storeFieldFile(
318                            BaseModel<?> baseModel, String fieldName, InputStream inputStream,
319                            ServiceContext serviceContext)
320                    throws Exception {
321    
322                    String dirName = getFileUploadPath(baseModel);
323    
324                    try {
325                            DLStoreUtil.addDirectory(
326                                    serviceContext.getCompanyId(), CompanyConstants.SYSTEM,
327                                    dirName);
328                    }
329                    catch (DuplicateDirectoryException dde) {
330                    }
331    
332                    String fileName = dirName + StringPool.SLASH + fieldName;
333    
334                    try {
335                            DLStoreUtil.addFile(
336                                    serviceContext.getCompanyId(), CompanyConstants.SYSTEM,
337                                    fileName, inputStream);
338                    }
339                    catch (DuplicateFileException dfe) {
340                    }
341    
342                    return fileName;
343            }
344    
345    }