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