001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.util.test;
016    
017    import com.liferay.portal.kernel.util.LocaleUtil;
018    import com.liferay.portal.kernel.workflow.WorkflowConstants;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portal.util.test.RandomTestUtil;
022    import com.liferay.portal.util.test.TestPropsValues;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordConstants;
025    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
026    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
030    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormValues;
031    
032    /**
033     * @author Marcellus Tavares
034     * @author Andr?? de Oliveira
035     */
036    public class DDLRecordTestHelper {
037    
038            public DDLRecordTestHelper(Group group, DDLRecordSet recordSet)
039                    throws Exception {
040    
041                    _group = group;
042                    _recordSet = recordSet;
043            }
044    
045            public DDLRecord addRecord() throws Exception {
046                    DDMStructure ddmStructure = _recordSet.getDDMStructure();
047    
048                    DDMForm ddmForm = ddmStructure.getDDMForm();
049    
050                    DDMFormValues ddmFormValues = DDLRecordTestUtil.createDDMFormValues(
051                            ddmForm, DDLRecordTestUtil.createAvailableLocales(LocaleUtil.US),
052                            LocaleUtil.US);
053    
054                    for (DDMFormField ddmFormField : ddmForm.getDDMFormFields()) {
055                            if (ddmStructure.isFieldPrivate(ddmFormField.getName())) {
056                                    continue;
057                            }
058    
059                            if (ddmFormField.isLocalizable()) {
060                                    ddmFormValues.addDDMFormFieldValue(
061                                            DDLRecordTestUtil.createLocalizedTextDDMFormFieldValue(
062                                                    ddmFormField.getName(), RandomTestUtil.randomString()));
063                            }
064                            else {
065                                    ddmFormValues.addDDMFormFieldValue(
066                                            DDLRecordTestUtil.createUnlocalizedTextDDMFormFieldValue(
067                                                    ddmFormField.getName(), RandomTestUtil.randomString()));
068                            }
069                    }
070    
071                    return addRecord(ddmFormValues, WorkflowConstants.ACTION_PUBLISH);
072            }
073    
074            public DDLRecord addRecord(DDMFormValues ddmFormValues, int workflowAction)
075                    throws Exception {
076    
077                    ServiceContext serviceContext = DDLRecordTestUtil.getServiceContext(
078                            workflowAction);
079    
080                    return DDLRecordLocalServiceUtil.addRecord(
081                            TestPropsValues.getUserId(), _group.getGroupId(),
082                            _recordSet.getRecordSetId(),
083                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, ddmFormValues,
084                            serviceContext);
085            }
086    
087            public DDLRecordSet getRecordSet() {
088                    return _recordSet;
089            }
090    
091            private final Group _group;
092            private final DDLRecordSet _recordSet;
093    
094    }