001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.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.search.Hits;
021    import com.liferay.portal.kernel.search.Indexable;
022    import com.liferay.portal.kernel.search.IndexableType;
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.ListUtil;
028    import com.liferay.portal.kernel.util.LocaleUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.StringUtil;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.workflow.WorkflowConstants;
035    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
036    import com.liferay.portal.model.User;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portlet.documentlibrary.util.DLUtil;
039    import com.liferay.portlet.dynamicdatalists.NoSuchRecordVersionException;
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.base.DDLRecordLocalServiceBaseImpl;
045    import com.liferay.portlet.dynamicdatalists.util.comparator.DDLRecordVersionVersionComparator;
046    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
047    import com.liferay.portlet.dynamicdatamapping.storage.Field;
048    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
049    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
050    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
051    
052    import java.io.Serializable;
053    
054    import java.util.ArrayList;
055    import java.util.Collection;
056    import java.util.Collections;
057    import java.util.Date;
058    import java.util.List;
059    import java.util.Locale;
060    import java.util.Map;
061    
062    /**
063     * Provides the local service for accessing, adding, deleting, and updating
064     * dynamic data list (DDL) records.
065     *
066     * @author Marcellus Tavares
067     * @author Eduardo Lundgren
068     */
069    public class DDLRecordLocalServiceImpl extends DDLRecordLocalServiceBaseImpl {
070    
071            @Indexable(type = IndexableType.REINDEX)
072            @Override
073            public DDLRecord addRecord(
074                            long userId, long groupId, long recordSetId, int displayIndex,
075                            Fields fields, ServiceContext serviceContext)
076                    throws PortalException, SystemException {
077    
078                    // Record
079    
080                    User user = userPersistence.findByPrimaryKey(userId);
081                    Date now = new Date();
082    
083                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
084                            recordSetId);
085    
086                    long recordId = counterLocalService.increment();
087    
088                    DDLRecord record = ddlRecordPersistence.create(recordId);
089    
090                    record.setUuid(serviceContext.getUuid());
091                    record.setGroupId(groupId);
092                    record.setCompanyId(user.getCompanyId());
093                    record.setUserId(user.getUserId());
094                    record.setUserName(user.getFullName());
095                    record.setVersionUserId(user.getUserId());
096                    record.setVersionUserName(user.getFullName());
097                    record.setCreateDate(serviceContext.getCreateDate(now));
098                    record.setModifiedDate(serviceContext.getModifiedDate(now));
099    
100                    long ddmStorageId = StorageEngineUtil.create(
101                            recordSet.getCompanyId(), recordSet.getDDMStructureId(), fields,
102                            serviceContext);
103    
104                    record.setDDMStorageId(ddmStorageId);
105    
106                    record.setRecordSetId(recordSetId);
107                    record.setVersion(DDLRecordConstants.VERSION_DEFAULT);
108                    record.setDisplayIndex(displayIndex);
109    
110                    ddlRecordPersistence.update(record);
111    
112                    // Record version
113    
114                    DDLRecordVersion recordVersion = addRecordVersion(
115                            user, record, ddmStorageId, DDLRecordConstants.VERSION_DEFAULT,
116                            displayIndex, WorkflowConstants.STATUS_DRAFT);
117    
118                    // Asset
119    
120                    Locale locale = serviceContext.getLocale();
121    
122                    updateAsset(
123                            userId, record, recordVersion, serviceContext.getAssetCategoryIds(),
124                            serviceContext.getAssetTagNames(), locale);
125    
126                    // Workflow
127    
128                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
129                            user.getCompanyId(), groupId, userId, DDLRecord.class.getName(),
130                            recordVersion.getRecordVersionId(), recordVersion, serviceContext);
131    
132                    return record;
133            }
134    
135            @Override
136            public DDLRecord addRecord(
137                            long userId, long groupId, long recordSetId, int displayIndex,
138                            Map<String, Serializable> fieldsMap, ServiceContext serviceContext)
139                    throws PortalException, SystemException {
140    
141                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
142                            recordSetId);
143    
144                    DDMStructure ddmStructure = recordSet.getDDMStructure();
145    
146                    Fields fields = toFields(
147                            ddmStructure.getStructureId(), fieldsMap,
148                            serviceContext.getLocale(), LocaleUtil.getSiteDefault());
149    
150                    return ddlRecordLocalService.addRecord(
151                            userId, groupId, recordSetId, displayIndex, fields, serviceContext);
152            }
153    
154            @Override
155            public void deleteRecord(DDLRecord record)
156                    throws PortalException, SystemException {
157    
158                    // Record
159    
160                    ddlRecordPersistence.remove(record);
161    
162                    // Record Versions
163    
164                    List<DDLRecordVersion> recordVersions =
165                            ddlRecordVersionPersistence.findByRecordId(record.getRecordId());
166    
167                    for (DDLRecordVersion recordVersion : recordVersions) {
168                            ddlRecordVersionPersistence.remove(recordVersion);
169    
170                            // Dynamic data mapping storage
171    
172                            StorageEngineUtil.deleteByClass(recordVersion.getDDMStorageId());
173    
174                            // Workflow
175    
176                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
177                                    record.getCompanyId(), record.getGroupId(),
178                                    DDLRecord.class.getName(), recordVersion.getPrimaryKey());
179                    }
180    
181                    // Asset
182    
183                    assetEntryLocalService.deleteEntry(
184                            DDLRecord.class.getName(), record.getRecordId());
185    
186                    // Indexer
187    
188                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
189                            DDLRecord.class);
190    
191                    indexer.delete(record);
192            }
193    
194            @Override
195            public void deleteRecord(long recordId)
196                    throws PortalException, SystemException {
197    
198                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
199    
200                    deleteRecord(record);
201            }
202    
203            @Override
204            public DDLRecord deleteRecordLocale(
205                            long recordId, Locale locale, ServiceContext serviceContext)
206                    throws PortalException, SystemException {
207    
208                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
209    
210                    Fields fields = StorageEngineUtil.getFields(record.getDDMStorageId());
211    
212                    for (Field field : fields) {
213                            Map<Locale, List<Serializable>> valuesMap = field.getValuesMap();
214    
215                            valuesMap.remove(locale);
216                    }
217    
218                    return ddlRecordLocalService.updateRecord(
219                            serviceContext.getUserId(), recordId, false,
220                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields, false,
221                            serviceContext);
222            }
223    
224            @Override
225            public void deleteRecords(long recordSetId)
226                    throws PortalException, SystemException {
227    
228                    List<DDLRecord> records = ddlRecordPersistence.findByRecordSetId(
229                            recordSetId);
230    
231                    for (DDLRecord record : records) {
232                            deleteRecord(record);
233                    }
234            }
235    
236            @Override
237            public DDLRecord fetchRecord(long recordId) throws SystemException {
238                    return ddlRecordPersistence.fetchByPrimaryKey(recordId);
239            }
240    
241            @Override
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 As of 6.2.0, replaced by {@link #getCompanyRecords(long, int,
253             *             int, int, int, OrderByComparator)}
254             */
255            @Override
256            public List<DDLRecord> getCompanyRecords(
257                            long companyId, int scope, int start, int end,
258                            OrderByComparator orderByComparator)
259                    throws SystemException {
260    
261                    return getCompanyRecords(
262                            companyId, WorkflowConstants.STATUS_ANY, scope, start, end,
263                            orderByComparator);
264            }
265    
266            /**
267             * @deprecated As of 6.2.0, replaced by {@link #getCompanyRecordsCount(long,
268             *             int, int)}
269             */
270            @Override
271            public int getCompanyRecordsCount(long companyId, int scope)
272                    throws SystemException {
273    
274                    return getCompanyRecordsCount(
275                            companyId, WorkflowConstants.STATUS_ANY, scope);
276            }
277    
278            @Override
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            @Override
286            public DDLRecordVersion getLatestRecordVersion(long recordId)
287                    throws PortalException, SystemException {
288    
289                    List<DDLRecordVersion> recordVersions =
290                            ddlRecordVersionPersistence.findByRecordId(recordId);
291    
292                    if (recordVersions.isEmpty()) {
293                            throw new NoSuchRecordVersionException(
294                                    "No record versions found for recordId " + recordId);
295                    }
296    
297                    recordVersions = ListUtil.copy(recordVersions);
298    
299                    Collections.sort(
300                            recordVersions, new DDLRecordVersionVersionComparator());
301    
302                    return recordVersions.get(0);
303            }
304    
305            @Override
306            public Long[] getMinAndMaxCompanyRecordIds(
307                            long companyId, int status, int scope)
308                    throws SystemException {
309    
310                    return ddlRecordFinder.findByC_S_S_MinAndMax(companyId, status, scope);
311            }
312    
313            @Override
314            public List<DDLRecord> getMinAndMaxCompanyRecords(
315                            long companyId, int status, int scope, long minRecordId,
316                            long maxRecordId)
317                    throws SystemException {
318    
319                    return ddlRecordFinder.findByC_S_S_MinAndMax(
320                            companyId, status, scope, minRecordId, maxRecordId);
321            }
322    
323            @Override
324            public DDLRecord getRecord(long recordId)
325                    throws PortalException, SystemException {
326    
327                    return ddlRecordPersistence.findByPrimaryKey(recordId);
328            }
329    
330            @Override
331            public List<DDLRecord> getRecords(long recordSetId) throws SystemException {
332                    return ddlRecordPersistence.findByRecordSetId(recordSetId);
333            }
334    
335            @Override
336            public List<DDLRecord> getRecords(
337                            long recordSetId, int status, int start, int end,
338                            OrderByComparator orderByComparator)
339                    throws SystemException {
340    
341                    return ddlRecordFinder.findByR_S(
342                            recordSetId, status, start, end, orderByComparator);
343            }
344    
345            @Override
346            public List<DDLRecord> getRecords(long recordSetId, long userId)
347                    throws SystemException {
348    
349                    return ddlRecordPersistence.findByR_U(recordSetId, userId);
350            }
351    
352            @Override
353            public int getRecordsCount(long recordSetId, int status)
354                    throws SystemException {
355    
356                    return ddlRecordFinder.countByR_S(recordSetId, status);
357            }
358    
359            @Override
360            public DDLRecordVersion getRecordVersion(long recordVersionId)
361                    throws PortalException, SystemException {
362    
363                    return ddlRecordVersionPersistence.findByPrimaryKey(recordVersionId);
364            }
365    
366            @Override
367            public DDLRecordVersion getRecordVersion(long recordId, String version)
368                    throws PortalException, SystemException {
369    
370                    return ddlRecordVersionPersistence.findByR_V(recordId, version);
371            }
372    
373            @Override
374            public List<DDLRecordVersion> getRecordVersions(
375                            long recordId, int start, int end,
376                            OrderByComparator orderByComparator)
377                    throws SystemException {
378    
379                    return ddlRecordVersionPersistence.findByRecordId(
380                            recordId, start, end, orderByComparator);
381            }
382    
383            @Override
384            public int getRecordVersionsCount(long recordId) throws SystemException {
385                    return ddlRecordVersionPersistence.countByRecordId(recordId);
386            }
387    
388            @Override
389            public void revertRecordVersion(
390                            long userId, long recordId, String version,
391                            ServiceContext serviceContext)
392                    throws PortalException, SystemException {
393    
394                    DDLRecordVersion recordVersion = getRecordVersion(recordId, version);
395    
396                    if (!recordVersion.isApproved()) {
397                            return;
398                    }
399    
400                    Fields fields = StorageEngineUtil.getFields(
401                            recordVersion.getDDMStorageId());
402    
403                    ddlRecordLocalService.updateRecord(
404                            userId, recordId, true, recordVersion.getDisplayIndex(), fields,
405                            false, serviceContext);
406            }
407    
408            @Override
409            public Hits search(SearchContext searchContext) throws SystemException {
410                    try {
411                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
412                                    DDLRecord.class);
413    
414                            return indexer.search(searchContext);
415                    }
416                    catch (Exception e) {
417                            throw new SystemException(e);
418                    }
419            }
420    
421            @Override
422            public void updateAsset(
423                            long userId, DDLRecord record, DDLRecordVersion recordVersion,
424                            long[] assetCategoryIds, String[] assetTagNames, Locale locale)
425                    throws PortalException, SystemException {
426    
427                    boolean addDraftAssetEntry = false;
428                    boolean visible = true;
429    
430                    if ((recordVersion != null) && !recordVersion.isApproved()) {
431                            String version = recordVersion.getVersion();
432    
433                            if (!version.equals(DDLRecordConstants.VERSION_DEFAULT)) {
434                                    int approvedRecordVersionsCount =
435                                            ddlRecordVersionPersistence.countByR_S(
436                                                    record.getRecordId(),
437                                                    WorkflowConstants.STATUS_APPROVED);
438    
439                                    if (approvedRecordVersionsCount > 0) {
440                                            addDraftAssetEntry = true;
441                                    }
442                            }
443    
444                            visible = false;
445                    }
446    
447                    DDLRecordSet recordSet = record.getRecordSet();
448    
449                    DDMStructure ddmStructure = recordSet.getDDMStructure();
450    
451                    String ddmStructureName = ddmStructure.getName(locale);
452    
453                    String recordSetName = recordSet.getName(locale);
454    
455                    String title = LanguageUtil.format(
456                            locale, "new-x-for-list-x",
457                            new Object[] {ddmStructureName, recordSetName}, false);
458    
459                    if (addDraftAssetEntry) {
460                            assetEntryLocalService.updateEntry(
461                                    userId, record.getGroupId(), record.getCreateDate(),
462                                    record.getModifiedDate(), DDLRecordConstants.getClassName(),
463                                    recordVersion.getRecordVersionId(), record.getUuid(), 0,
464                                    assetCategoryIds, assetTagNames, false, null, null, null,
465                                    ContentTypes.TEXT_HTML, title, null, StringPool.BLANK, null,
466                                    null, 0, 0, null, false);
467                    }
468                    else {
469                            assetEntryLocalService.updateEntry(
470                                    userId, record.getGroupId(), record.getCreateDate(),
471                                    record.getModifiedDate(), DDLRecordConstants.getClassName(),
472                                    record.getRecordId(), record.getUuid(), 0, assetCategoryIds,
473                                    assetTagNames, visible, null, null, null,
474                                    ContentTypes.TEXT_HTML, title, null, StringPool.BLANK, null,
475                                    null, 0, 0, null, false);
476                    }
477            }
478    
479            @Override
480            public DDLRecord updateRecord(
481                            long userId, long recordId, boolean majorVersion, int displayIndex,
482                            Fields fields, boolean mergeFields, ServiceContext serviceContext)
483                    throws PortalException, SystemException {
484    
485                    // Record
486    
487                    User user = userPersistence.findByPrimaryKey(userId);
488    
489                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
490    
491                    record.setModifiedDate(serviceContext.getModifiedDate(null));
492    
493                    ddlRecordPersistence.update(record);
494    
495                    // Record version
496    
497                    DDLRecordVersion recordVersion = record.getLatestRecordVersion();
498    
499                    if (recordVersion.isApproved()) {
500                            DDLRecordSet recordSet = record.getRecordSet();
501    
502                            if (mergeFields) {
503                                    Fields existingFields = StorageEngineUtil.getFields(
504                                            recordVersion.getDDMStorageId());
505    
506                                    fields = DDMUtil.mergeFields(fields, existingFields);
507                            }
508    
509                            long ddmStorageId = StorageEngineUtil.create(
510                                    recordSet.getCompanyId(), recordSet.getDDMStructureId(), fields,
511                                    serviceContext);
512                            String version = getNextVersion(
513                                    recordVersion.getVersion(), majorVersion,
514                                    serviceContext.getWorkflowAction());
515    
516                            recordVersion = addRecordVersion(
517                                    user, record, ddmStorageId, version, displayIndex,
518                                    WorkflowConstants.STATUS_DRAFT);
519                    }
520                    else {
521                            StorageEngineUtil.update(
522                                    recordVersion.getDDMStorageId(), fields, mergeFields,
523                                    serviceContext);
524    
525                            String version = recordVersion.getVersion();
526    
527                            updateRecordVersion(
528                                    user, recordVersion, version, displayIndex,
529                                    recordVersion.getStatus(), serviceContext);
530                    }
531    
532                    // Workflow
533    
534                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
535                            user.getCompanyId(), record.getGroupId(), userId,
536                            DDLRecord.class.getName(), recordVersion.getRecordVersionId(),
537                            recordVersion, serviceContext);
538    
539                    return record;
540            }
541    
542            @Override
543            public DDLRecord updateRecord(
544                            long userId, long recordId, int displayIndex,
545                            Map<String, Serializable> fieldsMap, boolean mergeFields,
546                            ServiceContext serviceContext)
547                    throws PortalException, SystemException {
548    
549                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(recordId);
550    
551                    Fields oldFields = record.getFields();
552    
553                    DDLRecordSet recordSet = record.getRecordSet();
554    
555                    DDMStructure ddmStructure = recordSet.getDDMStructure();
556    
557                    Fields fields = toFields(
558                            ddmStructure.getStructureId(), fieldsMap,
559                            serviceContext.getLocale(), oldFields.getDefaultLocale());
560    
561                    return ddlRecordLocalService.updateRecord(
562                            userId, recordId, false, displayIndex, fields, mergeFields,
563                            serviceContext);
564            }
565    
566            @Indexable(type = IndexableType.REINDEX)
567            @Override
568            public DDLRecord updateStatus(
569                            long userId, long recordVersionId, int status,
570                            ServiceContext serviceContext)
571                    throws PortalException, SystemException {
572    
573                    // Record version
574    
575                    User user = userPersistence.findByPrimaryKey(userId);
576    
577                    DDLRecordVersion recordVersion =
578                            ddlRecordVersionPersistence.findByPrimaryKey(recordVersionId);
579    
580                    recordVersion.setStatus(status);
581                    recordVersion.setStatusByUserId(user.getUserId());
582                    recordVersion.setStatusByUserName(user.getFullName());
583                    recordVersion.setStatusDate(new Date());
584    
585                    ddlRecordVersionPersistence.update(recordVersion);
586    
587                    // Record
588    
589                    DDLRecord record = ddlRecordPersistence.findByPrimaryKey(
590                            recordVersion.getRecordId());
591    
592                    if (status == WorkflowConstants.STATUS_APPROVED) {
593                            if (DLUtil.compareVersions(
594                                            record.getVersion(), recordVersion.getVersion()) <= 0) {
595    
596                                    record.setDDMStorageId(recordVersion.getDDMStorageId());
597                                    record.setVersion(recordVersion.getVersion());
598                                    record.setRecordSetId(recordVersion.getRecordSetId());
599                                    record.setDisplayIndex(recordVersion.getDisplayIndex());
600                                    record.setVersion(recordVersion.getVersion());
601                                    record.setVersionUserId(recordVersion.getUserId());
602                                    record.setVersionUserName(recordVersion.getUserName());
603                                    record.setModifiedDate(recordVersion.getCreateDate());
604    
605                                    ddlRecordPersistence.update(record);
606                            }
607                    }
608                    else {
609                            if (Validator.equals(
610                                            record.getVersion(), recordVersion.getVersion())) {
611    
612                                    String newVersion = DDLRecordConstants.VERSION_DEFAULT;
613    
614                                    List<DDLRecordVersion> approvedRecordVersions =
615                                            ddlRecordVersionPersistence.findByR_S(
616                                                    record.getRecordId(),
617                                                    WorkflowConstants.STATUS_APPROVED);
618    
619                                    if (!approvedRecordVersions.isEmpty()) {
620                                            newVersion = approvedRecordVersions.get(0).getVersion();
621                                    }
622    
623                                    record.setVersion(newVersion);
624    
625                                    ddlRecordPersistence.update(record);
626                            }
627                    }
628    
629                    return record;
630            }
631    
632            protected DDLRecordVersion addRecordVersion(
633                            User user, DDLRecord record, long ddmStorageId, String version,
634                            int displayIndex, int status)
635                    throws SystemException {
636    
637                    long recordVersionId = counterLocalService.increment();
638    
639                    DDLRecordVersion recordVersion = ddlRecordVersionPersistence.create(
640                            recordVersionId);
641    
642                    recordVersion.setGroupId(record.getGroupId());
643                    recordVersion.setCompanyId(record.getCompanyId());
644                    recordVersion.setUserId(user.getUserId());
645                    recordVersion.setUserName(user.getFullName());
646                    recordVersion.setCreateDate(record.getModifiedDate());
647                    recordVersion.setDDMStorageId(ddmStorageId);
648                    recordVersion.setRecordSetId(record.getRecordSetId());
649                    recordVersion.setRecordId(record.getRecordId());
650                    recordVersion.setVersion(version);
651                    recordVersion.setDisplayIndex(displayIndex);
652                    recordVersion.setStatus(status);
653                    recordVersion.setStatusByUserId(user.getUserId());
654                    recordVersion.setStatusByUserName(user.getFullName());
655                    recordVersion.setStatusDate(record.getModifiedDate());
656    
657                    ddlRecordVersionPersistence.update(recordVersion);
658    
659                    return recordVersion;
660            }
661    
662            protected String getNextVersion(
663                    String version, boolean majorVersion, int workflowAction) {
664    
665                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
666                            majorVersion = false;
667                    }
668    
669                    int[] versionParts = StringUtil.split(version, StringPool.PERIOD, 0);
670    
671                    if (majorVersion) {
672                            versionParts[0]++;
673                            versionParts[1] = 0;
674                    }
675                    else {
676                            versionParts[1]++;
677                    }
678    
679                    return versionParts[0] + StringPool.PERIOD + versionParts[1];
680            }
681    
682            protected Fields toFields(
683                    long ddmStructureId, Map<String, Serializable> fieldsMap, Locale locale,
684                    Locale defaultLocale) {
685    
686                    Fields fields = new Fields();
687    
688                    for (Map.Entry<String, Serializable> entry : fieldsMap.entrySet()) {
689                            Field field = new Field();
690    
691                            field.setDDMStructureId(ddmStructureId);
692                            field.setName(entry.getKey());
693    
694                            Serializable value = entry.getValue();
695    
696                            List<Serializable> serializableValues = _getSerializableValues(
697                                    value);
698    
699                            if (serializableValues != null) {
700                                    field.addValues(locale, serializableValues);
701    
702                                    if (!locale.equals(defaultLocale)) {
703                                            field.addValues(defaultLocale, serializableValues);
704                                    }
705                            }
706                            else {
707                                    field.addValue(locale, String.valueOf(value));
708    
709                                    if (!locale.equals(defaultLocale)) {
710                                            field.addValue(defaultLocale, String.valueOf(value));
711                                    }
712                            }
713    
714                            field.setDefaultLocale(defaultLocale);
715    
716                            fields.put(field);
717                    }
718    
719                    Field fieldsDisplayField = fields.get(_FIELDS_DISPLAY_NAME);
720    
721                    if (fieldsDisplayField == null) {
722                            StringBundler fieldsDisplayFieldSB = new StringBundler(
723                                    fieldsMap.size() * 4 - 1);
724    
725                            for (String fieldName : fields.getNames()) {
726                                    fieldsDisplayFieldSB.append(fieldName);
727                                    fieldsDisplayFieldSB.append(_INSTANCE_SEPARATOR);
728                                    fieldsDisplayFieldSB.append(StringUtil.randomString());
729                                    fieldsDisplayFieldSB.append(StringPool.COMMA);
730                            }
731    
732                            if (fieldsDisplayFieldSB.index() > 0) {
733                                    fieldsDisplayFieldSB.setIndex(fieldsDisplayFieldSB.index() - 1);
734                            }
735    
736                            fieldsDisplayField = new Field(
737                                    ddmStructureId, _FIELDS_DISPLAY_NAME,
738                                    fieldsDisplayFieldSB.toString());
739    
740                            fields.put(fieldsDisplayField);
741                    }
742    
743                    return fields;
744            }
745    
746            protected void updateRecordVersion(
747                            User user, DDLRecordVersion recordVersion, String version,
748                            int displayIndex, int status, ServiceContext serviceContext)
749                    throws SystemException {
750    
751                    recordVersion.setUserId(user.getUserId());
752                    recordVersion.setUserName(user.getFullName());
753                    recordVersion.setVersion(version);
754                    recordVersion.setDisplayIndex(displayIndex);
755                    recordVersion.setStatus(status);
756                    recordVersion.setStatusByUserId(user.getUserId());
757                    recordVersion.setStatusByUserName(user.getFullName());
758                    recordVersion.setStatusDate(serviceContext.getModifiedDate(null));
759    
760                    ddlRecordVersionPersistence.update(recordVersion);
761            }
762    
763            private List<Serializable> _getSerializableValues(Serializable value) {
764                    List<Serializable> serializableValues = null;
765    
766                    if (value instanceof Collection) {
767                            Collection<Serializable> values = (Collection<Serializable>)value;
768    
769                            serializableValues = new ArrayList<Serializable>(values);
770                    }
771                    else if (value instanceof Serializable[]) {
772                            Serializable[] values = (Serializable[])value;
773    
774                            serializableValues = ListUtil.toList(values);
775                    }
776                    else if (value instanceof boolean[]) {
777                            boolean[] values = (boolean[])value;
778    
779                            serializableValues = new ArrayList<Serializable>(values.length);
780    
781                            for (boolean serializableValue : values) {
782                                    serializableValues.add(serializableValue);
783                            }
784                    }
785                    else if (value instanceof double[]) {
786                            double[] values = (double[])value;
787    
788                            serializableValues = new ArrayList<Serializable>(values.length);
789    
790                            for (double serializableValue : values) {
791                                    serializableValues.add(serializableValue);
792                            }
793                    }
794                    else if (value instanceof float[]) {
795                            float[] values = (float[])value;
796    
797                            serializableValues = new ArrayList<Serializable>(values.length);
798    
799                            for (float serializableValue : values) {
800                                    serializableValues.add(serializableValue);
801                            }
802                    }
803                    else if (value instanceof int[]) {
804                            int[] values = (int[])value;
805    
806                            serializableValues = new ArrayList<Serializable>(values.length);
807    
808                            for (int serializableValue : values) {
809                                    serializableValues.add(serializableValue);
810                            }
811                    }
812                    else if (value instanceof long[]) {
813                            long[] values = (long[])value;
814    
815                            serializableValues = new ArrayList<Serializable>(values.length);
816    
817                            for (long serializableValue : values) {
818                                    serializableValues.add(serializableValue);
819                            }
820                    }
821                    else if (value instanceof short[]) {
822                            short[] values = (short[])value;
823    
824                            serializableValues = new ArrayList<Serializable>(values.length);
825    
826                            for (short serializableValue : values) {
827                                    serializableValues.add(serializableValue);
828                            }
829                    }
830    
831                    return serializableValues;
832            }
833    
834            private static final String _FIELDS_DISPLAY_NAME = "_fieldsDisplay";
835    
836            private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
837    
838    }