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