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.service.ServiceContext;
019    import com.liferay.portal.util.test.RandomTestUtil;
020    import com.liferay.portal.util.test.ServiceContextTestUtil;
021    import com.liferay.portal.util.test.TestPropsValues;
022    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecordConstants;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
025    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSetConstants;
026    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
027    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
029    
030    import java.io.Serializable;
031    
032    import java.util.HashMap;
033    import java.util.Locale;
034    import java.util.Map;
035    import java.util.Set;
036    
037    /**
038     * @author Daniel Kocsis
039     */
040    public class DDLTestUtil {
041    
042            public static DDLRecord addRecord(long groupId, long recordSetId)
043                    throws Exception {
044    
045                    Map<String, Serializable> fieldsMap =
046                            new HashMap<String, Serializable>();
047    
048                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
049                            recordSetId);
050    
051                    DDMStructure ddmStructure = recordSet.getDDMStructure();
052    
053                    Set<String> fieldNames = ddmStructure.getFieldNames();
054    
055                    for (String fieldName : fieldNames) {
056                            if (ddmStructure.isFieldPrivate(fieldName)) {
057                                    continue;
058                            }
059    
060                            fieldsMap.put(fieldName, null);
061                    }
062    
063                    return addRecord(groupId, recordSetId, fieldsMap);
064            }
065    
066            public static DDLRecord addRecord(
067                            long groupId, long recordSetId, Map<String, Serializable> fieldsMap)
068                    throws Exception {
069    
070                    long userId = TestPropsValues.getUserId();
071    
072                    ServiceContext serviceContext =
073                            ServiceContextTestUtil.getServiceContext(groupId);
074    
075                    return DDLRecordLocalServiceUtil.addRecord(
076                            userId, groupId, recordSetId,
077                            DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fieldsMap,
078                            serviceContext);
079            }
080    
081            public static DDLRecordSet addRecordSet(long groupId, long structureId)
082                    throws Exception {
083    
084                    String name = RandomTestUtil.randomString();
085                    Locale defaultLocale = LocaleUtil.getSiteDefault();
086    
087                    ServiceContext serviceContext =
088                            ServiceContextTestUtil.getServiceContext(groupId);
089    
090                    return addRecordSet(
091                            groupId, structureId, name, defaultLocale, serviceContext);
092            }
093    
094            public static DDLRecordSet addRecordSet(
095                            long groupId, long structureId, String name, Locale defaultLocale,
096                            ServiceContext serviceContext)
097                    throws Exception {
098    
099                    long userId = TestPropsValues.getUserId();
100                    String recordSetKey = null;
101    
102                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
103    
104                    nameMap.put(defaultLocale, name);
105    
106                    Map<Locale, String> descriptionMap = null;
107                    int minDisplayRows = DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT;
108                    int scope = DDLRecordSetConstants.SCOPE_DYNAMIC_DATA_LISTS;
109    
110                    return DDLRecordSetLocalServiceUtil.addRecordSet(
111                            userId, groupId, structureId, recordSetKey, nameMap, descriptionMap,
112                            minDisplayRows, scope, serviceContext);
113            }
114    
115    }