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.dynamicdatalists.util;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.templateparser.Transformer;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.xml.Document;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portal.kernel.xml.SAXReaderUtil;
032    import com.liferay.portal.model.Group;
033    import com.liferay.portal.model.PortletConstants;
034    import com.liferay.portal.service.GroupLocalServiceUtil;
035    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
036    import com.liferay.portal.service.ServiceContext;
037    import com.liferay.portal.theme.ThemeDisplay;
038    import com.liferay.portal.util.PortletKeys;
039    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
040    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
041    import com.liferay.portlet.dynamicdatalists.model.DDLRecordConstants;
042    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
043    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
044    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
045    import com.liferay.portlet.dynamicdatalists.service.DDLRecordServiceUtil;
046    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
047    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
048    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
049    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
050    import com.liferay.portlet.dynamicdatamapping.storage.Field;
051    import com.liferay.portlet.dynamicdatamapping.storage.FieldConstants;
052    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
053    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
054    import com.liferay.portlet.dynamicdatamapping.util.DDMImpl;
055    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
056    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
057    import com.liferay.portlet.journal.util.JournalUtil;
058    import com.liferay.util.portlet.PortletRequestUtil;
059    
060    import java.util.Date;
061    import java.util.Iterator;
062    import java.util.List;
063    import java.util.Map;
064    
065    import javax.portlet.PortletPreferences;
066    import javax.portlet.RenderRequest;
067    import javax.portlet.RenderResponse;
068    
069    import javax.servlet.http.HttpServletRequest;
070    import javax.servlet.http.HttpServletResponse;
071    
072    /**
073     * @author Marcelllus Tavares
074     * @author Eduardo Lundgren
075     */
076    public class DDLImpl implements DDL {
077    
078            @Override
079            public void addAllReservedEls(
080                    Element rootElement, Map<String, String> tokens,
081                    DDLRecordSet recordSet) {
082    
083                    JournalUtil.addReservedEl(
084                            rootElement, tokens, DDLConstants.RESERVED_RECORD_SET_ID,
085                            String.valueOf(recordSet.getRecordSetId()));
086    
087                    JournalUtil.addReservedEl(
088                            rootElement, tokens, DDLConstants.RESERVED_RECORD_SET_NAME,
089                            recordSet.getName());
090    
091                    JournalUtil.addReservedEl(
092                            rootElement, tokens, DDLConstants.RESERVED_RECORD_SET_DESCRIPTION,
093                            recordSet.getDescription());
094    
095                    JournalUtil.addReservedEl(
096                            rootElement, tokens, DDLConstants.RESERVED_DDM_STRUCTURE_ID,
097                            String.valueOf(recordSet.getDDMStructureId()));
098            }
099    
100            @Override
101            public JSONObject getRecordJSONObject(DDLRecord record) throws Exception {
102                    return getRecordJSONObject(record, false);
103            }
104    
105            @Override
106            public JSONObject getRecordJSONObject(
107                            DDLRecord record, boolean latestRecordVersion)
108                    throws Exception {
109    
110                    DDLRecordSet recordSet = record.getRecordSet();
111    
112                    DDMStructure ddmStructure = recordSet.getDDMStructure();
113    
114                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
115    
116                    for (String fieldName : ddmStructure.getFieldNames()) {
117                            jsonObject.put(fieldName, StringPool.BLANK);
118                    }
119    
120                    jsonObject.put("displayIndex", record.getDisplayIndex());
121                    jsonObject.put("recordId", record.getRecordId());
122    
123                    DDLRecordVersion recordVersion = record.getRecordVersion();
124    
125                    if (latestRecordVersion) {
126                            recordVersion = record.getLatestRecordVersion();
127                    }
128    
129                    Fields fields = StorageEngineUtil.getFields(
130                            recordVersion.getDDMStorageId());
131    
132                    Iterator<Field> itr = fields.iterator();
133    
134                    while (itr.hasNext()) {
135                            Field field = itr.next();
136    
137                            String fieldName = field.getName();
138                            String fieldType = field.getType();
139                            Object fieldValue = field.getValue();
140    
141                            if (fieldValue instanceof Date) {
142                                    jsonObject.put(fieldName, ((Date)fieldValue).getTime());
143                            }
144                            else if (fieldType.equals(DDMImpl.TYPE_DDM_DOCUMENTLIBRARY) &&
145                                             Validator.isNotNull(fieldValue)) {
146    
147                                    JSONObject fieldValueJSONObject =
148                                            JSONFactoryUtil.createJSONObject(
149                                                    String.valueOf(fieldValue));
150    
151                                    String uuid = fieldValueJSONObject.getString("uuid");
152                                    long groupId = fieldValueJSONObject.getLong("groupId");
153    
154                                    fieldValueJSONObject.put(
155                                            "title", getFileEntryTitle(uuid, groupId));
156    
157                                    jsonObject.put(fieldName, fieldValueJSONObject.toString());
158                            }
159                            else if ((fieldType.equals(DDMImpl.TYPE_RADIO) ||
160                                              fieldType.equals(DDMImpl.TYPE_SELECT)) &&
161                                             Validator.isNotNull(fieldValue)) {
162    
163                                    fieldValue = JSONFactoryUtil.createJSONArray(
164                                            String.valueOf(fieldValue));
165    
166                                    jsonObject.put(fieldName, (JSONArray)fieldValue);
167                            }
168                            else {
169                                    jsonObject.put(fieldName, String.valueOf(fieldValue));
170                            }
171                    }
172    
173                    return jsonObject;
174            }
175    
176            @Override
177            public JSONArray getRecordSetJSONArray(DDLRecordSet recordSet)
178                    throws Exception {
179    
180                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
181    
182                    DDMStructure ddmStructure = recordSet.getDDMStructure();
183    
184                    Map<String, Map<String, String>> fieldsMap =
185                            ddmStructure.getFieldsMap();
186    
187                    for (Map<String, String> fields : fieldsMap.values()) {
188                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
189    
190                            String dataType = fields.get(FieldConstants.DATA_TYPE);
191    
192                            jsonObject.put("dataType", dataType);
193    
194                            boolean editable = GetterUtil.getBoolean(
195                                    fields.get(FieldConstants.EDITABLE), true);
196    
197                            jsonObject.put("editable", editable);
198    
199                            String label = fields.get(FieldConstants.LABEL);
200    
201                            jsonObject.put("label", label);
202    
203                            String name = fields.get(FieldConstants.NAME);
204    
205                            jsonObject.put("name", name);
206    
207                            boolean required = GetterUtil.getBoolean(
208                                    fields.get(FieldConstants.REQUIRED));
209    
210                            jsonObject.put("required", required);
211    
212                            boolean sortable = GetterUtil.getBoolean(
213                                    fields.get(FieldConstants.SORTABLE), true);
214    
215                            jsonObject.put("sortable", sortable);
216    
217                            String type = fields.get(FieldConstants.TYPE);
218    
219                            jsonObject.put("type", type);
220    
221                            jsonArray.put(jsonObject);
222                    }
223    
224                    return jsonArray;
225            }
226    
227            @Override
228            public JSONArray getRecordsJSONArray(DDLRecordSet recordSet)
229                    throws Exception {
230    
231                    return getRecordsJSONArray(recordSet.getRecords(), false);
232            }
233    
234            @Override
235            public JSONArray getRecordsJSONArray(List<DDLRecord> records)
236                    throws Exception {
237    
238                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
239    
240                    for (DDLRecord record : records) {
241                            JSONObject jsonObject = getRecordJSONObject(record);
242    
243                            jsonArray.put(jsonObject);
244                    }
245    
246                    return jsonArray;
247            }
248    
249            @Override
250            public JSONArray getRecordsJSONArray(
251                            List<DDLRecord> records, boolean latestRecordVersion)
252                    throws Exception {
253    
254                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
255    
256                    for (DDLRecord record : records) {
257                            JSONObject jsonObject = getRecordJSONObject(
258                                    record, latestRecordVersion);
259    
260                            jsonArray.put(jsonObject);
261                    }
262    
263                    return jsonArray;
264            }
265    
266            @Override
267            public String getTemplateContent(
268                            long ddmTemplateId, DDLRecordSet recordSet,
269                            ThemeDisplay themeDisplay, RenderRequest renderRequest,
270                            RenderResponse renderResponse)
271                    throws Exception {
272    
273                    String viewMode = ParamUtil.getString(renderRequest, "viewMode");
274    
275                    String languageId = LanguageUtil.getLanguageId(renderRequest);
276    
277                    String xmlRequest = PortletRequestUtil.toXML(
278                            renderRequest, renderResponse);
279    
280                    if (Validator.isNull(xmlRequest)) {
281                            xmlRequest = "<request />";
282                    }
283    
284                    Map<String, String> tokens = JournalUtil.getTokens(
285                            recordSet.getGroupId(), themeDisplay, xmlRequest);
286    
287                    tokens.put("template_id", StringUtil.valueOf(ddmTemplateId));
288    
289                    String xml = StringPool.BLANK;
290    
291                    Document document = SAXReaderUtil.createDocument();
292    
293                    Element rootElement = document.addElement("root");
294    
295                    Document requestDocument = SAXReaderUtil.read(xmlRequest);
296    
297                    rootElement.add(requestDocument.getRootElement().createCopy());
298    
299                    addAllReservedEls(rootElement, tokens, recordSet);
300    
301                    xml = DDMXMLUtil.formatXML(document);
302    
303                    DDMTemplate template = DDMTemplateLocalServiceUtil.getTemplate(
304                            ddmTemplateId);
305    
306                    return _transformer.transform(
307                            themeDisplay, tokens, viewMode, languageId, xml,
308                            template.getScript(), template.getLanguage());
309            }
310    
311            @Override
312            public boolean isEditable(
313                            HttpServletRequest request, String portletId, long groupId)
314                    throws Exception {
315    
316                    boolean defaultValue = ParamUtil.getBoolean(request, "editable", true);
317    
318                    return isEditable(portletId, groupId, defaultValue);
319            }
320    
321            @Override
322            public boolean isEditable(
323                            PortletPreferences preferences, String portletId, long groupId)
324                    throws Exception {
325    
326                    boolean defaultValue = GetterUtil.getBoolean(
327                            preferences.getValue("editable", null), true);
328    
329                    return isEditable(portletId, groupId, defaultValue);
330            }
331    
332            @Override
333            public void sendRecordFileUpload(
334                            HttpServletRequest request, HttpServletResponse response,
335                            DDLRecord record, String fieldName)
336                    throws Exception {
337    
338                    Field field = record.getField(fieldName);
339    
340                    DDMUtil.sendFieldFile(request, response, field);
341            }
342    
343            @Override
344            public void sendRecordFileUpload(
345                            HttpServletRequest request, HttpServletResponse response,
346                            long recordId, String fieldName)
347                    throws Exception {
348    
349                    DDLRecord record = DDLRecordServiceUtil.getRecord(recordId);
350    
351                    sendRecordFileUpload(request, response, record, fieldName);
352            }
353    
354            @Override
355            public DDLRecord updateRecord(
356                            long recordId, long recordSetId, boolean mergeFields,
357                            boolean checkPermission, ServiceContext serviceContext)
358                    throws Exception {
359    
360                    DDLRecord record = DDLRecordLocalServiceUtil.fetchRecord(recordId);
361    
362                    PortletPreferences preferences =
363                            PortletPreferencesLocalServiceUtil.getPreferences(
364                                    serviceContext.getPortletPreferencesIds());
365    
366                    if (!isEditable(
367                                    preferences, serviceContext.getPortletId(),
368                                    serviceContext.getScopeGroupId())) {
369    
370                            return record;
371                    }
372    
373                    boolean majorVersion = ParamUtil.getBoolean(
374                            serviceContext, "majorVersion");
375    
376                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getDDLRecordSet(
377                            recordSetId);
378    
379                    DDMStructure ddmStructure = recordSet.getDDMStructure();
380    
381                    Fields fields = DDMUtil.getFields(
382                            ddmStructure.getStructureId(), serviceContext);
383    
384                    if (record != null) {
385                            if (checkPermission) {
386                                    record = DDLRecordServiceUtil.updateRecord(
387                                            recordId, majorVersion,
388                                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields,
389                                            mergeFields, serviceContext);
390                            }
391                            else {
392                                    record = DDLRecordLocalServiceUtil.updateRecord(
393                                            serviceContext.getUserId(), recordId, majorVersion,
394                                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields,
395                                            mergeFields, serviceContext);
396                            }
397                    }
398                    else {
399                            if (checkPermission) {
400                                    record = DDLRecordServiceUtil.addRecord(
401                                            serviceContext.getScopeGroupId(), recordSetId,
402                                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields,
403                                            serviceContext);
404                            }
405                            else {
406                                    record = DDLRecordLocalServiceUtil.addRecord(
407                                            serviceContext.getUserId(),
408                                            serviceContext.getScopeGroupId(), recordSetId,
409                                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields,
410                                            serviceContext);
411                            }
412    
413                    }
414    
415                    uploadRecordFieldFiles(record, serviceContext);
416    
417                    return record;
418            }
419    
420            @Override
421            public DDLRecord updateRecord(
422                            long recordId, long recordSetId, boolean mergeFields,
423                            ServiceContext serviceContext)
424                    throws Exception {
425    
426                    return updateRecord(
427                            recordId, recordSetId, mergeFields, true, serviceContext);
428            }
429    
430            @Override
431            public String uploadRecordFieldFile(
432                            DDLRecord record, String fieldName, ServiceContext serviceContext)
433                    throws Exception {
434    
435                    DDLRecordSet recordSet = record.getRecordSet();
436    
437                    DDMStructure ddmStructure = recordSet.getDDMStructure();
438    
439                    DDLRecordVersion recordVersion = record.getLatestRecordVersion();
440    
441                    return DDMUtil.uploadFieldFile(
442                            ddmStructure.getStructureId(), recordVersion.getDDMStorageId(),
443                            record, fieldName, serviceContext);
444            }
445    
446            protected String getFileEntryTitle(String uuid, long groupId) {
447                    try {
448                            FileEntry fileEntry =
449                                    DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
450                                            uuid, groupId);
451    
452                            return fileEntry.getTitle();
453                    }
454                    catch (Exception e) {
455                            return LanguageUtil.format(
456                                    LocaleUtil.getDefault(), "is-temporarily-unavailable",
457                                    "content");
458                    }
459            }
460    
461            protected boolean isEditable(
462                            String portletId, long groupId, boolean defaultValue)
463                    throws Exception {
464    
465                    String rootPortletId = PortletConstants.getRootPortletId(portletId);
466    
467                    if (rootPortletId.equals(PortletKeys.DYNAMIC_DATA_LISTS)) {
468                            return true;
469                    }
470    
471                    Group group = GroupLocalServiceUtil.fetchGroup(groupId);
472    
473                    if ((group == null) || group.isInStagingPortlet(portletId)) {
474                            return false;
475                    }
476    
477                    return defaultValue;
478            }
479    
480            protected void uploadRecordFieldFiles(
481                            DDLRecord record, ServiceContext serviceContext)
482                    throws Exception {
483    
484                    DDLRecordSet recordSet = record.getRecordSet();
485    
486                    DDMStructure ddmStructure = recordSet.getDDMStructure();
487    
488                    for (String fieldName : ddmStructure.getFieldNames()) {
489                            String fieldDataType = ddmStructure.getFieldDataType(fieldName);
490    
491                            if (fieldDataType.equals(FieldConstants.FILE_UPLOAD)) {
492                                    uploadRecordFieldFile(record, fieldName, serviceContext);
493                            }
494                    }
495            }
496    
497            private Transformer _transformer = new DDLTransformer();
498    
499    }