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.dynamicdatalists.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.search.Hits;
023    import com.liferay.portal.kernel.search.Indexer;
024    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.util.ContentTypes;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.ListUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
035    import com.liferay.portal.model.CompanyConstants;
036    import com.liferay.portal.model.User;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
039    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
040    import com.liferay.portlet.documentlibrary.util.DLUtil;
041    import com.liferay.portlet.dynamicdatalists.NoSuchRecordVersionException;
042    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
043    import com.liferay.portlet.dynamicdatalists.model.DDLRecordConstants;
044    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
045    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
046    import com.liferay.portlet.dynamicdatalists.service.base.DDLRecordLocalServiceBaseImpl;
047    import com.liferay.portlet.dynamicdatalists.util.comparator.DDLRecordVersionVersionComparator;
048    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
049    import com.liferay.portlet.dynamicdatamapping.storage.Field;
050    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
051    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
052    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
053    
054    import java.io.Serializable;
055    
056    import java.util.Collections;
057    import java.util.Date;
058    import java.util.Iterator;
059    import java.util.List;
060    import java.util.Locale;
061    import java.util.Map;
062    
063    /**
064     * @author Marcellus Tavares
065     * @author Eduardo Lundgren
066     */
067    public class DDLRecordLocalServiceImpl extends DDLRecordLocalServiceBaseImpl {
068    
069            public DDLRecord addRecord(
070                            long userId, long groupId, long recordSetId, int displayIndex,
071                            Fields fields, ServiceContext serviceContext)
072                    throws PortalException, SystemException {
073    
074                    // Record
075    
076                    User user = userPersistence.findByPrimaryKey(userId);
077                    Date now = new Date();
078    
079                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
080                            recordSetId);
081    
082                    long recordId = counterLocalService.increment();
083    
084                    DDLRecord record = ddlRecordPersistence.create(recordId);
085    
086                    record.setUuid(serviceContext.getUuid());
087                    record.setGroupId(groupId);
088                    record.setCompanyId(user.getCompanyId());
089                    record.setUserId(user.getUserId());
090                    record.setUserName(user.getFullName());
091                    record.setVersionUserId(user.getUserId());
092                    record.setVersionUserName(user.getFullName());
093                    record.setCreateDate(serviceContext.getCreateDate(now));
094                    record.setModifiedDate(serviceContext.getModifiedDate(now));
095    
096                    long ddmStorageId = StorageEngineUtil.create(
097                            recordSet.getCompanyId(), recordSet.getDDMStructureId(), fields,
098                            serviceContext);
099    
100                    record.setDDMStorageId(ddmStorageId);
101    
102                    record.setRecordSetId(recordSetId);
103                    record.setVersion(DDLRecordConstants.VERSION_DEFAULT);
104                    record.setDisplayIndex(displayIndex);
105    
106                    ddlRecordPersistence.update(record);
107    
108                    // Record version
109    
110                    DDLRecordVersion recordVersion = addRecordVersion(
111                            user, record, ddmStorageId, DDLRecordConstants.VERSION_DEFAULT,
112                            displayIndex, WorkflowConstants.STATUS_DRAFT);
113    
114                    // Asset
115    
116                    Locale locale = serviceContext.getLocale();
117    
118                    updateAsset(
119                            userId, record, recordVersion, serviceContext.getAssetCategoryIds(),
120                            serviceContext.getAssetTagNames(), locale);
121    
122                    // Workflow
123    
124                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
125                            user.getCompanyId(), groupId, userId, DDLRecord.class.getName(),
126                            recordVersion.getRecordVersionId(), recordVersion, serviceContext);
127    
128                    return record;
129            }
130    
131            public DDLRecord addRecord(
132                            long userId, long groupId, long recordSetId, int displayIndex,
133                            Map<String, Serializable> fieldsMap, ServiceContext serviceContext)
134                    throws PortalException, SystemException {
135    
136                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
137                            recordSetId);
138    
139                    DDMStructure ddmStructure = recordSet.getDDMStructure();
140    
141                    Fields fields = toFields(ddmStructure.getStructureId(), fieldsMap);
142    
143                    return addRecord(
144                            userId, groupId, recordSetId, displayIndex, fields, serviceContext);
145            }
146    
147            public void deleteRecord(DDLRecord record)
148                    throws PortalException, SystemException {
149    
150                    // Record
151    
152                    String dirName = DDMUtil.getFileUploadPath(record);
153    
154                    ddlRecordPersistence.remove(record);
155    
156                    // Record Versions
157    
158                    List<DDLRecordVersion> recordVersions =
159                            ddlRecordVersionPersistence.findByRecordId(record.getRecordId());
160    
161                    for (DDLRecordVersion recordVersion : recordVersions) {
162                            ddlRecordVersionPersistence.remove(recordVersion);
163    
164                            // Dynamic data mapping storage
165    
166                            StorageEngineUtil.deleteByClass(recordVersion.getDDMStorageId());
167    
168                            // Workflow
169    
170                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
171                                    record.getCompanyId(), record.getGroupId(),
172                                    DDLRecord.class.getName(), recordVersion.getPrimaryKey());
173                    }
174    
175                    // Indexer
176    
177                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
178                            DDLRecord.class);
179    
180                    indexer.delete(record);
181    
182                    // Document library
183    
184                    try {
185                            DLStoreUtil.deleteDirectory(
186                                    record.getCompanyId(), CompanyConstants.SYSTEM, dirName);
187                    }
188                    catch (NoSuchDirectoryException nsde) {
189                            if (_log.isDebugEnabled()) {
190                                    _log.debug(nsde.getMessage());
191                            }
192                    }
193            }
194    
195            public void deleteRecord(long recordId)
196                    throws PortalException, SystemException {
197    
198                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
199    
200                    deleteRecord(record);
201            }
202    
203            public DDLRecord deleteRecordLocale(
204                            long recordId, Locale locale, ServiceContext serviceContext)
205                    throws PortalException, SystemException {
206    
207                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
208    
209                    Fields fields = StorageEngineUtil.getFields(record.getDDMStorageId());
210    
211                    Iterator<Field> itr = fields.iterator();
212    
213                    while (itr.hasNext()) {
214                            Field field = itr.next();
215    
216                            Map<Locale, List<Serializable>> valuesMap = field.getValuesMap();
217    
218                            valuesMap.remove(locale);
219                    }
220    
221                    return updateRecord(
222                            serviceContext.getUserId(), recordId, false,
223                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields, false,
224                            serviceContext);
225            }
226    
227            public void deleteRecords(long recordSetId)
228                    throws PortalException, SystemException {
229    
230                    List<DDLRecord> records = ddlRecordPersistence.findByRecordSetId(
231                            recordSetId);
232    
233                    for (DDLRecord record : records) {
234                            deleteRecord(record);
235                    }
236            }
237    
238            public DDLRecord fetchRecord(long recordId) throws SystemException {
239                    return ddlRecordPersistence.fetchByPrimaryKey(recordId);
240            }
241    
242            public List<DDLRecord> getCompanyRecords(
243                            long companyId, int status, int scope, int start, int end,
244                            OrderByComparator orderByComparator)
245                    throws SystemException {
246    
247                    return ddlRecordFinder.findByC_S_S(
248                            companyId, status, scope, start, end, orderByComparator);
249            }
250    
251            /**
252             * @deprecated {@link #getCompanyRecords(long, int, int, int, int,
253             *             OrderByComparator)}
254             */
255            public List<DDLRecord> getCompanyRecords(
256                            long companyId, int scope, int start, int end,
257                            OrderByComparator orderByComparator)
258                    throws SystemException {
259    
260                    return getCompanyRecords(
261                            companyId, WorkflowConstants.STATUS_ANY, scope, start, end,
262                            orderByComparator);
263            }
264    
265            /**
266             * @deprecated {@link #getCompanyRecordsCount(long, int, int)}
267             */
268            public int getCompanyRecordsCount(long companyId, int scope)
269                    throws SystemException {
270    
271                    return getCompanyRecordsCount(
272                            companyId, WorkflowConstants.STATUS_ANY, scope);
273            }
274    
275            public int getCompanyRecordsCount(long companyId, int status, int scope)
276                    throws SystemException {
277    
278                    return ddlRecordFinder.countByC_S_S(companyId, status, scope);
279            }
280    
281            public DDLRecordVersion getLatestRecordVersion(long recordId)
282                    throws PortalException, SystemException {
283    
284                    List<DDLRecordVersion> recordVersions =
285                            ddlRecordVersionPersistence.findByRecordId(recordId);
286    
287                    if (recordVersions.isEmpty()) {
288                            throw new NoSuchRecordVersionException(
289                                    "No record versions found for recordId " + recordId);
290                    }
291    
292                    recordVersions = ListUtil.copy(recordVersions);
293    
294                    Collections.sort(
295                            recordVersions, new DDLRecordVersionVersionComparator());
296    
297                    return recordVersions.get(0);
298            }
299    
300            public Long[] getMinAndMaxCompanyRecordIds(
301                            long companyId, int status, int scope)
302                    throws SystemException {
303    
304                    return ddlRecordFinder.findByC_S_S_MinAndMax(companyId, status, scope);
305            }
306    
307            public List<DDLRecord> getMinAndMaxCompanyRecords(
308                            long companyId, int status, int scope, long minRecordId,
309                            long maxRecordId)
310                    throws SystemException {
311    
312                    return ddlRecordFinder.findByC_S_S_MinAndMax(
313                            companyId, status, scope, minRecordId, maxRecordId);
314            }
315    
316            public DDLRecord getRecord(long recordId)
317                    throws PortalException, SystemException {
318    
319                    return ddlRecordPersistence.findByPrimaryKey(recordId);
320            }
321    
322            public List<DDLRecord> getRecords(long recordSetId) throws SystemException {
323                    return ddlRecordPersistence.findByRecordSetId(recordSetId);
324            }
325    
326            public List<DDLRecord> getRecords(
327                            long recordSetId, int status, int start, int end,
328                            OrderByComparator orderByComparator)
329                    throws SystemException {
330    
331                    return ddlRecordFinder.findByR_S(
332                            recordSetId, status, start, end, orderByComparator);
333            }
334    
335            public List<DDLRecord> getRecords(long recordSetId, long userId)
336                    throws SystemException {
337    
338                    return ddlRecordPersistence.findByR_U(recordSetId, userId);
339            }
340    
341            public int getRecordsCount(long recordSetId, int status)
342                    throws SystemException {
343    
344                    return ddlRecordFinder.countByR_S(recordSetId, status);
345            }
346    
347            public DDLRecordVersion getRecordVersion(long recordVersionId)
348                    throws PortalException, SystemException {
349    
350                    return ddlRecordVersionPersistence.findByPrimaryKey(recordVersionId);
351            }
352    
353            public DDLRecordVersion getRecordVersion(long recordId, String version)
354                    throws PortalException, SystemException {
355    
356                    return ddlRecordVersionPersistence.findByR_V(recordId, version);
357            }
358    
359            public List<DDLRecordVersion> getRecordVersions(
360                            long recordId, int start, int end,
361                            OrderByComparator orderByComparator)
362                    throws SystemException {
363    
364                    return ddlRecordVersionPersistence.findByRecordId(
365                            recordId, start, end, orderByComparator);
366            }
367    
368            public int getRecordVersionsCount(long recordId) throws SystemException {
369                    return ddlRecordVersionPersistence.countByRecordId(recordId);
370            }
371    
372            public void revertRecordVersion(
373                            long userId, long recordId, String version,
374                            ServiceContext serviceContext)
375                    throws PortalException, SystemException {
376    
377                    DDLRecordVersion recordVersion = getRecordVersion(recordId, version);
378    
379                    if (!recordVersion.isApproved()) {
380                            return;
381                    }
382    
383                    Fields fields = StorageEngineUtil.getFields(
384                            recordVersion.getDDMStorageId());
385    
386                    updateRecord(
387                            userId, recordId, true, recordVersion.getDisplayIndex(), fields,
388                            false, serviceContext);
389            }
390    
391            public Hits search(SearchContext searchContext) throws SystemException {
392                    try {
393                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
394                                    DDLRecord.class);
395    
396                            return indexer.search(searchContext);
397                    }
398                    catch (Exception e) {
399                            throw new SystemException(e);
400                    }
401            }
402    
403            public void updateAsset(
404                            long userId, DDLRecord record, DDLRecordVersion recordVersion,
405                            long[] assetCategoryIds, String[] assetTagNames, Locale locale)
406                    throws PortalException, SystemException {
407    
408                    boolean addDraftAssetEntry = false;
409    
410                    if ((recordVersion != null) && !recordVersion.isApproved()) {
411                            String version = recordVersion.getVersion();
412    
413                            if (!version.equals(DDLRecordConstants.VERSION_DEFAULT)) {
414                                    int approvedRecordVersionsCount =
415                                            ddlRecordVersionPersistence.countByR_S(
416                                                    record.getRecordId(),
417                                                    WorkflowConstants.STATUS_APPROVED);
418    
419                                    if (approvedRecordVersionsCount > 0) {
420                                            addDraftAssetEntry = true;
421                                    }
422                            }
423                    }
424    
425                    boolean visible = false;
426    
427                    if ((recordVersion != null) && !recordVersion.isApproved()) {
428                            visible = false;
429                    }
430    
431                    DDLRecordSet recordSet = record.getRecordSet();
432    
433                    String title = LanguageUtil.format(
434                            locale, "new-record-for-list-x", recordSet.getName(locale));
435    
436                    if (addDraftAssetEntry) {
437                            assetEntryLocalService.updateEntry(
438                                    userId, record.getGroupId(), record.getCreateDate(),
439                                    record.getModifiedDate(), DDLRecordConstants.getClassName(),
440                                    recordVersion.getRecordVersionId(), record.getUuid(), 0,
441                                    assetCategoryIds, assetTagNames, false, null, null, null,
442                                    ContentTypes.TEXT_HTML, title, null, StringPool.BLANK, null,
443                                    null, 0, 0, null, false);
444                    }
445                    else {
446                            assetEntryLocalService.updateEntry(
447                                    userId, record.getGroupId(), record.getCreateDate(),
448                                    record.getModifiedDate(), DDLRecordConstants.getClassName(),
449                                    record.getRecordId(), record.getUuid(), 0, assetCategoryIds,
450                                    assetTagNames, visible, null, null, null,
451                                    ContentTypes.TEXT_HTML, title, null, StringPool.BLANK, null,
452                                    null, 0, 0, null, false);
453                    }
454            }
455    
456            public DDLRecord updateRecord(
457                            long userId, long recordId, boolean majorVersion, int displayIndex,
458                            Fields fields, boolean mergeFields, ServiceContext serviceContext)
459                    throws PortalException, SystemException {
460    
461                    // Record
462    
463                    User user = userPersistence.findByPrimaryKey(userId);
464    
465                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
466    
467                    record.setModifiedDate(serviceContext.getModifiedDate(null));
468    
469                    ddlRecordPersistence.update(record);
470    
471                    // Record version
472    
473                    DDLRecordVersion recordVersion = record.getLatestRecordVersion();
474    
475                    if (recordVersion.isApproved()) {
476                            DDLRecordSet recordSet = record.getRecordSet();
477    
478                            if (mergeFields) {
479                                    Fields existingFields = StorageEngineUtil.getFields(
480                                            recordVersion.getDDMStorageId());
481    
482                                    fields = DDMUtil.mergeFields(fields, existingFields);
483                            }
484    
485                            long ddmStorageId = StorageEngineUtil.create(
486                                    recordSet.getCompanyId(), recordSet.getDDMStructureId(), fields,
487                                    serviceContext);
488                            String version = getNextVersion(
489                                    recordVersion.getVersion(), majorVersion,
490                                    serviceContext.getWorkflowAction());
491    
492                            recordVersion = addRecordVersion(
493                                    user, record, ddmStorageId, version, displayIndex,
494                                    WorkflowConstants.STATUS_DRAFT);
495                    }
496                    else {
497                            StorageEngineUtil.update(
498                                    recordVersion.getDDMStorageId(), fields, mergeFields,
499                                    serviceContext);
500    
501                            String version = recordVersion.getVersion();
502    
503                            updateRecordVersion(
504                                    user, recordVersion, version, displayIndex,
505                                    recordVersion.getStatus(), serviceContext);
506                    }
507    
508                    // Workflow
509    
510                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
511                            user.getCompanyId(), record.getGroupId(), userId,
512                            DDLRecord.class.getName(), recordVersion.getRecordVersionId(),
513                            recordVersion, serviceContext);
514    
515                    return record;
516            }
517    
518            public DDLRecord updateRecord(
519                            long userId, long recordId, int displayIndex,
520                            Map<String, Serializable> fieldsMap, boolean mergeFields,
521                            ServiceContext serviceContext)
522                    throws PortalException, SystemException {
523    
524                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
525    
526                    DDLRecordSet recordSet = record.getRecordSet();
527    
528                    DDMStructure ddmStructure = recordSet.getDDMStructure();
529    
530                    Fields fields = toFields(ddmStructure.getStructureId(), fieldsMap);
531    
532                    return updateRecord(
533                            userId, recordId, false, displayIndex, fields, mergeFields,
534                            serviceContext);
535            }
536    
537            public DDLRecord updateStatus(
538                            long userId, long recordVersionId, int status,
539                            ServiceContext serviceContext)
540                    throws PortalException, SystemException {
541    
542                    // Record version
543    
544                    User user = userPersistence.findByPrimaryKey(userId);
545    
546                    DDLRecordVersion recordVersion =
547                            ddlRecordVersionPersistence.findByPrimaryKey(recordVersionId);
548    
549                    recordVersion.setStatus(status);
550                    recordVersion.setStatusByUserId(user.getUserId());
551                    recordVersion.setStatusByUserName(user.getFullName());
552                    recordVersion.setStatusDate(new Date());
553    
554                    ddlRecordVersionPersistence.update(recordVersion);
555    
556                    // Record
557    
558                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(
559                            recordVersion.getRecordId());
560    
561                    if (status == WorkflowConstants.STATUS_APPROVED) {
562                            if (DLUtil.compareVersions(
563                                            record.getVersion(), recordVersion.getVersion()) <= 0) {
564    
565                                    record.setDDMStorageId(recordVersion.getDDMStorageId());
566                                    record.setVersion(recordVersion.getVersion());
567                                    record.setRecordSetId(recordVersion.getRecordSetId());
568                                    record.setDisplayIndex(recordVersion.getDisplayIndex());
569                                    record.setVersion(recordVersion.getVersion());
570                                    record.setVersionUserId(recordVersion.getUserId());
571                                    record.setVersionUserName(recordVersion.getUserName());
572                                    record.setModifiedDate(recordVersion.getCreateDate());
573    
574                                    ddlRecordPersistence.update(record);
575                            }
576                    }
577                    else {
578                            if (Validator.equals(
579                                            record.getVersion(), recordVersion.getVersion())) {
580    
581                                    String newVersion = DDLRecordConstants.VERSION_DEFAULT;
582    
583                                    List<DDLRecordVersion> approvedRecordVersions =
584                                            ddlRecordVersionPersistence.findByR_S(
585                                                    record.getRecordId(),
586                                                    WorkflowConstants.STATUS_APPROVED);
587    
588                                    if (!approvedRecordVersions.isEmpty()) {
589                                            newVersion = approvedRecordVersions.get(0).getVersion();
590                                    }
591    
592                                    record.setVersion(newVersion);
593    
594                                    ddlRecordPersistence.update(record);
595                            }
596    
597                            // Indexer
598    
599                            if (Validator.equals(
600                                            recordVersion.getVersion(),
601                                            DDLRecordConstants.VERSION_DEFAULT)) {
602    
603                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
604                                            DDLRecord.class);
605    
606                                    indexer.delete(record);
607                            }
608                    }
609    
610                    // Indexer
611    
612                    if (status == WorkflowConstants.STATUS_APPROVED) {
613                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
614                                    DDLRecord.class);
615    
616                            indexer.reindex(record);
617                    }
618    
619                    return record;
620            }
621    
622            protected DDLRecordVersion addRecordVersion(
623                            User user, DDLRecord record, long ddmStorageId, String version,
624                            int displayIndex, int status)
625                    throws SystemException {
626    
627                    long recordVersionId = counterLocalService.increment();
628    
629                    DDLRecordVersion recordVersion = ddlRecordVersionPersistence.create(
630                            recordVersionId);
631    
632                    recordVersion.setGroupId(record.getGroupId());
633                    recordVersion.setCompanyId(record.getCompanyId());
634    
635                    long versionUserId = record.getVersionUserId();
636    
637                    if (versionUserId <= 0) {
638                            versionUserId = record.getUserId();
639                    }
640    
641                    recordVersion.setUserId(versionUserId);
642    
643                    String versionUserName = GetterUtil.getString(
644                            record.getVersionUserName(), record.getUserName());
645    
646                    recordVersion.setUserName(versionUserName);
647    
648                    recordVersion.setCreateDate(record.getModifiedDate());
649                    recordVersion.setDDMStorageId(ddmStorageId);
650                    recordVersion.setRecordSetId(record.getRecordSetId());
651                    recordVersion.setRecordId(record.getRecordId());
652                    recordVersion.setVersion(version);
653                    recordVersion.setDisplayIndex(displayIndex);
654                    recordVersion.setStatus(status);
655                    recordVersion.setStatusByUserId(user.getUserId());
656                    recordVersion.setStatusByUserName(user.getFullName());
657                    recordVersion.setStatusDate(record.getModifiedDate());
658    
659                    ddlRecordVersionPersistence.update(recordVersion);
660    
661                    return recordVersion;
662            }
663    
664            protected String getNextVersion(
665                    String version, boolean majorVersion, int workflowAction) {
666    
667                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
668                            majorVersion = false;
669                    }
670    
671                    int[] versionParts = StringUtil.split(version, StringPool.PERIOD, 0);
672    
673                    if (majorVersion) {
674                            versionParts[0]++;
675                            versionParts[1] = 0;
676                    }
677                    else {
678                            versionParts[1]++;
679                    }
680    
681                    return versionParts[0] + StringPool.PERIOD + versionParts[1];
682            }
683    
684            protected Fields toFields(
685                    long ddmStructureId, Map<String, Serializable> fieldsMap) {
686    
687                    Fields fields = new Fields();
688    
689                    for (String name : fieldsMap.keySet()) {
690                            String value = String.valueOf(fieldsMap.get(name));
691    
692                            Field field = new Field(ddmStructureId, name, value);
693    
694                            fields.put(field);
695                    }
696    
697                    return fields;
698            }
699    
700            protected void updateRecordVersion(
701                            User user, DDLRecordVersion recordVersion, String version,
702                            int displayIndex, int status, ServiceContext serviceContext)
703                    throws SystemException {
704    
705                    recordVersion.setVersion(version);
706                    recordVersion.setDisplayIndex(displayIndex);
707                    recordVersion.setStatus(status);
708                    recordVersion.setStatusByUserId(user.getUserId());
709                    recordVersion.setStatusByUserName(user.getFullName());
710                    recordVersion.setStatusDate(serviceContext.getModifiedDate(null));
711    
712                    ddlRecordVersionPersistence.update(recordVersion);
713            }
714    
715            private static Log _log = LogFactoryUtil.getLog(
716                    DDLRecordLocalServiceImpl.class);
717    
718    }